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.

1350 lines
32KB

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