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.

3552 lines
90KB

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