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.

3541 lines
87KB

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