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

1252 lines
30KB

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