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.

1213 lines
29KB

  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. #ifdef USE_POSIX_SHM
  531. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  532. * POSIX interface-dependent functions
  533. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  534. /* gain addressability to existing SHM registry segment
  535. *
  536. * sets up global registry pointers, if successful
  537. *
  538. * returns: 0 if existing registry accessed successfully
  539. * ENOENT if registry does not exist
  540. * EINVAL if registry exists, but has the wrong size
  541. */
  542. static int
  543. jack_access_registry (jack_shm_info_t *ri)
  544. {
  545. /* registry must be locked */
  546. int shm_fd;
  547. strncpy (registry_id, "/jack-shm-registry", sizeof (registry_id));
  548. /* try to open an existing segment */
  549. if ((shm_fd = shm_open (registry_id, O_RDWR, 0666)) < 0) {
  550. int rc = errno;
  551. if (errno != ENOENT) {
  552. jack_error ("Cannot open existing shm registry segment"
  553. " (%s)", strerror (errno));
  554. }
  555. close (shm_fd);
  556. return rc;
  557. }
  558. if ((ri->ptr.attached_at = mmap (0, JACK_SHM_REGISTRY_SIZE,
  559. PROT_READ|PROT_WRITE,
  560. MAP_SHARED, shm_fd, 0)) == MAP_FAILED) {
  561. jack_error ("Cannot mmap shm registry segment (%s)",
  562. strerror (errno));
  563. close (shm_fd);
  564. return EINVAL;
  565. }
  566. /* set up global pointers */
  567. ri->index = JACK_SHM_REGISTRY_INDEX;
  568. jack_shm_header = ri->ptr.attached_at;
  569. jack_shm_registry = (jack_shm_registry_t *) (jack_shm_header + 1);
  570. close (shm_fd); // steph
  571. return 0;
  572. }
  573. /* create a new SHM registry segment
  574. *
  575. * sets up global registry pointers, if successful
  576. *
  577. * returns: 0 if registry created successfully
  578. * nonzero error code if unable to allocate a new registry
  579. */
  580. static int
  581. jack_create_registry (jack_shm_info_t *ri)
  582. {
  583. /* registry must be locked */
  584. int shm_fd;
  585. strncpy (registry_id, "/jack-shm-registry", sizeof (registry_id));
  586. if ((shm_fd = shm_open (registry_id, O_RDWR|O_CREAT, 0666)) < 0) {
  587. int rc = errno;
  588. jack_error ("Cannot create shm registry segment (%s)",
  589. strerror (errno));
  590. return rc;
  591. }
  592. /* Previous shm_open result depends of the actual value of umask, force correct file permisssion here */
  593. if (fchmod(shm_fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH) < 0) {
  594. jack_log("Cannot chmod jack-shm-registry (%s) %d %d", strerror (errno));
  595. }
  596. /* Set the desired segment size. NOTE: the non-conformant Mac
  597. * OS X POSIX shm only allows ftruncate() on segment creation.
  598. */
  599. if (ftruncate (shm_fd, JACK_SHM_REGISTRY_SIZE) < 0) {
  600. int rc = errno;
  601. jack_error ("Cannot set registry size (%s)", strerror (errno));
  602. jack_remove_shm (&registry_id);
  603. close (shm_fd);
  604. return rc;
  605. }
  606. if ((ri->ptr.attached_at = mmap (0, JACK_SHM_REGISTRY_SIZE,
  607. PROT_READ|PROT_WRITE,
  608. MAP_SHARED, shm_fd, 0)) == MAP_FAILED) {
  609. jack_error ("Cannot mmap shm registry segment (%s)",
  610. strerror (errno));
  611. jack_remove_shm (&registry_id);
  612. close (shm_fd);
  613. return EINVAL;
  614. }
  615. /* set up global pointers */
  616. ri->index = JACK_SHM_REGISTRY_INDEX;
  617. jack_shm_header = ri->ptr.attached_at;
  618. jack_shm_registry = (jack_shm_registry_t *) (jack_shm_header + 1);
  619. /* initialize registry contents */
  620. jack_shm_init_registry ();
  621. close (shm_fd); // steph
  622. return 0;
  623. }
  624. static void
  625. jack_remove_shm (jack_shm_id_t *id)
  626. {
  627. /* registry may or may not be locked */
  628. shm_unlink ((char *) id);
  629. }
  630. void
  631. jack_release_shm (jack_shm_info_t* si)
  632. {
  633. /* registry may or may not be locked */
  634. if (si->ptr.attached_at != MAP_FAILED) {
  635. munmap (si->ptr.attached_at, jack_shm_registry[si->index].size);
  636. }
  637. }
  638. /* allocate a POSIX shared memory segment */
  639. int
  640. jack_shmalloc (const char *shm_name, jack_shmsize_t size, jack_shm_info_t* si)
  641. {
  642. jack_shm_registry_t* registry;
  643. int shm_fd;
  644. int rc = -1;
  645. char name[SHM_NAME_MAX+1];
  646. if (jack_shm_lock_registry () < 0) {
  647. jack_error ("jack_shm_lock_registry fails...");
  648. return -1;
  649. }
  650. if ((registry = jack_get_free_shm_info ()) == NULL) {
  651. jack_error ("shm registry full");
  652. goto unlock;
  653. }
  654. /* On Mac OS X, the maximum length of a shared memory segment
  655. * name is SHM_NAME_MAX (instead of NAME_MAX or PATH_MAX as
  656. * defined by the standard). Unfortunately, Apple sets this
  657. * value so small (about 31 bytes) that it is useless for
  658. * actual names. So, we construct a short name from the
  659. * registry index for uniqueness and ignore the shm_name
  660. * parameter. Bah!
  661. */
  662. snprintf (name, sizeof (name), "/jack-%d-%d", GetUID(), registry->index);
  663. if (strlen (name) >= sizeof (registry->id)) {
  664. jack_error ("shm segment name too long %s", name);
  665. goto unlock;
  666. }
  667. if ((shm_fd = shm_open (name, O_RDWR|O_CREAT, 0666)) < 0) {
  668. jack_error ("Cannot create shm segment %s (%s)",
  669. name, strerror (errno));
  670. goto unlock;
  671. }
  672. if (ftruncate (shm_fd, size) < 0) {
  673. jack_error ("Cannot set size of engine shm "
  674. "registry 0 (%s)",
  675. strerror (errno));
  676. close (shm_fd);
  677. goto unlock;
  678. }
  679. close (shm_fd);
  680. registry->size = size;
  681. strncpy (registry->id, name, sizeof (registry->id));
  682. registry->allocator = GetPID();
  683. si->index = registry->index;
  684. si->ptr.attached_at = MAP_FAILED; /* not attached */
  685. rc = 0; /* success */
  686. unlock:
  687. jack_shm_unlock_registry ();
  688. return rc;
  689. }
  690. int
  691. jack_attach_shm (jack_shm_info_t* si)
  692. {
  693. int shm_fd;
  694. jack_shm_registry_t *registry = &jack_shm_registry[si->index];
  695. if ((shm_fd = shm_open (registry->id,
  696. O_RDWR, 0666)) < 0) {
  697. jack_error ("Cannot open shm segment %s (%s)", registry->id,
  698. strerror (errno));
  699. return -1;
  700. }
  701. if ((si->ptr.attached_at = mmap (0, registry->size, PROT_READ|PROT_WRITE,
  702. MAP_SHARED, shm_fd, 0)) == MAP_FAILED) {
  703. jack_error ("Cannot mmap shm segment %s (%s)",
  704. registry->id,
  705. strerror (errno));
  706. close (shm_fd);
  707. return -1;
  708. }
  709. close (shm_fd);
  710. return 0;
  711. }
  712. int
  713. jack_attach_shm_read (jack_shm_info_t* si)
  714. {
  715. int shm_fd;
  716. jack_shm_registry_t *registry = &jack_shm_registry[si->index];
  717. if ((shm_fd = shm_open (registry->id,
  718. O_RDONLY, 0666)) < 0) {
  719. jack_error ("Cannot open shm segment %s (%s)", registry->id,
  720. strerror (errno));
  721. return -1;
  722. }
  723. if ((si->ptr.attached_at = mmap (0, registry->size, PROT_READ,
  724. MAP_SHARED, shm_fd, 0)) == MAP_FAILED) {
  725. jack_error ("Cannot mmap shm segment %s (%s)",
  726. registry->id,
  727. strerror (errno));
  728. close (shm_fd);
  729. return -1;
  730. }
  731. close (shm_fd);
  732. return 0;
  733. }
  734. #elif WIN32
  735. static int
  736. jack_access_registry (jack_shm_info_t *ri)
  737. {
  738. /* registry must be locked */
  739. HANDLE shm_fd;
  740. strncpy (registry_id, "jack-shm-registry", sizeof (registry_id));
  741. /* try to open an existing segment */
  742. if ((shm_fd = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, registry_id)) == NULL) {
  743. int rc = GetLastError();
  744. if (rc != ERROR_FILE_NOT_FOUND) {
  745. jack_error ("Cannot open existing shm registry segment (%ld)", rc);
  746. }
  747. return rc;
  748. }
  749. if ((ri->ptr.attached_at = MapViewOfFile (shm_fd, FILE_MAP_ALL_ACCESS, 0, 0, JACK_SHM_REGISTRY_SIZE)) == NULL) {
  750. jack_error ("Cannot mmap shm registry segment (%ld)", GetLastError());
  751. jack_remove_shm (&registry_id);
  752. CloseHandle (shm_fd);
  753. return EINVAL;
  754. }
  755. /* set up global pointers */
  756. ri->index = JACK_SHM_REGISTRY_INDEX;
  757. jack_shm_header = ri->ptr.attached_at;
  758. jack_shm_registry = (jack_shm_registry_t *) (jack_shm_header + 1);
  759. //CloseHandle(shm_fd); // TO CHECK
  760. return 0;
  761. }
  762. static int
  763. jack_create_registry (jack_shm_info_t *ri)
  764. {
  765. /* registry must be locked */
  766. HANDLE shm_fd;
  767. strncpy (registry_id, "jack-shm-registry", sizeof (registry_id));
  768. if ((shm_fd = CreateFileMapping(INVALID_HANDLE_VALUE,
  769. 0, PAGE_READWRITE,
  770. 0, JACK_SHM_REGISTRY_SIZE,
  771. registry_id)) == NULL || (shm_fd == INVALID_HANDLE_VALUE)) {
  772. int rc = GetLastError();
  773. jack_error ("Cannot create shm registry segment (%ld)", rc);
  774. return rc;
  775. }
  776. if ((ri->ptr.attached_at = MapViewOfFile (shm_fd, FILE_MAP_ALL_ACCESS, 0, 0, JACK_SHM_REGISTRY_SIZE)) == NULL) {
  777. jack_error ("Cannot mmap shm registry segment (%ld)", GetLastError());
  778. jack_remove_shm (&registry_id);
  779. CloseHandle (shm_fd);
  780. return EINVAL;
  781. }
  782. /* set up global pointers */
  783. ri->index = JACK_SHM_REGISTRY_INDEX;
  784. jack_shm_header = ri->ptr.attached_at;
  785. jack_shm_registry = (jack_shm_registry_t *) (jack_shm_header + 1);
  786. /* initialize registry contents */
  787. jack_shm_init_registry ();
  788. //CloseHandle(shm_fd); // TO CHECK
  789. return 0;
  790. }
  791. static void
  792. jack_remove_shm (jack_shm_id_t *id)
  793. {
  794. /* nothing to do */
  795. }
  796. void
  797. jack_release_shm (jack_shm_info_t* si)
  798. {
  799. /* registry may or may not be locked */
  800. if (si->ptr.attached_at != NULL) {
  801. UnmapViewOfFile (si->ptr.attached_at);
  802. }
  803. }
  804. int
  805. jack_shmalloc (const char *shm_name, jack_shmsize_t size, jack_shm_info_t* si)
  806. {
  807. jack_shm_registry_t* registry;
  808. HANDLE shm_fd;
  809. int rc = -1;
  810. char name[SHM_NAME_MAX+1];
  811. if (jack_shm_lock_registry () < 0) {
  812. jack_error ("jack_shm_lock_registry fails...");
  813. return -1;
  814. }
  815. if ((registry = jack_get_free_shm_info ()) == NULL) {
  816. jack_error ("shm registry full");
  817. goto unlock;
  818. }
  819. snprintf (name, sizeof (name), "jack-%d-%d", GetUID(), registry->index);
  820. if (strlen (name) >= sizeof (registry->id)) {
  821. jack_error ("shm segment name too long %s", name);
  822. goto unlock;
  823. }
  824. if ((shm_fd = CreateFileMapping(INVALID_HANDLE_VALUE,
  825. 0, PAGE_READWRITE,
  826. 0, size,
  827. name)) == NULL || (shm_fd == INVALID_HANDLE_VALUE)) {
  828. int rc = GetLastError();
  829. jack_error ("Cannot create shm segment (%ld)",rc);
  830. goto unlock;
  831. }
  832. //CloseHandle (shm_fd); // TO CHECK
  833. registry->size = size;
  834. strncpy (registry->id, name, sizeof (registry->id));
  835. registry->allocator = _getpid();
  836. si->index = registry->index;
  837. si->ptr.attached_at = NULL; /* not attached */
  838. rc = 0; /* success */
  839. unlock:
  840. jack_shm_unlock_registry ();
  841. return rc;
  842. }
  843. int
  844. jack_attach_shm (jack_shm_info_t* si)
  845. {
  846. HANDLE shm_fd;
  847. jack_shm_registry_t *registry = &jack_shm_registry[si->index];
  848. if ((shm_fd = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, registry->id)) == NULL) {
  849. int rc = GetLastError();
  850. jack_error ("Cannot open shm segment (%ld)",rc);
  851. return -1;
  852. }
  853. if ((si->ptr.attached_at = MapViewOfFile (shm_fd, FILE_MAP_ALL_ACCESS, 0, 0, registry->size)) == NULL) {
  854. jack_error ("Cannot mmap shm segment (%ld)", GetLastError());
  855. jack_remove_shm (&registry_id);
  856. CloseHandle (shm_fd);
  857. return -1;
  858. }
  859. //CloseHandle (shm_fd); // TO CHECK
  860. return 0;
  861. }
  862. int
  863. jack_attach_shm_read (jack_shm_info_t* si)
  864. {
  865. HANDLE shm_fd;
  866. jack_shm_registry_t *registry = &jack_shm_registry[si->index];
  867. if ((shm_fd = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, registry->id)) == NULL) {
  868. int rc = GetLastError();
  869. jack_error ("Cannot open shm segment (%ld)",rc);
  870. return -1;
  871. }
  872. if ((si->ptr.attached_at = MapViewOfFile (shm_fd, FILE_MAP_READ, 0, 0, registry->size)) == NULL) {
  873. jack_error("Cannot mmap shm segment (%ld)", GetLastError());
  874. jack_remove_shm(&registry_id);
  875. CloseHandle(shm_fd);
  876. return -1;
  877. }
  878. //CloseHandle (shm_fd); // TO CHECK
  879. return 0;
  880. }
  881. #else
  882. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  883. * System V interface-dependent functions
  884. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  885. /* gain addressability to existing SHM registry segment
  886. *
  887. * sets up global registry pointers, if successful
  888. *
  889. * returns: 0 if existing registry accessed successfully
  890. * ENOENT if registry does not exist
  891. * EINVAL if registry exists, but has the wrong size
  892. * other nonzero error code if unable to access registry
  893. */
  894. static int
  895. jack_access_registry (jack_shm_info_t *ri)
  896. {
  897. /* registry must be locked */
  898. /* try without IPC_CREAT to get existing segment */
  899. if ((registry_id = shmget (JACK_SHM_REGISTRY_KEY,
  900. JACK_SHM_REGISTRY_SIZE, 0666)) < 0) {
  901. switch (errno) {
  902. case ENOENT: /* segment does not exist */
  903. return ENOENT;
  904. case EINVAL: /* segment exists, but too small */
  905. /* attempt minimum size access */
  906. registry_id = shmget (JACK_SHM_REGISTRY_KEY, 1, 0666);
  907. return EINVAL;
  908. default: /* or other error */
  909. jack_error ("unable to access shm registry (%s)",
  910. strerror (errno));
  911. return errno;
  912. }
  913. }
  914. if ((ri->attached_at = shmat (registry_id, 0, 0)) < 0) {
  915. jack_error ("Cannot attach shm registry segment (%s)",
  916. strerror (errno));
  917. return EINVAL;
  918. }
  919. /* set up global pointers */
  920. ri->index = JACK_SHM_REGISTRY_INDEX;
  921. jack_shm_header = ri->attached_at;
  922. jack_shm_registry = (jack_shm_registry_t *) (jack_shm_header + 1);
  923. return 0;
  924. }
  925. /* create a new SHM registry segment
  926. *
  927. * sets up global registry pointers, if successful
  928. *
  929. * returns: 0 if registry created successfully
  930. * nonzero error code if unable to allocate a new registry
  931. */
  932. static int
  933. jack_create_registry (jack_shm_info_t *ri)
  934. {
  935. /* registry must be locked */
  936. if ((registry_id = shmget (JACK_SHM_REGISTRY_KEY,
  937. JACK_SHM_REGISTRY_SIZE,
  938. 0666|IPC_CREAT)) < 0) {
  939. jack_error ("Cannot create shm registry segment (%s)",
  940. strerror (errno));
  941. return errno;
  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. /* initialize registry contents */
  953. jack_shm_init_registry ();
  954. return 0;
  955. }
  956. static void
  957. jack_remove_shm (jack_shm_id_t *id)
  958. {
  959. /* registry may or may not be locked */
  960. shmctl (*id, IPC_RMID, NULL);
  961. }
  962. void
  963. jack_release_shm (jack_shm_info_t* si)
  964. {
  965. /* registry may or may not be locked */
  966. if (si->attached_at != MAP_FAILED) {
  967. shmdt (si->attached_at);
  968. }
  969. }
  970. int
  971. jack_shmalloc (const char* name_not_used, jack_shmsize_t size,
  972. jack_shm_info_t* si)
  973. {
  974. int shmflags;
  975. int shmid;
  976. int rc = -1;
  977. jack_shm_registry_t* registry;
  978. if (jack_shm_lock_registry () < 0) {
  979. jack_error ("jack_shm_lock_registry fails...");
  980. return -1;
  981. }
  982. if ((registry = jack_get_free_shm_info ())) {
  983. shmflags = 0666 | IPC_CREAT | IPC_EXCL;
  984. if ((shmid = shmget (IPC_PRIVATE, size, shmflags)) >= 0) {
  985. registry->size = size;
  986. registry->id = shmid;
  987. registry->allocator = getpid();
  988. si->index = registry->index;
  989. si->attached_at = MAP_FAILED; /* not attached */
  990. rc = 0;
  991. } else {
  992. jack_error ("Cannot create shm segment %s (%s)",
  993. name_not_used, strerror (errno));
  994. }
  995. }
  996. jack_shm_unlock_registry ();
  997. return rc;
  998. }
  999. int
  1000. jack_attach_shm (jack_shm_info_t* si)
  1001. {
  1002. if ((si->attached_at = shmat (jack_shm_registry[si->index].id, 0, 0)) < 0) {
  1003. jack_error ("Cannot attach shm segment (%s)",
  1004. strerror (errno));
  1005. jack_release_shm_info (si->index);
  1006. return -1;
  1007. }
  1008. return 0;
  1009. }
  1010. #endif /* !USE_POSIX_SHM */