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.

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