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.

3632 lines
92KB

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