jack2 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1279 lines
31KB

  1. /* This module provides a set of abstract shared memory interfaces
  2. * with support using both System V and POSIX shared memory
  3. * implementations. The code is divided into three sections:
  4. *
  5. * - common (interface-independent) code
  6. * - POSIX implementation
  7. * - System V implementation
  8. *
  9. * The implementation used is determined by whether USE_POSIX_SHM was
  10. * set in the ./configure step.
  11. */
  12. /*
  13. Copyright (C) 2001-2003 Paul Davis
  14. This program is free software; you can redistribute it and/or modify
  15. it under the terms of the GNU Lesser General Public License as published by
  16. the Free Software Foundation; either version 2.1 of the License, or
  17. (at your option) any later version.
  18. This program is distributed in the hope that it will be useful,
  19. but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. GNU Lesser General Public License for more details.
  22. You should have received a copy of the GNU Lesser General Public License
  23. along with this program; if not, write to the Free Software
  24. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  25. */
  26. #include "JackConstants.h"
  27. #ifdef WIN32
  28. #include <process.h>
  29. #include <stdio.h>
  30. #else
  31. #include <unistd.h>
  32. #include <fcntl.h>
  33. #include <stdio.h>
  34. #include <string.h>
  35. #include <signal.h>
  36. #include <limits.h>
  37. #include <errno.h>
  38. #include <dirent.h>
  39. #include <sys/mman.h>
  40. #include <sys/types.h>
  41. #include <sys/stat.h>
  42. #include <sys/shm.h>
  43. #include <sys/sem.h>
  44. #include <stdlib.h>
  45. #endif
  46. #include "shm.h"
  47. #include "JackError.h"
  48. static int GetUID()
  49. {
  50. #ifdef WIN32
  51. return _getpid();
  52. //#error "No getuid function available"
  53. #else
  54. return getuid();
  55. #endif
  56. }
  57. static int GetPID()
  58. {
  59. #ifdef WIN32
  60. return _getpid();
  61. #else
  62. return getpid();
  63. #endif
  64. }
  65. #ifdef USE_POSIX_SHM
  66. static jack_shmtype_t jack_shmtype = shm_POSIX;
  67. #elif WIN32
  68. static jack_shmtype_t jack_shmtype = shm_WIN32;
  69. #else
  70. static jack_shmtype_t jack_shmtype = shm_SYSV;
  71. #endif
  72. /* interface-dependent forward declarations */
  73. static int jack_access_registry (jack_shm_info_t *ri);
  74. static int jack_create_registry (jack_shm_info_t *ri);
  75. static void jack_remove_shm (jack_shm_id_t *id);
  76. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  77. * common interface-independent section
  78. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  79. /* The JACK SHM registry is a chunk of memory for keeping track of the
  80. * shared memory used by each active JACK server. This allows the
  81. * server to clean up shared memory when it exits. To avoid memory
  82. * leakage due to kill -9, crashes or debugger-driven exits, this
  83. * cleanup is also done when a new instance of that server starts.
  84. */
  85. /* per-process global data for the SHM interfaces */
  86. static jack_shm_id_t registry_id; /* SHM id for the registry */
  87. #ifdef WIN32
  88. static jack_shm_info_t registry_info = {/* SHM info for the registry */
  89. JACK_SHM_NULL_INDEX,
  90. NULL
  91. };
  92. #else
  93. static jack_shm_info_t registry_info = { /* SHM info for the registry */
  94. .index = JACK_SHM_NULL_INDEX,
  95. .ptr.attached_at = MAP_FAILED
  96. };
  97. #endif
  98. /* pointers to registry header and array */
  99. static jack_shm_header_t *jack_shm_header = NULL;
  100. static jack_shm_registry_t *jack_shm_registry = NULL;
  101. static char jack_shm_server_prefix[JACK_SERVER_NAME_SIZE] = "";
  102. /* jack_shm_lock_registry() serializes updates to the shared memory
  103. * segment JACK uses to keep track of the SHM segments allocated to
  104. * all its processes, including multiple servers.
  105. *
  106. * This is not a high-contention lock, but it does need to work across
  107. * multiple processes. High transaction rates and realtime safety are
  108. * not required. Any solution needs to at least be portable to POSIX
  109. * and POSIX-like systems.
  110. *
  111. * We must be particularly careful to ensure that the lock be released
  112. * if the owning process terminates abnormally. Otherwise, a segfault
  113. * or kill -9 at the wrong moment could prevent JACK from ever running
  114. * again on that machine until after a reboot.
  115. */
  116. #define JACK_SEMAPHORE_KEY 0x282929
  117. #ifndef USE_POSIX_SHM
  118. #define JACK_SHM_REGISTRY_KEY JACK_SEMAPHORE_KEY
  119. #endif
  120. static int semid = -1;
  121. #ifdef WIN32
  122. static bool check_process_running(DWORD process_id)
  123. {
  124. DWORD aProcesses [2048], cbNeeded, cProcesses;
  125. unsigned int i;
  126. // Enumerate all processes
  127. if (!EnumProcesses(aProcesses, sizeof(aProcesses), &cbNeeded)) {
  128. return false;
  129. }
  130. // Calculate how many process identifiers were returned.
  131. cProcesses = cbNeeded / sizeof(DWORD);
  132. for (i = 0; i < cProcesses; i++) {
  133. if (aProcesses [i] == dwProcessID) {
  134. // Process process_id is running...
  135. return true;
  136. }
  137. }
  138. return false;
  139. }
  140. static int
  141. semaphore_init () {return 0;}
  142. static int
  143. semaphore_add (int value) {return 0;}
  144. #else
  145. /* all semaphore errors are fatal -- issue message, but do not return */
  146. static void
  147. semaphore_error (char *msg)
  148. {
  149. jack_error ("JACK semaphore error: %s (%s)",
  150. msg, strerror (errno));
  151. }
  152. static int
  153. semaphore_init ()
  154. {
  155. key_t semkey = JACK_SEMAPHORE_KEY;
  156. struct sembuf sbuf;
  157. int create_flags = IPC_CREAT | IPC_EXCL
  158. | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
  159. /* Get semaphore ID associated with this key. */
  160. if ((semid = semget(semkey, 0, 0)) == -1) {
  161. /* Semaphore does not exist - Create. */
  162. if ((semid = semget(semkey, 1, create_flags)) != -1) {
  163. /* Initialize the semaphore, allow one owner. */
  164. sbuf.sem_num = 0;
  165. sbuf.sem_op = 1;
  166. sbuf.sem_flg = 0;
  167. if (semop(semid, &sbuf, 1) == -1) {
  168. semaphore_error ("semop");
  169. return -1;
  170. }
  171. } else if (errno == EEXIST) {
  172. if ((semid = semget(semkey, 0, 0)) == -1) {
  173. semaphore_error ("semget");
  174. return -1;
  175. }
  176. } else {
  177. semaphore_error ("semget creation");
  178. return -1;
  179. }
  180. }
  181. return 0;
  182. }
  183. static inline int
  184. semaphore_add (int value)
  185. {
  186. struct sembuf sbuf;
  187. sbuf.sem_num = 0;
  188. sbuf.sem_op = value;
  189. sbuf.sem_flg = SEM_UNDO;
  190. if (semop(semid, &sbuf, 1) == -1) {
  191. semaphore_error ("semop");
  192. return -1;
  193. }
  194. return 0;
  195. }
  196. #endif
  197. static int
  198. jack_shm_lock_registry (void)
  199. {
  200. if (semid == -1) {
  201. if (semaphore_init () < 0)
  202. return -1;
  203. }
  204. return semaphore_add (-1);
  205. }
  206. static void
  207. jack_shm_unlock_registry (void)
  208. {
  209. semaphore_add (1);
  210. }
  211. static void
  212. jack_shm_init_registry ()
  213. {
  214. /* registry must be locked */
  215. int i;
  216. memset (jack_shm_header, 0, JACK_SHM_REGISTRY_SIZE);
  217. jack_shm_header->magic = JACK_SHM_MAGIC;
  218. //jack_shm_header->protocol = JACK_PROTOCOL_VERSION;
  219. jack_shm_header->type = jack_shmtype;
  220. jack_shm_header->size = JACK_SHM_REGISTRY_SIZE;
  221. jack_shm_header->hdr_len = sizeof (jack_shm_header_t);
  222. jack_shm_header->entry_len = sizeof (jack_shm_registry_t);
  223. for (i = 0; i < MAX_SHM_ID; ++i) {
  224. jack_shm_registry[i].index = i;
  225. }
  226. }
  227. static int
  228. jack_shm_validate_registry ()
  229. {
  230. /* registry must be locked */
  231. if ((jack_shm_header->magic == JACK_SHM_MAGIC)
  232. //&& (jack_shm_header->protocol == JACK_PROTOCOL_VERSION)
  233. && (jack_shm_header->type == jack_shmtype)
  234. && (jack_shm_header->size == JACK_SHM_REGISTRY_SIZE)
  235. && (jack_shm_header->hdr_len == sizeof (jack_shm_header_t))
  236. && (jack_shm_header->entry_len == sizeof (jack_shm_registry_t))) {
  237. return 0; /* registry OK */
  238. }
  239. return -1;
  240. }
  241. /* set a unique per-user, per-server shm prefix string
  242. *
  243. * According to the POSIX standard:
  244. *
  245. * "The name argument conforms to the construction rules for a
  246. * pathname. If name begins with the slash character, then processes
  247. * calling shm_open() with the same value of name refer to the same
  248. * shared memory object, as long as that name has not been
  249. * removed. If name does not begin with the slash character, the
  250. * effect is implementation-defined. The interpretation of slash
  251. * characters other than the leading slash character in name is
  252. * implementation-defined."
  253. *
  254. * Since the Linux implementation does not allow slashes *within* the
  255. * name, in the interest of portability we use colons instead.
  256. */
  257. static void
  258. jack_set_server_prefix (const char *server_name)
  259. {
  260. snprintf (jack_shm_server_prefix, sizeof (jack_shm_server_prefix),
  261. "jack-%d:%s:", GetUID(), server_name);
  262. }
  263. /* gain server addressability to shared memory registration segment
  264. *
  265. * returns: 0 if successful
  266. */
  267. static int
  268. jack_server_initialize_shm (int new_registry)
  269. {
  270. int rc;
  271. if (jack_shm_header)
  272. return 0; /* already initialized */
  273. if (jack_shm_lock_registry () < 0) {
  274. jack_error ("jack_shm_lock_registry fails...");
  275. return -1;
  276. }
  277. rc = jack_access_registry (&registry_info);
  278. if (new_registry) {
  279. jack_remove_shm (&registry_id);
  280. rc = ENOENT;
  281. }
  282. switch (rc) {
  283. case ENOENT: /* registry does not exist */
  284. rc = jack_create_registry (&registry_info);
  285. break;
  286. case 0: /* existing registry */
  287. if (jack_shm_validate_registry () == 0)
  288. break;
  289. /* else it was invalid, so fall through */
  290. case EINVAL: /* bad registry */
  291. /* Apparently, this registry was created by an older
  292. * JACK version. Delete it so we can try again. */
  293. jack_release_shm (&registry_info);
  294. jack_remove_shm (&registry_id);
  295. if ((rc = jack_create_registry (&registry_info)) != 0) {
  296. jack_error ("incompatible shm registry (%s)",
  297. strerror (errno));
  298. #ifndef USE_POSIX_SHM
  299. jack_error ("to delete, use `ipcrm -M 0x%0.8x'",
  300. JACK_SHM_REGISTRY_KEY);
  301. #endif
  302. }
  303. break;
  304. default: /* failure return code */
  305. break;
  306. }
  307. jack_shm_unlock_registry ();
  308. return rc;
  309. }
  310. /* gain client addressability to shared memory registration segment
  311. *
  312. * NOTE: this function is no longer used for server initialization,
  313. * instead it calls jack_register_server().
  314. *
  315. * returns: 0 if successful
  316. */
  317. int
  318. jack_initialize_shm (const char *server_name)
  319. {
  320. int rc;
  321. if (jack_shm_header)
  322. return 0; /* already initialized */
  323. jack_set_server_prefix (server_name);
  324. if (jack_shm_lock_registry () < 0) {
  325. jack_error ("jack_shm_lock_registry fails...");
  326. return -1;
  327. }
  328. if ((rc = jack_access_registry (&registry_info)) == 0) {
  329. if ((rc = jack_shm_validate_registry ()) != 0) {
  330. jack_error ("Incompatible shm registry, "
  331. "are jackd and libjack in sync?");
  332. }
  333. }
  334. jack_shm_unlock_registry ();
  335. return rc;
  336. }
  337. char* jack_shm_addr (jack_shm_info_t* si)
  338. {
  339. return (char*)si->ptr.attached_at;
  340. }
  341. void
  342. jack_destroy_shm (jack_shm_info_t* si)
  343. {
  344. /* must NOT have the registry locked */
  345. if (si->index == JACK_SHM_NULL_INDEX)
  346. return; /* segment not allocated */
  347. jack_remove_shm (&jack_shm_registry[si->index].id);
  348. jack_release_shm_info (si->index);
  349. }
  350. jack_shm_registry_t *
  351. jack_get_free_shm_info ()
  352. {
  353. /* registry must be locked */
  354. jack_shm_registry_t* si = NULL;
  355. int i;
  356. for (i = 0; i < MAX_SHM_ID; ++i) {
  357. if (jack_shm_registry[i].size == 0) {
  358. break;
  359. }
  360. }
  361. if (i < MAX_SHM_ID) {
  362. si = &jack_shm_registry[i];
  363. }
  364. return si;
  365. }
  366. static void
  367. jack_release_shm_entry (jack_shm_registry_index_t index)
  368. {
  369. /* the registry must be locked */
  370. jack_shm_registry[index].size = 0;
  371. jack_shm_registry[index].allocator = 0;
  372. memset (&jack_shm_registry[index].id, 0,
  373. sizeof (jack_shm_registry[index].id));
  374. }
  375. int
  376. jack_release_shm_info (jack_shm_registry_index_t index)
  377. {
  378. /* must NOT have the registry locked */
  379. if (jack_shm_registry[index].allocator == GetPID()) {
  380. if (jack_shm_lock_registry () < 0) {
  381. jack_error ("jack_shm_lock_registry fails...");
  382. return -1;
  383. }
  384. jack_release_shm_entry (index);
  385. jack_shm_unlock_registry ();
  386. }
  387. return 0;
  388. }
  389. /* Claim server_name for this process.
  390. *
  391. * returns 0 if successful
  392. * EEXIST if server_name was already active for this user
  393. * ENOSPC if server registration limit reached
  394. * ENOMEM if unable to access shared memory registry
  395. */
  396. int
  397. jack_register_server (const char *server_name, int new_registry)
  398. {
  399. int i, res = 0;
  400. jack_set_server_prefix (server_name);
  401. if (jack_server_initialize_shm (new_registry))
  402. return ENOMEM;
  403. if (jack_shm_lock_registry () < 0) {
  404. jack_error ("jack_shm_lock_registry fails...");
  405. return -1;
  406. }
  407. /* See if server_name already registered. Since server names
  408. * are per-user, we register the unique server prefix string.
  409. */
  410. for (i = 0; i < MAX_SERVERS; i++) {
  411. if (strncmp (jack_shm_header->server[i].name,
  412. jack_shm_server_prefix,
  413. JACK_SERVER_NAME_SIZE) != 0)
  414. continue; /* no match */
  415. if (jack_shm_header->server[i].pid == GetPID()){
  416. res = 0; /* it's me */
  417. goto unlock;
  418. }
  419. /* see if server still exists */
  420. #ifdef WIN32
  421. if (check_process_running(jack_shm_header->server[i].pid)) {
  422. res = EEXIST; /* other server running */
  423. goto unlock;
  424. }
  425. #else
  426. if (kill (jack_shm_header->server[i].pid, 0) == 0) {
  427. res = EEXIST; /* other server running */
  428. goto unlock;
  429. }
  430. #endif
  431. /* it's gone, reclaim this entry */
  432. memset (&jack_shm_header->server[i], 0,
  433. sizeof (jack_shm_server_t));
  434. }
  435. /* find a free entry */
  436. for (i = 0; i < MAX_SERVERS; i++) {
  437. if (jack_shm_header->server[i].pid == 0)
  438. break;
  439. }
  440. if (i >= MAX_SERVERS){
  441. res = ENOSPC; /* out of space */
  442. goto unlock;
  443. }
  444. /* claim it */
  445. jack_shm_header->server[i].pid = GetPID();
  446. strncpy (jack_shm_header->server[i].name,
  447. jack_shm_server_prefix,
  448. JACK_SERVER_NAME_SIZE);
  449. unlock:
  450. jack_shm_unlock_registry ();
  451. return res;
  452. }
  453. /* release server_name registration */
  454. int
  455. jack_unregister_server (const char *server_name /* unused */)
  456. {
  457. int i;
  458. if (jack_shm_lock_registry () < 0) {
  459. jack_error ("jack_shm_lock_registry fails...");
  460. return -1;
  461. }
  462. for (i = 0; i < MAX_SERVERS; i++) {
  463. if (jack_shm_header->server[i].pid == GetPID()) {
  464. memset (&jack_shm_header->server[i], 0,
  465. sizeof (jack_shm_server_t));
  466. }
  467. }
  468. jack_shm_unlock_registry ();
  469. return 0;
  470. }
  471. /* called for server startup and termination */
  472. int
  473. jack_cleanup_shm ()
  474. {
  475. int i;
  476. int destroy;
  477. jack_shm_info_t copy;
  478. if (jack_shm_lock_registry () < 0) {
  479. jack_error ("jack_shm_lock_registry fails...");
  480. return -1;
  481. }
  482. for (i = 0; i < MAX_SHM_ID; i++) {
  483. jack_shm_registry_t* r;
  484. r = &jack_shm_registry[i];
  485. memcpy (&copy, r, sizeof (jack_shm_info_t));
  486. destroy = FALSE;
  487. /* ignore unused entries */
  488. if (r->allocator == 0)
  489. continue;
  490. /* is this my shm segment? */
  491. if (r->allocator == GetPID()) {
  492. /* allocated by this process, so unattach
  493. and destroy. */
  494. jack_release_shm (&copy);
  495. destroy = TRUE;
  496. } else {
  497. /* see if allocator still exists */
  498. #ifdef WIN32 // steph
  499. jack_info("TODO: kill API not available !!");
  500. #else
  501. if (kill (r->allocator, 0)) {
  502. if (errno == ESRCH) {
  503. /* allocator no longer exists,
  504. * so destroy */
  505. destroy = TRUE;
  506. }
  507. }
  508. #endif
  509. }
  510. if (destroy) {
  511. int index = copy.index;
  512. if ((index >= 0) && (index < MAX_SHM_ID)) {
  513. jack_remove_shm (&jack_shm_registry[index].id);
  514. jack_release_shm_entry (index);
  515. }
  516. r->size = 0;
  517. r->allocator = 0;
  518. }
  519. }
  520. jack_shm_unlock_registry ();
  521. return TRUE;
  522. }
  523. /* resize a shared memory segment
  524. *
  525. * There is no way to resize a System V shm segment. Resizing is
  526. * possible with POSIX shm, but not with the non-conformant Mac OS X
  527. * implementation. Since POSIX shm is mainly used on that platform,
  528. * it's simpler to treat them both the same.
  529. *
  530. * So, we always resize by deleting and reallocating. This is
  531. * tricky, because the old segment will not disappear until
  532. * all the clients have released it. We only do what we can
  533. * from here.
  534. *
  535. * This is not done under a single lock. I don't even want to think
  536. * about all the things that could possibly go wrong if multple
  537. * processes tried to resize the same segment concurrently. That
  538. * probably doesn't happen.
  539. */
  540. int
  541. jack_resize_shm (jack_shm_info_t* si, jack_shmsize_t size)
  542. {
  543. jack_shm_id_t id;
  544. /* The underlying type of `id' differs for SYSV and POSIX */
  545. memcpy (&id, &jack_shm_registry[si->index].id, sizeof (id));
  546. jack_release_shm (si);
  547. jack_destroy_shm (si);
  548. if (jack_shmalloc ((char *) id, size, si)) {
  549. return -1;
  550. }
  551. return jack_attach_shm (si);
  552. }
  553. int
  554. jack_attach_lib_shm (jack_shm_info_t* si)
  555. {
  556. int res = jack_attach_shm(si);
  557. if (res == 0)
  558. si->size = jack_shm_registry[si->index].size; // Keep size in si struct
  559. return res;
  560. }
  561. int
  562. jack_attach_lib_shm_read (jack_shm_info_t* si)
  563. {
  564. int res = jack_attach_shm_read(si);
  565. if (res == 0)
  566. si->size = jack_shm_registry[si->index].size; // Keep size in si struct
  567. return res;
  568. }
  569. #ifdef USE_POSIX_SHM
  570. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  571. * POSIX interface-dependent functions
  572. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  573. /* gain addressability to existing SHM registry segment
  574. *
  575. * sets up global registry pointers, if successful
  576. *
  577. * returns: 0 if existing registry accessed successfully
  578. * ENOENT if registry does not exist
  579. * EINVAL if registry exists, but has the wrong size
  580. */
  581. static int
  582. jack_access_registry (jack_shm_info_t *ri)
  583. {
  584. /* registry must be locked */
  585. int shm_fd;
  586. strncpy (registry_id, "/jack-shm-registry", sizeof (registry_id));
  587. /* try to open an existing segment */
  588. if ((shm_fd = shm_open (registry_id, O_RDWR, 0666)) < 0) {
  589. int rc = errno;
  590. if (errno != ENOENT) {
  591. jack_error ("Cannot open existing shm registry segment"
  592. " (%s)", strerror (errno));
  593. }
  594. close (shm_fd);
  595. return rc;
  596. }
  597. if ((ri->ptr.attached_at = mmap (0, JACK_SHM_REGISTRY_SIZE,
  598. PROT_READ|PROT_WRITE,
  599. MAP_SHARED, shm_fd, 0)) == MAP_FAILED) {
  600. jack_error ("Cannot mmap shm registry segment (%s)",
  601. strerror (errno));
  602. close (shm_fd);
  603. return EINVAL;
  604. }
  605. /* set up global pointers */
  606. ri->index = JACK_SHM_REGISTRY_INDEX;
  607. jack_shm_header = ri->ptr.attached_at;
  608. jack_shm_registry = (jack_shm_registry_t *) (jack_shm_header + 1);
  609. close (shm_fd); // steph
  610. return 0;
  611. }
  612. /* create a new SHM registry segment
  613. *
  614. * sets up global registry pointers, if successful
  615. *
  616. * returns: 0 if registry created successfully
  617. * nonzero error code if unable to allocate a new registry
  618. */
  619. static int
  620. jack_create_registry (jack_shm_info_t *ri)
  621. {
  622. /* registry must be locked */
  623. int shm_fd;
  624. strncpy (registry_id, "/jack-shm-registry", sizeof (registry_id));
  625. if ((shm_fd = shm_open (registry_id, O_RDWR|O_CREAT, 0666)) < 0) {
  626. int rc = errno;
  627. jack_error ("Cannot create shm registry segment (%s)",
  628. strerror (errno));
  629. return rc;
  630. }
  631. /* Previous shm_open result depends of the actual value of umask, force correct file permisssion here */
  632. if (fchmod(shm_fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH) < 0) {
  633. jack_log("Cannot chmod jack-shm-registry (%s) %d %d", strerror (errno));
  634. }
  635. /* Set the desired segment size. NOTE: the non-conformant Mac
  636. * OS X POSIX shm only allows ftruncate() on segment creation.
  637. */
  638. if (ftruncate (shm_fd, JACK_SHM_REGISTRY_SIZE) < 0) {
  639. int rc = errno;
  640. jack_error ("Cannot set registry size (%s)", strerror (errno));
  641. jack_remove_shm (&registry_id);
  642. close (shm_fd);
  643. return rc;
  644. }
  645. if ((ri->ptr.attached_at = mmap (0, JACK_SHM_REGISTRY_SIZE,
  646. PROT_READ|PROT_WRITE,
  647. MAP_SHARED, shm_fd, 0)) == MAP_FAILED) {
  648. jack_error ("Cannot mmap shm registry segment (%s)",
  649. strerror (errno));
  650. jack_remove_shm (&registry_id);
  651. close (shm_fd);
  652. return EINVAL;
  653. }
  654. /* set up global pointers */
  655. ri->index = JACK_SHM_REGISTRY_INDEX;
  656. jack_shm_header = ri->ptr.attached_at;
  657. jack_shm_registry = (jack_shm_registry_t *) (jack_shm_header + 1);
  658. /* initialize registry contents */
  659. jack_shm_init_registry ();
  660. close (shm_fd); // steph
  661. return 0;
  662. }
  663. static void
  664. jack_remove_shm (jack_shm_id_t *id)
  665. {
  666. /* registry may or may not be locked */
  667. shm_unlink ((char *) id);
  668. }
  669. void
  670. jack_release_shm (jack_shm_info_t* si)
  671. {
  672. /* registry may or may not be locked */
  673. if (si->ptr.attached_at != MAP_FAILED) {
  674. munmap (si->ptr.attached_at, jack_shm_registry[si->index].size);
  675. }
  676. }
  677. void
  678. jack_release_lib_shm (jack_shm_info_t* si)
  679. {
  680. /* registry may or may not be locked */
  681. if (si->ptr.attached_at != MAP_FAILED) {
  682. munmap (si->ptr.attached_at, si->size);
  683. }
  684. }
  685. /* allocate a POSIX shared memory segment */
  686. int
  687. jack_shmalloc (const char *shm_name, jack_shmsize_t size, jack_shm_info_t* si)
  688. {
  689. jack_shm_registry_t* registry;
  690. int shm_fd;
  691. int rc = -1;
  692. char name[SHM_NAME_MAX+1];
  693. if (jack_shm_lock_registry () < 0) {
  694. jack_error ("jack_shm_lock_registry fails...");
  695. return -1;
  696. }
  697. if ((registry = jack_get_free_shm_info ()) == NULL) {
  698. jack_error ("shm registry full");
  699. goto unlock;
  700. }
  701. /* On Mac OS X, the maximum length of a shared memory segment
  702. * name is SHM_NAME_MAX (instead of NAME_MAX or PATH_MAX as
  703. * defined by the standard). Unfortunately, Apple sets this
  704. * value so small (about 31 bytes) that it is useless for
  705. * actual names. So, we construct a short name from the
  706. * registry index for uniqueness and ignore the shm_name
  707. * parameter. Bah!
  708. */
  709. snprintf (name, sizeof (name), "/jack-%d-%d", GetUID(), registry->index);
  710. if (strlen (name) >= sizeof (registry->id)) {
  711. jack_error ("shm segment name too long %s", name);
  712. goto unlock;
  713. }
  714. if ((shm_fd = shm_open (name, O_RDWR|O_CREAT, 0666)) < 0) {
  715. jack_error ("Cannot create shm segment %s (%s)",
  716. name, strerror (errno));
  717. goto unlock;
  718. }
  719. if (ftruncate (shm_fd, size) < 0) {
  720. jack_error ("Cannot set size of engine shm "
  721. "registry 0 (%s)",
  722. strerror (errno));
  723. close (shm_fd);
  724. goto unlock;
  725. }
  726. close (shm_fd);
  727. registry->size = size;
  728. strncpy (registry->id, name, sizeof (registry->id));
  729. registry->allocator = GetPID();
  730. si->index = registry->index;
  731. si->ptr.attached_at = MAP_FAILED; /* not attached */
  732. rc = 0; /* success */
  733. unlock:
  734. jack_shm_unlock_registry ();
  735. return rc;
  736. }
  737. int
  738. jack_attach_shm (jack_shm_info_t* si)
  739. {
  740. int shm_fd;
  741. jack_shm_registry_t *registry = &jack_shm_registry[si->index];
  742. if ((shm_fd = shm_open (registry->id,
  743. O_RDWR, 0666)) < 0) {
  744. jack_error ("Cannot open shm segment %s (%s)", registry->id,
  745. strerror (errno));
  746. return -1;
  747. }
  748. if ((si->ptr.attached_at = mmap (0, registry->size, PROT_READ|PROT_WRITE,
  749. MAP_SHARED, shm_fd, 0)) == MAP_FAILED) {
  750. jack_error ("Cannot mmap shm segment %s (%s)",
  751. registry->id,
  752. strerror (errno));
  753. close (shm_fd);
  754. return -1;
  755. }
  756. close (shm_fd);
  757. return 0;
  758. }
  759. int
  760. jack_attach_shm_read (jack_shm_info_t* si)
  761. {
  762. int shm_fd;
  763. jack_shm_registry_t *registry = &jack_shm_registry[si->index];
  764. if ((shm_fd = shm_open (registry->id,
  765. O_RDONLY, 0666)) < 0) {
  766. jack_error ("Cannot open shm segment %s (%s)", registry->id,
  767. strerror (errno));
  768. return -1;
  769. }
  770. if ((si->ptr.attached_at = mmap (0, registry->size, PROT_READ,
  771. MAP_SHARED, shm_fd, 0)) == MAP_FAILED) {
  772. jack_error ("Cannot mmap shm segment %s (%s)",
  773. registry->id,
  774. strerror (errno));
  775. close (shm_fd);
  776. return -1;
  777. }
  778. close (shm_fd);
  779. return 0;
  780. }
  781. #elif WIN32
  782. static int
  783. jack_access_registry (jack_shm_info_t *ri)
  784. {
  785. /* registry must be locked */
  786. HANDLE shm_fd;
  787. strncpy (registry_id, "jack-shm-registry", sizeof (registry_id));
  788. /* try to open an existing segment */
  789. if ((shm_fd = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, registry_id)) == NULL) {
  790. int rc = GetLastError();
  791. if (rc != ERROR_FILE_NOT_FOUND) {
  792. jack_error ("Cannot open existing shm registry segment (%ld)", rc);
  793. }
  794. return rc;
  795. }
  796. if ((ri->ptr.attached_at = MapViewOfFile (shm_fd, FILE_MAP_ALL_ACCESS, 0, 0, JACK_SHM_REGISTRY_SIZE)) == NULL) {
  797. jack_error ("Cannot mmap shm registry segment (%ld)", GetLastError());
  798. jack_remove_shm (&registry_id);
  799. CloseHandle (shm_fd);
  800. return EINVAL;
  801. }
  802. /* set up global pointers */
  803. ri->index = JACK_SHM_REGISTRY_INDEX;
  804. jack_shm_header = ri->ptr.attached_at;
  805. jack_shm_registry = (jack_shm_registry_t *) (jack_shm_header + 1);
  806. //CloseHandle(shm_fd); // TO CHECK
  807. return 0;
  808. }
  809. static int
  810. jack_create_registry (jack_shm_info_t *ri)
  811. {
  812. /* registry must be locked */
  813. HANDLE shm_fd;
  814. strncpy (registry_id, "jack-shm-registry", sizeof (registry_id));
  815. if ((shm_fd = CreateFileMapping(INVALID_HANDLE_VALUE,
  816. 0, PAGE_READWRITE,
  817. 0, JACK_SHM_REGISTRY_SIZE,
  818. registry_id)) == NULL || (shm_fd == INVALID_HANDLE_VALUE)) {
  819. int rc = GetLastError();
  820. jack_error ("Cannot create shm registry segment (%ld)", rc);
  821. return rc;
  822. }
  823. if ((ri->ptr.attached_at = MapViewOfFile (shm_fd, FILE_MAP_ALL_ACCESS, 0, 0, JACK_SHM_REGISTRY_SIZE)) == NULL) {
  824. jack_error ("Cannot mmap shm registry segment (%ld)", GetLastError());
  825. jack_remove_shm (&registry_id);
  826. CloseHandle (shm_fd);
  827. return EINVAL;
  828. }
  829. /* set up global pointers */
  830. ri->index = JACK_SHM_REGISTRY_INDEX;
  831. jack_shm_header = ri->ptr.attached_at;
  832. jack_shm_registry = (jack_shm_registry_t *) (jack_shm_header + 1);
  833. /* initialize registry contents */
  834. jack_shm_init_registry ();
  835. //CloseHandle(shm_fd); // TO CHECK
  836. return 0;
  837. }
  838. static void
  839. jack_remove_shm (jack_shm_id_t *id)
  840. {
  841. /* nothing to do */
  842. }
  843. void
  844. jack_release_shm (jack_shm_info_t* si)
  845. {
  846. /* registry may or may not be locked */
  847. if (si->ptr.attached_at != NULL) {
  848. UnmapViewOfFile (si->ptr.attached_at);
  849. }
  850. }
  851. void
  852. jack_release_lib_shm (jack_shm_info_t* si)
  853. {
  854. jack_release_shm(si);
  855. }
  856. int
  857. jack_shmalloc (const char *shm_name, jack_shmsize_t size, jack_shm_info_t* si)
  858. {
  859. jack_shm_registry_t* registry;
  860. HANDLE shm_fd;
  861. int rc = -1;
  862. char name[SHM_NAME_MAX+1];
  863. if (jack_shm_lock_registry () < 0) {
  864. jack_error ("jack_shm_lock_registry fails...");
  865. return -1;
  866. }
  867. if ((registry = jack_get_free_shm_info ()) == NULL) {
  868. jack_error ("shm registry full");
  869. goto unlock;
  870. }
  871. snprintf (name, sizeof (name), "jack-%d-%d", GetUID(), registry->index);
  872. if (strlen (name) >= sizeof (registry->id)) {
  873. jack_error ("shm segment name too long %s", name);
  874. goto unlock;
  875. }
  876. if ((shm_fd = CreateFileMapping(INVALID_HANDLE_VALUE,
  877. 0, PAGE_READWRITE,
  878. 0, size,
  879. name)) == NULL || (shm_fd == INVALID_HANDLE_VALUE)) {
  880. int rc = GetLastError();
  881. jack_error ("Cannot create shm segment (%ld)",rc);
  882. goto unlock;
  883. }
  884. //CloseHandle (shm_fd); // TO CHECK
  885. registry->size = size;
  886. strncpy (registry->id, name, sizeof (registry->id));
  887. registry->allocator = _getpid();
  888. si->index = registry->index;
  889. si->ptr.attached_at = NULL; /* not attached */
  890. rc = 0; /* success */
  891. unlock:
  892. jack_shm_unlock_registry ();
  893. return rc;
  894. }
  895. int
  896. jack_attach_shm (jack_shm_info_t* si)
  897. {
  898. HANDLE shm_fd;
  899. jack_shm_registry_t *registry = &jack_shm_registry[si->index];
  900. if ((shm_fd = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, registry->id)) == NULL) {
  901. int rc = GetLastError();
  902. jack_error ("Cannot open shm segment (%ld)",rc);
  903. return -1;
  904. }
  905. if ((si->ptr.attached_at = MapViewOfFile (shm_fd, FILE_MAP_ALL_ACCESS, 0, 0, registry->size)) == NULL) {
  906. jack_error ("Cannot mmap shm segment (%ld)", GetLastError());
  907. jack_remove_shm (&registry_id);
  908. CloseHandle (shm_fd);
  909. return -1;
  910. }
  911. //CloseHandle (shm_fd); // TO CHECK
  912. return 0;
  913. }
  914. int
  915. jack_attach_shm_read (jack_shm_info_t* si)
  916. {
  917. HANDLE shm_fd;
  918. jack_shm_registry_t *registry = &jack_shm_registry[si->index];
  919. if ((shm_fd = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, registry->id)) == NULL) {
  920. int rc = GetLastError();
  921. jack_error ("Cannot open shm segment (%ld)",rc);
  922. return -1;
  923. }
  924. if ((si->ptr.attached_at = MapViewOfFile (shm_fd, FILE_MAP_READ, 0, 0, registry->size)) == NULL) {
  925. jack_error("Cannot mmap shm segment (%ld)", GetLastError());
  926. jack_remove_shm(&registry_id);
  927. CloseHandle(shm_fd);
  928. return -1;
  929. }
  930. //CloseHandle (shm_fd); // TO CHECK
  931. return 0;
  932. }
  933. #else
  934. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  935. * System V interface-dependent functions
  936. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  937. /* gain addressability to existing SHM registry segment
  938. *
  939. * sets up global registry pointers, if successful
  940. *
  941. * returns: 0 if existing registry accessed successfully
  942. * ENOENT if registry does not exist
  943. * EINVAL if registry exists, but has the wrong size
  944. * other nonzero error code if unable to access registry
  945. */
  946. static int
  947. jack_access_registry (jack_shm_info_t *ri)
  948. {
  949. /* registry must be locked */
  950. /* try without IPC_CREAT to get existing segment */
  951. if ((registry_id = shmget (JACK_SHM_REGISTRY_KEY,
  952. JACK_SHM_REGISTRY_SIZE, 0666)) < 0) {
  953. switch (errno) {
  954. case ENOENT: /* segment does not exist */
  955. return ENOENT;
  956. case EINVAL: /* segment exists, but too small */
  957. /* attempt minimum size access */
  958. registry_id = shmget (JACK_SHM_REGISTRY_KEY, 1, 0666);
  959. return EINVAL;
  960. default: /* or other error */
  961. jack_error ("unable to access shm registry (%s)",
  962. strerror (errno));
  963. return errno;
  964. }
  965. }
  966. if ((ri->attached_at = shmat (registry_id, 0, 0)) < 0) {
  967. jack_error ("Cannot attach shm registry segment (%s)",
  968. strerror (errno));
  969. return EINVAL;
  970. }
  971. /* set up global pointers */
  972. ri->index = JACK_SHM_REGISTRY_INDEX;
  973. jack_shm_header = ri->attached_at;
  974. jack_shm_registry = (jack_shm_registry_t *) (jack_shm_header + 1);
  975. return 0;
  976. }
  977. /* create a new SHM registry segment
  978. *
  979. * sets up global registry pointers, if successful
  980. *
  981. * returns: 0 if registry created successfully
  982. * nonzero error code if unable to allocate a new registry
  983. */
  984. static int
  985. jack_create_registry (jack_shm_info_t *ri)
  986. {
  987. /* registry must be locked */
  988. if ((registry_id = shmget (JACK_SHM_REGISTRY_KEY,
  989. JACK_SHM_REGISTRY_SIZE,
  990. 0666|IPC_CREAT)) < 0) {
  991. jack_error ("Cannot create shm registry segment (%s)",
  992. strerror (errno));
  993. return errno;
  994. }
  995. if ((ri->attached_at = shmat (registry_id, 0, 0)) < 0) {
  996. jack_error ("Cannot attach shm registry segment (%s)",
  997. strerror (errno));
  998. return EINVAL;
  999. }
  1000. /* set up global pointers */
  1001. ri->index = JACK_SHM_REGISTRY_INDEX;
  1002. jack_shm_header = ri->attached_at;
  1003. jack_shm_registry = (jack_shm_registry_t *) (jack_shm_header + 1);
  1004. /* initialize registry contents */
  1005. jack_shm_init_registry ();
  1006. return 0;
  1007. }
  1008. static void
  1009. jack_remove_shm (jack_shm_id_t *id)
  1010. {
  1011. /* registry may or may not be locked */
  1012. shmctl (*id, IPC_RMID, NULL);
  1013. }
  1014. void
  1015. jack_release_shm (jack_shm_info_t* si)
  1016. {
  1017. /* registry may or may not be locked */
  1018. if (si->attached_at != MAP_FAILED) {
  1019. shmdt (si->attached_at);
  1020. }
  1021. }
  1022. void
  1023. jack_release_lib_shm (jack_shm_info_t* si)
  1024. {
  1025. jack_release_shm(si);
  1026. }
  1027. int
  1028. jack_shmalloc (const char* name_not_used, jack_shmsize_t size,
  1029. jack_shm_info_t* si)
  1030. {
  1031. int shmflags;
  1032. int shmid;
  1033. int rc = -1;
  1034. jack_shm_registry_t* registry;
  1035. if (jack_shm_lock_registry () < 0) {
  1036. jack_error ("jack_shm_lock_registry fails...");
  1037. return -1;
  1038. }
  1039. if ((registry = jack_get_free_shm_info ())) {
  1040. shmflags = 0666 | IPC_CREAT | IPC_EXCL;
  1041. if ((shmid = shmget (IPC_PRIVATE, size, shmflags)) >= 0) {
  1042. registry->size = size;
  1043. registry->id = shmid;
  1044. registry->allocator = getpid();
  1045. si->index = registry->index;
  1046. si->attached_at = MAP_FAILED; /* not attached */
  1047. rc = 0;
  1048. } else {
  1049. jack_error ("Cannot create shm segment %s (%s)",
  1050. name_not_used, strerror (errno));
  1051. }
  1052. }
  1053. jack_shm_unlock_registry ();
  1054. return rc;
  1055. }
  1056. int
  1057. jack_attach_shm (jack_shm_info_t* si)
  1058. {
  1059. if ((si->attached_at = shmat (jack_shm_registry[si->index].id, 0, 0)) < 0) {
  1060. jack_error ("Cannot attach shm segment (%s)",
  1061. strerror (errno));
  1062. jack_release_shm_info (si->index);
  1063. return -1;
  1064. }
  1065. return 0;
  1066. }
  1067. #endif /* !USE_POSIX_SHM */