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.

3502 lines
87KB

  1. /*
  2. Copyright (C) 2001-2003 Paul Davis
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  14. $Id$
  15. */
  16. #include <math.h>
  17. #include <unistd.h>
  18. #include <sys/socket.h>
  19. #include <sys/poll.h>
  20. #include <sys/un.h>
  21. #include <sys/shm.h>
  22. #include <sys/stat.h>
  23. #include <errno.h>
  24. #include <fcntl.h>
  25. #include <sys/shm.h>
  26. #include <stdio.h>
  27. #include <stdarg.h>
  28. #include <dirent.h>
  29. #include <sys/ipc.h>
  30. #include <signal.h>
  31. #include <sys/types.h>
  32. #include <string.h>
  33. #include <limits.h>
  34. #include <sys/mman.h>
  35. #include <config.h>
  36. #include <jack/internal.h>
  37. #include <jack/engine.h>
  38. #include <jack/driver.h>
  39. #include <jack/time.h>
  40. #include <jack/version.h>
  41. #ifdef USE_CAPABILITIES
  42. /* capgetp and capsetp are linux only extensions, not posix */
  43. #undef _POSIX_SOURCE
  44. #include <sys/capability.h>
  45. #endif
  46. #define MAX_SHM_ID 256 /* likely use is more like 16 */
  47. /**
  48. * Time to wait for clients in msecs. Used when jackd is
  49. * run in non-ASIO mode and without realtime priority enabled.
  50. */
  51. #define JACKD_SOFT_MODE_TIMEOUT 500
  52. #define JACK_ERROR_WITH_SOCKETS 10000000
  53. typedef struct {
  54. jack_port_internal_t *source;
  55. jack_port_internal_t *destination;
  56. } jack_connection_internal_t;
  57. typedef struct _jack_client_internal {
  58. jack_client_control_t *control;
  59. int request_fd;
  60. int event_fd;
  61. int subgraph_start_fd;
  62. int subgraph_wait_fd;
  63. JSList *ports; /* protected by engine->client_lock */
  64. JSList *fed_by; /* protected by engine->client_lock */
  65. int shm_id;
  66. int shm_key;
  67. unsigned long execution_order;
  68. struct _jack_client_internal *next_client; /* not a linked list! */
  69. dlhandle handle;
  70. int (*initialize)(jack_client_t*, const char*); /* for internal clients only */
  71. void (*finish)(void); /* for internal clients only */
  72. int error;
  73. } jack_client_internal_t;
  74. typedef struct _jack_driver_info {
  75. jack_driver_t *(*initialize)(jack_client_t*, int, char**);
  76. void (*finish);
  77. char (*client_name);
  78. dlhandle handle;
  79. } jack_driver_info_t;
  80. static int jack_port_assign_buffer (jack_engine_t *, jack_port_internal_t *);
  81. static jack_port_internal_t *jack_get_port_by_name (jack_engine_t *, const char *name);
  82. static void jack_zombify_client (jack_engine_t *engine, jack_client_internal_t *client);
  83. static void jack_remove_client (jack_engine_t *engine, jack_client_internal_t *client);
  84. static void jack_client_delete (jack_engine_t *, jack_client_internal_t *);
  85. static jack_client_internal_t *jack_client_internal_new (jack_engine_t *engine, int fd, jack_client_connect_request_t *);
  86. static jack_client_internal_t *jack_client_internal_by_id (jack_engine_t *engine, jack_client_id_t id);
  87. static void jack_sort_graph (jack_engine_t *engine);
  88. static int jack_rechain_graph (jack_engine_t *engine);
  89. static int jack_get_fifo_fd (jack_engine_t *engine, unsigned int which_fifo);
  90. static void jack_clear_fifos (jack_engine_t *engine);
  91. static int jack_port_do_connect (jack_engine_t *engine, const char *source_port, const char *destination_port);
  92. static int jack_port_do_disconnect (jack_engine_t *engine, const char *source_port, const char *destination_port);
  93. static int jack_port_do_disconnect_all (jack_engine_t *engine, jack_port_id_t);
  94. static int jack_port_do_unregister (jack_engine_t *engine, jack_request_t *);
  95. static int jack_port_do_register (jack_engine_t *engine, jack_request_t *);
  96. static int jack_do_get_port_connections (jack_engine_t *engine, jack_request_t *req, int reply_fd);
  97. static void jack_port_release (jack_engine_t *engine, jack_port_internal_t *);
  98. static void jack_port_clear_connections (jack_engine_t *engine, jack_port_internal_t *port);
  99. static int jack_port_disconnect_internal (jack_engine_t *engine, jack_port_internal_t *src,
  100. jack_port_internal_t *dst, int sort_graph);
  101. static void jack_port_registration_notify (jack_engine_t *, jack_port_id_t, int);
  102. static int jack_send_connection_notification (jack_engine_t *, jack_client_id_t, jack_port_id_t, jack_port_id_t, int);
  103. static int jack_deliver_event (jack_engine_t *, jack_client_internal_t *, jack_event_t *);
  104. static void jack_deliver_event_to_all (jack_engine_t *engine, jack_event_t *event);
  105. static int jack_engine_process_lock (jack_engine_t *);
  106. static void jack_engine_process_unlock (jack_engine_t *);
  107. static int jack_engine_post_process (jack_engine_t *);
  108. static int internal_client_request (void*, jack_request_t *);
  109. static int jack_use_driver (jack_engine_t *engine, jack_driver_t *driver);
  110. static int *jack_shm_registry;
  111. static int jack_shm_id_cnt;
  112. static char *client_state_names[] = {
  113. "Not triggered",
  114. "Triggered",
  115. "Running",
  116. "Finished"
  117. };
  118. static inline int
  119. jack_client_is_internal (jack_client_internal_t *client)
  120. {
  121. return (client->control->type == ClientInternal) || (client->control->type == ClientDriver);
  122. }
  123. #define jack_lock_graph(engine) do { \
  124. DEBUG ("acquiring graph lock"); \
  125. pthread_mutex_lock (&engine->client_lock); \
  126. } while(0)
  127. #define jack_unlock_graph(engine) do { \
  128. DEBUG ("releasing graph lock"); \
  129. pthread_mutex_unlock (&engine->client_lock); \
  130. } while(0)
  131. static inline void
  132. jack_engine_reset_rolling_usecs (jack_engine_t *engine)
  133. {
  134. memset (engine->rolling_client_usecs, 0, sizeof (engine->rolling_client_usecs));
  135. engine->rolling_client_usecs_index = 0;
  136. engine->rolling_client_usecs_cnt = 0;
  137. if (engine->driver) {
  138. engine->rolling_interval = (int) floor (JACK_ENGINE_ROLLING_INTERVAL * 1000.0f / engine->driver->period_usecs);
  139. } else {
  140. engine->rolling_interval = JACK_ENGINE_ROLLING_INTERVAL; // whatever
  141. }
  142. engine->spare_usecs = 0;
  143. }
  144. static int
  145. make_sockets (int fd[2])
  146. {
  147. struct sockaddr_un addr;
  148. int i;
  149. /* First, the master server socket */
  150. if ((fd[0] = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) {
  151. jack_error ("cannot create server socket (%s)", strerror (errno));
  152. return -1;
  153. }
  154. addr.sun_family = AF_UNIX;
  155. for (i = 0; i < 999; i++) {
  156. snprintf (addr.sun_path, sizeof (addr.sun_path) - 1, "%s/jack_%d", jack_server_dir, i);
  157. if (access (addr.sun_path, F_OK) != 0) {
  158. break;
  159. }
  160. }
  161. if (i == 999) {
  162. jack_error ("all possible server socket names in use!!!");
  163. close (fd[0]);
  164. return -1;
  165. }
  166. if (bind (fd[0], (struct sockaddr *) &addr, sizeof (addr)) < 0) {
  167. jack_error ("cannot bind server to socket (%s)", strerror (errno));
  168. close (fd[0]);
  169. return -1;
  170. }
  171. if (listen (fd[0], 1) < 0) {
  172. jack_error ("cannot enable listen on server socket (%s)", strerror (errno));
  173. close (fd[0]);
  174. return -1;
  175. }
  176. /* Now the client/server event ack server socket */
  177. if ((fd[1] = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) {
  178. jack_error ("cannot create event ACK socket (%s)", strerror (errno));
  179. close (fd[0]);
  180. return -1;
  181. }
  182. addr.sun_family = AF_UNIX;
  183. for (i = 0; i < 999; i++) {
  184. snprintf (addr.sun_path, sizeof (addr.sun_path) - 1, "%s/jack_ack_%d", jack_server_dir, i);
  185. if (access (addr.sun_path, F_OK) != 0) {
  186. break;
  187. }
  188. }
  189. if (i == 999) {
  190. jack_error ("all possible server ACK socket names in use!!!");
  191. close (fd[0]);
  192. close (fd[1]);
  193. return -1;
  194. }
  195. if (bind (fd[1], (struct sockaddr *) &addr, sizeof (addr)) < 0) {
  196. jack_error ("cannot bind server to socket (%s)", strerror (errno));
  197. close (fd[0]);
  198. close (fd[1]);
  199. return -1;
  200. }
  201. if (listen (fd[1], 1) < 0) {
  202. jack_error ("cannot enable listen on server socket (%s)", strerror (errno));
  203. close (fd[0]);
  204. close (fd[1]);
  205. return -1;
  206. }
  207. return 0;
  208. }
  209. static int
  210. jack_initialize_shm ()
  211. {
  212. int shmid_id;
  213. void *addr;
  214. if (jack_shm_registry != NULL) {
  215. return 0;
  216. }
  217. /* grab a chunk of memory to store shm ids in. this is
  218. to allow our parent to clean up all such ids when
  219. if we exit. otherwise, they can get lost in crash
  220. or debugger driven exits.
  221. */
  222. if ((shmid_id = shmget (random(), sizeof(int) * MAX_SHM_ID, IPC_CREAT|0600)) < 0) {
  223. jack_error ("cannot create engine shm ID registry (%s)", strerror (errno));
  224. return -1;
  225. }
  226. if ((addr = shmat (shmid_id, 0, 0)) == (void *) -1) {
  227. jack_error ("cannot attach shm ID registry (%s)", strerror (errno));
  228. shmctl (shmid_id, IPC_RMID, 0);
  229. return -1;
  230. }
  231. if (shmctl (shmid_id, IPC_RMID, NULL)) {
  232. jack_error ("cannot mark shm ID registry as destroyed (%s)", strerror (errno));
  233. return -1;
  234. }
  235. jack_shm_registry = (int *) addr;
  236. jack_shm_id_cnt = 0;
  237. return 0;
  238. }
  239. static void
  240. jack_register_shm (int shmid)
  241. {
  242. if (jack_shm_id_cnt < MAX_SHM_ID) {
  243. jack_shm_registry[jack_shm_id_cnt++] = shmid;
  244. }
  245. }
  246. void
  247. jack_cleanup_shm ()
  248. {
  249. int i;
  250. for (i = 0; i < jack_shm_id_cnt; i++) {
  251. shmctl (jack_shm_registry[i], IPC_RMID, NULL);
  252. }
  253. }
  254. void
  255. jack_cleanup_files ()
  256. {
  257. DIR *dir;
  258. struct dirent *dirent;
  259. /* its important that we remove all files that jackd creates
  260. because otherwise subsequent attempts to start jackd will
  261. believe that an instance is already running.
  262. */
  263. if ((dir = opendir (jack_server_dir)) == NULL) {
  264. fprintf (stderr, "jack(%d): cannot open jack FIFO directory (%s)\n", getpid(), strerror (errno));
  265. return;
  266. }
  267. while ((dirent = readdir (dir)) != NULL) {
  268. if (strncmp (dirent->d_name, "jack-", 5) == 0 || strncmp (dirent->d_name, "jack_", 5) == 0) {
  269. char fullpath[PATH_MAX+1];
  270. snprintf (fullpath, sizeof (fullpath), "%s/%s", jack_server_dir, dirent->d_name);
  271. unlink (fullpath);
  272. }
  273. }
  274. closedir (dir);
  275. }
  276. static int
  277. jack_add_port_segment (jack_engine_t *engine, unsigned long nports)
  278. {
  279. jack_event_t event;
  280. jack_port_segment_info_t *si;
  281. key_t key;
  282. int id;
  283. char *addr;
  284. size_t offset;
  285. size_t size;
  286. size_t step;
  287. key = random();
  288. size = nports * sizeof (jack_default_audio_sample_t) * engine->control->buffer_size;
  289. if ((id = shmget (key, size, IPC_CREAT|0666)) < 0) {
  290. jack_error ("cannot create new port segment of %d bytes, key = 0x%x (%s)", size, key, strerror (errno));
  291. return -1;
  292. }
  293. jack_register_shm (id);
  294. if ((addr = shmat (id, 0, 0)) == (char *) -1) {
  295. jack_error ("cannot attach new port segment (%s)", strerror (errno));
  296. shmctl (id, IPC_RMID, 0);
  297. return -1;
  298. }
  299. si = (jack_port_segment_info_t *) malloc (sizeof (jack_port_segment_info_t));
  300. si->shm_key = key;
  301. si->address = addr;
  302. engine->port_segments = jack_slist_prepend (engine->port_segments, si);
  303. engine->port_segment_key = key; /* XXX fix me */
  304. engine->port_segment_address = addr; /* XXX fix me */
  305. pthread_mutex_lock (&engine->buffer_lock);
  306. offset = 0;
  307. step = engine->control->buffer_size * sizeof (jack_default_audio_sample_t);
  308. while (offset < size) {
  309. jack_port_buffer_info_t *bi;
  310. bi = (jack_port_buffer_info_t *) malloc (sizeof (jack_port_buffer_info_t));
  311. bi->shm_key = key;
  312. bi->offset = offset;
  313. /* we append because we want the list to be in memory-address order */
  314. engine->port_buffer_freelist = jack_slist_append (engine->port_buffer_freelist, bi);
  315. offset += step;
  316. }
  317. /* convert the first chunk of the segment into a zero-filled area */
  318. if (engine->silent_buffer == NULL) {
  319. engine->silent_buffer = (jack_port_buffer_info_t *) engine->port_buffer_freelist->data;
  320. engine->port_buffer_freelist = jack_slist_remove_link (engine->port_buffer_freelist, engine->port_buffer_freelist);
  321. memset (engine->port_segment_address + engine->silent_buffer->offset, 0,
  322. sizeof (jack_default_audio_sample_t) * engine->control->buffer_size);
  323. }
  324. pthread_mutex_unlock (&engine->buffer_lock);
  325. /* tell everybody about it */
  326. event.type = NewPortBufferSegment;
  327. event.x.key = key;
  328. event.y.addr = addr;
  329. jack_deliver_event_to_all (engine, &event);
  330. return 0;
  331. }
  332. static int
  333. jack_set_buffer_size (jack_engine_t *engine, jack_nframes_t nframes)
  334. {
  335. /* XXX this is not really right, since it only works for
  336. audio ports. it also doesn't resize the zero filled
  337. area.
  338. */
  339. engine->control->buffer_size = nframes;
  340. jack_add_port_segment (engine, engine->control->port_max);
  341. return 0;
  342. }
  343. static int
  344. jack_set_sample_rate (jack_engine_t *engine, jack_nframes_t nframes)
  345. {
  346. engine->control->current_time.frame_rate = nframes;
  347. engine->control->pending_time.frame_rate = nframes;
  348. return 0;
  349. }
  350. int
  351. jack_engine_process_lock (jack_engine_t *engine)
  352. {
  353. return pthread_mutex_trylock (&engine->client_lock);
  354. }
  355. void
  356. jack_engine_process_unlock (jack_engine_t *engine)
  357. {
  358. pthread_mutex_unlock (&engine->client_lock);
  359. }
  360. static int
  361. jack_process (jack_engine_t *engine, jack_nframes_t nframes)
  362. {
  363. jack_client_internal_t *client;
  364. jack_client_control_t *ctl;
  365. JSList *node;
  366. int status;
  367. char c;
  368. float delayed_usecs;
  369. unsigned long long now, then;
  370. engine->process_errors = 0;
  371. for (node = engine->clients; node; node = jack_slist_next (node)) {
  372. ctl = ((jack_client_internal_t *) node->data)->control;
  373. ctl->state = NotTriggered;
  374. ctl->nframes = nframes;
  375. }
  376. for (node = engine->clients; engine->process_errors == 0 && node; ) {
  377. client = (jack_client_internal_t *) node->data;
  378. DEBUG ("considering client %s for processing", client->control->name);
  379. if (!client->control->active || client->control->dead) {
  380. node = jack_slist_next (node);
  381. continue;
  382. }
  383. ctl = client->control;
  384. ctl->timed_out = 0;
  385. if (jack_client_is_internal (client)) {
  386. /* internal client ("plugin") */
  387. if (ctl->process) {
  388. DEBUG ("calling process() on an internal client");
  389. ctl->state = Running;
  390. /* XXX how to time out an internal client? */
  391. engine->current_client = client;
  392. if (ctl->process (nframes, ctl->process_arg) == 0) {
  393. ctl->state = Finished;
  394. } else {
  395. jack_error ("internal client %s failed", client->control->name);
  396. engine->process_errors++;
  397. break;
  398. }
  399. } else {
  400. DEBUG ("internal client has no process() function");
  401. ctl->state = Finished;
  402. }
  403. node = jack_slist_next (node);
  404. } else {
  405. /* external subgraph */
  406. ctl->state = Triggered; // a race exists if we do this after the write(2)
  407. ctl->signalled_at = jack_get_microseconds();
  408. ctl->awake_at = 0;
  409. ctl->finished_at = 0;
  410. engine->current_client = client;
  411. DEBUG ("calling process() on an external subgraph, fd==%d", client->subgraph_start_fd);
  412. if (write (client->subgraph_start_fd, &c, sizeof (c)) != sizeof (c)) {
  413. jack_error ("cannot initiate graph processing (%s)", strerror (errno));
  414. engine->process_errors++;
  415. break;
  416. }
  417. then = jack_get_microseconds ();
  418. if (engine->asio_mode) {
  419. engine->driver->wait (engine->driver, client->subgraph_wait_fd, &status, &delayed_usecs);
  420. } else {
  421. struct pollfd pfd[1];
  422. int poll_timeout = (engine->control->real_time == 0 ? JACKD_SOFT_MODE_TIMEOUT : engine->driver->period_usecs/1000);
  423. pfd[0].fd = client->subgraph_wait_fd;
  424. pfd[0].events = POLLERR|POLLIN|POLLHUP|POLLNVAL;
  425. DEBUG ("waiting on fd==%d for process() subgraph to finish", client->subgraph_wait_fd);
  426. if (poll (pfd, 1, poll_timeout) < 0) {
  427. jack_error ("poll on subgraph processing failed (%s)", strerror (errno));
  428. status = -1;
  429. }
  430. if (pfd[0].revents & ~POLLIN) {
  431. jack_error ("subgraph starting at %s lost client", client->control->name);
  432. status = -2;
  433. }
  434. if (pfd[0].revents & POLLIN) {
  435. status = 0;
  436. } else {
  437. jack_error ("subgraph starting at %s timed out (subgraph_wait_fd=%d, status = %d, state = %s)",
  438. client->control->name, client->subgraph_wait_fd, status,
  439. client_state_names[client->control->state]);
  440. status = 1;
  441. }
  442. }
  443. now = jack_get_microseconds ();
  444. if (status != 0) {
  445. if (engine->verbose) {
  446. fprintf (stderr, "at %Lu client waiting on %d took %Lu usecs, status = %d sig = %Lu awa = %Lu fin = %Lu dur=%Lu\n",
  447. now,
  448. client->subgraph_wait_fd,
  449. now - then,
  450. status,
  451. ctl->signalled_at,
  452. ctl->awake_at,
  453. ctl->finished_at,
  454. ctl->finished_at - ctl->signalled_at);
  455. }
  456. /* we can only consider the timeout a client error if it actually woke up.
  457. its possible that the kernel scheduler screwed us up and
  458. never woke up the client in time. sigh.
  459. */
  460. if (ctl->awake_at > 0) {
  461. ctl->timed_out++;
  462. }
  463. engine->process_errors++;
  464. break;
  465. } else {
  466. DEBUG ("reading byte from subgraph_wait_fd==%d", client->subgraph_wait_fd);
  467. if (read (client->subgraph_wait_fd, &c, sizeof(c)) != sizeof (c)) {
  468. jack_error ("pp: cannot clean up byte from graph wait fd (%s)", strerror (errno));
  469. client->error++;
  470. break;
  471. }
  472. }
  473. /* Move to next internal client (or end of client list) */
  474. while (node) {
  475. if (jack_client_is_internal (((jack_client_internal_t *) node->data))) {
  476. break;
  477. }
  478. node = jack_slist_next (node);
  479. }
  480. }
  481. }
  482. return engine->process_errors > 0;
  483. }
  484. static int
  485. jack_engine_post_process (jack_engine_t *engine)
  486. {
  487. jack_client_control_t *ctl;
  488. jack_client_internal_t *client;
  489. JSList *node;
  490. int need_remove = FALSE;
  491. engine->control->pending_time.usecs = engine->control->current_time.usecs;
  492. engine->control->current_time = engine->control->pending_time;
  493. /* find any clients that need removal due to timeouts, etc. */
  494. for (node = engine->clients; node; node = jack_slist_next (node) ) {
  495. client = (jack_client_internal_t *) node->data;
  496. ctl = client->control;
  497. /* this check is invalid for internal clients and external
  498. clients with no process callback.
  499. */
  500. if (!jack_client_is_internal (client) && ctl->process) {
  501. if (ctl->awake_at != 0 && ctl->state > NotTriggered && ctl->state != Finished && ctl->timed_out++) {
  502. fprintf (stderr, "client %s error: awake_at = %Lu state = %d timed_out = %d\n",
  503. ctl->name,
  504. ctl->awake_at,
  505. ctl->state,
  506. ctl->timed_out);
  507. client->error++;
  508. }
  509. }
  510. if (client->error) {
  511. need_remove = TRUE;
  512. }
  513. }
  514. if (need_remove) {
  515. JSList *tmp;
  516. int need_sort = FALSE;
  517. /* remove all dead clients */
  518. for (node = engine->clients; node; ) {
  519. tmp = jack_slist_next (node);
  520. client = (jack_client_internal_t *) node->data;
  521. if (client->error) {
  522. /* if we have a communication problem with the client,
  523. remove it. otherwise, turn it into a zombie. the client
  524. will/should realize this and will close it sockets.
  525. then we'll end up back here again and will finally
  526. remove the client.
  527. */
  528. if (client->error >= JACK_ERROR_WITH_SOCKETS) {
  529. if (engine->verbose) {
  530. fprintf (stderr, "removing failed client %s state = %s errors = %d\n",
  531. client->control->name, client_state_names[client->control->state],
  532. client->error);
  533. }
  534. jack_remove_client (engine, (jack_client_internal_t *) node->data);
  535. } else {
  536. if (engine->verbose) {
  537. fprintf (stderr, "zombifying failed client %s state = %s errors = %d\n",
  538. client->control->name, client_state_names[client->control->state],
  539. client->error);
  540. }
  541. jack_zombify_client (engine, (jack_client_internal_t *) node->data);
  542. client->error = 0;
  543. }
  544. need_sort = TRUE;
  545. }
  546. node = tmp;
  547. }
  548. if (need_sort) {
  549. jack_sort_graph (engine);
  550. }
  551. jack_engine_reset_rolling_usecs (engine);
  552. }
  553. return 0;
  554. }
  555. static int
  556. jack_load_client (jack_engine_t *engine, jack_client_internal_t *client, const char *so_name)
  557. {
  558. const char *errstr;
  559. char path_to_so[PATH_MAX+1];
  560. snprintf (path_to_so, sizeof (path_to_so), ADDON_DIR "/%s.so", so_name);
  561. client->handle = dlopen (path_to_so, RTLD_NOW|RTLD_GLOBAL);
  562. if (client->handle == 0) {
  563. if ((errstr = dlerror ()) != 0) {
  564. jack_error ("can't load \"%s\": %s", path_to_so, errstr);
  565. } else {
  566. jack_error ("bizarre error loading shared object %s", so_name);
  567. }
  568. return -1;
  569. }
  570. client->initialize = dlsym (client->handle, "jack_initialize");
  571. if ((errstr = dlerror ()) != 0) {
  572. jack_error ("no initialize function in shared object %s\n", so_name);
  573. dlclose (client->handle);
  574. client->handle = 0;
  575. return -1;
  576. }
  577. client->finish = (void (*)(void)) dlsym (client->handle, "jack_finish");
  578. if ((errstr = dlerror ()) != 0) {
  579. jack_error ("no finish function in in shared object %s", so_name);
  580. dlclose (client->handle);
  581. client->handle = 0;
  582. return -1;
  583. }
  584. return 0;
  585. }
  586. static void
  587. jack_client_unload (jack_client_internal_t *client)
  588. {
  589. if (client->handle) {
  590. if (client->finish) {
  591. client->finish ();
  592. }
  593. dlclose (client->handle);
  594. }
  595. }
  596. static jack_client_internal_t *
  597. setup_client (jack_engine_t *engine, int client_fd, jack_client_connect_request_t *req, jack_client_connect_result_t *res)
  598. {
  599. JSList *node;
  600. jack_client_internal_t *client = NULL;
  601. for (node = engine->clients; node; node = jack_slist_next (node)) {
  602. client = (jack_client_internal_t *) node->data;
  603. if (strncmp(req->name, (char*)client->control->name, sizeof(req->name)) == 0) {
  604. jack_error ("cannot create new client; %s already exists", client->control->name);
  605. return NULL;
  606. }
  607. }
  608. if ((client = jack_client_internal_new (engine, client_fd, req)) == NULL) {
  609. jack_error ("cannot create new client object");
  610. return 0;
  611. }
  612. if (engine->verbose) {
  613. fprintf (stderr, "new client: %s, id = %ld type %d @ %p fd = %d\n",
  614. client->control->name, client->control->id,
  615. req->type, client->control, client_fd);
  616. }
  617. res->protocol_v = jack_protocol_version;
  618. res->client_key = client->shm_key;
  619. res->control_key = engine->control_key;
  620. res->port_segment_key = engine->port_segment_key;
  621. res->realtime = engine->control->real_time;
  622. res->realtime_priority = engine->rtpriority - 1;
  623. if (jack_client_is_internal(client)) {
  624. /* set up the pointers necessary for the request system
  625. to work.
  626. */
  627. client->control->deliver_request = internal_client_request;
  628. client->control->deliver_arg = engine;
  629. /* the client is in the same address space */
  630. res->client_control = client->control;
  631. res->engine_control = engine->control;
  632. } else {
  633. strcpy (res->fifo_prefix, engine->fifo_prefix);
  634. }
  635. jack_lock_graph (engine);
  636. engine->clients = jack_slist_prepend (engine->clients, client);
  637. jack_engine_reset_rolling_usecs (engine);
  638. switch (client->control->type) {
  639. case ClientDriver:
  640. case ClientInternal:
  641. /* an internal client still needs to be able to make
  642. regular JACK API calls, which need a jack_client_t
  643. structure. create one here for it.
  644. */
  645. client->control->private_client = jack_client_alloc_internal (client->control, engine->control);
  646. jack_unlock_graph (engine);
  647. /* call its initialization function */
  648. if (client->control->type == ClientInternal) {
  649. /* tell it about the port segment. XXX fix to work with multiples */
  650. jack_client_handle_new_port_segment (client->control->private_client,
  651. engine->port_segment_key,
  652. engine->port_segment_address);
  653. if (client->initialize (client->control->private_client, req->object_data)) {
  654. jack_client_delete (engine, client);
  655. return 0;
  656. }
  657. }
  658. /* its good to go */
  659. break;
  660. default:
  661. if (engine->pfd_max >= engine->pfd_size) {
  662. engine->pfd = (struct pollfd *) realloc (engine->pfd, sizeof (struct pollfd) * engine->pfd_size + 16);
  663. engine->pfd_size += 16;
  664. }
  665. engine->pfd[engine->pfd_max].fd = client->request_fd;
  666. engine->pfd[engine->pfd_max].events = POLLIN|POLLPRI|POLLERR|POLLHUP|POLLNVAL;
  667. engine->pfd_max++;
  668. jack_unlock_graph (engine);
  669. break;
  670. }
  671. return client;
  672. }
  673. static jack_driver_info_t *
  674. jack_load_driver (jack_engine_t *engine, char *so_name)
  675. {
  676. const char *errstr;
  677. char path_to_so[PATH_MAX+1];
  678. jack_driver_info_t *info;
  679. info = (jack_driver_info_t *) calloc (1, sizeof (*info));
  680. snprintf (path_to_so, sizeof (path_to_so), ADDON_DIR "/jack_%s.so", so_name);
  681. info->handle = dlopen (path_to_so, RTLD_NOW|RTLD_GLOBAL);
  682. if (info->handle == NULL) {
  683. if ((errstr = dlerror ()) != 0) {
  684. jack_error ("can't load \"%s\": %s", path_to_so, errstr);
  685. } else {
  686. jack_error ("bizarre error loading driver shared object %s", path_to_so);
  687. }
  688. goto fail;
  689. }
  690. info->initialize = dlsym (info->handle, "driver_initialize");
  691. if ((errstr = dlerror ()) != 0) {
  692. jack_error ("no initialize function in shared object %s\n", path_to_so);
  693. goto fail;
  694. }
  695. info->finish = dlsym (info->handle, "driver_finish");
  696. if ((errstr = dlerror ()) != 0) {
  697. jack_error ("no finish function in in shared driver object %s", path_to_so);
  698. goto fail;
  699. }
  700. info->client_name = (char *) dlsym (info->handle, "driver_client_name");
  701. fprintf (stderr, "driver client name = [%s]\n", info->client_name);
  702. if ((errstr = dlerror ()) != 0) {
  703. jack_error ("no client name in in shared driver object %s", path_to_so);
  704. goto fail;
  705. }
  706. return info;
  707. fail:
  708. if (info->handle) {
  709. dlclose (info->handle);
  710. }
  711. free (info);
  712. return NULL;
  713. }
  714. void
  715. jack_driver_unload (jack_driver_t *driver)
  716. {
  717. driver->finish (driver);
  718. dlclose (driver->handle);
  719. }
  720. int
  721. jack_engine_load_driver (jack_engine_t *engine, int argc, char *argv[])
  722. {
  723. jack_client_connect_request_t req;
  724. jack_client_connect_result_t res;
  725. jack_client_internal_t *client;
  726. jack_driver_t *driver;
  727. jack_driver_info_t *info;
  728. if ((info = jack_load_driver (engine, argv[0])) == NULL) {
  729. return -1;
  730. }
  731. req.type = ClientDriver;
  732. snprintf (req.name, sizeof (req.name), "%s", info->client_name);
  733. if ((client = setup_client (engine, -1, &req, &res)) == NULL) {
  734. return -1;
  735. }
  736. if ((driver = info->initialize (client->control->private_client, argc, argv)) != 0) {
  737. driver->handle = info->handle;
  738. driver->finish = info->finish;
  739. }
  740. free (info);
  741. if (jack_use_driver (engine, driver)) {
  742. jack_driver_unload (driver);
  743. jack_client_delete (engine, client);
  744. return -1;
  745. }
  746. return 0;
  747. }
  748. static int
  749. handle_unload_client (jack_engine_t *engine, int client_fd, jack_client_connect_request_t *req)
  750. {
  751. JSList *node;
  752. jack_client_connect_result_t res;
  753. res.status = -1;
  754. fprintf (stderr, "unloading client \"%s\"\n", req->name);
  755. jack_lock_graph (engine);
  756. for (node = engine->clients; node; node = jack_slist_next (node)) {
  757. if (strcmp ((char *) ((jack_client_internal_t *) node->data)->control->name, req->name) == 0) {
  758. jack_remove_client (engine, (jack_client_internal_t *) node->data);
  759. fprintf (stderr, "found it\n");
  760. res.status = 0;
  761. break;
  762. }
  763. }
  764. jack_unlock_graph (engine);
  765. return 0;
  766. }
  767. static int
  768. handle_new_client (jack_engine_t *engine, int client_fd)
  769. {
  770. jack_client_internal_t *client;
  771. jack_client_connect_request_t req;
  772. jack_client_connect_result_t res;
  773. if (read (client_fd, &req, sizeof (req)) != sizeof (req)) {
  774. jack_error ("cannot read connection request from client");
  775. return -1;
  776. }
  777. if (!req.load) {
  778. return handle_unload_client (engine, client_fd, &req);
  779. }
  780. if ((client = setup_client (engine, client_fd, &req, &res)) == NULL) {
  781. return -1;
  782. }
  783. if (write (client->request_fd, &res, sizeof (res)) != sizeof (res)) {
  784. jack_error ("cannot write connection response to client");
  785. jack_client_delete (engine, client);
  786. return -1;
  787. }
  788. switch (client->control->type) {
  789. case ClientDriver:
  790. case ClientInternal:
  791. close (client_fd);
  792. break;
  793. default:
  794. break;
  795. }
  796. return 0;
  797. }
  798. static int
  799. handle_client_ack_connection (jack_engine_t *engine, int client_fd)
  800. {
  801. jack_client_internal_t *client;
  802. jack_client_connect_ack_request_t req;
  803. jack_client_connect_ack_result_t res;
  804. if (read (client_fd, &req, sizeof (req)) != sizeof (req)) {
  805. jack_error ("cannot read ACK connection request from client");
  806. return -1;
  807. }
  808. if ((client = jack_client_internal_by_id (engine, req.client_id)) == NULL) {
  809. jack_error ("unknown client ID in ACK connection request");
  810. return -1;
  811. }
  812. client->event_fd = client_fd;
  813. res.status = 0;
  814. if (write (client->event_fd, &res, sizeof (res)) != sizeof (res)) {
  815. jack_error ("cannot write ACK connection response to client");
  816. return -1;
  817. }
  818. return 0;
  819. }
  820. #ifdef USE_CAPABILITIES
  821. static int check_capabilities (jack_engine_t *engine)
  822. {
  823. cap_t caps = cap_init();
  824. cap_flag_value_t cap;
  825. pid_t pid;
  826. int have_all_caps = 1;
  827. if (caps == NULL) {
  828. if (engine->verbose) {
  829. fprintf (stderr, "check: could not allocate capability working storage\n");
  830. }
  831. return 0;
  832. }
  833. pid = getpid ();
  834. cap_clear (caps);
  835. if (capgetp (pid, caps)) {
  836. if (engine->verbose) {
  837. fprintf (stderr, "check: could not get capabilities for process %d\n", pid);
  838. }
  839. return 0;
  840. }
  841. /* check that we are able to give capabilites to other processes */
  842. cap_get_flag(caps, CAP_SETPCAP, CAP_EFFECTIVE, &cap);
  843. if (cap == CAP_CLEAR) {
  844. have_all_caps = 0;
  845. goto done;
  846. }
  847. /* check that we have the capabilities we want to transfer */
  848. cap_get_flag(caps, CAP_SYS_NICE, CAP_EFFECTIVE, &cap);
  849. if (cap == CAP_CLEAR) {
  850. have_all_caps = 0;
  851. goto done;
  852. }
  853. cap_get_flag(caps, CAP_SYS_RESOURCE, CAP_EFFECTIVE, &cap);
  854. if (cap == CAP_CLEAR) {
  855. have_all_caps = 0;
  856. goto done;
  857. }
  858. cap_get_flag(caps, CAP_IPC_LOCK, CAP_EFFECTIVE, &cap);
  859. if (cap == CAP_CLEAR) {
  860. have_all_caps = 0;
  861. goto done;
  862. }
  863. done:
  864. cap_free (caps);
  865. return have_all_caps;
  866. }
  867. static int give_capabilities (jack_engine_t *engine, pid_t pid)
  868. {
  869. cap_t caps = cap_init();
  870. const unsigned caps_size = 3;
  871. cap_value_t cap_list[] = { CAP_SYS_NICE, CAP_SYS_RESOURCE, CAP_IPC_LOCK};
  872. if (caps == NULL) {
  873. if (engine->verbose) {
  874. fprintf (stderr, "give: could not allocate capability working storage\n");
  875. }
  876. return -1;
  877. }
  878. cap_clear(caps);
  879. if (capgetp (pid, caps)) {
  880. if (engine->verbose) {
  881. fprintf (stderr, "give: could not get current capabilities for process %d\n", pid);
  882. }
  883. cap_clear(caps);
  884. }
  885. cap_set_flag(caps, CAP_EFFECTIVE, caps_size, cap_list , CAP_SET);
  886. cap_set_flag(caps, CAP_INHERITABLE, caps_size, cap_list , CAP_SET);
  887. cap_set_flag(caps, CAP_PERMITTED, caps_size, cap_list , CAP_SET);
  888. if (capsetp (pid, caps)) {
  889. cap_free (caps);
  890. return -1;
  891. }
  892. cap_free (caps);
  893. return 0;
  894. }
  895. static int
  896. jack_set_client_capabilities (jack_engine_t *engine, jack_client_id_t id)
  897. {
  898. JSList *node;
  899. int ret = -1;
  900. jack_lock_graph (engine);
  901. for (node = engine->clients; node; node = jack_slist_next (node)) {
  902. jack_client_internal_t *client = (jack_client_internal_t *) node->data;
  903. if (client->control->id == id) {
  904. /* before sending this request the client has already checked
  905. that the engine has realtime capabilities, that it is running
  906. realtime and that the pid is defined
  907. */
  908. ret = give_capabilities (engine, client->control->pid);
  909. if (ret) {
  910. jack_error ("could not give capabilities to process %d\n",
  911. client->control->pid);
  912. } else {
  913. if (engine->verbose) {
  914. fprintf (stderr, "gave capabilities to process %d\n",
  915. client->control->pid);
  916. }
  917. }
  918. }
  919. }
  920. jack_unlock_graph (engine);
  921. return ret;
  922. }
  923. #endif
  924. static int
  925. jack_client_activate (jack_engine_t *engine, jack_client_id_t id)
  926. {
  927. jack_client_internal_t *client;
  928. JSList *node;
  929. int ret = -1;
  930. jack_lock_graph (engine);
  931. for (node = engine->clients; node; node = jack_slist_next (node)) {
  932. if (((jack_client_internal_t *) node->data)->control->id == id) {
  933. client = (jack_client_internal_t *) node->data;
  934. client->control->active = TRUE;
  935. /* we call this to make sure the
  936. FIFO is built+ready by the time
  937. the client needs it. we don't
  938. care about the return value at
  939. this point.
  940. */
  941. jack_get_fifo_fd (engine, ++engine->external_client_cnt);
  942. jack_sort_graph (engine);
  943. ret = 0;
  944. break;
  945. }
  946. }
  947. jack_unlock_graph (engine);
  948. return ret;
  949. }
  950. static int
  951. jack_client_do_deactivate (jack_engine_t *engine, jack_client_internal_t *client, int sort_graph)
  952. {
  953. /* called must hold engine->client_lock and must have checked for and/or
  954. cleared all connections held by client.
  955. */
  956. client->control->active = FALSE;
  957. if (!jack_client_is_internal (client) && engine->external_client_cnt > 0) {
  958. engine->external_client_cnt--;
  959. }
  960. if (sort_graph) {
  961. jack_sort_graph (engine);
  962. }
  963. return 0;
  964. }
  965. static void
  966. jack_client_disconnect (jack_engine_t *engine, jack_client_internal_t *client)
  967. {
  968. JSList *node;
  969. jack_port_internal_t *port;
  970. /* call tree **** MUST HOLD *** engine->client_lock */
  971. for (node = client->ports; node; node = jack_slist_next (node)) {
  972. port = (jack_port_internal_t *) node->data;
  973. jack_port_clear_connections (engine, port);
  974. jack_port_release (engine, port);
  975. }
  976. jack_slist_free (client->ports);
  977. jack_slist_free (client->fed_by);
  978. client->fed_by = 0;
  979. client->ports = 0;
  980. }
  981. static int
  982. jack_client_deactivate (jack_engine_t *engine, jack_client_id_t id)
  983. {
  984. JSList *node;
  985. int ret = -1;
  986. jack_lock_graph (engine);
  987. for (node = engine->clients; node; node = jack_slist_next (node)) {
  988. jack_client_internal_t *client = (jack_client_internal_t *) node->data;
  989. if (client->control->id == id) {
  990. JSList *portnode;
  991. jack_port_internal_t *port;
  992. if (client == engine->timebase_client) {
  993. engine->timebase_client = 0;
  994. engine->control->current_time.frame = 0;
  995. engine->control->pending_time.frame = 0;
  996. engine->control->current_time.transport_state = JackTransportStopped;
  997. engine->control->pending_time.transport_state = JackTransportStopped;
  998. }
  999. for (portnode = client->ports; portnode; portnode = jack_slist_next (portnode)) {
  1000. port = (jack_port_internal_t *) portnode->data;
  1001. jack_port_clear_connections (engine, port);
  1002. }
  1003. ret = jack_client_do_deactivate (engine, node->data, TRUE);
  1004. break;
  1005. }
  1006. }
  1007. jack_unlock_graph (engine);
  1008. return ret;
  1009. }
  1010. static int
  1011. jack_set_timebase (jack_engine_t *engine, jack_client_id_t client)
  1012. {
  1013. int ret = -1;
  1014. jack_lock_graph (engine);
  1015. if ((engine->timebase_client = jack_client_internal_by_id (engine, client)) != 0) {
  1016. ret = 0;
  1017. }
  1018. jack_unlock_graph (engine);
  1019. return ret;
  1020. }
  1021. static int
  1022. handle_client_socket_error (jack_engine_t *engine, int fd)
  1023. {
  1024. jack_client_internal_t *client = 0;
  1025. JSList *node;
  1026. jack_lock_graph (engine);
  1027. for (node = engine->clients; node; node = jack_slist_next (node)) {
  1028. if (jack_client_is_internal((jack_client_internal_t *) node->data)) {
  1029. continue;
  1030. }
  1031. if (((jack_client_internal_t *) node->data)->request_fd == fd) {
  1032. client = (jack_client_internal_t *) node->data;
  1033. if (client->error < JACK_ERROR_WITH_SOCKETS) {
  1034. client->error += JACK_ERROR_WITH_SOCKETS;
  1035. }
  1036. break;
  1037. }
  1038. }
  1039. jack_unlock_graph (engine);
  1040. return 0;
  1041. }
  1042. static void
  1043. do_request (jack_engine_t *engine, jack_request_t *req, int *reply_fd)
  1044. {
  1045. pthread_mutex_lock (&engine->request_lock);
  1046. DEBUG ("got a request of type %d", req->type);
  1047. switch (req->type) {
  1048. case RegisterPort:
  1049. req->status = jack_port_do_register (engine, req);
  1050. break;
  1051. case UnRegisterPort:
  1052. req->status = jack_port_do_unregister (engine, req);
  1053. break;
  1054. case ConnectPorts:
  1055. req->status = jack_port_do_connect (engine, req->x.connect.source_port, req->x.connect.destination_port);
  1056. break;
  1057. case DisconnectPort:
  1058. req->status = jack_port_do_disconnect_all (engine, req->x.port_info.port_id);
  1059. break;
  1060. case DisconnectPorts:
  1061. req->status = jack_port_do_disconnect (engine, req->x.connect.source_port, req->x.connect.destination_port);
  1062. break;
  1063. case ActivateClient:
  1064. req->status = jack_client_activate (engine, req->x.client_id);
  1065. break;
  1066. case DeactivateClient:
  1067. req->status = jack_client_deactivate (engine, req->x.client_id);
  1068. break;
  1069. case SetTimeBaseClient:
  1070. req->status = jack_set_timebase (engine, req->x.client_id);
  1071. break;
  1072. #ifdef USE_CAPABILITIES
  1073. case SetClientCapabilities:
  1074. req->status = jack_set_client_capabilities (engine, req->x.client_id);
  1075. break;
  1076. #endif
  1077. case GetPortConnections:
  1078. case GetPortNConnections:
  1079. if ((req->status = jack_do_get_port_connections (engine, req, *reply_fd)) == 0) {
  1080. /* we have already replied, don't do it again */
  1081. *reply_fd = -1;
  1082. }
  1083. break;
  1084. default:
  1085. /* some requests are handled entirely on the client side,
  1086. by adjusting the shared memory area(s)
  1087. */
  1088. break;
  1089. }
  1090. pthread_mutex_unlock (&engine->request_lock);
  1091. DEBUG ("status of request: %d", req->status);
  1092. }
  1093. static int
  1094. internal_client_request (void* ptr, jack_request_t *request)
  1095. {
  1096. do_request ((jack_engine_t*) ptr, request, 0);
  1097. return request->status;
  1098. }
  1099. static int
  1100. handle_external_client_request (jack_engine_t *engine, int fd)
  1101. {
  1102. jack_request_t req;
  1103. jack_client_internal_t *client = 0;
  1104. int reply_fd;
  1105. JSList *node;
  1106. ssize_t r;
  1107. DEBUG ("HIT: before lock");
  1108. jack_lock_graph (engine);
  1109. DEBUG ("HIT: before for");
  1110. for (node = engine->clients; node; node = jack_slist_next (node)) {
  1111. if (((jack_client_internal_t *) node->data)->request_fd == fd) {
  1112. DEBUG ("HIT: in for");
  1113. client = (jack_client_internal_t *) node->data;
  1114. break;
  1115. }
  1116. }
  1117. DEBUG ("HIT: after for");
  1118. jack_unlock_graph (engine);
  1119. if (client == NULL) {
  1120. jack_error ("client input on unknown fd %d!", fd);
  1121. return -1;
  1122. }
  1123. if ((r = read (client->request_fd, &req, sizeof (req))) < (ssize_t) sizeof (req)) {
  1124. jack_error ("cannot read request from client (%d/%d/%s)", r, sizeof(req), strerror (errno));
  1125. client->error++;
  1126. return -1;
  1127. }
  1128. reply_fd = client->request_fd;
  1129. do_request (engine, &req, &reply_fd);
  1130. if (reply_fd >= 0) {
  1131. DEBUG ("replying to client");
  1132. if (write (reply_fd, &req, sizeof (req)) < (ssize_t) sizeof (req)) {
  1133. jack_error ("cannot write request result to client");
  1134. return -1;
  1135. }
  1136. } else {
  1137. DEBUG ("*not* replying to client");
  1138. }
  1139. return 0;
  1140. }
  1141. static void *
  1142. jack_server_thread (void *arg)
  1143. {
  1144. jack_engine_t *engine = (jack_engine_t *) arg;
  1145. struct sockaddr_un client_addr;
  1146. socklen_t client_addrlen;
  1147. struct pollfd *pfd;
  1148. int client_socket;
  1149. int done = 0;
  1150. int i;
  1151. int max;
  1152. pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  1153. engine->pfd[0].fd = engine->fds[0];
  1154. engine->pfd[0].events = POLLIN|POLLERR;
  1155. engine->pfd[1].fd = engine->fds[1];
  1156. engine->pfd[1].events = POLLIN|POLLERR;
  1157. engine->pfd_max = 2;
  1158. while (!done) {
  1159. DEBUG ("start while");
  1160. /* XXX race here with new external clients
  1161. causing engine->pfd to be reallocated.
  1162. I don't know how to solve this
  1163. short of copying the entire
  1164. contents of the pfd struct. Ick.
  1165. */
  1166. max = engine->pfd_max;
  1167. pfd = engine->pfd;
  1168. if (poll (pfd, max, 10000) < 0) {
  1169. if (errno == EINTR) {
  1170. continue;
  1171. }
  1172. jack_error ("poll failed (%s)", strerror (errno));
  1173. break;
  1174. }
  1175. /* check the master server socket */
  1176. if (pfd[0].revents & POLLERR) {
  1177. jack_error ("error on server socket");
  1178. break;
  1179. }
  1180. if (pfd[0].revents & POLLIN) {
  1181. DEBUG ("pfd[0].revents & POLLIN");
  1182. memset (&client_addr, 0, sizeof (client_addr));
  1183. client_addrlen = sizeof (client_addr);
  1184. if ((client_socket = accept (engine->fds[0], (struct sockaddr *) &client_addr, &client_addrlen)) < 0) {
  1185. jack_error ("cannot accept new connection (%s)", strerror (errno));
  1186. } else if (handle_new_client (engine, client_socket) < 0) {
  1187. jack_error ("cannot complete new client connection process");
  1188. close (client_socket);
  1189. }
  1190. }
  1191. /* check the ACK server socket */
  1192. if (pfd[1].revents & POLLERR) {
  1193. jack_error ("error on server ACK socket");
  1194. break;
  1195. }
  1196. if (pfd[1].revents & POLLIN) {
  1197. DEBUG ("pfd[1].revents & POLLIN");
  1198. memset (&client_addr, 0, sizeof (client_addr));
  1199. client_addrlen = sizeof (client_addr);
  1200. if ((client_socket = accept (engine->fds[1], (struct sockaddr *) &client_addr, &client_addrlen)) < 0) {
  1201. jack_error ("cannot accept new ACK connection (%s)", strerror (errno));
  1202. } else if (handle_client_ack_connection (engine, client_socket)) {
  1203. jack_error ("cannot complete client ACK connection process");
  1204. close (client_socket);
  1205. }
  1206. }
  1207. /* check each client socket */
  1208. for (i = 2; i < max; i++) {
  1209. if (pfd[i].fd < 0) {
  1210. continue;
  1211. }
  1212. if (pfd[i].revents & ~POLLIN) {
  1213. handle_client_socket_error (engine, pfd[i].fd);
  1214. } else if (pfd[i].revents & POLLIN) {
  1215. if (handle_external_client_request (engine, pfd[i].fd)) {
  1216. jack_error ("bad hci\n");
  1217. }
  1218. }
  1219. }
  1220. }
  1221. return 0;
  1222. }
  1223. jack_engine_t *
  1224. jack_engine_new (int realtime, int rtpriority, int verbose)
  1225. {
  1226. jack_engine_t *engine;
  1227. size_t control_size;
  1228. void *addr;
  1229. unsigned int i;
  1230. #ifdef USE_CAPABILITIES
  1231. uid_t uid = getuid ();
  1232. uid_t euid = geteuid ();
  1233. #endif
  1234. jack_init_time ();
  1235. engine = (jack_engine_t *) malloc (sizeof (jack_engine_t));
  1236. engine->driver = 0;
  1237. engine->process = jack_process;
  1238. engine->set_sample_rate = jack_set_sample_rate;
  1239. engine->set_buffer_size = jack_set_buffer_size;
  1240. engine->process_lock = jack_engine_process_lock;
  1241. engine->process_unlock = jack_engine_process_unlock;
  1242. engine->post_process = jack_engine_post_process;
  1243. engine->next_client_id = 1;
  1244. engine->timebase_client = 0;
  1245. engine->port_max = 128;
  1246. engine->rtpriority = rtpriority;
  1247. engine->silent_buffer = 0;
  1248. engine->verbose = verbose;
  1249. engine->asio_mode = FALSE;
  1250. jack_engine_reset_rolling_usecs (engine);
  1251. pthread_mutex_init (&engine->client_lock, 0);
  1252. pthread_mutex_init (&engine->buffer_lock, 0);
  1253. pthread_mutex_init (&engine->port_lock, 0);
  1254. pthread_mutex_init (&engine->request_lock, 0);
  1255. engine->clients = 0;
  1256. engine->port_segments = 0;
  1257. engine->port_buffer_freelist = 0;
  1258. engine->pfd_size = 16;
  1259. engine->pfd_max = 0;
  1260. engine->pfd = (struct pollfd *) malloc (sizeof (struct pollfd) * engine->pfd_size);
  1261. engine->fifo_size = 16;
  1262. engine->fifo = (int *) malloc (sizeof (int) * engine->fifo_size);
  1263. for (i = 0; i < engine->fifo_size; i++) {
  1264. engine->fifo[i] = -1;
  1265. }
  1266. engine->external_client_cnt = 0;
  1267. srandom (time ((time_t *) 0));
  1268. engine->control_key = random();
  1269. control_size = sizeof (jack_control_t) + (sizeof (jack_port_shared_t) * engine->port_max);
  1270. if (jack_initialize_shm (engine)) {
  1271. return 0;
  1272. }
  1273. if ((engine->control_shm_id = shmget (engine->control_key, control_size, IPC_CREAT|0644)) < 0) {
  1274. jack_error ("cannot create engine control shared memory segment (%s)", strerror (errno));
  1275. return 0;
  1276. }
  1277. jack_register_shm (engine->control_shm_id);
  1278. if ((addr = shmat (engine->control_shm_id, 0, 0)) == (void *) -1) {
  1279. jack_error ("cannot attach control shared memory segment (%s)", strerror (errno));
  1280. shmctl (engine->control_shm_id, IPC_RMID, 0);
  1281. return 0;
  1282. }
  1283. engine->control = (jack_control_t *) addr;
  1284. engine->control->engine = engine;
  1285. /* Mark all ports as available */
  1286. for (i = 0; i < engine->port_max; i++) {
  1287. engine->control->ports[i].in_use = 0;
  1288. engine->control->ports[i].id = i;
  1289. }
  1290. /* allocate internal port structures so that we can keep
  1291. track of port connections.
  1292. */
  1293. engine->internal_ports = (jack_port_internal_t *) malloc (sizeof (jack_port_internal_t) * engine->port_max);
  1294. for (i = 0; i < engine->port_max; i++) {
  1295. engine->internal_ports[i].connections = 0;
  1296. }
  1297. if (make_sockets (engine->fds) < 0) {
  1298. jack_error ("cannot create server sockets");
  1299. return 0;
  1300. }
  1301. engine->control->port_max = engine->port_max;
  1302. engine->control->real_time = realtime;
  1303. engine->control->client_priority = engine->rtpriority - 1;
  1304. engine->control->cpu_load = 0;
  1305. engine->control->buffer_size = 0;
  1306. engine->control->current_time.frame_rate = 0;
  1307. engine->control->current_time.frame = 0;
  1308. engine->control->pending_time.frame_rate = 0;
  1309. engine->control->pending_time.frame = 0;
  1310. engine->control->internal = 0;
  1311. engine->control->has_capabilities = 0;
  1312. #ifdef USE_CAPABILITIES
  1313. if (uid == 0 || euid == 0) {
  1314. if (engine->verbose) {
  1315. fprintf (stderr, "running with uid=%d and euid=%d, will not try to use capabilites\n",
  1316. uid, euid);
  1317. }
  1318. } else {
  1319. /* only try to use capabilities if we are not running as root */
  1320. engine->control->has_capabilities = check_capabilities (engine);
  1321. if (engine->control->has_capabilities == 0) {
  1322. if (engine->verbose) {
  1323. fprintf (stderr, "required capabilities not available\n");
  1324. }
  1325. }
  1326. if (engine->verbose) {
  1327. size_t size;
  1328. cap_t cap = cap_init();
  1329. capgetp(0, cap);
  1330. fprintf (stderr, "capabilities: %s\n", cap_to_text(cap, &size));
  1331. }
  1332. }
  1333. #endif
  1334. engine->control->engine_ok = 1;
  1335. snprintf (engine->fifo_prefix, sizeof (engine->fifo_prefix), "%s/jack-ack-fifo-%d", jack_server_dir, getpid());
  1336. (void) jack_get_fifo_fd (engine, 0);
  1337. pthread_create (&engine->server_thread, 0, &jack_server_thread, engine);
  1338. pthread_detach (engine->server_thread);
  1339. return engine;
  1340. }
  1341. static int
  1342. jack_become_real_time (pthread_t thread, int priority)
  1343. {
  1344. struct sched_param rtparam;
  1345. int x;
  1346. memset (&rtparam, 0, sizeof (rtparam));
  1347. rtparam.sched_priority = priority;
  1348. if ((x = pthread_setschedparam (thread, SCHED_FIFO, &rtparam)) != 0) {
  1349. jack_error ("cannot set thread to real-time priority (FIFO/%d) (%d: %s)", rtparam.sched_priority, x, strerror (errno));
  1350. return -1;
  1351. }
  1352. if (mlockall (MCL_CURRENT | MCL_FUTURE) != 0) {
  1353. jack_error ("cannot lock down memory for RT thread (%s)", strerror (errno));
  1354. return -1;
  1355. }
  1356. return 0;
  1357. }
  1358. #ifdef HAVE_ON_EXIT
  1359. static void
  1360. cancel_cleanup (int status, void *arg)
  1361. {
  1362. jack_engine_t *engine = (jack_engine_t *) arg;
  1363. engine->control->engine_ok = 0;
  1364. engine->driver->stop (engine->driver);
  1365. engine->driver->finish (engine->driver);
  1366. }
  1367. #else
  1368. #ifdef HAVE_ATEXIT
  1369. jack_engine_t *global_engine;
  1370. static void
  1371. cancel_cleanup (void)
  1372. {
  1373. jack_engine_t *engine = global_engine;
  1374. engine->driver->stop (engine->driver);
  1375. engine->driver->finish (engine->driver);
  1376. }
  1377. #else
  1378. #error "Don't know how to make an exit handler"
  1379. #endif /* HAVE_ATEXIT */
  1380. #endif /* HAVE_ON_EXIT */
  1381. static void *
  1382. watchdog_thread (void *arg)
  1383. {
  1384. jack_engine_t *engine = (jack_engine_t *) arg;
  1385. int watchdog_priority = (engine->rtpriority) > 89 ? 99 : engine->rtpriority + 10;
  1386. pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  1387. if (jack_become_real_time (pthread_self(), watchdog_priority)) {
  1388. return 0;
  1389. }
  1390. engine->watchdog_check = 0;
  1391. while (1) {
  1392. usleep (5000000);
  1393. if (engine->watchdog_check == 0) {
  1394. jack_error ("jackd watchdog: timeout - killing jackd");
  1395. /* kill the process group of the current client */
  1396. kill (-engine->current_client->control->pid, SIGKILL);
  1397. /* kill our process group */
  1398. kill (-getpgrp(), SIGKILL);
  1399. /*NOTREACHED*/
  1400. exit (1);
  1401. }
  1402. engine->watchdog_check = 0;
  1403. }
  1404. }
  1405. static int
  1406. jack_start_watchdog (jack_engine_t *engine)
  1407. {
  1408. pthread_t watchdog;
  1409. if (pthread_create (&watchdog, 0, watchdog_thread, engine)) {
  1410. jack_error ("cannot start watchdog thread");
  1411. return -1;
  1412. }
  1413. pthread_detach (watchdog);
  1414. return 0;
  1415. }
  1416. static void
  1417. jack_engine_notify_clients_about_delay (jack_engine_t *engine)
  1418. {
  1419. JSList *node;
  1420. jack_event_t event;
  1421. event.type = XRun;
  1422. jack_lock_graph (engine);
  1423. for (node = engine->clients; node; node = jack_slist_next (node)) {
  1424. jack_deliver_event (engine, (jack_client_internal_t *) node->data, &event);
  1425. }
  1426. jack_unlock_graph (engine);
  1427. }
  1428. static inline void
  1429. jack_inc_frame_time (jack_engine_t *engine, jack_nframes_t amount)
  1430. {
  1431. jack_frame_timer_t *time = &engine->control->frame_timer;
  1432. // atomic_inc (&time->guard1, 1);
  1433. // really need a memory barrier here
  1434. time->guard1++;
  1435. time->frames += amount;
  1436. time->stamp = jack_get_microseconds ();
  1437. // atomic_inc (&time->guard2, 1);
  1438. // might need a memory barrier here
  1439. time->guard2++;
  1440. }
  1441. static void *
  1442. jack_main_thread (void *arg)
  1443. {
  1444. jack_engine_t *engine = (jack_engine_t *) arg;
  1445. jack_driver_t *driver = engine->driver;
  1446. int consecutive_excessive_delays;
  1447. unsigned long long cycle_end;
  1448. jack_nframes_t nframes;
  1449. if (engine->control->real_time) {
  1450. if (jack_start_watchdog (engine)) {
  1451. pthread_exit (0);
  1452. }
  1453. if (jack_become_real_time (pthread_self(), engine->rtpriority)) {
  1454. engine->control->real_time = 0;
  1455. }
  1456. }
  1457. pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  1458. #ifdef HAVE_ON_EXIT
  1459. on_exit (cancel_cleanup, engine);
  1460. #else
  1461. #ifdef HAVE_ATEXIT
  1462. global_engine = engine;
  1463. atexit (cancel_cleanup);
  1464. #else
  1465. #error "Don't know how to install an exit handler"
  1466. #endif /* HAVE_ATEXIT */
  1467. #endif /* HAVE_ON_EXIT */
  1468. if (driver->start (driver)) {
  1469. jack_error ("cannot start driver");
  1470. pthread_exit (0);
  1471. }
  1472. consecutive_excessive_delays = 0;
  1473. engine->watchdog_check = 1;
  1474. nframes = 0;
  1475. while (1) {
  1476. int status;
  1477. float delayed_usecs;
  1478. if ((nframes = driver->wait (driver, -1, &status, &delayed_usecs)) == 0) {
  1479. /* the driver detected an xrun, and restarted */
  1480. continue;
  1481. }
  1482. jack_inc_frame_time (engine, nframes);
  1483. engine->watchdog_check = 1;
  1484. #define WORK_SCALE 1.0f
  1485. if (engine->control->real_time != 0 && engine->spare_usecs && ((WORK_SCALE * engine->spare_usecs) <= delayed_usecs)) {
  1486. fprintf (stderr, "delay of %.3f usecs exceeds estimated spare time of %.3f; restart ...\n",
  1487. delayed_usecs, WORK_SCALE * engine->spare_usecs);
  1488. if (++consecutive_excessive_delays > 10) {
  1489. jack_error ("too many consecutive interrupt delays ... engine stopping");
  1490. break;
  1491. }
  1492. if (driver->stop (driver)) {
  1493. jack_error ("cannot stop current driver");
  1494. break;
  1495. }
  1496. jack_engine_notify_clients_about_delay (engine);
  1497. if (driver->start (driver)) {
  1498. jack_error ("cannot restart current driver after delay");
  1499. break;
  1500. }
  1501. continue;
  1502. } else {
  1503. consecutive_excessive_delays = 0;
  1504. }
  1505. if (status != 0) {
  1506. jack_error ("driver wait function failed, exiting");
  1507. pthread_exit (0);
  1508. }
  1509. /* this will execute the entire jack graph */
  1510. switch (driver->process (driver, nframes)) {
  1511. case -1:
  1512. jack_error ("driver process function failed, exiting");
  1513. pthread_exit (0);
  1514. break;
  1515. case 1:
  1516. if (driver->start (driver)) {
  1517. jack_error ("cannot restart driver");
  1518. pthread_exit (0);
  1519. }
  1520. break;
  1521. default:
  1522. break;
  1523. }
  1524. cycle_end = jack_get_microseconds ();
  1525. /* store the execution time for later averaging */
  1526. engine->rolling_client_usecs[engine->rolling_client_usecs_index++] =
  1527. cycle_end - engine->control->current_time.usecs;
  1528. if (engine->rolling_client_usecs_index >= JACK_ENGINE_ROLLING_COUNT) {
  1529. engine->rolling_client_usecs_index = 0;
  1530. }
  1531. /* every so often, recompute the current maximum use over the
  1532. last JACK_ENGINE_ROLLING_COUNT client iterations.
  1533. */
  1534. if (++engine->rolling_client_usecs_cnt % engine->rolling_interval == 0) {
  1535. float max_usecs = 0.0f;
  1536. int i;
  1537. for (i = 0; i < JACK_ENGINE_ROLLING_COUNT; i++) {
  1538. if (engine->rolling_client_usecs[i] > max_usecs) {
  1539. max_usecs = engine->rolling_client_usecs[i];
  1540. }
  1541. }
  1542. if (max_usecs < engine->driver->period_usecs) {
  1543. engine->spare_usecs = engine->driver->period_usecs - max_usecs;
  1544. } else {
  1545. engine->spare_usecs = 0;
  1546. }
  1547. engine->control->cpu_load = (1.0f - (engine->spare_usecs / engine->driver->period_usecs)) * 50.0f + (engine->control->cpu_load * 0.5f);
  1548. if (engine->verbose) {
  1549. fprintf (stderr, "load = %.4f max usecs: %.3f, spare = %.3f\n",
  1550. engine->control->cpu_load, max_usecs, engine->spare_usecs);
  1551. }
  1552. }
  1553. }
  1554. pthread_exit (0);
  1555. }
  1556. int
  1557. jack_run (jack_engine_t *engine)
  1558. {
  1559. if (engine->driver == NULL) {
  1560. jack_error ("engine driver not set; cannot start");
  1561. return -1;
  1562. }
  1563. return pthread_create (&engine->main_thread, 0, jack_main_thread, engine);
  1564. }
  1565. int
  1566. jack_wait (jack_engine_t *engine)
  1567. {
  1568. void *ret = 0;
  1569. int err;
  1570. if ((err = pthread_join (engine->main_thread, &ret)) != 0) {
  1571. switch (err) {
  1572. case EINVAL:
  1573. jack_error ("cannot join with audio thread (thread detached, or another thread is waiting)");
  1574. break;
  1575. case ESRCH:
  1576. jack_error ("cannot join with audio thread (thread no longer exists)");
  1577. break;
  1578. case EDEADLK:
  1579. jack_error ("programming error: jack_wait() called by audio thread");
  1580. break;
  1581. default:
  1582. jack_error ("cannot join with audio thread (%s)", strerror (errno));
  1583. }
  1584. }
  1585. return (int) ((intptr_t)ret);
  1586. }
  1587. int
  1588. jack_engine_delete (jack_engine_t *engine)
  1589. {
  1590. if (engine) {
  1591. return pthread_cancel (engine->main_thread);
  1592. }
  1593. return 0;
  1594. }
  1595. static jack_client_internal_t *
  1596. jack_client_internal_new (jack_engine_t *engine, int fd, jack_client_connect_request_t *req)
  1597. {
  1598. jack_client_internal_t *client;
  1599. key_t shm_key = 0;
  1600. int shm_id = 0;
  1601. void *addr = 0;
  1602. switch (req->type) {
  1603. case ClientInternal:
  1604. case ClientDriver:
  1605. break;
  1606. case ClientExternal:
  1607. shm_key = random();
  1608. if ((shm_id = shmget (shm_key, sizeof (jack_client_control_t), IPC_CREAT|0666)) < 0) {
  1609. jack_error ("cannot create client control block");
  1610. return 0;
  1611. }
  1612. jack_register_shm (shm_id);
  1613. if ((addr = shmat (shm_id, 0, 0)) == (void *) -1) {
  1614. jack_error ("cannot attach new client control block");
  1615. shmctl (shm_id, IPC_RMID, 0);
  1616. return 0;
  1617. }
  1618. break;
  1619. }
  1620. client = (jack_client_internal_t *) malloc (sizeof (jack_client_internal_t));
  1621. client->request_fd = fd;
  1622. client->event_fd = -1;
  1623. client->ports = 0;
  1624. client->fed_by = 0;
  1625. client->execution_order = UINT_MAX;
  1626. client->next_client = NULL;
  1627. client->handle = NULL;
  1628. client->finish = NULL;
  1629. client->error = 0;
  1630. if (req->type != ClientExternal) {
  1631. client->control = (jack_client_control_t *) malloc (sizeof (jack_client_control_t));
  1632. } else {
  1633. client->shm_id = shm_id;
  1634. client->shm_key = shm_key;
  1635. client->control = (jack_client_control_t *) addr;
  1636. }
  1637. client->control->type = req->type;
  1638. client->control->active = 0;
  1639. client->control->dead = FALSE;
  1640. client->control->timed_out = 0;
  1641. client->control->id = engine->next_client_id++;
  1642. strcpy ((char *) client->control->name, req->name);
  1643. client->subgraph_start_fd = -1;
  1644. client->subgraph_wait_fd = -1;
  1645. client->control->process = NULL;
  1646. client->control->process_arg = NULL;
  1647. client->control->bufsize = NULL;
  1648. client->control->bufsize_arg = NULL;
  1649. client->control->srate = NULL;
  1650. client->control->srate_arg = NULL;
  1651. client->control->port_register = NULL;
  1652. client->control->port_register_arg = NULL;
  1653. client->control->graph_order = NULL;
  1654. client->control->graph_order_arg = NULL;
  1655. if (req->type == ClientInternal) {
  1656. if (jack_load_client (engine, client, req->object_path)) {
  1657. jack_error ("cannot dynamically load client from \"%s\"", req->object_path);
  1658. jack_client_delete (engine, client);
  1659. return 0;
  1660. }
  1661. }
  1662. return client;
  1663. }
  1664. static void
  1665. jack_port_clear_connections (jack_engine_t *engine, jack_port_internal_t *port)
  1666. {
  1667. JSList *node, *next;
  1668. for (node = port->connections; node; ) {
  1669. next = jack_slist_next (node);
  1670. jack_port_disconnect_internal (engine,
  1671. ((jack_connection_internal_t *) node->data)->source,
  1672. ((jack_connection_internal_t *) node->data)->destination,
  1673. FALSE);
  1674. node = next;
  1675. }
  1676. jack_slist_free (port->connections);
  1677. port->connections = 0;
  1678. }
  1679. static void
  1680. jack_zombify_client (jack_engine_t *engine, jack_client_internal_t *client)
  1681. {
  1682. if (engine->verbose) {
  1683. fprintf (stderr, "senor %s - you are a zombie\n", client->control->name);
  1684. }
  1685. /* caller must hold the client_lock */
  1686. /* this stops jack_deliver_event() from doing anything */
  1687. client->control->dead = TRUE;
  1688. if (client == engine->timebase_client) {
  1689. engine->timebase_client = 0;
  1690. engine->control->current_time.frame = 0;
  1691. engine->control->pending_time.frame = 0;
  1692. engine->control->current_time.transport_state = JackTransportStopped;
  1693. engine->control->pending_time.transport_state = JackTransportStopped;
  1694. }
  1695. jack_client_disconnect (engine, client);
  1696. jack_client_do_deactivate (engine, client, FALSE);
  1697. }
  1698. static void
  1699. jack_remove_client (jack_engine_t *engine, jack_client_internal_t *client)
  1700. {
  1701. unsigned int i;
  1702. JSList *node;
  1703. /* caller must hold the client_lock */
  1704. if (engine->verbose) {
  1705. fprintf (stderr, "adios senor %s\n", client->control->name);
  1706. }
  1707. /* if its not already a zombie, make it so */
  1708. if (!client->control->dead) {
  1709. jack_zombify_client (engine, client);
  1710. }
  1711. /* try to force the server thread to return from poll */
  1712. close (client->event_fd);
  1713. close (client->request_fd);
  1714. if (client->control->type == ClientExternal) {
  1715. /* rearrange the pollfd array so that things work right the
  1716. next time we go into poll(2).
  1717. */
  1718. for (i = 0; i < engine->pfd_max; i++) {
  1719. if (engine->pfd[i].fd == client->request_fd) {
  1720. if (i+1 < engine->pfd_max) {
  1721. memmove (&engine->pfd[i], &engine->pfd[i+1], sizeof (struct pollfd) * (engine->pfd_max - i));
  1722. }
  1723. engine->pfd_max--;
  1724. }
  1725. }
  1726. }
  1727. for (node = engine->clients; node; node = jack_slist_next (node)) {
  1728. if (((jack_client_internal_t *) node->data)->control->id == client->control->id) {
  1729. engine->clients = jack_slist_remove_link (engine->clients, node);
  1730. jack_slist_free_1 (node);
  1731. break;
  1732. }
  1733. }
  1734. jack_client_delete (engine, client);
  1735. }
  1736. static void
  1737. jack_client_delete (jack_engine_t *engine, jack_client_internal_t *client)
  1738. {
  1739. if (jack_client_is_internal (client)) {
  1740. jack_client_unload (client);
  1741. free ((char *) client->control);
  1742. } else {
  1743. shmdt ((void *) client->control);
  1744. shmctl(client->shm_id,IPC_RMID,0);
  1745. }
  1746. free (client);
  1747. }
  1748. jack_client_internal_t *
  1749. jack_client_by_name (jack_engine_t *engine, const char *name)
  1750. {
  1751. jack_client_internal_t *client = NULL;
  1752. JSList *node;
  1753. jack_lock_graph (engine);
  1754. for (node = engine->clients; node; node = jack_slist_next (node)) {
  1755. if (strcmp ((const char *) ((jack_client_internal_t *) node->data)->control->name, name) == 0) {
  1756. client = (jack_client_internal_t *) node->data;
  1757. break;
  1758. }
  1759. }
  1760. jack_unlock_graph (engine);
  1761. return client;
  1762. }
  1763. jack_client_internal_t *
  1764. jack_client_internal_by_id (jack_engine_t *engine, jack_client_id_t id)
  1765. {
  1766. jack_client_internal_t *client = NULL;
  1767. JSList *node;
  1768. /* call tree ***MUST HOLD*** the graph lock */
  1769. for (node = engine->clients; node; node = jack_slist_next (node)) {
  1770. if (((jack_client_internal_t *) node->data)->control->id == id) {
  1771. client = (jack_client_internal_t *) node->data;
  1772. break;
  1773. }
  1774. }
  1775. return client;
  1776. }
  1777. static void
  1778. jack_deliver_event_to_all (jack_engine_t *engine, jack_event_t *event)
  1779. {
  1780. JSList *node;
  1781. jack_lock_graph (engine);
  1782. for (node = engine->clients; node; node = jack_slist_next (node)) {
  1783. jack_deliver_event (engine, (jack_client_internal_t *) node->data, event);
  1784. }
  1785. jack_unlock_graph (engine);
  1786. }
  1787. static int
  1788. jack_deliver_event (jack_engine_t *engine, jack_client_internal_t *client, jack_event_t *event)
  1789. {
  1790. char status;
  1791. /* caller must hold the graph lock */
  1792. DEBUG ("delivering event (type %d)", event->type);
  1793. if (client->control->dead) {
  1794. return 0;
  1795. }
  1796. if (jack_client_is_internal (client)) {
  1797. fprintf (stderr, "delivering event %d to IP client %s\n", event->type, client->control->name);
  1798. switch (event->type) {
  1799. case PortConnected:
  1800. case PortDisconnected:
  1801. jack_client_handle_port_connection (client->control->private_client, event);
  1802. break;
  1803. case BufferSizeChange:
  1804. if (client->control->bufsize) {
  1805. client->control->bufsize (event->x.n, client->control->bufsize_arg);
  1806. }
  1807. break;
  1808. case SampleRateChange:
  1809. if (client->control->srate) {
  1810. client->control->srate (event->x.n, client->control->bufsize_arg);
  1811. }
  1812. break;
  1813. case GraphReordered:
  1814. if (client->control->graph_order) {
  1815. client->control->graph_order (client->control->graph_order_arg);
  1816. }
  1817. break;
  1818. case XRun:
  1819. if (client->control->xrun) {
  1820. client->control->xrun (client->control->xrun_arg);
  1821. }
  1822. break;
  1823. case NewPortBufferSegment:
  1824. jack_client_handle_new_port_segment (client->control->private_client, event->x.key, event->y.addr);
  1825. break;
  1826. default:
  1827. /* internal clients don't need to know */
  1828. break;
  1829. }
  1830. } else {
  1831. DEBUG ("engine writing on event fd");
  1832. if (write (client->event_fd, event, sizeof (*event)) != sizeof (*event)) {
  1833. jack_error ("cannot send event to client [%s] (%s)", client->control->name, strerror (errno));
  1834. client->error++;
  1835. }
  1836. DEBUG ("engine reading from event fd");
  1837. if (!client->error && (read (client->event_fd, &status, sizeof (status)) != sizeof (status))) {
  1838. jack_error ("cannot read event response from client [%s] (%s)", client->control->name, strerror (errno));
  1839. client->error++;
  1840. }
  1841. if (status != 0) {
  1842. jack_error ("bad status for client event handling (type = %d)", event->type);
  1843. client->error++;
  1844. }
  1845. }
  1846. DEBUG ("event delivered");
  1847. return 0;
  1848. }
  1849. int
  1850. jack_rechain_graph (jack_engine_t *engine)
  1851. {
  1852. JSList *node, *next;
  1853. unsigned long n;
  1854. int err = 0;
  1855. jack_client_internal_t *client, *subgraph_client, *next_client;
  1856. jack_event_t event;
  1857. jack_clear_fifos (engine);
  1858. subgraph_client = 0;
  1859. if (engine->verbose) {
  1860. fprintf(stderr, "++ jack_rechain_graph():\n");
  1861. }
  1862. event.type = GraphReordered;
  1863. for (n = 0, node = engine->clients, next = NULL; node; node = next) {
  1864. next = jack_slist_next (node);
  1865. if (((jack_client_internal_t *) node->data)->control->active) {
  1866. client = (jack_client_internal_t *) node->data;
  1867. /* find the next active client. its ok for this to be NULL */
  1868. while (next) {
  1869. if (((jack_client_internal_t *) next->data)->control->active) {
  1870. break;
  1871. }
  1872. next = jack_slist_next (next);
  1873. };
  1874. if (next == NULL) {
  1875. next_client = NULL;
  1876. } else {
  1877. next_client = (jack_client_internal_t *) next->data;
  1878. }
  1879. client->execution_order = n;
  1880. client->next_client = next_client;
  1881. if (jack_client_is_internal (client)) {
  1882. /* break the chain for the current subgraph. the server
  1883. will wait for chain on the nth FIFO, and will
  1884. then execute this internal client.
  1885. */
  1886. if (subgraph_client) {
  1887. subgraph_client->subgraph_wait_fd = jack_get_fifo_fd (engine, n);
  1888. if (engine->verbose) {
  1889. fprintf(stderr, "client %s: wait_fd=%d, execution_order=%lu.\n",
  1890. subgraph_client->control->name, subgraph_client->subgraph_wait_fd, n);
  1891. }
  1892. n++;
  1893. }
  1894. if (engine->verbose) {
  1895. fprintf(stderr, "client %s: internal client, execution_order=%lu.\n",
  1896. client->control->name, n);
  1897. }
  1898. /* this does the right thing for internal clients too */
  1899. jack_deliver_event (engine, client, &event);
  1900. subgraph_client = 0;
  1901. } else {
  1902. if (subgraph_client == NULL) {
  1903. /* start a new subgraph. the engine will start the chain
  1904. by writing to the nth FIFO.
  1905. */
  1906. subgraph_client = client;
  1907. subgraph_client->subgraph_start_fd = jack_get_fifo_fd (engine, n);
  1908. if (engine->verbose) {
  1909. fprintf(stderr, "client %s: start_fd=%d, execution_order=%lu.\n",
  1910. subgraph_client->control->name, subgraph_client->subgraph_start_fd, n);
  1911. }
  1912. }
  1913. else {
  1914. if (engine->verbose) {
  1915. fprintf(stderr, "client %s: in subgraph after %s, execution_order=%lu.\n",
  1916. client->control->name, subgraph_client->control->name, n);
  1917. }
  1918. subgraph_client->subgraph_wait_fd = -1;
  1919. }
  1920. /* make sure fifo for 'n + 1' exists
  1921. * before issuing client reorder
  1922. */
  1923. (void) jack_get_fifo_fd(engine, client->execution_order + 1);
  1924. event.x.n = client->execution_order;
  1925. jack_deliver_event (engine, client, &event);
  1926. n++;
  1927. }
  1928. }
  1929. }
  1930. if (subgraph_client) {
  1931. subgraph_client->subgraph_wait_fd = jack_get_fifo_fd (engine, n);
  1932. if (engine->verbose) {
  1933. fprintf(stderr, "client %s: wait_fd=%d, execution_order=%lu (last client).\n",
  1934. subgraph_client->control->name, subgraph_client->subgraph_wait_fd, n);
  1935. }
  1936. }
  1937. if (engine->verbose) {
  1938. fprintf(stderr, "-- jack_rechain_graph()\n");
  1939. }
  1940. return err;
  1941. }
  1942. static void
  1943. jack_trace_terminal (jack_client_internal_t *c1, jack_client_internal_t *rbase)
  1944. {
  1945. jack_client_internal_t *c2;
  1946. /* make a copy of the existing list of routes that feed c1. this provides
  1947. us with an atomic snapshot of c1's "fed-by" state, which will be
  1948. modified as we progress ...
  1949. */
  1950. JSList *existing;
  1951. JSList *node;
  1952. if (c1->fed_by == NULL) {
  1953. return;
  1954. }
  1955. existing = jack_slist_copy (c1->fed_by);
  1956. /* for each route that feeds c1, recurse, marking it as feeding
  1957. rbase as well.
  1958. */
  1959. for (node = existing; node; node = jack_slist_next (node)) {
  1960. c2 = (jack_client_internal_t *) node->data;
  1961. /* c2 is a route that feeds c1 which somehow feeds base. mark
  1962. base as being fed by c2, but don't do it more than
  1963. once.
  1964. */
  1965. if (c2 != rbase && c2 != c1) {
  1966. if (jack_slist_find (rbase->fed_by, c2) == NULL) {
  1967. rbase->fed_by = jack_slist_prepend (rbase->fed_by, c2);
  1968. }
  1969. /* FIXME: if c2->fed_by is not up-to-date, we may end up
  1970. recursing infinitely (kaiv)
  1971. */
  1972. if (jack_slist_find (c2->fed_by, c1) == NULL) {
  1973. /* now recurse, so that we can mark base as being fed by
  1974. all routes that feed c2
  1975. */
  1976. jack_trace_terminal (c2, rbase);
  1977. }
  1978. }
  1979. }
  1980. jack_slist_free (existing);
  1981. }
  1982. static int
  1983. jack_client_sort (jack_client_internal_t *a, jack_client_internal_t *b)
  1984. {
  1985. if (jack_slist_find (a->fed_by, b)) {
  1986. if (jack_slist_find (b->fed_by, a)) {
  1987. /* feedback loop: if `a' is the driver
  1988. client, let that execute first.
  1989. */
  1990. if (a->control->type == ClientDriver) {
  1991. /* b comes after a */
  1992. return -1;
  1993. }
  1994. }
  1995. /* a comes after b */
  1996. return 1;
  1997. } else if (jack_slist_find (b->fed_by, a)) {
  1998. if (jack_slist_find (a->fed_by, b)) {
  1999. /* feedback loop: if `b' is the driver
  2000. client, let that execute first.
  2001. */
  2002. if (b->control->type == ClientDriver) {
  2003. /* b comes before a */
  2004. return 1;
  2005. }
  2006. }
  2007. /* b comes after a */
  2008. return -1;
  2009. } else {
  2010. /* we don't care */
  2011. return 0;
  2012. }
  2013. }
  2014. static int
  2015. jack_client_feeds (jack_client_internal_t *might, jack_client_internal_t *target)
  2016. {
  2017. JSList *pnode, *cnode;
  2018. /* Check every port of `might' for an outbound connection to `target'
  2019. */
  2020. for (pnode = might->ports; pnode; pnode = jack_slist_next (pnode)) {
  2021. jack_port_internal_t *port;
  2022. port = (jack_port_internal_t *) pnode->data;
  2023. for (cnode = port->connections; cnode; cnode = jack_slist_next (cnode)) {
  2024. jack_connection_internal_t *c;
  2025. c = (jack_connection_internal_t *) cnode->data;
  2026. if (c->source->shared->client_id == might->control->id &&
  2027. c->destination->shared->client_id == target->control->id) {
  2028. return 1;
  2029. }
  2030. }
  2031. }
  2032. return 0;
  2033. }
  2034. static jack_nframes_t
  2035. jack_get_port_total_latency (jack_engine_t *engine, jack_port_internal_t *port, int hop_count, int toward_port)
  2036. {
  2037. JSList *node;
  2038. jack_nframes_t latency;
  2039. jack_nframes_t max_latency = 0;
  2040. /* call tree must hold engine->client_lock. */
  2041. latency = port->shared->latency;
  2042. /* we don't prevent cyclic graphs, so we have to do something to bottom out
  2043. in the event that they are created.
  2044. */
  2045. if (hop_count > 8) {
  2046. return latency;
  2047. }
  2048. for (node = port->connections; node; node = jack_slist_next (node)) {
  2049. jack_nframes_t this_latency;
  2050. jack_connection_internal_t *connection;
  2051. connection = (jack_connection_internal_t *) node->data;
  2052. if ((toward_port && (connection->source->shared == port->shared)) ||
  2053. (!toward_port && (connection->destination->shared == port->shared))) {
  2054. continue;
  2055. }
  2056. /* if we're a destination in the connection, recurse on the source to
  2057. get its total latency
  2058. */
  2059. if (connection->destination == port) {
  2060. if (connection->source->shared->flags & JackPortIsTerminal) {
  2061. this_latency = connection->source->shared->latency;
  2062. } else {
  2063. this_latency = jack_get_port_total_latency (engine, connection->source, hop_count + 1,
  2064. toward_port);
  2065. }
  2066. } else {
  2067. /* "port" is the source, so get the latency of the destination */
  2068. if (connection->destination->shared->flags & JackPortIsTerminal) {
  2069. this_latency = connection->destination->shared->latency;
  2070. } else {
  2071. this_latency = jack_get_port_total_latency (engine, connection->destination, hop_count + 1,
  2072. toward_port);
  2073. }
  2074. }
  2075. if (this_latency > max_latency) {
  2076. max_latency = this_latency;
  2077. }
  2078. }
  2079. return latency + max_latency;
  2080. }
  2081. static void
  2082. jack_compute_all_port_total_latencies (jack_engine_t *engine)
  2083. {
  2084. jack_port_shared_t *shared = engine->control->ports;
  2085. unsigned int i;
  2086. int toward_port;
  2087. for (i = 0; i < engine->control->port_max; i++) {
  2088. if (shared[i].in_use) {
  2089. if (shared[i].flags & JackPortIsOutput) {
  2090. toward_port = FALSE;
  2091. } else {
  2092. toward_port = TRUE;
  2093. }
  2094. shared[i].total_latency = jack_get_port_total_latency (engine, &engine->internal_ports[i], 0, toward_port);
  2095. }
  2096. }
  2097. }
  2098. /**
  2099. * Sorts the network of clients using the following
  2100. * algorithm:
  2101. *
  2102. * 1) figure out who is connected to whom:
  2103. *
  2104. * foreach client1
  2105. * foreach input port
  2106. * foreach client2
  2107. * foreach output port
  2108. * if client1->input port connected to client2->output port
  2109. * mark client1 fed by client 2
  2110. *
  2111. * 2) trace the connections as terminal arcs in the graph so that
  2112. * if client A feeds client B who feeds client C, mark client C
  2113. * as fed by client A as well as client B, and so forth.
  2114. *
  2115. * 3) now sort according to whether or not client1->fed_by (client2) is true.
  2116. * if the condition is true, client2 must execute before client1
  2117. *
  2118. */
  2119. static void
  2120. jack_sort_graph (jack_engine_t *engine)
  2121. {
  2122. JSList *node, *onode;
  2123. jack_client_internal_t *client;
  2124. jack_client_internal_t *oclient;
  2125. /* called, obviously, must hold engine->client_lock */
  2126. for (node = engine->clients; node; node = jack_slist_next (node)) {
  2127. client = (jack_client_internal_t *) node->data;
  2128. jack_slist_free (client->fed_by);
  2129. client->fed_by = 0;
  2130. for (onode = engine->clients; onode; onode = jack_slist_next (onode)) {
  2131. oclient = (jack_client_internal_t *) onode->data;
  2132. if (jack_client_feeds (oclient, client)) {
  2133. client->fed_by = jack_slist_prepend (client->fed_by, oclient);
  2134. }
  2135. }
  2136. }
  2137. for (node = engine->clients; node; node = jack_slist_next (node)) {
  2138. jack_trace_terminal ((jack_client_internal_t *) node->data,
  2139. (jack_client_internal_t *) node->data);
  2140. }
  2141. engine->clients = jack_slist_sort (engine->clients, (JCompareFunc) jack_client_sort);
  2142. jack_compute_all_port_total_latencies (engine);
  2143. jack_rechain_graph (engine);
  2144. }
  2145. /**
  2146. * Dumps current engine configuration to stderr.
  2147. */
  2148. void jack_dump_configuration(jack_engine_t *engine, int take_lock)
  2149. {
  2150. JSList *clientnode, *portnode, *connectionnode;
  2151. jack_client_internal_t *client;
  2152. jack_client_control_t *ctl;
  2153. jack_port_internal_t *port;
  2154. jack_connection_internal_t* connection;
  2155. int n, m, o;
  2156. fprintf(stderr, "engine.c: <-- dump begins -->\n");
  2157. if (take_lock) {
  2158. jack_lock_graph (engine);
  2159. }
  2160. for (n = 0, clientnode = engine->clients; clientnode; clientnode = jack_slist_next (clientnode)) {
  2161. client = (jack_client_internal_t *) clientnode->data;
  2162. ctl = client->control;
  2163. fprintf (stderr, "client #%d: %s (type: %d, process? %s, fed by %d clients) start=%d wait=%d\n",
  2164. ++n,
  2165. ctl->name,
  2166. ctl->type,
  2167. ctl->process ? "yes" : "no",
  2168. jack_slist_length(client->fed_by),
  2169. client->subgraph_start_fd,
  2170. client->subgraph_wait_fd);
  2171. for(m = 0, portnode = client->ports; portnode; portnode = jack_slist_next (portnode)) {
  2172. port = (jack_port_internal_t *) portnode->data;
  2173. fprintf(stderr, "\t port #%d: %s\n", ++m, port->shared->name);
  2174. for(o = 0, connectionnode = port->connections;
  2175. connectionnode;
  2176. connectionnode = jack_slist_next (connectionnode)) {
  2177. connection = (jack_connection_internal_t *) connectionnode->data;
  2178. fprintf(stderr, "\t\t connection #%d: %s %s\n",
  2179. ++o,
  2180. (port->shared->flags & JackPortIsInput) ? "<-" : "->",
  2181. (port->shared->flags & JackPortIsInput) ?
  2182. connection->source->shared->name :
  2183. connection->destination->shared->name);
  2184. }
  2185. }
  2186. }
  2187. if (take_lock) {
  2188. jack_unlock_graph (engine);
  2189. }
  2190. fprintf(stderr, "engine.c: <-- dump ends -->\n");
  2191. }
  2192. static int
  2193. jack_port_do_connect (jack_engine_t *engine,
  2194. const char *source_port,
  2195. const char *destination_port)
  2196. {
  2197. jack_connection_internal_t *connection;
  2198. jack_port_internal_t *srcport, *dstport;
  2199. jack_port_id_t src_id, dst_id;
  2200. fprintf (stderr, "got connect request\n");
  2201. if ((srcport = jack_get_port_by_name (engine, source_port)) == NULL) {
  2202. jack_error ("unknown source port in attempted connection [%s]", source_port);
  2203. return -1;
  2204. }
  2205. if ((dstport = jack_get_port_by_name (engine, destination_port)) == NULL) {
  2206. jack_error ("unknown destination port in attempted connection [%s]", destination_port);
  2207. return -1;
  2208. }
  2209. if ((dstport->shared->flags & JackPortIsInput) == 0) {
  2210. jack_error ("destination port in attempted connection of %s and %s is not an input port",
  2211. source_port, destination_port);
  2212. return -1;
  2213. }
  2214. if ((srcport->shared->flags & JackPortIsOutput) == 0) {
  2215. jack_error ("source port in attempted connection of %s and %s is not an output port",
  2216. source_port, destination_port);
  2217. return -1;
  2218. }
  2219. if (srcport->shared->locked) {
  2220. jack_error ("source port %s is locked against connection changes", source_port);
  2221. return -1;
  2222. }
  2223. if (dstport->shared->locked) {
  2224. jack_error ("destination port %s is locked against connection changes", destination_port);
  2225. return -1;
  2226. }
  2227. if (strcmp (srcport->shared->type_info.type_name,
  2228. dstport->shared->type_info.type_name) != 0) {
  2229. jack_error ("ports used in attemped connection are not of the same data type");
  2230. return -1;
  2231. }
  2232. connection = (jack_connection_internal_t *) malloc (sizeof (jack_connection_internal_t));
  2233. connection->source = srcport;
  2234. connection->destination = dstport;
  2235. src_id = srcport->shared->id;
  2236. dst_id = dstport->shared->id;
  2237. jack_lock_graph (engine);
  2238. if (dstport->connections && dstport->shared->type_info.mixdown == NULL) {
  2239. jack_error ("cannot make multiple connections to a port of type [%s]", dstport->shared->type_info.type_name);
  2240. free (connection);
  2241. jack_unlock_graph (engine);
  2242. return -1;
  2243. } else {
  2244. if (engine->verbose) {
  2245. fprintf (stderr, "connect %s and %s\n",
  2246. srcport->shared->name,
  2247. dstport->shared->name);
  2248. }
  2249. dstport->connections = jack_slist_prepend (dstport->connections, connection);
  2250. srcport->connections = jack_slist_prepend (srcport->connections, connection);
  2251. jack_sort_graph (engine);
  2252. DEBUG ("actually sorted the graph...");
  2253. jack_send_connection_notification (engine, srcport->shared->client_id, src_id, dst_id, TRUE);
  2254. jack_send_connection_notification (engine, dstport->shared->client_id, dst_id, src_id, TRUE);
  2255. }
  2256. jack_unlock_graph (engine);
  2257. return 0;
  2258. }
  2259. int
  2260. jack_port_disconnect_internal (jack_engine_t *engine,
  2261. jack_port_internal_t *srcport,
  2262. jack_port_internal_t *dstport,
  2263. int sort_graph)
  2264. {
  2265. JSList *node;
  2266. jack_connection_internal_t *connect;
  2267. int ret = -1;
  2268. jack_port_id_t src_id, dst_id;
  2269. /* call tree **** MUST HOLD **** engine->client_lock. */
  2270. for (node = srcport->connections; node; node = jack_slist_next (node)) {
  2271. connect = (jack_connection_internal_t *) node->data;
  2272. if (connect->source == srcport && connect->destination == dstport) {
  2273. if (engine->verbose) {
  2274. fprintf (stderr, "DIS-connect %s and %s\n",
  2275. srcport->shared->name,
  2276. dstport->shared->name);
  2277. }
  2278. srcport->connections = jack_slist_remove (srcport->connections, connect);
  2279. dstport->connections = jack_slist_remove (dstport->connections, connect);
  2280. src_id = srcport->shared->id;
  2281. dst_id = dstport->shared->id;
  2282. /* this is a bit harsh, but it basically says that if we actually
  2283. do a disconnect, and its the last one, then make sure that
  2284. any input monitoring is turned off on the srcport. this isn't
  2285. ideal for all situations, but it works better for most of them.
  2286. */
  2287. if (srcport->connections == NULL) {
  2288. srcport->shared->monitor_requests = 0;
  2289. }
  2290. jack_send_connection_notification (engine, srcport->shared->client_id, src_id, dst_id, FALSE);
  2291. jack_send_connection_notification (engine, dstport->shared->client_id, dst_id, src_id, FALSE);
  2292. free (connect);
  2293. ret = 0;
  2294. break;
  2295. }
  2296. }
  2297. if (sort_graph) {
  2298. jack_sort_graph (engine);
  2299. }
  2300. return ret;
  2301. }
  2302. static int
  2303. jack_port_do_disconnect_all (jack_engine_t *engine,
  2304. jack_port_id_t port_id)
  2305. {
  2306. if (port_id >= engine->control->port_max) {
  2307. jack_error ("illegal port ID in attempted disconnection [%u]", port_id);
  2308. return -1;
  2309. }
  2310. if (engine->verbose) {
  2311. fprintf (stderr, "clear connections for %s\n", engine->internal_ports[port_id].shared->name);
  2312. }
  2313. jack_lock_graph (engine);
  2314. jack_port_clear_connections (engine, &engine->internal_ports[port_id]);
  2315. jack_sort_graph (engine);
  2316. jack_unlock_graph (engine);
  2317. return 0;
  2318. }
  2319. static int
  2320. jack_port_do_disconnect (jack_engine_t *engine,
  2321. const char *source_port,
  2322. const char *destination_port)
  2323. {
  2324. jack_port_internal_t *srcport, *dstport;
  2325. int ret = -1;
  2326. if ((srcport = jack_get_port_by_name (engine, source_port)) == NULL) {
  2327. jack_error ("unknown source port in attempted disconnection [%s]", source_port);
  2328. return -1;
  2329. }
  2330. if ((dstport = jack_get_port_by_name (engine, destination_port)) == NULL) {
  2331. jack_error ("unknown destination port in attempted connection [%s]", destination_port);
  2332. return -1;
  2333. }
  2334. jack_lock_graph (engine);
  2335. ret = jack_port_disconnect_internal (engine, srcport, dstport, TRUE);
  2336. jack_unlock_graph (engine);
  2337. return ret;
  2338. }
  2339. static int
  2340. jack_get_fifo_fd (jack_engine_t *engine, unsigned int which_fifo)
  2341. {
  2342. /* caller must hold client_lock */
  2343. char path[PATH_MAX+1];
  2344. struct stat statbuf;
  2345. snprintf (path, sizeof (path), "%s-%d", engine->fifo_prefix, which_fifo);
  2346. DEBUG ("%s", path);
  2347. if (stat (path, &statbuf)) {
  2348. if (errno == ENOENT) {
  2349. if (mknod (path, 0666|S_IFIFO, 0) < 0) {
  2350. jack_error ("cannot create inter-client FIFO [%s] (%s)\n", path, strerror (errno));
  2351. return -1;
  2352. }
  2353. } else {
  2354. jack_error ("cannot check on FIFO %d\n", which_fifo);
  2355. return -1;
  2356. }
  2357. } else {
  2358. if (!S_ISFIFO(statbuf.st_mode)) {
  2359. jack_error ("FIFO %d (%s) already exists, but is not a FIFO!\n", which_fifo, path);
  2360. return -1;
  2361. }
  2362. }
  2363. if (which_fifo >= engine->fifo_size) {
  2364. unsigned int i;
  2365. engine->fifo = (int *) realloc (engine->fifo, sizeof (int) * engine->fifo_size + 16);
  2366. for (i = engine->fifo_size; i < engine->fifo_size + 16; i++) {
  2367. engine->fifo[i] = -1;
  2368. }
  2369. engine->fifo_size += 16;
  2370. }
  2371. if (engine->fifo[which_fifo] < 0) {
  2372. if ((engine->fifo[which_fifo] = open (path, O_RDWR|O_CREAT|O_NONBLOCK, 0666)) < 0) {
  2373. jack_error ("cannot open fifo [%s] (%s)", path, strerror (errno));
  2374. return -1;
  2375. }
  2376. DEBUG ("opened engine->fifo[%d] == %d (%s)", which_fifo, engine->fifo[which_fifo], path);
  2377. }
  2378. return engine->fifo[which_fifo];
  2379. }
  2380. static void
  2381. jack_clear_fifos (jack_engine_t *engine)
  2382. {
  2383. /* caller must hold client_lock */
  2384. unsigned int i;
  2385. char buf[16];
  2386. /* this just drains the existing FIFO's of any data left in them
  2387. by aborted clients, etc. there is only ever going to be
  2388. 0, 1 or 2 bytes in them, but we'll allow for up to 16.
  2389. */
  2390. for (i = 0; i < engine->fifo_size; i++) {
  2391. if (engine->fifo[i] >= 0) {
  2392. int nread = read (engine->fifo[i], buf, sizeof (buf));
  2393. if (nread < 0 && errno != EAGAIN) {
  2394. jack_error ("clear fifo[%d] error: %s", i, strerror (errno));
  2395. }
  2396. }
  2397. }
  2398. }
  2399. static int
  2400. jack_use_driver (jack_engine_t *engine, jack_driver_t *driver)
  2401. {
  2402. if (engine->driver) {
  2403. engine->driver->detach (engine->driver, engine);
  2404. engine->driver = 0;
  2405. }
  2406. if (driver) {
  2407. if (driver->attach (driver, engine)) {
  2408. return -1;
  2409. }
  2410. engine->rolling_interval = (int) floor ((JACK_ENGINE_ROLLING_INTERVAL * 1000.0f) / driver->period_usecs);
  2411. }
  2412. engine->driver = driver;
  2413. return 0;
  2414. }
  2415. /* PORT RELATED FUNCTIONS */
  2416. static jack_port_id_t
  2417. jack_get_free_port (jack_engine_t *engine)
  2418. {
  2419. jack_port_id_t i;
  2420. pthread_mutex_lock (&engine->port_lock);
  2421. for (i = 0; i < engine->port_max; i++) {
  2422. if (engine->control->ports[i].in_use == 0) {
  2423. engine->control->ports[i].in_use = 1;
  2424. break;
  2425. }
  2426. }
  2427. pthread_mutex_unlock (&engine->port_lock);
  2428. if (i == engine->port_max) {
  2429. return (jack_port_id_t) -1;
  2430. }
  2431. return i;
  2432. }
  2433. static void
  2434. jack_port_release (jack_engine_t *engine, jack_port_internal_t *port)
  2435. {
  2436. pthread_mutex_lock (&engine->port_lock);
  2437. port->shared->in_use = 0;
  2438. if (port->buffer_info) {
  2439. pthread_mutex_lock (&engine->buffer_lock);
  2440. engine->port_buffer_freelist = jack_slist_prepend (engine->port_buffer_freelist, port->buffer_info);
  2441. port->buffer_info = NULL;
  2442. pthread_mutex_unlock (&engine->buffer_lock);
  2443. }
  2444. pthread_mutex_unlock (&engine->port_lock);
  2445. }
  2446. jack_port_internal_t *
  2447. jack_get_port_internal_by_name (jack_engine_t *engine, const char *name)
  2448. {
  2449. jack_port_id_t id;
  2450. pthread_mutex_lock (&engine->port_lock);
  2451. for (id = 0; id < engine->port_max; id++) {
  2452. if (strcmp (engine->control->ports[id].name, name) == 0) {
  2453. break;
  2454. }
  2455. }
  2456. pthread_mutex_unlock (&engine->port_lock);
  2457. if (id != engine->port_max) {
  2458. return &engine->internal_ports[id];
  2459. } else {
  2460. return NULL;
  2461. }
  2462. }
  2463. int
  2464. jack_port_do_register (jack_engine_t *engine, jack_request_t *req)
  2465. {
  2466. jack_port_id_t port_id;
  2467. jack_port_shared_t *shared;
  2468. jack_port_internal_t *port;
  2469. jack_client_internal_t *client;
  2470. jack_lock_graph (engine);
  2471. if ((client = jack_client_internal_by_id (engine, req->x.port_info.client_id)) == NULL) {
  2472. jack_error ("unknown client id in port registration request");
  2473. return -1;
  2474. }
  2475. jack_unlock_graph (engine);
  2476. if ((port_id = jack_get_free_port (engine)) == (jack_port_id_t) -1) {
  2477. jack_error ("no ports available!");
  2478. return -1;
  2479. }
  2480. shared = &engine->control->ports[port_id];
  2481. strcpy (shared->name, req->x.port_info.name);
  2482. shared->client_id = req->x.port_info.client_id;
  2483. shared->flags = req->x.port_info.flags;
  2484. shared->buffer_size = req->x.port_info.buffer_size;
  2485. shared->latency = 0;
  2486. shared->monitor_requests = 0;
  2487. shared->locked = 0;
  2488. port = &engine->internal_ports[port_id];
  2489. port->shared = shared;
  2490. port->connections = 0;
  2491. if (jack_port_assign_buffer (engine, port)) {
  2492. jack_error ("cannot assign buffer for port");
  2493. return -1;
  2494. }
  2495. jack_lock_graph (engine);
  2496. client->ports = jack_slist_prepend (client->ports, port);
  2497. jack_port_registration_notify (engine, port_id, TRUE);
  2498. jack_unlock_graph (engine);
  2499. if (engine->verbose) {
  2500. fprintf (stderr, "registered port %s, offset = %u\n", shared->name, (unsigned int)shared->offset);
  2501. }
  2502. req->x.port_info.port_id = port_id;
  2503. return 0;
  2504. }
  2505. int
  2506. jack_port_do_unregister (jack_engine_t *engine, jack_request_t *req)
  2507. {
  2508. jack_client_internal_t *client;
  2509. jack_port_shared_t *shared;
  2510. jack_port_internal_t *port;
  2511. if (req->x.port_info.port_id < 0 || req->x.port_info.port_id > engine->port_max) {
  2512. jack_error ("invalid port ID %d in unregister request", req->x.port_info.port_id);
  2513. return -1;
  2514. }
  2515. shared = &engine->control->ports[req->x.port_info.port_id];
  2516. if (shared->client_id != req->x.port_info.client_id) {
  2517. jack_error ("Client %d is not allowed to remove port %s", req->x.port_info.client_id, shared->name);
  2518. return -1;
  2519. }
  2520. jack_lock_graph (engine);
  2521. if ((client = jack_client_internal_by_id (engine, shared->client_id)) == NULL) {
  2522. jack_error ("unknown client id in port registration request");
  2523. jack_unlock_graph (engine);
  2524. return -1;
  2525. }
  2526. port = &engine->internal_ports[req->x.port_info.port_id];
  2527. jack_port_clear_connections (engine, port);
  2528. jack_port_release (engine, &engine->internal_ports[req->x.port_info.port_id]);
  2529. client->ports = jack_slist_remove (client->ports, port);
  2530. jack_port_registration_notify (engine, req->x.port_info.port_id, FALSE);
  2531. jack_unlock_graph (engine);
  2532. return 0;
  2533. }
  2534. int
  2535. jack_do_get_port_connections (jack_engine_t *engine, jack_request_t *req, int reply_fd)
  2536. {
  2537. jack_port_internal_t *port;
  2538. JSList *node;
  2539. unsigned int i;
  2540. int ret = -1;
  2541. int internal = FALSE;
  2542. jack_lock_graph (engine);
  2543. port = &engine->internal_ports[req->x.port_info.port_id];
  2544. DEBUG ("Getting connections for port '%s'.", port->shared->name);
  2545. req->x.port_connections.nports = jack_slist_length (port->connections);
  2546. req->status = 0;
  2547. /* figure out if this is an internal or external client */
  2548. for (node = engine->clients; node; node = jack_slist_next (node)) {
  2549. if (((jack_client_internal_t *) node->data)->request_fd == reply_fd) {
  2550. internal = jack_client_is_internal((jack_client_internal_t *) node->data);
  2551. break;
  2552. }
  2553. }
  2554. if (!internal) {
  2555. if (write (reply_fd, req, sizeof (*req)) < (ssize_t) sizeof (req)) {
  2556. jack_error ("cannot write GetPortConnections result to client via fd = %d (%s)",
  2557. reply_fd, strerror (errno));
  2558. goto out;
  2559. }
  2560. } else {
  2561. req->x.port_connections.ports = (const char **) malloc (sizeof (char *) * req->x.port_connections.nports);
  2562. }
  2563. if (req->type == GetPortConnections) {
  2564. for (i = 0, node = port->connections; node; node = jack_slist_next (node), ++i) {
  2565. jack_port_id_t port_id;
  2566. if (((jack_connection_internal_t *) node->data)->source == port) {
  2567. port_id = ((jack_connection_internal_t *) node->data)->destination->shared->id;
  2568. } else {
  2569. port_id = ((jack_connection_internal_t *) node->data)->source->shared->id;
  2570. }
  2571. if (internal) {
  2572. /* internal client asking for names. store in malloc'ed space, client frees
  2573. */
  2574. req->x.port_connections.ports[i] = engine->control->ports[port_id].name;
  2575. } else {
  2576. /* external client asking for names. we write the port id's to the reply fd.
  2577. */
  2578. if (write (reply_fd, &port_id, sizeof (port_id)) < (ssize_t) sizeof (port_id)) {
  2579. jack_error ("cannot write port id to client");
  2580. goto out;
  2581. }
  2582. }
  2583. }
  2584. }
  2585. ret = 0;
  2586. out:
  2587. req->status = ret;
  2588. jack_unlock_graph (engine);
  2589. return ret;
  2590. }
  2591. void
  2592. jack_port_registration_notify (jack_engine_t *engine, jack_port_id_t port_id, int yn)
  2593. {
  2594. jack_event_t event;
  2595. jack_client_internal_t *client;
  2596. JSList *node;
  2597. event.type = (yn ? PortRegistered : PortUnregistered);
  2598. event.x.port_id = port_id;
  2599. for (node = engine->clients; node; node = jack_slist_next (node)) {
  2600. client = (jack_client_internal_t *) node->data;
  2601. if (!client->control->active) {
  2602. continue;
  2603. }
  2604. if (client->control->port_register) {
  2605. if (jack_deliver_event (engine, client, &event)) {
  2606. jack_error ("cannot send port registration notification to %s (%s)",
  2607. client->control->name, strerror (errno));
  2608. }
  2609. }
  2610. }
  2611. }
  2612. int
  2613. jack_port_assign_buffer (jack_engine_t *engine, jack_port_internal_t *port)
  2614. {
  2615. JSList *node;
  2616. jack_port_segment_info_t *psi = 0;
  2617. jack_port_buffer_info_t *bi;
  2618. port->shared->shm_key = -1;
  2619. if (port->shared->flags & JackPortIsInput) {
  2620. port->shared->offset = 0;
  2621. return 0;
  2622. }
  2623. pthread_mutex_lock (&engine->buffer_lock);
  2624. if (engine->port_buffer_freelist == NULL) {
  2625. jack_error ("all port buffers in use!");
  2626. pthread_mutex_unlock (&engine->buffer_lock);
  2627. return -1;
  2628. }
  2629. bi = (jack_port_buffer_info_t *) engine->port_buffer_freelist->data;
  2630. for (node = engine->port_segments; node; node = jack_slist_next (node)) {
  2631. psi = (jack_port_segment_info_t *) node->data;
  2632. if (bi->shm_key == psi->shm_key) {
  2633. port->shared->shm_key = psi->shm_key;
  2634. port->shared->offset = bi->offset;
  2635. port->buffer_info = bi;
  2636. break;
  2637. }
  2638. }
  2639. if (engine->verbose) {
  2640. fprintf (stderr, "port %s buf shm key 0x%x at offset %d bi = %p\n",
  2641. port->shared->name,
  2642. port->shared->shm_key,
  2643. (int)port->shared->offset,
  2644. port->buffer_info);
  2645. }
  2646. if (port->shared->shm_key >= 0) {
  2647. engine->port_buffer_freelist = jack_slist_remove (engine->port_buffer_freelist, bi);
  2648. } else {
  2649. jack_error ("port segment info for 0x%x:%d not found!", bi->shm_key, bi->offset);
  2650. }
  2651. pthread_mutex_unlock (&engine->buffer_lock);
  2652. if (port->shared->shm_key < 0) {
  2653. return -1;
  2654. } else {
  2655. return 0;
  2656. }
  2657. }
  2658. static jack_port_internal_t *
  2659. jack_get_port_by_name (jack_engine_t *engine, const char *name)
  2660. {
  2661. jack_port_id_t id;
  2662. /* Note the potential race on "in_use". Other design
  2663. elements prevent this from being a problem.
  2664. */
  2665. for (id = 0; id < engine->port_max; id++) {
  2666. if (engine->control->ports[id].in_use && strcmp (engine->control->ports[id].name, name) == 0) {
  2667. return &engine->internal_ports[id];
  2668. }
  2669. }
  2670. return NULL;
  2671. }
  2672. static int
  2673. jack_send_connection_notification (jack_engine_t *engine, jack_client_id_t client_id,
  2674. jack_port_id_t self_id, jack_port_id_t other_id, int connected)
  2675. {
  2676. jack_client_internal_t *client;
  2677. jack_event_t event;
  2678. if ((client = jack_client_internal_by_id (engine, client_id)) == NULL) {
  2679. jack_error ("no such client %d during connection notification", client_id);
  2680. return -1;
  2681. }
  2682. if (client->control->active) {
  2683. event.type = (connected ? PortConnected : PortDisconnected);
  2684. event.x.self_id = self_id;
  2685. event.y.other_id = other_id;
  2686. if (jack_deliver_event (engine, client, &event)) {
  2687. jack_error ("cannot send port connection notification to client %s (%s)",
  2688. client->control->name, strerror (errno));
  2689. return -1;
  2690. }
  2691. }
  2692. return 0;
  2693. }
  2694. void
  2695. jack_set_asio_mode (jack_engine_t *engine, int yn)
  2696. {
  2697. engine->asio_mode = yn;
  2698. }