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.

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