jack1 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.

712 lines
16KB

  1. /*
  2. Copyright (C) 2003 Paul Davis
  3. Copyright (C) 2004 Jack O'Quin
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. $Id$
  16. */
  17. #include <config.h>
  18. #include <unistd.h>
  19. #include <fcntl.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <signal.h>
  23. #include <limits.h>
  24. #include <errno.h>
  25. #include <dirent.h>
  26. #include <sys/mman.h>
  27. #include <sys/types.h>
  28. #include <sys/shm.h>
  29. #include <sysdeps/ipc.h>
  30. #include <jack/shm.h>
  31. #include <jack/internal.h>
  32. #include <jack/version.h>
  33. #ifdef USE_POSIX_SHM
  34. static jack_shmtype_t jack_shmtype = shm_POSIX;
  35. static char *shmtype_name = "POSIX";
  36. #else
  37. static jack_shmtype_t jack_shmtype = shm_SYSV;
  38. static char *shmtype_name = "System V";
  39. #endif
  40. /* interface-dependent forward declarations */
  41. static int jack_access_registry (jack_shm_info_t *ri);
  42. static void jack_remove_shm (jack_shm_id_t *id);
  43. /* This module supports both System V and POSIX shared memory
  44. * interfaces. The code is divided into three sections:
  45. *
  46. * - common (interface-independent) code
  47. * - POSIX implementation
  48. * - System V implementation
  49. */
  50. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  51. * common interface-independent section
  52. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  53. /* global data */
  54. jack_shm_id_t registry_id; /* SHM id for the registry */
  55. jack_shm_info_t registry_info = { /* SHM info for the registry */
  56. .index = JACK_SHM_NULL_INDEX,
  57. .attached_at = MAP_FAILED
  58. };
  59. /* pointers to registry header and array */
  60. static jack_shm_header_t* jack_shm_header = NULL;
  61. static jack_shm_registry_t* jack_shm_registry = NULL;
  62. static void
  63. jack_shm_lock_registry (void)
  64. {
  65. /* XXX magic with semaphores here */
  66. //JOQ: this is really needed now with multiple servers
  67. }
  68. static void
  69. jack_shm_unlock_registry (void)
  70. {
  71. /* XXX magic with semaphores here */
  72. //JOQ: this is really needed now with multiple servers
  73. }
  74. static void
  75. jack_shm_init_registry ()
  76. {
  77. /* registry must be locked */
  78. int i;
  79. memset (jack_shm_header, 0, JACK_SHM_REGISTRY_SIZE);
  80. jack_shm_header->magic = JACK_SHM_MAGIC;
  81. jack_shm_header->protocol = jack_protocol_version;
  82. jack_shm_header->type = jack_shmtype;
  83. jack_shm_header->size = JACK_SHM_REGISTRY_SIZE;
  84. jack_shm_header->hdr_len = sizeof (jack_shm_header_t);
  85. jack_shm_header->entry_len = sizeof (jack_shm_registry_t);
  86. for (i = 0; i < MAX_SHM_ID; ++i) {
  87. jack_shm_registry[i].index = i;
  88. }
  89. }
  90. static int
  91. jack_shm_validate_registry ()
  92. {
  93. /* registry must be locked */
  94. if ((jack_shm_header->magic == JACK_SHM_MAGIC)
  95. && (jack_shm_header->protocol == jack_protocol_version)
  96. && (jack_shm_header->type == jack_shmtype)
  97. && (jack_shm_header->size == JACK_SHM_REGISTRY_SIZE)
  98. && (jack_shm_header->hdr_len == sizeof (jack_shm_header_t))
  99. && (jack_shm_header->entry_len == sizeof (jack_shm_registry_t))) {
  100. return 0; /* registry OK */
  101. }
  102. jack_error ("incompatible shm registry (%s)", strerror (errno));
  103. /* Apparently, this registry was created by an older JACK
  104. * version. Delete it and try again. */
  105. jack_release_shm (&registry_info);
  106. jack_remove_shm (&registry_id);
  107. return -1;
  108. }
  109. /* gain addressibility to shared memory registration segment
  110. *
  111. * returns: 0 if successful
  112. */
  113. int
  114. jack_initialize_shm (void)
  115. {
  116. int rc;
  117. if (jack_shm_header)
  118. return 0; /* already initialized */
  119. jack_shm_lock_registry ();
  120. rc = jack_access_registry (&registry_info);
  121. switch (rc) {
  122. case 1: /* newly-created registry */
  123. jack_shm_init_registry ();
  124. rc = 0; /* success */
  125. break;
  126. case 0: /* existing registry */
  127. if (jack_shm_validate_registry () == 0)
  128. break;
  129. /* else it was invalid, so fall through */
  130. case -2: /* bad registry */
  131. /* it's gone now, so try again */
  132. rc = jack_access_registry (&registry_info);
  133. if (rc == 1) { /* new registry created? */
  134. jack_shm_init_registry ();
  135. rc = 0; /* problem solved */
  136. } else {
  137. jack_error ("incompatible shm registry (%s)",
  138. strerror (errno));
  139. #ifndef USE_POSIX_SHM
  140. jack_error ("to delete, use `ipcrm -M 0x%0.8x'",
  141. JACK_SHM_REGISTRY_KEY);
  142. #endif
  143. rc = -1; /* FUBAR */
  144. }
  145. break;
  146. default: /* failure return code */
  147. }
  148. jack_shm_unlock_registry ();
  149. return rc;
  150. }
  151. void
  152. jack_destroy_shm (jack_shm_info_t* si)
  153. {
  154. /* must NOT have the registry locked */
  155. if (si->index == JACK_SHM_NULL_INDEX)
  156. return; /* segment not allocated */
  157. jack_remove_shm (&jack_shm_registry[si->index].id);
  158. jack_release_shm_info (si->index);
  159. }
  160. jack_shm_registry_t *
  161. jack_get_free_shm_info ()
  162. {
  163. jack_shm_registry_t* si = NULL;
  164. int i;
  165. jack_shm_lock_registry ();
  166. for (i = 0; i < MAX_SHM_ID; ++i) {
  167. if (jack_shm_registry[i].size == 0) {
  168. break;
  169. }
  170. }
  171. if (i < MAX_SHM_ID) {
  172. si = &jack_shm_registry[i];
  173. }
  174. jack_shm_unlock_registry ();
  175. return si;
  176. }
  177. void
  178. jack_release_shm_info (jack_shm_registry_index_t index)
  179. {
  180. /* must NOT have the registry locked */
  181. if (jack_shm_registry[index].allocator == getpid()) {
  182. jack_shm_lock_registry ();
  183. jack_shm_registry[index].size = 0;
  184. jack_shm_registry[index].allocator = 0;
  185. jack_shm_unlock_registry ();
  186. }
  187. }
  188. /* Claim server_name for this process.
  189. *
  190. * returns 0 if successful
  191. * EEXIST if server_name was already active for this user
  192. * ENOSPC if server registration limit reached
  193. */
  194. int
  195. jack_register_server (const char *server_name)
  196. {
  197. int i;
  198. pid_t my_pid = getpid ();
  199. fprintf (stderr, "JACK compiled with %s SHM support.\n", shmtype_name);
  200. jack_shm_lock_registry ();
  201. /* See if server_name already registered. Since server names
  202. * are per-user, we register the server directory path name,
  203. * which must be unique. */
  204. for (i = 0; i < MAX_SERVERS; i++) {
  205. if (strncmp (jack_shm_header->server[i].name,
  206. jack_server_dir (server_name),
  207. JACK_SERVER_NAME_SIZE) != 0)
  208. continue; /* no match */
  209. if (jack_shm_header->server[i].pid == my_pid)
  210. return 0; /* it's me */
  211. /* see if server still exists */
  212. if (kill (jack_shm_header->server[i].pid, 0) == 0) {
  213. return EEXIST; /* other server running */
  214. }
  215. }
  216. /* find a free entry */
  217. for (i = 0; i < MAX_SERVERS; i++) {
  218. if (jack_shm_header->server[i].pid == 0)
  219. break;
  220. }
  221. if (i >= MAX_SERVERS)
  222. return ENOSPC; /* out of space */
  223. /* claim it */
  224. jack_shm_header->server[i].pid = my_pid;
  225. strncpy (jack_shm_header->server[i].name,
  226. jack_server_dir (server_name),
  227. JACK_SERVER_NAME_SIZE);
  228. jack_shm_unlock_registry ();
  229. return 0;
  230. }
  231. /* release server_name registration */
  232. void
  233. jack_unregister_server (const char *server_name /* unused */)
  234. {
  235. int i;
  236. pid_t my_pid = getpid ();
  237. jack_shm_lock_registry ();
  238. for (i = 0; i < MAX_SERVERS; i++) {
  239. if (jack_shm_header->server[i].pid == my_pid) {
  240. jack_shm_header->server[i].pid = 0;
  241. memset (jack_shm_header->server[i].name, 0,
  242. JACK_SERVER_NAME_SIZE);
  243. }
  244. }
  245. jack_shm_unlock_registry ();
  246. }
  247. /* called for server startup and termination */
  248. int
  249. jack_cleanup_shm ()
  250. {
  251. int i;
  252. int destroy;
  253. jack_shm_info_t copy;
  254. pid_t my_pid = getpid ();
  255. jack_shm_lock_registry ();
  256. for (i = 0; i < MAX_SHM_ID; i++) {
  257. jack_shm_registry_t* r;
  258. r = &jack_shm_registry[i];
  259. copy.index = r->index;
  260. destroy = FALSE;
  261. /* ignore unused entries */
  262. if (r->allocator == 0)
  263. continue;
  264. /* is this my shm segment? */
  265. if (r->allocator == my_pid) {
  266. /* allocated by this process, so unattach
  267. and destroy. */
  268. jack_release_shm (&copy);
  269. destroy = TRUE;
  270. } else {
  271. /* see if allocator still exists */
  272. if (kill (r->allocator, 0)) {
  273. if (errno == ESRCH) {
  274. /* allocator no longer exists,
  275. * so destroy */
  276. destroy = TRUE;
  277. }
  278. }
  279. }
  280. if (destroy) {
  281. int index = copy.index;
  282. if ((index >= 0) && (index < MAX_SHM_ID)) {
  283. jack_remove_shm (&jack_shm_registry[index].id);
  284. jack_shm_registry[index].size = 0;
  285. jack_shm_registry[index].allocator = 0;
  286. }
  287. r->size = 0;
  288. r->allocator = 0;
  289. }
  290. }
  291. jack_shm_unlock_registry ();
  292. return TRUE;
  293. }
  294. #ifdef USE_POSIX_SHM
  295. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  296. * POSIX interface-dependent functions
  297. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  298. /* gain addressibility to SHM registry segment
  299. *
  300. * sets up global registry pointers, if successful
  301. *
  302. * returns: 1 if newly created
  303. * 0 if existing registry
  304. * -1 if unsuccessful
  305. * -2 if registry existed, but was the wrong size
  306. */
  307. static int
  308. jack_access_registry (jack_shm_info_t *ri)
  309. {
  310. /* registry must be locked */
  311. int shm_fd;
  312. int new_registry = 0;
  313. int rc = -1;
  314. int perm;
  315. jack_shmsize_t size = JACK_SHM_REGISTRY_SIZE;
  316. /* grab a chunk of memory to store shm ids in. this is
  317. to allow clean up of all segments whenever JACK
  318. starts (or stops). */
  319. perm = O_RDWR;
  320. strncpy (registry_id, "/jack-shm-registry", sizeof (registry_id));
  321. /* try without O_CREAT to see if it already exists */
  322. if ((shm_fd = shm_open (registry_id, perm, 0666)) < 0) {
  323. if (errno == ENOENT) {
  324. /* it doesn't exist, so create it */
  325. perm = O_RDWR|O_CREAT;
  326. if ((shm_fd =
  327. shm_open (registry_id, perm, 0666)) < 0) {
  328. jack_error ("cannot create shm registry segment"
  329. " (%s)", strerror (errno));
  330. goto error;
  331. }
  332. new_registry = 1;
  333. } else {
  334. jack_error ("cannot open existing shm registry segment"
  335. " (%s)", strerror (errno));
  336. goto error;
  337. }
  338. }
  339. if (perm & O_CREAT) {
  340. if (ftruncate (shm_fd, size) < 0) {
  341. jack_error ("cannot set size of engine shm registry 1"
  342. "(%s)", strerror (errno));
  343. jack_remove_shm (&registry_id);
  344. rc = -2;
  345. goto error;
  346. }
  347. }
  348. if ((ri->attached_at = mmap (0, size, PROT_READ|PROT_WRITE,
  349. MAP_SHARED, shm_fd, 0)) == MAP_FAILED) {
  350. jack_error ("cannot mmap shm registry segment (%s)",
  351. strerror (errno));
  352. goto error;
  353. }
  354. /* set up global pointers */
  355. ri->index = JACK_SHM_REGISTRY_INDEX;
  356. jack_shm_header = ri->attached_at;
  357. jack_shm_registry = (jack_shm_registry_t *) (jack_shm_header + 1);
  358. rc = new_registry;
  359. error:
  360. close (shm_fd);
  361. return rc;
  362. }
  363. static void
  364. jack_remove_shm (jack_shm_id_t *id)
  365. {
  366. shm_unlink (*id);
  367. }
  368. void
  369. jack_release_shm (jack_shm_info_t* si)
  370. {
  371. //printf("client->jack_release_shm \n");
  372. if (si->attached_at != MAP_FAILED) {
  373. //printf("client->jack_release_shm 1 \n");
  374. munmap (si->attached_at, jack_shm_registry[si->index].size);
  375. }
  376. }
  377. int
  378. jack_shmalloc (const char *shm_name, jack_shmsize_t size, jack_shm_info_t* si)
  379. {
  380. jack_shm_registry_t* registry;
  381. int shm_fd;
  382. int perm = O_RDWR|O_CREAT;
  383. if ((registry = jack_get_free_shm_info ()) == NULL) {
  384. return -1;
  385. }
  386. if ((shm_fd = shm_open (shm_name, O_RDWR|O_CREAT, 0666)) < 0) {
  387. jack_error ("cannot create shm segment %s (%s)", shm_name,
  388. strerror (errno));
  389. return -1;
  390. }
  391. if (perm & O_CREAT) {
  392. if (ftruncate (shm_fd, size) < 0) {
  393. jack_error ("cannot set size of engine shm "
  394. "registry 0 (%s)",
  395. strerror (errno));
  396. return -1;
  397. }
  398. }
  399. close (shm_fd);
  400. registry->size = size;
  401. snprintf (registry->id, sizeof (registry->id), "%s", shm_name);
  402. registry->allocator = getpid();
  403. si->index = registry->index;
  404. si->attached_at = MAP_FAILED; /* segment not attached */
  405. return 0;
  406. }
  407. int
  408. jack_attach_shm (jack_shm_info_t* si)
  409. {
  410. int shm_fd;
  411. jack_shm_registry_t *registry = &jack_shm_registry[si->index];
  412. if ((shm_fd = shm_open (registry->id,
  413. O_RDWR, 0666)) < 0) {
  414. jack_error ("cannot open shm segment %s (%s)", registry->id,
  415. strerror (errno));
  416. return -1;
  417. }
  418. if ((si->attached_at = mmap (0, registry->size, PROT_READ|PROT_WRITE,
  419. MAP_SHARED, shm_fd, 0)) == MAP_FAILED) {
  420. jack_error ("cannot mmap shm segment %s (%s)",
  421. registry->id,
  422. strerror (errno));
  423. close (shm_fd);
  424. return -1;
  425. }
  426. close (shm_fd);
  427. return 0;
  428. }
  429. int
  430. jack_resize_shm (jack_shm_info_t* si, jack_shmsize_t size)
  431. {
  432. int shm_fd;
  433. jack_shm_registry_t *registry = &jack_shm_registry[si->index];
  434. if ((shm_fd = shm_open (registry->id, O_RDWR, 0666)) < 0) {
  435. jack_error ("cannot create shm segment %s (%s)", registry->id,
  436. strerror (errno));
  437. return -1;
  438. }
  439. munmap (si->attached_at, registry->size);
  440. if (ftruncate (shm_fd, size) < 0) {
  441. jack_error ("cannot set size of shm segment %s "
  442. "(%s)", registry->id, strerror (errno));
  443. return -1;
  444. }
  445. if ((si->attached_at = mmap (0, size, PROT_READ|PROT_WRITE,
  446. MAP_SHARED, shm_fd, 0)) == MAP_FAILED) {
  447. jack_error ("cannot mmap shm segment %s (%s)", registry->id,
  448. strerror (errno));
  449. close (shm_fd);
  450. return -1;
  451. }
  452. close (shm_fd);
  453. return 0;
  454. }
  455. #else
  456. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  457. * System V interface-dependent functions
  458. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  459. /* gain addressibility to SHM registry segment
  460. *
  461. * sets up global registry pointers, if successful
  462. *
  463. * returns: 1 if newly created
  464. * 0 if existing registry
  465. * -1 if unsuccessful
  466. * -2 if registry existed, but was the wrong size
  467. */
  468. static int
  469. jack_access_registry (jack_shm_info_t *ri)
  470. {
  471. /* registry must be locked */
  472. int shmflags = 0666;
  473. int shmid;
  474. int new_registry = 0; /* true if new segment created */
  475. key_t key = JACK_SHM_REGISTRY_KEY;
  476. jack_shmsize_t size = JACK_SHM_REGISTRY_SIZE;
  477. /* grab a chunk of memory to store shm ids in. this is
  478. to allow our parent to clean up all such ids when
  479. if we exit. otherwise, they can get lost in crash
  480. or debugger driven exits.
  481. */
  482. /* try without IPC_CREAT to check if it already exists */
  483. if ((shmid = shmget (key, size, shmflags)) < 0) {
  484. switch (errno) {
  485. case ENOENT: /* did not exist */
  486. if ((shmid = shmget (key, size,
  487. shmflags|IPC_CREAT)) < 0) {
  488. jack_error ("cannot create shm registry segment"
  489. " (%s)", strerror (errno));
  490. return -1;
  491. }
  492. new_registry = 1;
  493. break;
  494. case EINVAL: /* exists, but too small */
  495. /* try to remove it */
  496. if ((shmid = shmget (key, 1, shmflags)) >= 0) {
  497. shmctl (shmid, IPC_RMID, NULL);
  498. return -2;
  499. }
  500. default:
  501. jack_error ("unable to access shm registry (%s)",
  502. strerror (errno));
  503. return -1;
  504. }
  505. }
  506. if ((ri->attached_at = shmat (shmid, 0, 0)) < 0) {
  507. jack_error ("cannot attach shm registry segment (%s)",
  508. strerror (errno));
  509. return -1;
  510. }
  511. /* set up global pointers */
  512. ri->index = JACK_SHM_REGISTRY_INDEX;
  513. jack_shm_header = ri->attached_at;
  514. jack_shm_registry = (jack_shm_registry_t *) (jack_shm_header + 1);
  515. registry_id = shmid;
  516. return new_registry;
  517. }
  518. static void
  519. jack_remove_shm (jack_shm_id_t *id)
  520. {
  521. shmctl (*id, IPC_RMID, NULL);
  522. }
  523. void
  524. jack_release_shm (jack_shm_info_t* si)
  525. {
  526. if (si->attached_at != MAP_FAILED) {
  527. shmdt (si->attached_at);
  528. }
  529. }
  530. int
  531. jack_shmalloc (const char* name_not_used, jack_shmsize_t size,
  532. jack_shm_info_t* si)
  533. {
  534. int shmflags;
  535. int shmid;
  536. jack_shm_registry_t* registry;
  537. if ((registry = jack_get_free_shm_info ()) == NULL) {
  538. return -1;
  539. }
  540. shmflags = 0666 | IPC_CREAT | IPC_EXCL;
  541. if ((shmid = shmget (IPC_PRIVATE, size, shmflags)) < 0) {
  542. jack_error ("cannot create shm segment %s (%s)",
  543. name_not_used, strerror (errno));
  544. return -1;
  545. }
  546. registry->size = size;
  547. registry->id = shmid;
  548. registry->allocator = getpid();
  549. si->index = registry->index;
  550. si->attached_at = MAP_FAILED; /* segment not attached */
  551. return 0;
  552. }
  553. int
  554. jack_attach_shm (jack_shm_info_t* si)
  555. {
  556. if ((si->attached_at = shmat (jack_shm_registry[si->index].id,
  557. 0, 0)) < 0) {
  558. jack_error ("cannot attach shm segment (%s)",
  559. strerror (errno));
  560. jack_release_shm_info (si->index);
  561. return -1;
  562. }
  563. return 0;
  564. }
  565. int
  566. jack_resize_shm (jack_shm_info_t* si, jack_shmsize_t size)
  567. {
  568. /* There is no way to resize a System V shm segment. So, we
  569. * delete it and allocate a new one. This is tricky, because
  570. * the old segment will not disappear until all the clients
  571. * have released it. We can only do what we can from here.
  572. */
  573. jack_release_shm (si);
  574. jack_destroy_shm (si);
  575. if (jack_shmalloc ("not used", size, si)) {
  576. return -1;
  577. }
  578. return jack_attach_shm (si);
  579. }
  580. #endif /* !USE_POSIX_SHM */