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