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.

1291 lines
31KB

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