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.

1363 lines
33KB

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