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.

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