jack1 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.

916 lines
23KB

  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 <config.h>
  27. #include <unistd.h>
  28. #include <fcntl.h>
  29. #include <stdio.h>
  30. #include <string.h>
  31. #include <signal.h>
  32. #include <limits.h>
  33. #include <errno.h>
  34. #include <dirent.h>
  35. #include <sys/mman.h>
  36. #include <sys/types.h>
  37. #include <sys/stat.h>
  38. #include <sysdeps/ipc.h>
  39. #include <sys/shm.h>
  40. #include <sys/sem.h>
  41. #include <sysdeps/ipc.h>
  42. #include <jack/shm.h>
  43. #include <jack/internal.h>
  44. #include <jack/version.h>
  45. #ifdef USE_POSIX_SHM
  46. static jack_shmtype_t jack_shmtype = shm_POSIX;
  47. #else
  48. static jack_shmtype_t jack_shmtype = shm_SYSV;
  49. #endif
  50. /* interface-dependent forward declarations */
  51. static int jack_access_registry (jack_shm_info_t *ri);
  52. static int jack_create_registry (jack_shm_info_t *ri);
  53. static void jack_remove_shm (jack_shm_id_t *id);
  54. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  55. * common interface-independent section
  56. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  57. /* The JACK SHM registry is a chunk of memory for keeping track of the
  58. * shared memory used by each active JACK server. This allows the
  59. * server to clean up shared memory when it exits. To avoid memory
  60. * leakage due to kill -9, crashes or debugger-driven exits, this
  61. * cleanup is also done when a new instance of that server starts.
  62. */
  63. /* per-process global data for the SHM interfaces */
  64. static jack_shm_id_t registry_id; /* SHM id for the registry */
  65. static jack_shm_info_t registry_info = { /* SHM info for the registry */
  66. .index = JACK_SHM_NULL_INDEX,
  67. .attached_at = MAP_FAILED
  68. };
  69. /* pointers to registry header and array */
  70. static jack_shm_header_t *jack_shm_header = NULL;
  71. static jack_shm_registry_t *jack_shm_registry = NULL;
  72. static char jack_shm_server_prefix[JACK_SERVER_NAME_SIZE] = "";
  73. /* jack_shm_lock_registry() serializes updates to the shared memory
  74. * segment JACK uses to keep track of the SHM segements allocated to
  75. * all its processes, including multiple servers.
  76. *
  77. * This is not a high-contention lock, but it does need to work across
  78. * multiple processes. High transaction rates and realtime safety are
  79. * not required. Any solution needs to at least be portable to POSIX
  80. * and POSIX-like systems.
  81. *
  82. * We must be particularly careful to ensure that the lock be released
  83. * if the owning process terminates abnormally. Otherwise, a segfault
  84. * or kill -9 at the wrong moment could prevent JACK from ever running
  85. * again on that machine until after a reboot.
  86. */
  87. #ifndef USE_POSIX_SHM
  88. #define JACK_SHM_REGISTRY_KEY JACK_SEMAPHORE_KEY
  89. #endif
  90. static int semid = -1;
  91. /* all semaphore errors are fatal -- issue message, but do not return */
  92. static void
  93. semaphore_error (char *msg)
  94. {
  95. jack_error ("Fatal JACK semaphore error: %s (%s)",
  96. msg, strerror (errno));
  97. abort ();
  98. }
  99. static void
  100. semaphore_init ()
  101. {
  102. key_t semkey = JACK_SEMAPHORE_KEY;
  103. struct sembuf sbuf;
  104. int create_flags = IPC_CREAT | IPC_EXCL
  105. | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
  106. /* Get semaphore ID associated with this key. */
  107. if ((semid = semget(semkey, 0, 0)) == -1) {
  108. /* Semaphore does not exist - Create. */
  109. if ((semid = semget(semkey, 1, create_flags)) != -1) {
  110. /* Initialize the semaphore, allow one owner. */
  111. sbuf.sem_num = 0;
  112. sbuf.sem_op = 1;
  113. sbuf.sem_flg = 0;
  114. if (semop(semid, &sbuf, 1) == -1) {
  115. semaphore_error ("semop");
  116. }
  117. } else if (errno == EEXIST) {
  118. if ((semid = semget(semkey, 0, 0)) == -1) {
  119. semaphore_error ("semget");
  120. }
  121. } else {
  122. semaphore_error ("semget creation");
  123. }
  124. }
  125. }
  126. static inline void
  127. semaphore_add (int value)
  128. {
  129. struct sembuf sbuf;
  130. sbuf.sem_num = 0;
  131. sbuf.sem_op = value;
  132. sbuf.sem_flg = SEM_UNDO;
  133. if (semop(semid, &sbuf, 1) == -1) {
  134. semaphore_error ("semop");
  135. }
  136. }
  137. static void
  138. jack_shm_lock_registry (void)
  139. {
  140. if (semid == -1)
  141. semaphore_init ();
  142. semaphore_add (-1);
  143. }
  144. static void
  145. jack_shm_unlock_registry (void)
  146. {
  147. semaphore_add (1);
  148. }
  149. static void
  150. jack_shm_init_registry ()
  151. {
  152. /* registry must be locked */
  153. int i;
  154. memset (jack_shm_header, 0, JACK_SHM_REGISTRY_SIZE);
  155. jack_shm_header->magic = JACK_SHM_MAGIC;
  156. jack_shm_header->protocol = jack_protocol_version;
  157. jack_shm_header->type = jack_shmtype;
  158. jack_shm_header->size = JACK_SHM_REGISTRY_SIZE;
  159. jack_shm_header->hdr_len = sizeof (jack_shm_header_t);
  160. jack_shm_header->entry_len = sizeof (jack_shm_registry_t);
  161. for (i = 0; i < MAX_SHM_ID; ++i) {
  162. jack_shm_registry[i].index = i;
  163. }
  164. }
  165. static int
  166. jack_shm_validate_registry ()
  167. {
  168. /* registry must be locked */
  169. if ((jack_shm_header->magic == JACK_SHM_MAGIC)
  170. && (jack_shm_header->protocol == jack_protocol_version)
  171. && (jack_shm_header->type == jack_shmtype)
  172. && (jack_shm_header->size == JACK_SHM_REGISTRY_SIZE)
  173. && (jack_shm_header->hdr_len == sizeof (jack_shm_header_t))
  174. && (jack_shm_header->entry_len == sizeof (jack_shm_registry_t))) {
  175. return 0; /* registry OK */
  176. }
  177. return -1;
  178. }
  179. /* set a unique per-user, per-server shm prefix string
  180. *
  181. * According to the POSIX standard:
  182. *
  183. * "The name argument conforms to the construction rules for a
  184. * pathname. If name begins with the slash character, then processes
  185. * calling shm_open() with the same value of name refer to the same
  186. * shared memory object, as long as that name has not been
  187. * removed. If name does not begin with the slash character, the
  188. * effect is implementation-defined. The interpretation of slash
  189. * characters other than the leading slash character in name is
  190. * implementation-defined."
  191. *
  192. * Since the Linux implementation does not allow slashes *within* the
  193. * name, in the interest of portability we use colons instead.
  194. */
  195. static void
  196. jack_set_server_prefix (const char *server_name)
  197. {
  198. snprintf (jack_shm_server_prefix, sizeof (jack_shm_server_prefix),
  199. "/jack-%d:%s:", getuid (), server_name);
  200. }
  201. /* gain server addressability to shared memory registration segment
  202. *
  203. * returns: 0 if successful
  204. */
  205. static int
  206. jack_server_initialize_shm (int new_registry)
  207. {
  208. int rc;
  209. if (jack_shm_header)
  210. return 0; /* already initialized */
  211. jack_shm_lock_registry ();
  212. rc = jack_access_registry (&registry_info);
  213. if (new_registry) {
  214. jack_remove_shm (&registry_id);
  215. rc = ENOENT;
  216. }
  217. switch (rc) {
  218. case ENOENT: /* registry does not exist */
  219. rc = jack_create_registry (&registry_info);
  220. break;
  221. case 0: /* existing registry */
  222. if (jack_shm_validate_registry () == 0)
  223. break;
  224. /* else it was invalid, so fall through */
  225. case EINVAL: /* bad registry */
  226. /* Apparently, this registry was created by an older
  227. * JACK version. Delete it so we can try again. */
  228. jack_release_shm (&registry_info);
  229. jack_remove_shm (&registry_id);
  230. if ((rc = jack_create_registry (&registry_info)) != 0) {
  231. jack_error ("incompatible shm registry (%s)",
  232. strerror (errno));
  233. #ifndef USE_POSIX_SHM
  234. jack_error ("to delete, use `ipcrm -M 0x%0.8x'",
  235. JACK_SHM_REGISTRY_KEY);
  236. #endif
  237. }
  238. break;
  239. default: /* failure return code */
  240. break;
  241. }
  242. jack_shm_unlock_registry ();
  243. return rc;
  244. }
  245. /* gain client addressability to shared memory registration segment
  246. *
  247. * NOTE: this function is no longer used for server initialization,
  248. * instead it calls jack_register_server().
  249. *
  250. * returns: 0 if successful
  251. */
  252. int
  253. jack_initialize_shm (const char *server_name)
  254. {
  255. int rc;
  256. if (jack_shm_header)
  257. return 0; /* already initialized */
  258. jack_set_server_prefix (server_name);
  259. jack_shm_lock_registry ();
  260. if ((rc = jack_access_registry (&registry_info)) == 0) {
  261. if ((rc = jack_shm_validate_registry ()) != 0) {
  262. jack_error ("Incompatible shm registry, "
  263. "are jackd and libjack in sync?");
  264. }
  265. }
  266. jack_shm_unlock_registry ();
  267. return rc;
  268. }
  269. void
  270. jack_destroy_shm (jack_shm_info_t* si)
  271. {
  272. /* must NOT have the registry locked */
  273. if (si->index == JACK_SHM_NULL_INDEX)
  274. return; /* segment not allocated */
  275. jack_remove_shm (&jack_shm_registry[si->index].id);
  276. jack_release_shm_info (si->index);
  277. }
  278. jack_shm_registry_t *
  279. jack_get_free_shm_info ()
  280. {
  281. /* registry must be locked */
  282. jack_shm_registry_t* si = NULL;
  283. int i;
  284. for (i = 0; i < MAX_SHM_ID; ++i) {
  285. if (jack_shm_registry[i].size == 0) {
  286. break;
  287. }
  288. }
  289. if (i < MAX_SHM_ID) {
  290. si = &jack_shm_registry[i];
  291. }
  292. return si;
  293. }
  294. static inline void
  295. jack_release_shm_entry (jack_shm_registry_index_t index)
  296. {
  297. /* the registry must be locked */
  298. jack_shm_registry[index].size = 0;
  299. jack_shm_registry[index].allocator = 0;
  300. memset (&jack_shm_registry[index].id, 0,
  301. sizeof (jack_shm_registry[index].id));
  302. }
  303. void
  304. jack_release_shm_info (jack_shm_registry_index_t index)
  305. {
  306. /* must NOT have the registry locked */
  307. if (jack_shm_registry[index].allocator == getpid()) {
  308. jack_shm_lock_registry ();
  309. jack_release_shm_entry (index);
  310. jack_shm_unlock_registry ();
  311. }
  312. }
  313. /* Claim server_name for this process.
  314. *
  315. * returns 0 if successful
  316. * EEXIST if server_name was already active for this user
  317. * ENOSPC if server registration limit reached
  318. * ENOMEM if unable to access shared memory registry
  319. */
  320. int
  321. jack_register_server (const char *server_name, int new_registry)
  322. {
  323. int i;
  324. pid_t my_pid = getpid ();
  325. jack_set_server_prefix (server_name);
  326. jack_info ("JACK compiled with %s SHM support.", JACK_SHM_TYPE);
  327. if (jack_server_initialize_shm (new_registry))
  328. return ENOMEM;
  329. jack_shm_lock_registry ();
  330. /* See if server_name already registered. Since server names
  331. * are per-user, we register the unique server prefix string.
  332. */
  333. for (i = 0; i < MAX_SERVERS; i++) {
  334. if (strncmp (jack_shm_header->server[i].name,
  335. jack_shm_server_prefix,
  336. JACK_SERVER_NAME_SIZE) != 0)
  337. continue; /* no match */
  338. if (jack_shm_header->server[i].pid == my_pid)
  339. return 0; /* it's me */
  340. /* see if server still exists */
  341. if (kill (jack_shm_header->server[i].pid, 0) == 0) {
  342. return EEXIST; /* other server running */
  343. }
  344. /* it's gone, reclaim this entry */
  345. memset (&jack_shm_header->server[i], 0,
  346. sizeof (jack_shm_server_t));
  347. }
  348. /* find a free entry */
  349. for (i = 0; i < MAX_SERVERS; i++) {
  350. if (jack_shm_header->server[i].pid == 0)
  351. break;
  352. }
  353. if (i >= MAX_SERVERS)
  354. return ENOSPC; /* out of space */
  355. /* claim it */
  356. jack_shm_header->server[i].pid = my_pid;
  357. strncpy (jack_shm_header->server[i].name,
  358. jack_shm_server_prefix,
  359. JACK_SERVER_NAME_SIZE);
  360. jack_shm_unlock_registry ();
  361. return 0;
  362. }
  363. /* release server_name registration */
  364. void
  365. jack_unregister_server (const char *server_name /* unused */)
  366. {
  367. int i;
  368. pid_t my_pid = getpid ();
  369. jack_shm_lock_registry ();
  370. for (i = 0; i < MAX_SERVERS; i++) {
  371. if (jack_shm_header->server[i].pid == my_pid) {
  372. memset (&jack_shm_header->server[i], 0,
  373. sizeof (jack_shm_server_t));
  374. }
  375. }
  376. jack_shm_unlock_registry ();
  377. }
  378. /* called for server startup and termination */
  379. int
  380. jack_cleanup_shm ()
  381. {
  382. int i;
  383. int destroy;
  384. jack_shm_info_t copy;
  385. pid_t my_pid = getpid ();
  386. jack_shm_lock_registry ();
  387. for (i = 0; i < MAX_SHM_ID; i++) {
  388. jack_shm_registry_t* r;
  389. r = &jack_shm_registry[i];
  390. memcpy (&copy, r, sizeof (jack_shm_info_t));
  391. destroy = FALSE;
  392. /* ignore unused entries */
  393. if (r->allocator == 0)
  394. continue;
  395. /* is this my shm segment? */
  396. if (r->allocator == my_pid) {
  397. /* allocated by this process, so unattach
  398. and destroy. */
  399. jack_release_shm (&copy);
  400. destroy = TRUE;
  401. } else {
  402. /* see if allocator still exists */
  403. if (kill (r->allocator, 0)) {
  404. if (errno == ESRCH) {
  405. /* allocator no longer exists,
  406. * so destroy */
  407. destroy = TRUE;
  408. }
  409. }
  410. }
  411. if (destroy) {
  412. int index = copy.index;
  413. if ((index >= 0) && (index < MAX_SHM_ID)) {
  414. jack_remove_shm (&jack_shm_registry[index].id);
  415. jack_release_shm_entry (index);
  416. }
  417. r->size = 0;
  418. r->allocator = 0;
  419. }
  420. }
  421. jack_shm_unlock_registry ();
  422. return TRUE;
  423. }
  424. /* resize a shared memory segment
  425. *
  426. * There is no way to resize a System V shm segment. Resizing is
  427. * possible with POSIX shm, but not with the non-conformant Mac OS X
  428. * implementation. Since POSIX shm is mainly used on that platform,
  429. * it's simpler to treat them both the same.
  430. *
  431. * So, we always resize by deleting and reallocating. This is
  432. * tricky, because the old segment will not disappear until
  433. * all the clients have released it. We only do what we can
  434. * from here.
  435. *
  436. * This is not done under a single lock. I don't even want to think
  437. * about all the things that could possibly go wrong if multple
  438. * processes tried to resize the same segment concurrently. That
  439. * probably doesn't happen.
  440. */
  441. int
  442. jack_resize_shm (jack_shm_info_t* si, jack_shmsize_t size)
  443. {
  444. jack_release_shm (si);
  445. jack_destroy_shm (si);
  446. if (jack_shmalloc (size, si)) {
  447. return -1;
  448. }
  449. return jack_attach_shm (si);
  450. }
  451. #ifdef USE_POSIX_SHM
  452. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  453. * POSIX interface-dependent functions
  454. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  455. /* gain addressability to existing SHM registry segment
  456. *
  457. * sets up global registry pointers, if successful
  458. *
  459. * returns: 0 if existing registry accessed successfully
  460. * ENOENT if registry does not exist
  461. * EINVAL if registry exists, but has the wrong size
  462. */
  463. static int
  464. jack_access_registry (jack_shm_info_t *ri)
  465. {
  466. /* registry must be locked */
  467. int shm_fd;
  468. strncpy (registry_id, "/jack-shm-registry", sizeof (registry_id));
  469. /* try to open an existing segment */
  470. if ((shm_fd = shm_open (registry_id, O_RDWR, 0666)) < 0) {
  471. int rc = errno;
  472. if (errno != ENOENT) {
  473. jack_error ("cannot open existing shm registry segment"
  474. " (%s)", strerror (errno));
  475. }
  476. close (shm_fd);
  477. return rc;
  478. }
  479. if ((ri->attached_at = mmap (0, JACK_SHM_REGISTRY_SIZE,
  480. PROT_READ|PROT_WRITE,
  481. MAP_SHARED, shm_fd, 0)) == MAP_FAILED) {
  482. jack_error ("cannot mmap shm registry segment (%s)",
  483. strerror (errno));
  484. close (shm_fd);
  485. return EINVAL;
  486. }
  487. /* set up global pointers */
  488. ri->index = JACK_SHM_REGISTRY_INDEX;
  489. jack_shm_header = ri->attached_at;
  490. jack_shm_registry = (jack_shm_registry_t *) (jack_shm_header + 1);
  491. return 0;
  492. }
  493. /* create a new SHM registry segment
  494. *
  495. * sets up global registry pointers, if successful
  496. *
  497. * returns: 0 if registry created successfully
  498. * nonzero error code if unable to allocate a new registry
  499. */
  500. static int
  501. jack_create_registry (jack_shm_info_t *ri)
  502. {
  503. /* registry must be locked */
  504. int shm_fd;
  505. strncpy (registry_id, "/jack-shm-registry", sizeof (registry_id));
  506. if ((shm_fd = shm_open (registry_id, O_RDWR|O_CREAT, 0666)) < 0) {
  507. int rc = errno;
  508. jack_error ("cannot create shm registry segment (%s)",
  509. strerror (errno));
  510. return rc;
  511. }
  512. /* Set the desired segment size. NOTE: the non-conformant Mac
  513. * OS X POSIX shm only allows ftruncate() on segment creation.
  514. */
  515. if (ftruncate (shm_fd, JACK_SHM_REGISTRY_SIZE) < 0) {
  516. int rc = errno;
  517. jack_error ("cannot set registry size (%s)", strerror (errno));
  518. jack_remove_shm (&registry_id);
  519. close (shm_fd);
  520. return rc;
  521. }
  522. if ((ri->attached_at = mmap (0, JACK_SHM_REGISTRY_SIZE,
  523. PROT_READ|PROT_WRITE,
  524. MAP_SHARED, shm_fd, 0)) == MAP_FAILED) {
  525. jack_error ("cannot mmap shm registry segment (%s)",
  526. strerror (errno));
  527. jack_remove_shm (&registry_id);
  528. close (shm_fd);
  529. return EINVAL;
  530. }
  531. /* set up global pointers */
  532. ri->index = JACK_SHM_REGISTRY_INDEX;
  533. jack_shm_header = ri->attached_at;
  534. jack_shm_registry = (jack_shm_registry_t *) (jack_shm_header + 1);
  535. /* initialize registry contents */
  536. jack_shm_init_registry ();
  537. return 0;
  538. }
  539. static void
  540. jack_remove_shm (jack_shm_id_t *id)
  541. {
  542. /* registry may or may not be locked */
  543. /* note that in many cases the client has already removed
  544. the shm segment, so this failing is not an error.
  545. XXX it would be good to differentiate between these
  546. two conditions.
  547. */
  548. shm_unlink ((char *) id);
  549. }
  550. void
  551. jack_release_shm (jack_shm_info_t* si)
  552. {
  553. /* registry may or may not be locked */
  554. if (si->attached_at != MAP_FAILED) {
  555. munmap (si->attached_at, jack_shm_registry[si->index].size);
  556. }
  557. }
  558. /* allocate a POSIX shared memory segment */
  559. int
  560. jack_shmalloc (jack_shmsize_t size, jack_shm_info_t* si)
  561. {
  562. jack_shm_registry_t* registry;
  563. int shm_fd;
  564. int rc = -1;
  565. char name[SHM_NAME_MAX+1];
  566. jack_shm_lock_registry ();
  567. if ((registry = jack_get_free_shm_info ()) == NULL) {
  568. jack_error ("shm registry full");
  569. goto unlock;
  570. }
  571. /* On Mac OS X, the maximum length of a shared memory segment
  572. * name is SHM_NAME_MAX (instead of NAME_MAX or PATH_MAX as
  573. * defined by the standard). Unfortunately, Apple sets this
  574. * value so small (about 31 bytes) that it is useless for
  575. * actual names. So, we construct a short name from the
  576. * registry index for uniqueness.
  577. */
  578. snprintf (name, sizeof (name), "/jack-%d", registry->index);
  579. if (strlen (name) >= sizeof (registry->id)) {
  580. jack_error ("shm segment name too long %s", name);
  581. goto unlock;
  582. }
  583. if ((shm_fd = shm_open (name, O_RDWR|O_CREAT, 0666)) < 0) {
  584. jack_error ("cannot create shm segment %s (%s)",
  585. name, strerror (errno));
  586. goto unlock;
  587. }
  588. if (ftruncate (shm_fd, size) < 0) {
  589. jack_error ("cannot set size of engine shm "
  590. "registry 0 (%s)",
  591. strerror (errno));
  592. close (shm_fd);
  593. goto unlock;
  594. }
  595. close (shm_fd);
  596. registry->size = size;
  597. strncpy (registry->id, name, sizeof (registry->id));
  598. registry->allocator = getpid();
  599. si->index = registry->index;
  600. si->attached_at = MAP_FAILED; /* not attached */
  601. rc = 0; /* success */
  602. unlock:
  603. jack_shm_unlock_registry ();
  604. return rc;
  605. }
  606. int
  607. jack_attach_shm (jack_shm_info_t* si)
  608. {
  609. int shm_fd;
  610. jack_shm_registry_t *registry = &jack_shm_registry[si->index];
  611. if ((shm_fd = shm_open (registry->id,
  612. O_RDWR, 0666)) < 0) {
  613. jack_error ("cannot open shm segment %s (%s)", registry->id,
  614. strerror (errno));
  615. return -1;
  616. }
  617. if ((si->attached_at = mmap (0, registry->size, PROT_READ|PROT_WRITE,
  618. MAP_SHARED, shm_fd, 0)) == MAP_FAILED) {
  619. jack_error ("cannot mmap shm segment %s (%s)",
  620. registry->id,
  621. strerror (errno));
  622. close (shm_fd);
  623. return -1;
  624. }
  625. close (shm_fd);
  626. return 0;
  627. }
  628. #else
  629. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  630. * System V interface-dependent functions
  631. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  632. /* gain addressability to existing SHM registry segment
  633. *
  634. * sets up global registry pointers, if successful
  635. *
  636. * returns: 0 if existing registry accessed successfully
  637. * ENOENT if registry does not exist
  638. * EINVAL if registry exists, but has the wrong size
  639. * other nonzero error code if unable to access registry
  640. */
  641. static int
  642. jack_access_registry (jack_shm_info_t *ri)
  643. {
  644. /* registry must be locked */
  645. /* try without IPC_CREAT to get existing segment */
  646. if ((registry_id = shmget (JACK_SHM_REGISTRY_KEY,
  647. JACK_SHM_REGISTRY_SIZE, 0666)) < 0) {
  648. switch (errno) {
  649. case ENOENT: /* segment does not exist */
  650. return ENOENT;
  651. case EINVAL: /* segment exists, but too small */
  652. /* attempt minimum size access */
  653. registry_id = shmget (JACK_SHM_REGISTRY_KEY, 1, 0666);
  654. return EINVAL;
  655. default: /* or other error */
  656. jack_error ("unable to access shm registry (%s)",
  657. strerror (errno));
  658. return errno;
  659. }
  660. }
  661. if ((ri->attached_at = shmat (registry_id, 0, 0)) < 0) {
  662. jack_error ("cannot attach shm registry segment (%s)",
  663. strerror (errno));
  664. return EINVAL;
  665. }
  666. /* set up global pointers */
  667. ri->index = JACK_SHM_REGISTRY_INDEX;
  668. jack_shm_header = ri->attached_at;
  669. jack_shm_registry = (jack_shm_registry_t *) (jack_shm_header + 1);
  670. return 0;
  671. }
  672. /* create a new SHM registry segment
  673. *
  674. * sets up global registry pointers, if successful
  675. *
  676. * returns: 0 if registry created successfully
  677. * nonzero error code if unable to allocate a new registry
  678. */
  679. static int
  680. jack_create_registry (jack_shm_info_t *ri)
  681. {
  682. /* registry must be locked */
  683. if ((registry_id = shmget (JACK_SHM_REGISTRY_KEY,
  684. JACK_SHM_REGISTRY_SIZE,
  685. 0666|IPC_CREAT)) < 0) {
  686. jack_error ("cannot create shm registry segment (%s)",
  687. strerror (errno));
  688. return errno;
  689. }
  690. if ((ri->attached_at = shmat (registry_id, 0, 0)) < 0) {
  691. jack_error ("cannot attach shm registry segment (%s)",
  692. strerror (errno));
  693. return EINVAL;
  694. }
  695. /* set up global pointers */
  696. ri->index = JACK_SHM_REGISTRY_INDEX;
  697. jack_shm_header = ri->attached_at;
  698. jack_shm_registry = (jack_shm_registry_t *) (jack_shm_header + 1);
  699. /* initialize registry contents */
  700. jack_shm_init_registry ();
  701. return 0;
  702. }
  703. static void
  704. jack_remove_shm (jack_shm_id_t *id)
  705. {
  706. /* registry may or may not be locked */
  707. /* this call can fail if we are attempting to
  708. remove a segment that was already deleted
  709. by the client. XXX i suppose the
  710. function should take a "canfail" argument.
  711. */
  712. shmctl (*id, IPC_RMID, NULL);
  713. }
  714. void
  715. jack_release_shm (jack_shm_info_t* si)
  716. {
  717. /* registry may or may not be locked */
  718. if (si->attached_at != MAP_FAILED) {
  719. shmdt (si->attached_at);
  720. }
  721. }
  722. int
  723. jack_shmalloc (jack_shmsize_t size, jack_shm_info_t* si)
  724. {
  725. int shmflags;
  726. int shmid;
  727. int rc = -1;
  728. jack_shm_registry_t* registry;
  729. jack_shm_lock_registry ();
  730. if ((registry = jack_get_free_shm_info ())) {
  731. shmflags = 0666 | IPC_CREAT | IPC_EXCL;
  732. if ((shmid = shmget (IPC_PRIVATE, size, shmflags)) >= 0) {
  733. registry->size = size;
  734. registry->id = shmid;
  735. registry->allocator = getpid();
  736. si->index = registry->index;
  737. si->attached_at = MAP_FAILED; /* not attached */
  738. rc = 0;
  739. } else {
  740. jack_error ("cannot create shm segment (%s)",
  741. strerror (errno));
  742. }
  743. }
  744. jack_shm_unlock_registry ();
  745. return rc;
  746. }
  747. int
  748. jack_attach_shm (jack_shm_info_t* si)
  749. {
  750. if ((si->attached_at = shmat (jack_shm_registry[si->index].id,
  751. 0, 0)) < 0) {
  752. jack_error ("cannot attach shm segment (%s)",
  753. strerror (errno));
  754. jack_release_shm_info (si->index);
  755. return -1;
  756. }
  757. return 0;
  758. }
  759. #endif /* !USE_POSIX_SHM */