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.

3520 lines
87KB

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