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.

928 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 "shm.h"
  43. #include "internal.h"
  44. #include "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. }
  143. semaphore_add (-1);
  144. }
  145. static void
  146. jack_shm_unlock_registry (void)
  147. {
  148. semaphore_add (1);
  149. }
  150. static void
  151. jack_shm_init_registry ()
  152. {
  153. /* registry must be locked */
  154. int i;
  155. memset (jack_shm_header, 0, JACK_SHM_REGISTRY_SIZE);
  156. jack_shm_header->magic = JACK_SHM_MAGIC;
  157. jack_shm_header->protocol = jack_protocol_version;
  158. jack_shm_header->type = jack_shmtype;
  159. jack_shm_header->size = JACK_SHM_REGISTRY_SIZE;
  160. jack_shm_header->hdr_len = sizeof(jack_shm_header_t);
  161. jack_shm_header->entry_len = sizeof(jack_shm_registry_t);
  162. for (i = 0; i < MAX_SHM_ID; ++i)
  163. jack_shm_registry[i].index = i;
  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. }
  212. jack_shm_lock_registry ();
  213. rc = jack_access_registry (&registry_info);
  214. if (new_registry) {
  215. jack_remove_shm (&registry_id);
  216. rc = ENOENT;
  217. }
  218. switch (rc) {
  219. case ENOENT: /* registry does not exist */
  220. rc = jack_create_registry (&registry_info);
  221. break;
  222. case 0: /* existing registry */
  223. if (jack_shm_validate_registry () == 0) {
  224. break;
  225. }
  226. /* else it was invalid, so fall through */
  227. case EINVAL: /* bad registry */
  228. /* Apparently, this registry was created by an older
  229. * JACK version. Delete it so we can try again. */
  230. jack_release_shm (&registry_info);
  231. jack_remove_shm (&registry_id);
  232. if ((rc = jack_create_registry (&registry_info)) != 0) {
  233. jack_error ("incompatible shm registry (%s)",
  234. strerror (errno));
  235. #ifndef USE_POSIX_SHM
  236. jack_error ("to delete, use `ipcrm -M 0x%0.8x'",
  237. JACK_SHM_REGISTRY_KEY);
  238. #endif
  239. }
  240. break;
  241. default: /* failure return code */
  242. break;
  243. }
  244. jack_shm_unlock_registry ();
  245. return rc;
  246. }
  247. /* gain client addressability to shared memory registration segment
  248. *
  249. * NOTE: this function is no longer used for server initialization,
  250. * instead it calls jack_register_server().
  251. *
  252. * returns: 0 if successful
  253. */
  254. int
  255. jack_initialize_shm (const char *server_name)
  256. {
  257. int rc;
  258. if (jack_shm_header) {
  259. return 0; /* already initialized */
  260. }
  261. jack_set_server_prefix (server_name);
  262. jack_shm_lock_registry ();
  263. if ((rc = jack_access_registry (&registry_info)) == 0) {
  264. if ((rc = jack_shm_validate_registry ()) != 0) {
  265. jack_error ("Incompatible shm registry, "
  266. "are jackd and libjack in sync?");
  267. }
  268. }
  269. jack_shm_unlock_registry ();
  270. return rc;
  271. }
  272. void
  273. jack_destroy_shm (jack_shm_info_t* si)
  274. {
  275. /* must NOT have the registry locked */
  276. if (si->index == JACK_SHM_NULL_INDEX) {
  277. return; /* segment not allocated */
  278. }
  279. jack_remove_shm (&jack_shm_registry[si->index].id);
  280. jack_release_shm_info (si->index);
  281. }
  282. jack_shm_registry_t *
  283. jack_get_free_shm_info ()
  284. {
  285. /* registry must be locked */
  286. jack_shm_registry_t* si = NULL;
  287. int i;
  288. for (i = 0; i < MAX_SHM_ID; ++i) {
  289. if (jack_shm_registry[i].size == 0) {
  290. break;
  291. }
  292. }
  293. if (i < MAX_SHM_ID) {
  294. si = &jack_shm_registry[i];
  295. }
  296. return si;
  297. }
  298. static inline void
  299. jack_release_shm_entry (jack_shm_registry_index_t index)
  300. {
  301. /* the registry must be locked */
  302. jack_shm_registry[index].size = 0;
  303. jack_shm_registry[index].allocator = 0;
  304. memset (&jack_shm_registry[index].id, 0,
  305. sizeof(jack_shm_registry[index].id));
  306. }
  307. void
  308. jack_release_shm_info (jack_shm_registry_index_t index)
  309. {
  310. /* must NOT have the registry locked */
  311. if (jack_shm_registry[index].allocator == getpid ()) {
  312. jack_shm_lock_registry ();
  313. jack_release_shm_entry (index);
  314. jack_shm_unlock_registry ();
  315. }
  316. }
  317. /* Claim server_name for this process.
  318. *
  319. * returns 0 if successful
  320. * EEXIST if server_name was already active for this user
  321. * ENOSPC if server registration limit reached
  322. * ENOMEM if unable to access shared memory registry
  323. */
  324. int
  325. jack_register_server (const char *server_name, int new_registry)
  326. {
  327. int i;
  328. pid_t my_pid = getpid ();
  329. jack_set_server_prefix (server_name);
  330. jack_info ("JACK compiled with %s SHM support.", JACK_SHM_TYPE);
  331. if (jack_server_initialize_shm (new_registry)) {
  332. return ENOMEM;
  333. }
  334. jack_shm_lock_registry ();
  335. /* See if server_name already registered. Since server names
  336. * are per-user, we register the unique server prefix string.
  337. */
  338. for (i = 0; i < MAX_SERVERS; i++) {
  339. if (strncmp (jack_shm_header->server[i].name,
  340. jack_shm_server_prefix,
  341. JACK_SERVER_NAME_SIZE) != 0) {
  342. continue; /* no match */
  343. }
  344. if (jack_shm_header->server[i].pid == my_pid) {
  345. jack_shm_unlock_registry ();
  346. return 0; /* it's me */
  347. }
  348. /* see if server still exists */
  349. if (kill (jack_shm_header->server[i].pid, 0) == 0) {
  350. jack_shm_unlock_registry ();
  351. return EEXIST; /* other server running */
  352. }
  353. /* it's gone, reclaim this entry */
  354. memset (&jack_shm_header->server[i], 0,
  355. sizeof(jack_shm_server_t));
  356. }
  357. /* find a free entry */
  358. for (i = 0; i < MAX_SERVERS; i++)
  359. if (jack_shm_header->server[i].pid == 0) {
  360. break;
  361. }
  362. if (i >= MAX_SERVERS) {
  363. jack_shm_unlock_registry ();
  364. return ENOSPC; /* out of space */
  365. }
  366. /* claim it */
  367. jack_shm_header->server[i].pid = my_pid;
  368. strncpy (jack_shm_header->server[i].name,
  369. jack_shm_server_prefix,
  370. JACK_SERVER_NAME_SIZE);
  371. jack_shm_unlock_registry ();
  372. return 0;
  373. }
  374. /* release server_name registration */
  375. void
  376. jack_unregister_server (const char *server_name /* unused */)
  377. {
  378. int i;
  379. pid_t my_pid = getpid ();
  380. jack_shm_lock_registry ();
  381. for (i = 0; i < MAX_SERVERS; i++) {
  382. if (jack_shm_header->server[i].pid == my_pid) {
  383. memset (&jack_shm_header->server[i], 0,
  384. sizeof(jack_shm_server_t));
  385. }
  386. }
  387. jack_shm_unlock_registry ();
  388. }
  389. /* called for server startup and termination */
  390. int
  391. jack_cleanup_shm ()
  392. {
  393. int i;
  394. int destroy;
  395. jack_shm_info_t copy;
  396. pid_t my_pid = getpid ();
  397. jack_shm_lock_registry ();
  398. for (i = 0; i < MAX_SHM_ID; i++) {
  399. jack_shm_registry_t* r;
  400. r = &jack_shm_registry[i];
  401. memcpy (&copy, r, sizeof(jack_shm_info_t));
  402. destroy = FALSE;
  403. /* ignore unused entries */
  404. if (r->allocator == 0) {
  405. continue;
  406. }
  407. /* is this my shm segment? */
  408. if (r->allocator == my_pid) {
  409. /* allocated by this process, so unattach
  410. and destroy. */
  411. jack_release_shm (&copy);
  412. destroy = TRUE;
  413. } else {
  414. /* see if allocator still exists */
  415. if (kill (r->allocator, 0)) {
  416. if (errno == ESRCH) {
  417. /* allocator no longer exists,
  418. * so destroy */
  419. destroy = TRUE;
  420. }
  421. }
  422. }
  423. if (destroy) {
  424. int index = copy.index;
  425. if ((index >= 0) && (index < MAX_SHM_ID)) {
  426. jack_remove_shm (&jack_shm_registry[index].id);
  427. jack_release_shm_entry (index);
  428. }
  429. r->size = 0;
  430. r->allocator = 0;
  431. }
  432. }
  433. jack_shm_unlock_registry ();
  434. return TRUE;
  435. }
  436. /* resize a shared memory segment
  437. *
  438. * There is no way to resize a System V shm segment. Resizing is
  439. * possible with POSIX shm, but not with the non-conformant Mac OS X
  440. * implementation. Since POSIX shm is mainly used on that platform,
  441. * it's simpler to treat them both the same.
  442. *
  443. * So, we always resize by deleting and reallocating. This is
  444. * tricky, because the old segment will not disappear until
  445. * all the clients have released it. We only do what we can
  446. * from here.
  447. *
  448. * This is not done under a single lock. I don't even want to think
  449. * about all the things that could possibly go wrong if multple
  450. * processes tried to resize the same segment concurrently. That
  451. * probably doesn't happen.
  452. */
  453. int
  454. jack_resize_shm (jack_shm_info_t* si, jack_shmsize_t size)
  455. {
  456. jack_release_shm (si);
  457. jack_destroy_shm (si);
  458. if (jack_shmalloc (size, si)) {
  459. return -1;
  460. }
  461. return jack_attach_shm (si);
  462. }
  463. #ifdef USE_POSIX_SHM
  464. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  465. * POSIX interface-dependent functions
  466. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  467. /* gain addressability to existing SHM registry segment
  468. *
  469. * sets up global registry pointers, if successful
  470. *
  471. * returns: 0 if existing registry accessed successfully
  472. * ENOENT if registry does not exist
  473. * EINVAL if registry exists, but has the wrong size
  474. */
  475. static int
  476. jack_access_registry (jack_shm_info_t *ri)
  477. {
  478. /* registry must be locked */
  479. int shm_fd;
  480. strncpy (registry_id, "/jack-shm-registry", sizeof(registry_id));
  481. /* try to open an existing segment */
  482. if ((shm_fd = shm_open (registry_id, O_RDWR, 0666)) < 0) {
  483. int rc = errno;
  484. if (errno != ENOENT) {
  485. jack_error ("cannot open existing shm registry segment"
  486. " (%s)", strerror (errno));
  487. }
  488. close (shm_fd);
  489. return rc;
  490. }
  491. if ((ri->attached_at = mmap (0, JACK_SHM_REGISTRY_SIZE,
  492. PROT_READ | PROT_WRITE,
  493. MAP_SHARED, shm_fd, 0)) == MAP_FAILED) {
  494. jack_error ("cannot mmap shm registry segment (%s)",
  495. strerror (errno));
  496. close (shm_fd);
  497. return EINVAL;
  498. }
  499. /* set up global pointers */
  500. ri->index = JACK_SHM_REGISTRY_INDEX;
  501. jack_shm_header = ri->attached_at;
  502. jack_shm_registry = (jack_shm_registry_t*)(jack_shm_header + 1);
  503. return 0;
  504. }
  505. /* create a new SHM registry segment
  506. *
  507. * sets up global registry pointers, if successful
  508. *
  509. * returns: 0 if registry created successfully
  510. * nonzero error code if unable to allocate a new registry
  511. */
  512. static int
  513. jack_create_registry (jack_shm_info_t *ri)
  514. {
  515. /* registry must be locked */
  516. int shm_fd;
  517. strncpy (registry_id, "/jack-shm-registry", sizeof(registry_id));
  518. if ((shm_fd = shm_open (registry_id, O_RDWR | O_CREAT, 0666)) < 0) {
  519. int rc = errno;
  520. jack_error ("cannot create shm registry segment (%s)",
  521. strerror (errno));
  522. return rc;
  523. }
  524. /* Set the desired segment size. NOTE: the non-conformant Mac
  525. * OS X POSIX shm only allows ftruncate() on segment creation.
  526. */
  527. if (ftruncate (shm_fd, JACK_SHM_REGISTRY_SIZE) < 0) {
  528. int rc = errno;
  529. jack_error ("cannot set registry size (%s)", strerror (errno));
  530. jack_remove_shm (&registry_id);
  531. close (shm_fd);
  532. return rc;
  533. }
  534. if ((ri->attached_at = mmap (0, JACK_SHM_REGISTRY_SIZE,
  535. PROT_READ | PROT_WRITE,
  536. MAP_SHARED, shm_fd, 0)) == MAP_FAILED) {
  537. jack_error ("cannot mmap shm registry segment (%s)",
  538. strerror (errno));
  539. jack_remove_shm (&registry_id);
  540. close (shm_fd);
  541. return EINVAL;
  542. }
  543. /* set up global pointers */
  544. ri->index = JACK_SHM_REGISTRY_INDEX;
  545. jack_shm_header = ri->attached_at;
  546. jack_shm_registry = (jack_shm_registry_t*)(jack_shm_header + 1);
  547. /* initialize registry contents */
  548. jack_shm_init_registry ();
  549. return 0;
  550. }
  551. static void
  552. jack_remove_shm (jack_shm_id_t *id)
  553. {
  554. /* registry may or may not be locked */
  555. /* note that in many cases the client has already removed
  556. the shm segment, so this failing is not an error.
  557. XXX it would be good to differentiate between these
  558. two conditions.
  559. */
  560. shm_unlink ((char*)id);
  561. }
  562. void
  563. jack_release_shm (jack_shm_info_t* si)
  564. {
  565. /* registry may or may not be locked */
  566. if (si->attached_at != MAP_FAILED) {
  567. munmap (si->attached_at, jack_shm_registry[si->index].size);
  568. }
  569. }
  570. /* allocate a POSIX shared memory segment */
  571. int
  572. jack_shmalloc (jack_shmsize_t size, jack_shm_info_t* si)
  573. {
  574. jack_shm_registry_t* registry;
  575. int shm_fd;
  576. int rc = -1;
  577. char name[SHM_NAME_MAX + 1];
  578. jack_shm_lock_registry ();
  579. if ((registry = jack_get_free_shm_info ()) == NULL) {
  580. jack_error ("shm registry full");
  581. goto unlock;
  582. }
  583. /* On Mac OS X, the maximum length of a shared memory segment
  584. * name is SHM_NAME_MAX (instead of NAME_MAX or PATH_MAX as
  585. * defined by the standard). Unfortunately, Apple sets this
  586. * value so small (about 31 bytes) that it is useless for
  587. * actual names. So, we construct a short name from the
  588. * registry index for uniqueness.
  589. */
  590. snprintf (name, sizeof(name), "/jack-%d", registry->index);
  591. if (strlen (name) >= sizeof(registry->id)) {
  592. jack_error ("shm segment name too long %s", name);
  593. goto unlock;
  594. }
  595. if ((shm_fd = shm_open (name, O_RDWR | O_CREAT, 0666)) < 0) {
  596. jack_error ("cannot create shm segment %s (%s)",
  597. name, strerror (errno));
  598. goto unlock;
  599. }
  600. if (ftruncate (shm_fd, size) < 0) {
  601. jack_error ("cannot set size of engine shm "
  602. "registry 0 (%s)",
  603. strerror (errno));
  604. close (shm_fd);
  605. goto unlock;
  606. }
  607. close (shm_fd);
  608. registry->size = size;
  609. strncpy (registry->id, name, sizeof(registry->id));
  610. registry->allocator = getpid ();
  611. si->index = registry->index;
  612. si->attached_at = MAP_FAILED; /* not attached */
  613. rc = 0; /* success */
  614. unlock:
  615. jack_shm_unlock_registry ();
  616. return rc;
  617. }
  618. int
  619. jack_attach_shm (jack_shm_info_t* si)
  620. {
  621. int shm_fd;
  622. jack_shm_registry_t *registry = &jack_shm_registry[si->index];
  623. if ((shm_fd = shm_open (registry->id,
  624. O_RDWR, 0666)) < 0) {
  625. jack_error ("cannot open shm segment %s (%s)", registry->id,
  626. strerror (errno));
  627. return -1;
  628. }
  629. if ((si->attached_at = mmap (0, registry->size, PROT_READ | PROT_WRITE,
  630. MAP_SHARED, shm_fd, 0)) == MAP_FAILED) {
  631. jack_error ("cannot mmap shm segment %s (%s)",
  632. registry->id,
  633. strerror (errno));
  634. close (shm_fd);
  635. return -1;
  636. }
  637. close (shm_fd);
  638. return 0;
  639. }
  640. #else
  641. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  642. * System V interface-dependent functions
  643. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  644. /* gain addressability to existing SHM registry segment
  645. *
  646. * sets up global registry pointers, if successful
  647. *
  648. * returns: 0 if existing registry accessed successfully
  649. * ENOENT if registry does not exist
  650. * EINVAL if registry exists, but has the wrong size
  651. * other nonzero error code if unable to access registry
  652. */
  653. static int
  654. jack_access_registry (jack_shm_info_t *ri)
  655. {
  656. /* registry must be locked */
  657. /* try without IPC_CREAT to get existing segment */
  658. if ((registry_id = shmget (JACK_SHM_REGISTRY_KEY,
  659. JACK_SHM_REGISTRY_SIZE, 0666)) < 0) {
  660. switch (errno) {
  661. case ENOENT: /* segment does not exist */
  662. return ENOENT;
  663. case EINVAL: /* segment exists, but too small */
  664. /* attempt minimum size access */
  665. registry_id = shmget (JACK_SHM_REGISTRY_KEY, 1, 0666);
  666. return EINVAL;
  667. default: /* or other error */
  668. jack_error ("unable to access shm registry (%s)",
  669. strerror (errno));
  670. return errno;
  671. }
  672. }
  673. if ((ri->attached_at = shmat (registry_id, 0, 0)) < 0) {
  674. jack_error ("cannot attach shm registry segment (%s)",
  675. strerror (errno));
  676. return EINVAL;
  677. }
  678. /* set up global pointers */
  679. ri->index = JACK_SHM_REGISTRY_INDEX;
  680. jack_shm_header = ri->attached_at;
  681. jack_shm_registry = (jack_shm_registry_t*)(jack_shm_header + 1);
  682. return 0;
  683. }
  684. /* create a new SHM registry segment
  685. *
  686. * sets up global registry pointers, if successful
  687. *
  688. * returns: 0 if registry created successfully
  689. * nonzero error code if unable to allocate a new registry
  690. */
  691. static int
  692. jack_create_registry (jack_shm_info_t *ri)
  693. {
  694. /* registry must be locked */
  695. if ((registry_id = shmget (JACK_SHM_REGISTRY_KEY,
  696. JACK_SHM_REGISTRY_SIZE,
  697. 0666 | IPC_CREAT)) < 0) {
  698. jack_error ("cannot create shm registry segment (%s)",
  699. strerror (errno));
  700. return errno;
  701. }
  702. if ((ri->attached_at = shmat (registry_id, 0, 0)) < 0) {
  703. jack_error ("cannot attach shm registry segment (%s)",
  704. strerror (errno));
  705. return EINVAL;
  706. }
  707. /* set up global pointers */
  708. ri->index = JACK_SHM_REGISTRY_INDEX;
  709. jack_shm_header = ri->attached_at;
  710. jack_shm_registry = (jack_shm_registry_t*)(jack_shm_header + 1);
  711. /* initialize registry contents */
  712. jack_shm_init_registry ();
  713. return 0;
  714. }
  715. static void
  716. jack_remove_shm (jack_shm_id_t *id)
  717. {
  718. /* registry may or may not be locked */
  719. /* this call can fail if we are attempting to
  720. remove a segment that was already deleted
  721. by the client. XXX i suppose the
  722. function should take a "canfail" argument.
  723. */
  724. shmctl (*id, IPC_RMID, NULL);
  725. }
  726. void
  727. jack_release_shm (jack_shm_info_t* si)
  728. {
  729. /* registry may or may not be locked */
  730. if (si->attached_at != MAP_FAILED) {
  731. shmdt (si->attached_at);
  732. }
  733. }
  734. int
  735. jack_shmalloc (jack_shmsize_t size, jack_shm_info_t* si)
  736. {
  737. int shmflags;
  738. int shmid;
  739. int rc = -1;
  740. jack_shm_registry_t* registry;
  741. jack_shm_lock_registry ();
  742. if ((registry = jack_get_free_shm_info ())) {
  743. shmflags = 0666 | IPC_CREAT | IPC_EXCL;
  744. if ((shmid = shmget (IPC_PRIVATE, size, shmflags)) >= 0) {
  745. registry->size = size;
  746. registry->id = shmid;
  747. registry->allocator = getpid ();
  748. si->index = registry->index;
  749. si->attached_at = MAP_FAILED; /* not attached */
  750. rc = 0;
  751. } else {
  752. jack_error ("cannot create shm segment (%s)",
  753. strerror (errno));
  754. }
  755. }
  756. jack_shm_unlock_registry ();
  757. return rc;
  758. }
  759. int
  760. jack_attach_shm (jack_shm_info_t* si)
  761. {
  762. if ((si->attached_at = shmat (jack_shm_registry[si->index].id,
  763. 0, 0)) < 0) {
  764. jack_error ("cannot attach shm segment (%s)",
  765. strerror (errno));
  766. jack_release_shm_info (si->index);
  767. return -1;
  768. }
  769. return 0;
  770. }
  771. #endif /* !USE_POSIX_SHM */