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.

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