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