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.

914 lines
22KB

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