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.

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