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.

4187 lines
100KB

  1. /* -*- mode: c; c-file-style: "bsd"; -*- */
  2. /*
  3. Copyright (C) 2001-2003 Paul Davis
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. $Id$
  16. */
  17. #include <config.h>
  18. #include <math.h>
  19. #include <unistd.h>
  20. #include <sys/socket.h>
  21. #include <sys/un.h>
  22. #include <sys/stat.h>
  23. #include <errno.h>
  24. #include <fcntl.h>
  25. #include <stdio.h>
  26. #include <stdarg.h>
  27. #ifdef HAVE_STDINT_H
  28. #include <stdint.h>
  29. #endif
  30. #include <dirent.h>
  31. #include <signal.h>
  32. #include <sys/types.h>
  33. #include <string.h>
  34. #include <limits.h>
  35. #include <jack/internal.h>
  36. #include <jack/engine.h>
  37. #include <jack/driver.h>
  38. #include <jack/version.h>
  39. #include <jack/shm.h>
  40. #include <jack/thread.h>
  41. #include <sysdeps/poll.h>
  42. #include <sysdeps/ipc.h>
  43. #ifdef USE_MLOCK
  44. #include <sys/mman.h>
  45. #endif /* USE_MLOCK */
  46. #ifdef USE_CAPABILITIES
  47. /* capgetp and capsetp are linux only extensions, not posix */
  48. #undef _POSIX_SOURCE
  49. #include <sys/capability.h>
  50. #endif
  51. #include "transengine.h"
  52. #define JACK_ERROR_WITH_SOCKETS 10000000
  53. typedef struct {
  54. jack_port_internal_t *source;
  55. jack_port_internal_t *destination;
  56. } jack_connection_internal_t;
  57. typedef struct _jack_driver_info {
  58. jack_driver_t *(*initialize)(jack_client_t*, const JSList *);
  59. void (*finish);
  60. char (*client_name);
  61. dlhandle handle;
  62. } jack_driver_info_t;
  63. static int jack_port_assign_buffer (jack_engine_t *,
  64. jack_port_internal_t *);
  65. static jack_port_internal_t *jack_get_port_by_name (jack_engine_t *,
  66. const char *name);
  67. static void jack_zombify_client (jack_engine_t *engine,
  68. jack_client_internal_t *client);
  69. static void jack_remove_client (jack_engine_t *engine,
  70. jack_client_internal_t *client);
  71. static void jack_client_delete (jack_engine_t *, jack_client_internal_t *);
  72. static jack_client_internal_t
  73. *jack_setup_client_control (jack_engine_t *engine, int fd,
  74. jack_client_connect_request_t *);
  75. static void jack_sort_graph (jack_engine_t *engine);
  76. static int jack_rechain_graph (jack_engine_t *engine);
  77. static int jack_get_fifo_fd (jack_engine_t *engine, unsigned int which_fifo);
  78. static void jack_clear_fifos (jack_engine_t *engine);
  79. static int jack_port_do_connect (jack_engine_t *engine,
  80. const char *source_port,
  81. const char *destination_port);
  82. static int jack_port_do_disconnect (jack_engine_t *engine,
  83. const char *source_port,
  84. const char *destination_port);
  85. static int jack_port_do_disconnect_all (jack_engine_t *engine,
  86. jack_port_id_t);
  87. static int jack_port_do_unregister (jack_engine_t *engine, jack_request_t *);
  88. static int jack_port_do_register (jack_engine_t *engine, jack_request_t *);
  89. static int jack_do_get_port_connections (jack_engine_t *engine,
  90. jack_request_t *req, int reply_fd);
  91. static void jack_port_release (jack_engine_t *engine, jack_port_internal_t *);
  92. static void jack_port_clear_connections (jack_engine_t *engine,
  93. jack_port_internal_t *port);
  94. static int jack_port_disconnect_internal (jack_engine_t *engine,
  95. jack_port_internal_t *src,
  96. jack_port_internal_t *dst,
  97. int sort_graph);
  98. static void jack_port_registration_notify (jack_engine_t *,
  99. jack_port_id_t, int);
  100. static int jack_send_connection_notification (jack_engine_t *,
  101. jack_client_id_t,
  102. jack_port_id_t,
  103. jack_port_id_t, int);
  104. static int jack_deliver_event (jack_engine_t *, jack_client_internal_t *,
  105. jack_event_t *);
  106. static void jack_deliver_event_to_all (jack_engine_t *engine,
  107. jack_event_t *event);
  108. static void jack_engine_post_process (jack_engine_t *);
  109. static int internal_client_request (void*, jack_request_t *);
  110. static int jack_use_driver (jack_engine_t *engine, jack_driver_t *driver);
  111. static int jack_run_cycle (jack_engine_t *engine, jack_nframes_t nframes,
  112. float delayed_usecs);
  113. static void jack_engine_notify_clients_about_delay (jack_engine_t *engine);
  114. static void jack_engine_driver_exit (jack_engine_t* engine);
  115. static int jack_start_freewheeling (jack_engine_t* engine);
  116. static int jack_stop_freewheeling (jack_engine_t* engine);
  117. static int jack_start_watchdog (jack_engine_t *engine);
  118. static char *client_state_names[] = {
  119. "Not triggered",
  120. "Triggered",
  121. "Running",
  122. "Finished"
  123. };
  124. static inline int
  125. jack_client_is_internal (jack_client_internal_t *client)
  126. {
  127. return (client->control->type == ClientInternal) ||
  128. (client->control->type == ClientDriver);
  129. }
  130. static inline int
  131. jack_rolling_interval (jack_time_t period_usecs)
  132. {
  133. return floor ((JACK_ENGINE_ROLLING_INTERVAL * 1000.0f) / period_usecs);
  134. }
  135. static inline void
  136. jack_engine_reset_rolling_usecs (jack_engine_t *engine)
  137. {
  138. memset (engine->rolling_client_usecs, 0,
  139. sizeof (engine->rolling_client_usecs));
  140. engine->rolling_client_usecs_index = 0;
  141. engine->rolling_client_usecs_cnt = 0;
  142. if (engine->driver) {
  143. engine->rolling_interval =
  144. jack_rolling_interval (engine->driver->period_usecs);
  145. } else {
  146. engine->rolling_interval = JACK_ENGINE_ROLLING_INTERVAL;
  147. }
  148. engine->spare_usecs = 0;
  149. }
  150. static inline jack_port_type_info_t *
  151. jack_port_type_info (jack_engine_t *engine, jack_port_internal_t *port)
  152. {
  153. /* Returns a pointer to the port type information in the
  154. engine's shared control structure.
  155. */
  156. return &engine->control->port_types[port->shared->ptype_id];
  157. }
  158. static inline jack_port_buffer_list_t *
  159. jack_port_buffer_list (jack_engine_t *engine, jack_port_internal_t *port)
  160. {
  161. /* Points to the engine's private port buffer list struct. */
  162. return &engine->port_buffers[port->shared->ptype_id];
  163. }
  164. static int
  165. make_sockets (int fd[2])
  166. {
  167. struct sockaddr_un addr;
  168. int i;
  169. /* First, the master server socket */
  170. if ((fd[0] = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) {
  171. jack_error ("cannot create server socket (%s)",
  172. strerror (errno));
  173. return -1;
  174. }
  175. addr.sun_family = AF_UNIX;
  176. for (i = 0; i < 999; i++) {
  177. snprintf (addr.sun_path, sizeof (addr.sun_path) - 1,
  178. "%s/jack_%d_%d", jack_server_dir, getuid (), i);
  179. if (access (addr.sun_path, F_OK) != 0) {
  180. break;
  181. }
  182. }
  183. if (i == 999) {
  184. jack_error ("all possible server socket names in use!!!");
  185. close (fd[0]);
  186. return -1;
  187. }
  188. if (bind (fd[0], (struct sockaddr *) &addr, sizeof (addr)) < 0) {
  189. jack_error ("cannot bind server to socket (%s)",
  190. strerror (errno));
  191. close (fd[0]);
  192. return -1;
  193. }
  194. if (listen (fd[0], 1) < 0) {
  195. jack_error ("cannot enable listen on server socket (%s)",
  196. strerror (errno));
  197. close (fd[0]);
  198. return -1;
  199. }
  200. /* Now the client/server event ack server socket */
  201. if ((fd[1] = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) {
  202. jack_error ("cannot create event ACK socket (%s)",
  203. strerror (errno));
  204. close (fd[0]);
  205. return -1;
  206. }
  207. addr.sun_family = AF_UNIX;
  208. for (i = 0; i < 999; i++) {
  209. snprintf (addr.sun_path, sizeof (addr.sun_path) - 1,
  210. "%s/jack_%d_ack_%d", jack_server_dir, getuid (), i);
  211. if (access (addr.sun_path, F_OK) != 0) {
  212. break;
  213. }
  214. }
  215. if (i == 999) {
  216. jack_error ("all possible server ACK socket names in use!!!");
  217. close (fd[0]);
  218. close (fd[1]);
  219. return -1;
  220. }
  221. if (bind (fd[1], (struct sockaddr *) &addr, sizeof (addr)) < 0) {
  222. jack_error ("cannot bind server to socket (%s)",
  223. strerror (errno));
  224. close (fd[0]);
  225. close (fd[1]);
  226. return -1;
  227. }
  228. if (listen (fd[1], 1) < 0) {
  229. jack_error ("cannot enable listen on server socket (%s)",
  230. strerror (errno));
  231. close (fd[0]);
  232. close (fd[1]);
  233. return -1;
  234. }
  235. return 0;
  236. }
  237. void
  238. jack_cleanup_files ()
  239. {
  240. DIR *dir;
  241. struct dirent *dirent;
  242. /* its important that we remove all files that jackd creates
  243. because otherwise subsequent attempts to start jackd will
  244. believe that an instance is already running.
  245. */
  246. if ((dir = opendir (jack_server_dir)) == NULL) {
  247. fprintf (stderr, "jack(%d): cannot open jack FIFO directory "
  248. "(%s)\n", getpid(), strerror (errno));
  249. return;
  250. }
  251. while ((dirent = readdir (dir)) != NULL) {
  252. /* jack-99999999- is 14 chars long */
  253. char name_prefix1[15];
  254. char name_prefix2[15];
  255. snprintf (name_prefix1, sizeof (name_prefix1), "jack-%d-", getuid ());
  256. snprintf (name_prefix2, sizeof (name_prefix2), "jack_%d_", getuid ());
  257. if (strncmp (dirent->d_name, name_prefix1, strlen(name_prefix1)) == 0 ||
  258. strncmp (dirent->d_name, name_prefix2, strlen(name_prefix2)) == 0) {
  259. char fullpath[PATH_MAX+1];
  260. snprintf (fullpath, sizeof (fullpath), "%s/%s",
  261. jack_server_dir, dirent->d_name);
  262. unlink (fullpath);
  263. }
  264. }
  265. closedir (dir);
  266. }
  267. void
  268. jack_engine_place_port_buffers (jack_engine_t* engine,
  269. jack_port_type_id_t ptid,
  270. jack_shmsize_t one_buffer,
  271. jack_shmsize_t size,
  272. unsigned long nports)
  273. {
  274. jack_shmsize_t offset; /* shared memory offset */
  275. jack_port_buffer_info_t *bi;
  276. jack_port_buffer_list_t* pti = &engine->port_buffers[ptid];
  277. pthread_mutex_lock (&pti->lock);
  278. offset = 0;
  279. if (pti->info) {
  280. /* Buffer info array already allocated for this port
  281. * type. This must be a resize operation, so
  282. * recompute the buffer offsets, but leave the free
  283. * list alone.
  284. */
  285. int i;
  286. bi = pti->info;
  287. while (offset < size) {
  288. bi->offset = offset;
  289. offset += one_buffer;
  290. ++bi;
  291. }
  292. /* update any existing output port offsets */
  293. for (i = 0; i < engine->port_max; i++) {
  294. jack_port_shared_t *port = &engine->control->ports[i];
  295. if (port->in_use &&
  296. (port->flags & JackPortIsOutput) &&
  297. port->ptype_id == ptid) {
  298. bi = engine->internal_ports[i].buffer_info;
  299. if (bi) {
  300. port->offset = bi->offset;
  301. }
  302. }
  303. }
  304. } else {
  305. /* Allocate an array of buffer info structures for all
  306. * the buffers in the segment. Chain them to the free
  307. * list in memory address order, offset zero must come
  308. * first.
  309. */
  310. bi = pti->info = (jack_port_buffer_info_t *)
  311. malloc (nports * sizeof (jack_port_buffer_info_t));
  312. while (offset < size) {
  313. bi->offset = offset;
  314. pti->freelist = jack_slist_append (pti->freelist, bi);
  315. offset += one_buffer;
  316. ++bi;
  317. }
  318. /* allocate the first buffer of the audio port segment
  319. * for a zero-filled area
  320. */
  321. if (ptid == JACK_AUDIO_PORT_TYPE) {
  322. engine->silent_buffer = (jack_port_buffer_info_t *)
  323. pti->freelist->data;
  324. pti->freelist =
  325. jack_slist_remove_link (pti->freelist,
  326. pti->freelist);
  327. }
  328. }
  329. pthread_mutex_unlock (&pti->lock);
  330. }
  331. // JOQ: this should have a return code...
  332. static void
  333. jack_resize_port_segment (jack_engine_t *engine,
  334. jack_port_type_id_t ptid,
  335. unsigned long nports)
  336. {
  337. jack_event_t event;
  338. jack_shmsize_t one_buffer; /* size of one buffer */
  339. jack_shmsize_t size; /* segment size */
  340. jack_port_type_info_t* port_type = &engine->control->port_types[ptid];
  341. jack_shm_info_t* shm_info = &engine->port_segment[ptid];
  342. if (port_type->buffer_scale_factor < 0) {
  343. one_buffer = port_type->buffer_size;
  344. } else {
  345. one_buffer = sizeof (jack_default_audio_sample_t)
  346. * port_type->buffer_scale_factor
  347. * engine->control->buffer_size;
  348. }
  349. size = nports * one_buffer;
  350. if (shm_info->attached_at == 0) {
  351. char name[64];
  352. /* no segment allocated, yet */
  353. snprintf (name, sizeof(name), "/jck-[%s]",
  354. port_type->type_name);
  355. if (jack_shmalloc (name, size, shm_info)) {
  356. jack_error ("cannot create new port segment of %d"
  357. " bytes, name = %s (%s)",
  358. size, name,
  359. strerror (errno));
  360. return;
  361. }
  362. if (jack_attach_shm (shm_info)) {
  363. jack_error ("cannot attach to new port segment "
  364. "(name=%s) (%s)", name, strerror (errno));
  365. return;
  366. }
  367. engine->control->port_types[ptid].shm_registry_index =
  368. shm_info->index;
  369. } else {
  370. /* resize existing buffer segment */
  371. if (jack_resize_shm (shm_info, size)) {
  372. jack_error ("cannot resize port segment to %d bytes,"
  373. " (%s)", size,
  374. strerror (errno));
  375. return;
  376. }
  377. }
  378. jack_engine_place_port_buffers (engine, ptid, one_buffer, size, nports);
  379. if (ptid == JACK_AUDIO_PORT_TYPE) {
  380. /* Always zero `nframes' samples, it could have
  381. * changed. The server's global variable
  382. * jack_zero_filled_buffer is for internal clients.
  383. * External clients will set their copies during the
  384. * AttachPortSegment event. */
  385. jack_zero_filled_buffer =
  386. jack_shm_addr (shm_info)
  387. + engine->silent_buffer->offset;
  388. memset (jack_zero_filled_buffer, 0, one_buffer);
  389. }
  390. #ifdef USE_MLOCK
  391. if (engine->control->real_time) {
  392. /* Although we've called mlockall(CURRENT|FUTURE), the
  393. * Linux VM manager still allows newly allocated pages
  394. * to fault on first reference. This mlock() ensures
  395. * that any new pages are present before restarting
  396. * the process cycle. Since memory locks do not
  397. * stack, they can still be unlocked with a single
  398. * munlockall().
  399. */
  400. int rc = mlock (jack_shm_addr (shm_info), size);
  401. if (rc < 0) {
  402. jack_error("JACK: unable to mlock() port buffers: "
  403. "%s", strerror(errno));
  404. }
  405. }
  406. #endif /* USE_MLOCK */
  407. /* Tell everybody about this segment. */
  408. event.type = AttachPortSegment;
  409. event.y.ptid = ptid;
  410. jack_deliver_event_to_all (engine, &event);
  411. }
  412. /* The driver invokes this callback both initially and whenever its
  413. * buffer size changes.
  414. */
  415. static int
  416. jack_driver_buffer_size (jack_engine_t *engine, jack_nframes_t nframes)
  417. {
  418. int i;
  419. jack_event_t event;
  420. JSList *node;
  421. VERBOSE (engine, "new buffer size %" PRIu32 "\n", nframes);
  422. engine->control->buffer_size = nframes;
  423. if (engine->driver)
  424. engine->rolling_interval =
  425. jack_rolling_interval (engine->driver->period_usecs);
  426. for (i = 0; i < engine->control->n_port_types; ++i) {
  427. jack_resize_port_segment (engine, i, engine->control->port_max);
  428. }
  429. /* update shared client copy of nframes */
  430. jack_lock_graph (engine);
  431. for (node = engine->clients; node; node = jack_slist_next (node)) {
  432. jack_client_internal_t *client = node->data;
  433. client->control->nframes = nframes;
  434. }
  435. jack_unlock_graph (engine);
  436. event.type = BufferSizeChange;
  437. jack_deliver_event_to_all (engine, &event);
  438. return 0;
  439. }
  440. /* handle client SetBufferSize request */
  441. int
  442. jack_set_buffer_size_request (jack_engine_t *engine, jack_nframes_t nframes)
  443. {
  444. /* precondition: caller holds the request_lock */
  445. int rc;
  446. jack_driver_t* driver = engine->driver;
  447. if (driver == NULL)
  448. return ENXIO; /* no such device */
  449. if (!jack_power_of_two(nframes)) {
  450. jack_error("buffer size %" PRIu32 " not a power of 2",
  451. nframes);
  452. return EINVAL;
  453. }
  454. rc = driver->bufsize(driver, nframes);
  455. if (rc != 0)
  456. jack_error("driver does not support %" PRIu32
  457. "-frame buffers", nframes);
  458. return rc;
  459. }
  460. static JSList *
  461. jack_process_internal(jack_engine_t *engine, JSList *node,
  462. jack_nframes_t nframes)
  463. {
  464. jack_client_internal_t *client;
  465. jack_client_control_t *ctl;
  466. client = (jack_client_internal_t *) node->data;
  467. ctl = client->control;
  468. /* internal client ("plugin") */
  469. DEBUG ("invoking an internal client's callbacks");
  470. ctl->state = Running;
  471. engine->current_client = client;
  472. /* XXX how to time out an internal client? */
  473. if (ctl->sync_cb)
  474. jack_call_sync_client (ctl->private_client);
  475. if (ctl->process)
  476. if (ctl->process (nframes, ctl->process_arg)) {
  477. jack_error ("internal client %s failed", ctl->name);
  478. engine->process_errors++;
  479. }
  480. if (ctl->timebase_cb)
  481. jack_call_timebase_master (ctl->private_client);
  482. ctl->state = Finished;
  483. if (engine->process_errors)
  484. return NULL; /* will stop the loop */
  485. else
  486. return jack_slist_next (node);
  487. }
  488. #ifdef JACK_USE_MACH_THREADS
  489. static JSList *
  490. jack_process_external(jack_engine_t *engine, JSList *node)
  491. {
  492. jack_client_internal_t * client = (jack_client_internal_t *) node->data;
  493. jack_client_control_t *ctl;
  494. client = (jack_client_internal_t *) node->data;
  495. ctl = client->control;
  496. engine->current_client = client;
  497. // a race exists if we do this after the write(2)
  498. ctl->state = Triggered;
  499. ctl->signalled_at = jack_get_microseconds();
  500. ctl->awake_at = 0;
  501. ctl->finished_at = 0;
  502. if (jack_client_resume(client) < 0) {
  503. jack_error("Client will be removed\n");
  504. ctl->state = Finished;
  505. }
  506. return jack_slist_next (node);
  507. }
  508. #else /* !JACK_USE_MACH_THREADS */
  509. static JSList *
  510. jack_process_external(jack_engine_t *engine, JSList *node)
  511. {
  512. int status = 0;
  513. char c;
  514. struct pollfd pfd[1];
  515. int poll_timeout;
  516. jack_client_internal_t *client;
  517. jack_client_control_t *ctl;
  518. jack_time_t now, then;
  519. client = (jack_client_internal_t *) node->data;
  520. ctl = client->control;
  521. /* external subgraph */
  522. /* a race exists if we do this after the write(2) */
  523. ctl->state = Triggered;
  524. ctl->signalled_at = jack_get_microseconds();
  525. ctl->awake_at = 0;
  526. ctl->finished_at = 0;
  527. engine->current_client = client;
  528. DEBUG ("calling process() on an external subgraph, fd==%d",
  529. client->subgraph_start_fd);
  530. if (write (client->subgraph_start_fd, &c, sizeof (c)) != sizeof (c)) {
  531. jack_error ("cannot initiate graph processing (%s)",
  532. strerror (errno));
  533. engine->process_errors++;
  534. return NULL; /* will stop the loop */
  535. }
  536. then = jack_get_microseconds ();
  537. if (engine->freewheeling) {
  538. poll_timeout = 10000; /* 10 seconds */
  539. } else {
  540. poll_timeout = (engine->control->real_time == 0 ?
  541. engine->client_timeout_msecs :
  542. 1 + engine->driver->period_usecs/1000);
  543. }
  544. pfd[0].fd = client->subgraph_wait_fd;
  545. pfd[0].events = POLLERR|POLLIN|POLLHUP|POLLNVAL;
  546. DEBUG ("waiting on fd==%d for process() subgraph to finish",
  547. client->subgraph_wait_fd);
  548. if (poll (pfd, 1, poll_timeout) < 0) {
  549. jack_error ("poll on subgraph processing failed (%s)",
  550. strerror (errno));
  551. status = -1;
  552. }
  553. DEBUG ("\n\n\n\n\n back from subgraph poll, revents = 0x%x\n\n\n", pfd[0].revents);
  554. if (pfd[0].revents & ~POLLIN) {
  555. jack_error ("subgraph starting at %s lost client",
  556. client->control->name);
  557. status = -2;
  558. }
  559. if (pfd[0].revents & POLLIN) {
  560. status = 0;
  561. } else {
  562. jack_error ("subgraph starting at %s timed out "
  563. "(subgraph_wait_fd=%d, status = %d, state = %s)",
  564. client->control->name,
  565. client->subgraph_wait_fd, status,
  566. client_state_names[client->control->state]);
  567. status = 1;
  568. }
  569. now = jack_get_microseconds ();
  570. if (status != 0) {
  571. VERBOSE (engine, "at %" PRIu64
  572. " waiting on %d for %" PRIu64
  573. " usecs, status = %d sig = %" PRIu64
  574. " awa = %" PRIu64 " fin = %" PRIu64
  575. " dur=%" PRIu64 "\n",
  576. now,
  577. client->subgraph_wait_fd,
  578. now - then,
  579. status,
  580. ctl->signalled_at,
  581. ctl->awake_at,
  582. ctl->finished_at,
  583. ctl->finished_at? (ctl->finished_at -
  584. ctl->signalled_at): 0);
  585. /* we can only consider the timeout a client error if
  586. * it actually woke up. its possible that the kernel
  587. * scheduler screwed us up and never woke up the
  588. * client in time. sigh.
  589. */
  590. if (ctl->awake_at > 0) {
  591. ctl->timed_out++;
  592. }
  593. engine->process_errors++;
  594. return NULL; /* will stop the loop */
  595. } else {
  596. DEBUG ("reading byte from subgraph_wait_fd==%d",
  597. client->subgraph_wait_fd);
  598. if (read (client->subgraph_wait_fd, &c, sizeof(c))
  599. != sizeof (c)) {
  600. jack_error ("pp: cannot clean up byte from graph wait "
  601. "fd (%s)", strerror (errno));
  602. client->error++;
  603. return NULL; /* will stop the loop */
  604. }
  605. }
  606. /* Move to next internal client (or end of client list) */
  607. while (node) {
  608. if (jack_client_is_internal ((jack_client_internal_t *)
  609. node->data)) {
  610. break;
  611. }
  612. node = jack_slist_next (node);
  613. }
  614. return node;
  615. }
  616. #endif /* JACK_USE_MACH_THREADS */
  617. static int
  618. jack_engine_process (jack_engine_t *engine, jack_nframes_t nframes)
  619. {
  620. /* precondition: caller has graph_lock */
  621. jack_client_internal_t *client;
  622. JSList *node;
  623. engine->process_errors = 0;
  624. engine->watchdog_check = 1;
  625. for (node = engine->clients; node; node = jack_slist_next (node)) {
  626. jack_client_control_t *ctl =
  627. ((jack_client_internal_t *) node->data)->control;
  628. ctl->state = NotTriggered;
  629. ctl->nframes = nframes;
  630. ctl->timed_out = 0;
  631. }
  632. for (node = engine->clients; engine->process_errors == 0 && node; ) {
  633. client = (jack_client_internal_t *) node->data;
  634. DEBUG ("considering client %s for processing",
  635. client->control->name);
  636. if (!client->control->active || client->control->dead) {
  637. node = jack_slist_next (node);
  638. } else if (jack_client_is_internal (client)) {
  639. node = jack_process_internal (engine, node, nframes);
  640. } else {
  641. node = jack_process_external (engine, node);
  642. }
  643. }
  644. return engine->process_errors > 0;
  645. }
  646. static void
  647. jack_calc_cpu_load(jack_engine_t *engine)
  648. {
  649. jack_time_t cycle_end = jack_get_microseconds ();
  650. /* store the execution time for later averaging */
  651. engine->rolling_client_usecs[engine->rolling_client_usecs_index++] =
  652. cycle_end - engine->control->current_time.usecs;
  653. if (engine->rolling_client_usecs_index >= JACK_ENGINE_ROLLING_COUNT) {
  654. engine->rolling_client_usecs_index = 0;
  655. }
  656. /* every so often, recompute the current maximum use over the
  657. last JACK_ENGINE_ROLLING_COUNT client iterations.
  658. */
  659. if (++engine->rolling_client_usecs_cnt
  660. % engine->rolling_interval == 0) {
  661. float max_usecs = 0.0f;
  662. int i;
  663. for (i = 0; i < JACK_ENGINE_ROLLING_COUNT; i++) {
  664. if (engine->rolling_client_usecs[i] > max_usecs) {
  665. max_usecs = engine->rolling_client_usecs[i];
  666. }
  667. }
  668. if (max_usecs > engine->max_usecs) {
  669. engine->max_usecs = max_usecs;
  670. }
  671. if (max_usecs < engine->driver->period_usecs) {
  672. engine->spare_usecs =
  673. engine->driver->period_usecs - max_usecs;
  674. } else {
  675. engine->spare_usecs = 0;
  676. }
  677. engine->control->cpu_load =
  678. (1.0f - (engine->spare_usecs /
  679. engine->driver->period_usecs)) * 50.0f
  680. + (engine->control->cpu_load * 0.5f);
  681. VERBOSE (engine, "load = %.4f max usecs: %.3f, "
  682. "spare = %.3f\n", engine->control->cpu_load,
  683. max_usecs, engine->spare_usecs);
  684. }
  685. }
  686. static void
  687. jack_remove_clients (jack_engine_t* engine)
  688. {
  689. JSList *tmp, *node;
  690. int need_sort = FALSE;
  691. jack_client_internal_t *client;
  692. /* remove all dead clients */
  693. for (node = engine->clients; node; ) {
  694. tmp = jack_slist_next (node);
  695. client = (jack_client_internal_t *) node->data;
  696. if (client->error) {
  697. /* if we have a communication problem with the
  698. client, remove it. otherwise, turn it into
  699. a zombie. the client will/should realize
  700. this and will close its sockets. then
  701. we'll end up back here again and will
  702. finally remove the client.
  703. */
  704. if (client->error >= JACK_ERROR_WITH_SOCKETS) {
  705. VERBOSE (engine, "removing failed "
  706. "client %s state = %s errors"
  707. " = %d\n",
  708. client->control->name,
  709. client_state_names[
  710. client->control->state
  711. ],
  712. client->error);
  713. jack_remove_client (engine,
  714. (jack_client_internal_t *)
  715. node->data);
  716. } else {
  717. VERBOSE (engine, "client failure: "
  718. "client %s state = %s errors"
  719. " = %d\n",
  720. client->control->name,
  721. client_state_names[
  722. client->control->state
  723. ],
  724. client->error);
  725. jack_zombify_client (engine,
  726. (jack_client_internal_t *)
  727. node->data);
  728. client->error = 0;
  729. }
  730. need_sort = TRUE;
  731. }
  732. node = tmp;
  733. }
  734. if (need_sort) {
  735. jack_sort_graph (engine);
  736. }
  737. jack_engine_reset_rolling_usecs (engine);
  738. }
  739. static void
  740. jack_engine_post_process (jack_engine_t *engine)
  741. {
  742. /* precondition: caller holds the graph lock. */
  743. jack_client_control_t *ctl;
  744. jack_client_internal_t *client;
  745. JSList *node;
  746. int need_remove = FALSE;
  747. jack_transport_cycle_end (engine);
  748. /* find any clients that need removal due to timeouts, etc. */
  749. for (node = engine->clients; node; node = jack_slist_next (node) ) {
  750. client = (jack_client_internal_t *) node->data;
  751. ctl = client->control;
  752. /* this check is invalid for internal clients and
  753. external clients with no process callback.
  754. */
  755. if (!jack_client_is_internal (client) && ctl->process) {
  756. if (ctl->awake_at != 0 &&
  757. ctl->state > NotTriggered &&
  758. ctl->state != Finished &&
  759. ctl->timed_out++) {
  760. VERBOSE(engine, "client %s error: awake_at = %"
  761. PRIu64
  762. " state = %d timed_out = %d\n",
  763. ctl->name,
  764. ctl->awake_at,
  765. ctl->state,
  766. ctl->timed_out);
  767. client->error++;
  768. }
  769. }
  770. if (client->error) {
  771. need_remove = TRUE;
  772. }
  773. }
  774. if (need_remove) {
  775. jack_remove_clients (engine);
  776. }
  777. jack_calc_cpu_load (engine);
  778. }
  779. static int
  780. jack_load_client (jack_engine_t *engine, jack_client_internal_t *client,
  781. const char *so_name)
  782. {
  783. const char *errstr;
  784. char path_to_so[PATH_MAX+1];
  785. snprintf (path_to_so, sizeof (path_to_so), ADDON_DIR "/%s.so", so_name);
  786. client->handle = dlopen (path_to_so, RTLD_NOW|RTLD_GLOBAL);
  787. if (client->handle == 0) {
  788. if ((errstr = dlerror ()) != 0) {
  789. jack_error ("can't load \"%s\": %s", path_to_so,
  790. errstr);
  791. } else {
  792. jack_error ("bizarre error loading shared object %s",
  793. so_name);
  794. }
  795. return -1;
  796. }
  797. client->initialize = dlsym (client->handle, "jack_initialize");
  798. if ((errstr = dlerror ()) != 0) {
  799. jack_error ("no initialize function in shared object %s\n",
  800. so_name);
  801. dlclose (client->handle);
  802. client->handle = 0;
  803. return -1;
  804. }
  805. client->finish = (void (*)(void *)) dlsym (client->handle,
  806. "jack_finish");
  807. if ((errstr = dlerror ()) != 0) {
  808. jack_error ("no finish function in in shared object %s",
  809. so_name);
  810. dlclose (client->handle);
  811. client->handle = 0;
  812. return -1;
  813. }
  814. return 0;
  815. }
  816. static void
  817. jack_client_unload (jack_client_internal_t *client)
  818. {
  819. if (client->handle) {
  820. if (client->finish) {
  821. client->finish (client->control->process_arg);
  822. }
  823. dlclose (client->handle);
  824. }
  825. }
  826. static jack_client_internal_t *
  827. setup_client (jack_engine_t *engine, int client_fd,
  828. jack_client_connect_request_t *req,
  829. jack_client_connect_result_t *res)
  830. {
  831. JSList *node;
  832. jack_client_internal_t *client = NULL;
  833. for (node = engine->clients; node; node = jack_slist_next (node)) {
  834. client = (jack_client_internal_t *) node->data;
  835. if (strncmp(req->name, (char*)client->control->name,
  836. sizeof(req->name)) == 0) {
  837. jack_error ("cannot create new client; %s already"
  838. " exists", client->control->name);
  839. return NULL;
  840. }
  841. }
  842. if ((client = jack_setup_client_control (engine, client_fd, req))
  843. == NULL) {
  844. jack_error ("cannot create new client object");
  845. return 0;
  846. }
  847. VERBOSE (engine, "new client: %s, id = %" PRIu32
  848. " type %d @ %p fd = %d\n",
  849. client->control->name, client->control->id,
  850. req->type, client->control, client_fd);
  851. res->protocol_v = jack_protocol_version;
  852. res->client_shm = client->control_shm;
  853. res->engine_shm = engine->control_shm;
  854. res->realtime = engine->control->real_time;
  855. res->realtime_priority = engine->rtpriority - 1;
  856. #ifdef JACK_USE_MACH_THREADS
  857. /* specific resources for server/client real-time thread
  858. * communication */
  859. res->portnum = client->portnum;
  860. #endif
  861. if (jack_client_is_internal(client)) {
  862. /* set up the pointers necessary for the request
  863. * system to work. */
  864. client->control->deliver_request = internal_client_request;
  865. client->control->deliver_arg = engine;
  866. /* the client is in the same address space */
  867. res->client_control = client->control;
  868. res->engine_control = engine->control;
  869. } else {
  870. strcpy (res->fifo_prefix, engine->fifo_prefix);
  871. }
  872. /* add new client to the clients list */
  873. jack_lock_graph (engine);
  874. engine->clients = jack_slist_prepend (engine->clients, client);
  875. jack_engine_reset_rolling_usecs (engine);
  876. switch (client->control->type) {
  877. case ClientDriver:
  878. case ClientInternal:
  879. /* an internal client still needs to be able to make
  880. regular JACK API calls, which need a jack_client_t
  881. structure. create one here for it.
  882. */
  883. client->control->private_client =
  884. jack_client_alloc_internal (client->control, engine);
  885. jack_unlock_graph (engine);
  886. /* call its initialization function */
  887. if (client->control->type == ClientInternal) {
  888. if (client->initialize (client->control->private_client,
  889. req->object_data)) {
  890. /* failed: clean up client data */
  891. VERBOSE (engine,
  892. "%s jack_initialize() failed!\n",
  893. client->control->name);
  894. jack_lock_graph (engine);
  895. jack_remove_client (engine, client);
  896. jack_unlock_graph (engine);
  897. return NULL;
  898. }
  899. }
  900. /* its good to go */
  901. break;
  902. default:
  903. if (engine->pfd_max >= engine->pfd_size) {
  904. engine->pfd = (struct pollfd *)
  905. realloc (engine->pfd, sizeof (struct pollfd)
  906. * engine->pfd_size + 16);
  907. engine->pfd_size += 16;
  908. }
  909. engine->pfd[engine->pfd_max].fd = client->request_fd;
  910. engine->pfd[engine->pfd_max].events =
  911. POLLIN|POLLPRI|POLLERR|POLLHUP|POLLNVAL;
  912. engine->pfd_max++;
  913. jack_unlock_graph (engine);
  914. break;
  915. }
  916. return client;
  917. }
  918. static jack_driver_info_t *
  919. jack_load_driver (jack_engine_t *engine, jack_driver_desc_t * driver_desc)
  920. {
  921. const char *errstr;
  922. jack_driver_info_t *info;
  923. info = (jack_driver_info_t *) calloc (1, sizeof (*info));
  924. info->handle = dlopen (driver_desc->file, RTLD_NOW|RTLD_GLOBAL);
  925. if (info->handle == NULL) {
  926. if ((errstr = dlerror ()) != 0) {
  927. jack_error ("can't load \"%s\": %s", driver_desc->file,
  928. errstr);
  929. } else {
  930. jack_error ("bizarre error loading driver shared "
  931. "object %s", driver_desc->file);
  932. }
  933. goto fail;
  934. }
  935. info->initialize = dlsym (info->handle, "driver_initialize");
  936. if ((errstr = dlerror ()) != 0) {
  937. jack_error ("no initialize function in shared object %s\n",
  938. driver_desc->file);
  939. goto fail;
  940. }
  941. info->finish = dlsym (info->handle, "driver_finish");
  942. if ((errstr = dlerror ()) != 0) {
  943. jack_error ("no finish function in in shared driver object %s",
  944. driver_desc->file);
  945. goto fail;
  946. }
  947. info->client_name = (char *) dlsym (info->handle, "driver_client_name");
  948. if ((errstr = dlerror ()) != 0) {
  949. jack_error ("no client name in in shared driver object %s",
  950. driver_desc->file);
  951. goto fail;
  952. }
  953. return info;
  954. fail:
  955. if (info->handle) {
  956. dlclose (info->handle);
  957. }
  958. free (info);
  959. return NULL;
  960. }
  961. void
  962. jack_driver_unload (jack_driver_t *driver)
  963. {
  964. driver->finish (driver);
  965. dlclose (driver->handle);
  966. }
  967. int
  968. jack_engine_load_driver (jack_engine_t *engine, jack_driver_desc_t * driver_desc, JSList * driver_params)
  969. {
  970. jack_client_connect_request_t req;
  971. jack_client_connect_result_t res;
  972. jack_client_internal_t *client;
  973. jack_driver_t *driver;
  974. jack_driver_info_t *info;
  975. if ((info = jack_load_driver (engine, driver_desc)) == NULL) {
  976. return -1;
  977. }
  978. req.type = ClientDriver;
  979. snprintf (req.name, sizeof (req.name), "%s", info->client_name);
  980. if ((client = setup_client (engine, -1, &req, &res)) == NULL) {
  981. return -1;
  982. }
  983. if ((driver = info->initialize (client->control->private_client,
  984. driver_params)) == NULL) {
  985. free (info);
  986. return -1;
  987. }
  988. driver->handle = info->handle;
  989. driver->finish = info->finish;
  990. driver->internal_client = client;
  991. free (info);
  992. if (jack_use_driver (engine, driver)) {
  993. jack_driver_unload (driver);
  994. jack_client_delete (engine, client);
  995. return -1;
  996. }
  997. engine->driver_desc = driver_desc;
  998. engine->driver_params = driver_params;
  999. if (engine->control->real_time) {
  1000. /* Stephane Letz : letz@grame.fr
  1001. Watch dog thread is not needed on MacOSX since CoreAudio drivers
  1002. already contains a similar mechanism.
  1003. */
  1004. #ifndef JACK_USE_MACH_THREADS
  1005. if (jack_start_watchdog (engine)) {
  1006. return -1;
  1007. }
  1008. engine->watchdog_check = 1;
  1009. #endif
  1010. }
  1011. return 0;
  1012. }
  1013. static int
  1014. handle_unload_client (jack_engine_t *engine, int client_fd,
  1015. jack_client_connect_request_t *req)
  1016. {
  1017. JSList *node;
  1018. jack_client_connect_result_t res;
  1019. res.status = -1;
  1020. VERBOSE (engine, "unloading client \"%s\"\n", req->name);
  1021. jack_lock_graph (engine);
  1022. for (node = engine->clients; node; node = jack_slist_next (node)) {
  1023. if (strcmp ((char *) ((jack_client_internal_t *)
  1024. node->data)->control->name,
  1025. req->name) == 0) {
  1026. jack_remove_client (engine, (jack_client_internal_t *)
  1027. node->data);
  1028. res.status = 0;
  1029. break;
  1030. }
  1031. }
  1032. jack_unlock_graph (engine);
  1033. return 0;
  1034. }
  1035. static int
  1036. handle_new_client (jack_engine_t *engine, int client_fd)
  1037. {
  1038. jack_client_internal_t *client;
  1039. jack_client_connect_request_t req;
  1040. jack_client_connect_result_t res;
  1041. res.status = 0;
  1042. if (read (client_fd, &req, sizeof (req)) != sizeof (req)) {
  1043. jack_error ("cannot read connection request from client");
  1044. return -1;
  1045. }
  1046. if (!req.load) {
  1047. return handle_unload_client (engine, client_fd, &req);
  1048. }
  1049. if ((client = setup_client (engine, client_fd, &req, &res)) == NULL) {
  1050. return -1;
  1051. }
  1052. if (write (client->request_fd, &res, sizeof (res)) != sizeof (res)) {
  1053. jack_error ("cannot write connection response to client");
  1054. jack_client_delete (engine, client);
  1055. return -1;
  1056. }
  1057. switch (client->control->type) {
  1058. case ClientDriver:
  1059. case ClientInternal:
  1060. close (client_fd);
  1061. break;
  1062. default:
  1063. break;
  1064. }
  1065. return 0;
  1066. }
  1067. static int
  1068. handle_client_ack_connection (jack_engine_t *engine, int client_fd)
  1069. {
  1070. jack_client_internal_t *client;
  1071. jack_client_connect_ack_request_t req;
  1072. jack_client_connect_ack_result_t res;
  1073. if (read (client_fd, &req, sizeof (req)) != sizeof (req)) {
  1074. jack_error ("cannot read ACK connection request from client");
  1075. return -1;
  1076. }
  1077. if ((client = jack_client_internal_by_id (engine, req.client_id))
  1078. == NULL) {
  1079. jack_error ("unknown client ID in ACK connection request");
  1080. return -1;
  1081. }
  1082. client->event_fd = client_fd;
  1083. res.status = 0;
  1084. if (write (client->event_fd, &res, sizeof (res)) != sizeof (res)) {
  1085. jack_error ("cannot write ACK connection response to client");
  1086. return -1;
  1087. }
  1088. return 0;
  1089. }
  1090. #ifdef USE_CAPABILITIES
  1091. static int check_capabilities (jack_engine_t *engine)
  1092. {
  1093. cap_t caps = cap_init();
  1094. cap_flag_value_t cap;
  1095. pid_t pid;
  1096. int have_all_caps = 1;
  1097. if (caps == NULL) {
  1098. VERBOSE (engine, "check: could not allocate capability"
  1099. " working storage\n");
  1100. return 0;
  1101. }
  1102. pid = getpid ();
  1103. cap_clear (caps);
  1104. if (capgetp (pid, caps)) {
  1105. VERBOSE (engine, "check: could not get capabilities "
  1106. "for process %d\n", pid);
  1107. return 0;
  1108. }
  1109. /* check that we are able to give capabilites to other processes */
  1110. cap_get_flag(caps, CAP_SETPCAP, CAP_EFFECTIVE, &cap);
  1111. if (cap == CAP_CLEAR) {
  1112. have_all_caps = 0;
  1113. goto done;
  1114. }
  1115. /* check that we have the capabilities we want to transfer */
  1116. cap_get_flag(caps, CAP_SYS_NICE, CAP_EFFECTIVE, &cap);
  1117. if (cap == CAP_CLEAR) {
  1118. have_all_caps = 0;
  1119. goto done;
  1120. }
  1121. cap_get_flag(caps, CAP_SYS_RESOURCE, CAP_EFFECTIVE, &cap);
  1122. if (cap == CAP_CLEAR) {
  1123. have_all_caps = 0;
  1124. goto done;
  1125. }
  1126. cap_get_flag(caps, CAP_IPC_LOCK, CAP_EFFECTIVE, &cap);
  1127. if (cap == CAP_CLEAR) {
  1128. have_all_caps = 0;
  1129. goto done;
  1130. }
  1131. done:
  1132. cap_free (caps);
  1133. return have_all_caps;
  1134. }
  1135. static int give_capabilities (jack_engine_t *engine, pid_t pid)
  1136. {
  1137. cap_t caps = cap_init();
  1138. const unsigned caps_size = 3;
  1139. cap_value_t cap_list[] = {CAP_SYS_NICE, CAP_SYS_RESOURCE, CAP_IPC_LOCK};
  1140. if (caps == NULL) {
  1141. VERBOSE (engine, "give: could not allocate capability"
  1142. " working storage\n");
  1143. return -1;
  1144. }
  1145. cap_clear(caps);
  1146. if (capgetp (pid, caps)) {
  1147. VERBOSE (engine, "give: could not get current "
  1148. "capabilities for process %d\n", pid);
  1149. cap_clear(caps);
  1150. }
  1151. cap_set_flag(caps, CAP_EFFECTIVE, caps_size, cap_list , CAP_SET);
  1152. cap_set_flag(caps, CAP_INHERITABLE, caps_size, cap_list , CAP_SET);
  1153. cap_set_flag(caps, CAP_PERMITTED, caps_size, cap_list , CAP_SET);
  1154. if (capsetp (pid, caps)) {
  1155. cap_free (caps);
  1156. return -1;
  1157. }
  1158. cap_free (caps);
  1159. return 0;
  1160. }
  1161. static int
  1162. jack_set_client_capabilities (jack_engine_t *engine, jack_client_id_t id)
  1163. {
  1164. JSList *node;
  1165. int ret = -1;
  1166. jack_lock_graph (engine);
  1167. for (node = engine->clients; node; node = jack_slist_next (node)) {
  1168. jack_client_internal_t *client =
  1169. (jack_client_internal_t *) node->data;
  1170. if (client->control->id == id) {
  1171. /* before sending this request the client has
  1172. already checked that the engine has
  1173. realtime capabilities, that it is running
  1174. realtime and that the pid is defined
  1175. */
  1176. ret = give_capabilities (engine, client->control->pid);
  1177. if (ret) {
  1178. jack_error ("could not give capabilities to "
  1179. "process %d\n",
  1180. client->control->pid);
  1181. } else {
  1182. VERBOSE (engine, "gave capabilities to"
  1183. " process %d\n",
  1184. client->control->pid);
  1185. }
  1186. }
  1187. }
  1188. jack_unlock_graph (engine);
  1189. return ret;
  1190. }
  1191. #endif /* USE_CAPABILITIES */
  1192. static int
  1193. jack_client_activate (jack_engine_t *engine, jack_client_id_t id)
  1194. {
  1195. jack_client_internal_t *client;
  1196. JSList *node;
  1197. int ret = -1;
  1198. jack_lock_graph (engine);
  1199. for (node = engine->clients; node; node = jack_slist_next (node)) {
  1200. if (((jack_client_internal_t *) node->data)->control->id
  1201. == id) {
  1202. client = (jack_client_internal_t *) node->data;
  1203. client->control->active = TRUE;
  1204. jack_transport_activate(engine, client);
  1205. /* we call this to make sure the FIFO is
  1206. * built+ready by the time the client needs
  1207. * it. we don't care about the return value at
  1208. * this point.
  1209. */
  1210. jack_get_fifo_fd (engine,
  1211. ++engine->external_client_cnt);
  1212. jack_sort_graph (engine);
  1213. ret = 0;
  1214. break;
  1215. }
  1216. }
  1217. jack_unlock_graph (engine);
  1218. return ret;
  1219. }
  1220. static int
  1221. jack_client_do_deactivate (jack_engine_t *engine,
  1222. jack_client_internal_t *client, int sort_graph)
  1223. {
  1224. /* caller must hold engine->client_lock and must have checked for and/or
  1225. * cleared all connections held by client. */
  1226. client->control->active = FALSE;
  1227. jack_transport_client_exit (engine, client);
  1228. if (!jack_client_is_internal (client) &&
  1229. engine->external_client_cnt > 0) {
  1230. engine->external_client_cnt--;
  1231. }
  1232. if (sort_graph) {
  1233. jack_sort_graph (engine);
  1234. }
  1235. return 0;
  1236. }
  1237. static void
  1238. jack_client_disconnect (jack_engine_t *engine, jack_client_internal_t *client)
  1239. {
  1240. JSList *node;
  1241. jack_port_internal_t *port;
  1242. /* call tree **** MUST HOLD *** engine->client_lock */
  1243. for (node = client->ports; node; node = jack_slist_next (node)) {
  1244. port = (jack_port_internal_t *) node->data;
  1245. jack_port_clear_connections (engine, port);
  1246. jack_port_registration_notify (engine, port->shared->id, FALSE);
  1247. jack_port_release (engine, port);
  1248. }
  1249. jack_slist_free (client->ports);
  1250. jack_slist_free (client->fed_by);
  1251. client->fed_by = 0;
  1252. client->ports = 0;
  1253. }
  1254. static int
  1255. jack_client_deactivate (jack_engine_t *engine, jack_client_id_t id)
  1256. {
  1257. JSList *node;
  1258. int ret = -1;
  1259. jack_lock_graph (engine);
  1260. for (node = engine->clients; node; node = jack_slist_next (node)) {
  1261. jack_client_internal_t *client =
  1262. (jack_client_internal_t *) node->data;
  1263. if (client->control->id == id) {
  1264. JSList *portnode;
  1265. jack_port_internal_t *port;
  1266. for (portnode = client->ports; portnode;
  1267. portnode = jack_slist_next (portnode)) {
  1268. port = (jack_port_internal_t *) portnode->data;
  1269. jack_port_clear_connections (engine, port);
  1270. }
  1271. ret = jack_client_do_deactivate (engine, client, TRUE);
  1272. break;
  1273. }
  1274. }
  1275. jack_unlock_graph (engine);
  1276. return ret;
  1277. }
  1278. static int
  1279. handle_client_socket_error (jack_engine_t *engine, int fd)
  1280. {
  1281. jack_client_internal_t *client = 0;
  1282. JSList *node;
  1283. #ifndef DEFER_CLIENT_REMOVE_TO_AUDIO_THREAD
  1284. jack_lock_graph (engine);
  1285. for (node = engine->clients; node; node = jack_slist_next (node)) {
  1286. if (jack_client_is_internal((jack_client_internal_t *)
  1287. node->data)) {
  1288. continue;
  1289. }
  1290. if (((jack_client_internal_t *) node->data)->request_fd == fd) {
  1291. client = (jack_client_internal_t *) node->data;
  1292. break;
  1293. }
  1294. }
  1295. if (client) {
  1296. VERBOSE (engine, "removing failed client %s state = "
  1297. "%s errors = %d\n", client->control->name,
  1298. client_state_names[client->control->state],
  1299. client->error);
  1300. jack_remove_client(engine, client);
  1301. jack_sort_graph (engine);
  1302. }
  1303. jack_unlock_graph (engine);
  1304. #else
  1305. jack_lock_graph (engine);
  1306. for (node = engine->clients; node; node = jack_slist_next (node)) {
  1307. if (jack_client_is_internal((jack_client_internal_t *)
  1308. node->data)) {
  1309. continue;
  1310. }
  1311. if (((jack_client_internal_t *) node->data)->request_fd == fd) {
  1312. client = (jack_client_internal_t *) node->data;
  1313. if (client->error < JACK_ERROR_WITH_SOCKETS) {
  1314. client->error += JACK_ERROR_WITH_SOCKETS;
  1315. }
  1316. break;
  1317. }
  1318. }
  1319. jack_unlock_graph (engine);
  1320. #endif
  1321. return 0;
  1322. }
  1323. static void
  1324. do_request (jack_engine_t *engine, jack_request_t *req, int *reply_fd)
  1325. {
  1326. pthread_mutex_lock (&engine->request_lock);
  1327. DEBUG ("got a request of type %d", req->type);
  1328. switch (req->type) {
  1329. case RegisterPort:
  1330. req->status = jack_port_do_register (engine, req);
  1331. break;
  1332. case UnRegisterPort:
  1333. req->status = jack_port_do_unregister (engine, req);
  1334. break;
  1335. case ConnectPorts:
  1336. req->status = jack_port_do_connect
  1337. (engine, req->x.connect.source_port,
  1338. req->x.connect.destination_port);
  1339. break;
  1340. case DisconnectPort:
  1341. req->status = jack_port_do_disconnect_all
  1342. (engine, req->x.port_info.port_id);
  1343. break;
  1344. case DisconnectPorts:
  1345. req->status = jack_port_do_disconnect
  1346. (engine, req->x.connect.source_port,
  1347. req->x.connect.destination_port);
  1348. break;
  1349. case ActivateClient:
  1350. req->status = jack_client_activate (engine, req->x.client_id);
  1351. break;
  1352. case DeactivateClient:
  1353. req->status = jack_client_deactivate (engine, req->x.client_id);
  1354. break;
  1355. case SetTimeBaseClient:
  1356. req->status = jack_timebase_set (engine,
  1357. req->x.timebase.client_id,
  1358. req->x.timebase.conditional);
  1359. break;
  1360. case ResetTimeBaseClient:
  1361. req->status = jack_timebase_reset (engine, req->x.client_id);
  1362. break;
  1363. case SetSyncClient:
  1364. req->status =
  1365. jack_transport_client_set_sync (engine,
  1366. req->x.client_id);
  1367. break;
  1368. case ResetSyncClient:
  1369. req->status =
  1370. jack_transport_client_reset_sync (engine,
  1371. req->x.client_id);
  1372. break;
  1373. case SetSyncTimeout:
  1374. req->status = jack_transport_set_sync_timeout (engine,
  1375. req->x.timeout);
  1376. break;
  1377. #ifdef USE_CAPABILITIES
  1378. case SetClientCapabilities:
  1379. req->status = jack_set_client_capabilities (engine,
  1380. req->x.client_id);
  1381. break;
  1382. #endif /* USE_CAPABILITIES */
  1383. case GetPortConnections:
  1384. case GetPortNConnections:
  1385. if ((req->status =
  1386. jack_do_get_port_connections (engine, req, *reply_fd))
  1387. == 0) {
  1388. /* we have already replied, don't do it again */
  1389. *reply_fd = -1;
  1390. }
  1391. break;
  1392. case FreeWheel:
  1393. req->status = jack_start_freewheeling (engine);
  1394. break;
  1395. case StopFreeWheel:
  1396. req->status = jack_stop_freewheeling (engine);
  1397. break;
  1398. case SetBufferSize:
  1399. req->status = jack_set_buffer_size_request (engine,
  1400. req->x.nframes);
  1401. default:
  1402. /* some requests are handled entirely on the client
  1403. * side, by adjusting the shared memory area(s) */
  1404. break;
  1405. }
  1406. pthread_mutex_unlock (&engine->request_lock);
  1407. DEBUG ("status of request: %d", req->status);
  1408. }
  1409. static int
  1410. internal_client_request (void* ptr, jack_request_t *request)
  1411. {
  1412. do_request ((jack_engine_t*) ptr, request, 0);
  1413. return request->status;
  1414. }
  1415. static int
  1416. handle_external_client_request (jack_engine_t *engine, int fd)
  1417. {
  1418. jack_request_t req;
  1419. jack_client_internal_t *client = 0;
  1420. int reply_fd;
  1421. JSList *node;
  1422. ssize_t r;
  1423. DEBUG ("HIT: before lock");
  1424. jack_lock_graph (engine);
  1425. DEBUG ("HIT: before for");
  1426. for (node = engine->clients; node; node = jack_slist_next (node)) {
  1427. if (((jack_client_internal_t *) node->data)->request_fd == fd) {
  1428. DEBUG ("HIT: in for");
  1429. client = (jack_client_internal_t *) node->data;
  1430. break;
  1431. }
  1432. }
  1433. DEBUG ("HIT: after for");
  1434. jack_unlock_graph (engine);
  1435. if (client == NULL) {
  1436. jack_error ("client input on unknown fd %d!", fd);
  1437. return -1;
  1438. }
  1439. if ((r = read (client->request_fd, &req, sizeof (req)))
  1440. < (ssize_t) sizeof (req)) {
  1441. jack_error ("cannot read request from client (%d/%d/%s)",
  1442. r, sizeof(req), strerror (errno));
  1443. client->error++;
  1444. return -1;
  1445. }
  1446. reply_fd = client->request_fd;
  1447. do_request (engine, &req, &reply_fd);
  1448. if (reply_fd >= 0) {
  1449. DEBUG ("replying to client");
  1450. if (write (reply_fd, &req, sizeof (req))
  1451. < (ssize_t) sizeof (req)) {
  1452. jack_error ("cannot write request result to client");
  1453. return -1;
  1454. }
  1455. } else {
  1456. DEBUG ("*not* replying to client");
  1457. }
  1458. return 0;
  1459. }
  1460. static void *
  1461. jack_server_thread (void *arg)
  1462. {
  1463. jack_engine_t *engine = (jack_engine_t *) arg;
  1464. struct sockaddr_un client_addr;
  1465. socklen_t client_addrlen;
  1466. struct pollfd *pfd;
  1467. int client_socket;
  1468. int done = 0;
  1469. int i;
  1470. int max;
  1471. engine->pfd[0].fd = engine->fds[0];
  1472. engine->pfd[0].events = POLLIN|POLLERR;
  1473. engine->pfd[1].fd = engine->fds[1];
  1474. engine->pfd[1].events = POLLIN|POLLERR;
  1475. engine->pfd_max = 2;
  1476. while (!done) {
  1477. DEBUG ("start while");
  1478. /* XXX race here with new external clients
  1479. causing engine->pfd to be reallocated.
  1480. I don't know how to solve this
  1481. short of copying the entire
  1482. contents of the pfd struct. Ick.
  1483. */
  1484. max = engine->pfd_max;
  1485. pfd = engine->pfd;
  1486. if (poll (pfd, max, 10000) < 0) {
  1487. if (errno == EINTR) {
  1488. continue;
  1489. }
  1490. jack_error ("poll failed (%s)", strerror (errno));
  1491. break;
  1492. }
  1493. DEBUG("server thread back from poll");
  1494. /* check each client socket before handling other request*/
  1495. for (i = 2; i < max; i++) {
  1496. if (pfd[i].fd < 0) {
  1497. continue;
  1498. }
  1499. if (pfd[i].revents & ~POLLIN) {
  1500. handle_client_socket_error (engine, pfd[i].fd);
  1501. } else if (pfd[i].revents & POLLIN) {
  1502. if (handle_external_client_request
  1503. (engine, pfd[i].fd)) {
  1504. jack_error ("could not handle external"
  1505. " client request");
  1506. #ifdef JACK_USE_MACH_THREADS
  1507. /* poll is implemented using
  1508. select (see the macosx/fakepoll
  1509. code). When the socket is closed
  1510. select does not return any error,
  1511. POLLIN is true and the next read
  1512. will return 0 bytes. This
  1513. behaviour is diffrent from the
  1514. Linux poll behaviour. Thus we use
  1515. this condition as a socket error
  1516. and remove the client.
  1517. */
  1518. handle_client_socket_error(engine, pfd[i].fd);
  1519. #endif /* JACK_USE_MACH_THREADS */
  1520. }
  1521. }
  1522. }
  1523. /* check the master server socket */
  1524. if (pfd[0].revents & POLLERR) {
  1525. jack_error ("error on server socket");
  1526. break;
  1527. }
  1528. if (pfd[0].revents & POLLIN) {
  1529. DEBUG ("pfd[0].revents & POLLIN");
  1530. memset (&client_addr, 0, sizeof (client_addr));
  1531. client_addrlen = sizeof (client_addr);
  1532. if ((client_socket =
  1533. accept (engine->fds[0],
  1534. (struct sockaddr *) &client_addr,
  1535. &client_addrlen)) < 0) {
  1536. jack_error ("cannot accept new connection (%s)",
  1537. strerror (errno));
  1538. } else if (handle_new_client (engine, client_socket)
  1539. < 0) {
  1540. jack_error ("cannot complete new client "
  1541. "connection process");
  1542. close (client_socket);
  1543. }
  1544. }
  1545. /* check the ACK server socket */
  1546. if (pfd[1].revents & POLLERR) {
  1547. jack_error ("error on server ACK socket");
  1548. break;
  1549. }
  1550. if (pfd[1].revents & POLLIN) {
  1551. DEBUG ("pfd[1].revents & POLLIN");
  1552. memset (&client_addr, 0, sizeof (client_addr));
  1553. client_addrlen = sizeof (client_addr);
  1554. if ((client_socket =
  1555. accept (engine->fds[1],
  1556. (struct sockaddr *) &client_addr,
  1557. &client_addrlen)) < 0) {
  1558. jack_error ("cannot accept new ACK connection"
  1559. " (%s)", strerror (errno));
  1560. } else if (handle_client_ack_connection
  1561. (engine, client_socket)) {
  1562. jack_error ("cannot complete client ACK "
  1563. "connection process");
  1564. close (client_socket);
  1565. }
  1566. }
  1567. }
  1568. return 0;
  1569. }
  1570. jack_engine_t *
  1571. jack_engine_new (int realtime, int rtpriority,
  1572. int do_mlock, int do_unlock,
  1573. int temporary,
  1574. int verbose, int client_timeout, unsigned int port_max, pid_t wait_pid,
  1575. JSList *drivers)
  1576. {
  1577. jack_engine_t *engine;
  1578. unsigned int i;
  1579. #ifdef USE_CAPABILITIES
  1580. uid_t uid = getuid ();
  1581. uid_t euid = geteuid ();
  1582. #endif /* USE_CAPABILITIES */
  1583. jack_init_time ();
  1584. engine = (jack_engine_t *) malloc (sizeof (jack_engine_t));
  1585. engine->drivers = drivers;
  1586. engine->driver = NULL;
  1587. engine->driver_desc = NULL;
  1588. engine->driver_params = NULL;
  1589. engine->set_sample_rate = jack_set_sample_rate;
  1590. engine->set_buffer_size = jack_driver_buffer_size;
  1591. engine->run_cycle = jack_run_cycle;
  1592. engine->delay = jack_engine_notify_clients_about_delay;
  1593. engine->driver_exit = jack_engine_driver_exit;
  1594. engine->transport_cycle_start = jack_transport_cycle_start;
  1595. engine->client_timeout_msecs = client_timeout;
  1596. engine->next_client_id = 1;
  1597. engine->port_max = port_max;
  1598. engine->rtpriority = rtpriority;
  1599. engine->silent_buffer = 0;
  1600. engine->verbose = verbose;
  1601. engine->temporary = temporary;
  1602. engine->freewheeling = 0;
  1603. engine->wait_pid = wait_pid;
  1604. jack_engine_reset_rolling_usecs (engine);
  1605. engine->max_usecs = 0.0f;
  1606. pthread_mutex_init (&engine->client_lock, 0);
  1607. pthread_mutex_init (&engine->port_lock, 0);
  1608. pthread_mutex_init (&engine->request_lock, 0);
  1609. engine->clients = 0;
  1610. engine->pfd_size = 16;
  1611. engine->pfd_max = 0;
  1612. engine->pfd = (struct pollfd *) malloc (sizeof (struct pollfd)
  1613. * engine->pfd_size);
  1614. engine->fifo_size = 16;
  1615. engine->fifo = (int *) malloc (sizeof (int) * engine->fifo_size);
  1616. for (i = 0; i < engine->fifo_size; i++) {
  1617. engine->fifo[i] = -1;
  1618. }
  1619. engine->external_client_cnt = 0;
  1620. srandom (time ((time_t *) 0));
  1621. if (jack_initialize_shm ()) {
  1622. return 0;
  1623. }
  1624. if (jack_shmalloc ("/jack-engine",
  1625. sizeof (jack_control_t)
  1626. + ((sizeof (jack_port_shared_t) * engine->port_max)),
  1627. &engine->control_shm)) {
  1628. jack_error ("cannot create engine control shared memory "
  1629. "segment (%s)", strerror (errno));
  1630. return 0;
  1631. }
  1632. if (jack_attach_shm (&engine->control_shm)) {
  1633. jack_error ("cannot attach to engine control shared memory (%s)",
  1634. strerror (errno));
  1635. jack_destroy_shm (&engine->control_shm);
  1636. return 0;
  1637. }
  1638. engine->control = (jack_control_t *)
  1639. jack_shm_addr (&engine->control_shm);
  1640. /* Setup port type information from builtins. buffer space is
  1641. * allocated when the driver calls jack_driver_buffer_size().
  1642. */
  1643. for (i = 0; jack_builtin_port_types[i].type_name[0]; ++i) {
  1644. memcpy (&engine->control->port_types[i],
  1645. &jack_builtin_port_types[i],
  1646. sizeof (jack_port_type_info_t));
  1647. VERBOSE (engine, "registered builtin port type %s\n",
  1648. engine->control->port_types[i].type_name);
  1649. /* the port type id is index into port_types array */
  1650. engine->control->port_types[i].ptype_id = i;
  1651. /* be sure to initialize mutex correctly */
  1652. pthread_mutex_init (&engine->port_buffers[i].lock, NULL);
  1653. /* set buffer list info correctly */
  1654. engine->port_buffers[i].freelist = NULL;
  1655. engine->port_buffers[i].info = NULL;
  1656. /* mark each port segment as not allocated */
  1657. engine->port_segment[i].index = -1;
  1658. engine->port_segment[i].attached_at = 0;
  1659. }
  1660. engine->control->n_port_types = i;
  1661. /* Mark all ports as available */
  1662. for (i = 0; i < engine->port_max; i++) {
  1663. engine->control->ports[i].in_use = 0;
  1664. engine->control->ports[i].id = i;
  1665. }
  1666. /* allocate internal port structures so that we can keep track
  1667. * of port connections.
  1668. */
  1669. engine->internal_ports = (jack_port_internal_t *)
  1670. malloc (sizeof (jack_port_internal_t) * engine->port_max);
  1671. for (i = 0; i < engine->port_max; i++) {
  1672. engine->internal_ports[i].connections = 0;
  1673. }
  1674. if (make_sockets (engine->fds) < 0) {
  1675. jack_error ("cannot create server sockets");
  1676. return 0;
  1677. }
  1678. engine->control->port_max = engine->port_max;
  1679. engine->control->real_time = realtime;
  1680. engine->control->client_priority = (realtime
  1681. ? engine->rtpriority - 1
  1682. : 0);
  1683. engine->control->do_mlock = do_mlock;
  1684. engine->control->do_munlock = do_unlock;
  1685. engine->control->cpu_load = 0;
  1686. engine->control->buffer_size = 0;
  1687. jack_transport_init (engine);
  1688. jack_set_sample_rate (engine, 0);
  1689. engine->control->internal = 0;
  1690. engine->control->has_capabilities = 0;
  1691. #ifdef JACK_USE_MACH_THREADS
  1692. /* specific resources for server/client real-time thread
  1693. * communication */
  1694. engine->servertask = mach_task_self();
  1695. if (task_get_bootstrap_port(engine->servertask, &engine->bp)){
  1696. jack_error("Jackd: Can't find bootstrap mach port");
  1697. return 0;
  1698. }
  1699. engine->portnum = 0;
  1700. #endif /* JACK_USE_MACH_THREADS */
  1701. #ifdef USE_CAPABILITIES
  1702. if (uid == 0 || euid == 0) {
  1703. VERBOSE (engine, "running with uid=%d and euid=%d, "
  1704. "will not try to use capabilites\n",
  1705. uid, euid);
  1706. } else {
  1707. /* only try to use capabilities if not running as root */
  1708. engine->control->has_capabilities = check_capabilities (engine);
  1709. if (engine->control->has_capabilities == 0) {
  1710. VERBOSE (engine, "required capabilities not "
  1711. "available\n");
  1712. }
  1713. if (engine->verbose) {
  1714. size_t size;
  1715. cap_t cap = cap_init();
  1716. capgetp(0, cap);
  1717. VERBOSE (engine, "capabilities: %s\n",
  1718. cap_to_text(cap, &size));
  1719. }
  1720. }
  1721. #endif /* USE_CAPABILITIES */
  1722. #ifdef USE_MLOCK
  1723. if (realtime && do_mlock && (mlockall (MCL_CURRENT | MCL_FUTURE) != 0)) {
  1724. jack_error ("cannot lock down memory for jackd (%s)",
  1725. strerror (errno));
  1726. #ifdef ENSURE_MLOCK
  1727. return NULL;
  1728. #endif /* ENSURE_MLOCK */
  1729. }
  1730. #endif /* USE_MLOCK */
  1731. engine->control->engine_ok = 1;
  1732. snprintf (engine->fifo_prefix, sizeof (engine->fifo_prefix),
  1733. "%s/jack-%d-ack-fifo-%d", jack_server_dir, getuid (), getpid());
  1734. (void) jack_get_fifo_fd (engine, 0);
  1735. jack_create_thread (&engine->server_thread, 0, FALSE, &jack_server_thread,
  1736. engine);
  1737. return engine;
  1738. }
  1739. static void *
  1740. jack_watchdog_thread (void *arg)
  1741. {
  1742. jack_engine_t *engine = (jack_engine_t *) arg;
  1743. engine->watchdog_check = 0;
  1744. while (1) {
  1745. sleep (5);
  1746. if ( engine->watchdog_check == 0) {
  1747. jack_error ("jackd watchdog: timeout - killing jackd");
  1748. /* Kill the current client's process group. */
  1749. if (engine->current_client) {
  1750. kill (-engine->current_client->
  1751. control->pgrp, SIGKILL);
  1752. }
  1753. /* kill our process group, trying to get a dump */
  1754. kill (-getpgrp(), SIGABRT);
  1755. /*NOTREACHED*/
  1756. exit (1);
  1757. #if 0 /* suppress watchdog message */
  1758. } else {
  1759. VERBOSE(engine, "jackd watchdog: all is well.\n");
  1760. #endif
  1761. }
  1762. engine->watchdog_check = 0;
  1763. }
  1764. }
  1765. static int
  1766. jack_start_watchdog (jack_engine_t *engine)
  1767. {
  1768. int watchdog_priority = engine->rtpriority + 10;
  1769. int max_priority = sched_get_priority_max (SCHED_FIFO);
  1770. if ((max_priority != -1) &&
  1771. (max_priority < watchdog_priority))
  1772. watchdog_priority = max_priority;
  1773. if (jack_create_thread (&engine->watchdog_thread, watchdog_priority, TRUE,
  1774. jack_watchdog_thread, engine)) {
  1775. jack_error ("cannot start watchdog thread");
  1776. return -1;
  1777. }
  1778. return 0;
  1779. }
  1780. static void
  1781. jack_engine_notify_clients_about_delay (jack_engine_t *engine)
  1782. {
  1783. JSList *node;
  1784. jack_event_t event;
  1785. event.type = XRun;
  1786. jack_lock_graph (engine);
  1787. for (node = engine->clients; node; node = jack_slist_next (node)) {
  1788. jack_deliver_event (engine,
  1789. (jack_client_internal_t *) node->data,
  1790. &event);
  1791. }
  1792. jack_unlock_graph (engine);
  1793. }
  1794. static inline void
  1795. jack_inc_frame_time (jack_engine_t *engine, jack_nframes_t amount)
  1796. {
  1797. jack_frame_timer_t *time = &engine->control->frame_timer;
  1798. // atomic_inc (&time->guard1, 1);
  1799. // really need a memory barrier here
  1800. time->guard1++;
  1801. time->frames += amount;
  1802. time->stamp = engine->driver->last_wait_ust;
  1803. // atomic_inc (&time->guard2, 1);
  1804. // might need a memory barrier here
  1805. time->guard2++;
  1806. }
  1807. static void*
  1808. jack_engine_freewheel (void *arg)
  1809. {
  1810. jack_engine_t* engine = (jack_engine_t *) arg;
  1811. VERBOSE (engine, "freewheel thread starting ...\n");
  1812. /* we should not be running SCHED_FIFO, so we don't
  1813. have to do anything about scheduling.
  1814. */
  1815. while (engine->freewheeling) {
  1816. jack_lock_graph (engine);
  1817. if (jack_engine_process (engine,
  1818. engine->control->buffer_size)) {
  1819. jack_error ("process cycle within freewheel failed");
  1820. jack_unlock_graph (engine);
  1821. break;
  1822. }
  1823. jack_unlock_graph (engine);
  1824. }
  1825. VERBOSE (engine, "freewheel came to an end, naturally\n");
  1826. return 0;
  1827. }
  1828. static int
  1829. jack_start_freewheeling (jack_engine_t* engine)
  1830. {
  1831. jack_event_t event;
  1832. if (engine->freewheeling) {
  1833. return 0;
  1834. }
  1835. if (engine->driver == NULL) {
  1836. jack_error ("cannot start freewheeling without a driver!");
  1837. return -1;
  1838. }
  1839. /* stop driver before telling anyone about it so
  1840. there are no more process() calls being handled.
  1841. */
  1842. if (engine->driver->stop (engine->driver)) {
  1843. jack_error ("could not stop driver for freewheeling");
  1844. return -1;
  1845. }
  1846. engine->freewheeling = 1;
  1847. event.type = StartFreewheel;
  1848. jack_deliver_event_to_all (engine, &event);
  1849. if (jack_create_thread (&engine->freewheel_thread, 0, FALSE,
  1850. jack_engine_freewheel, engine)) {
  1851. jack_error ("could not start create freewheel thread");
  1852. return -1;
  1853. }
  1854. return 0;
  1855. }
  1856. static int
  1857. jack_stop_freewheeling (jack_engine_t* engine)
  1858. {
  1859. jack_event_t event;
  1860. void *ftstatus;
  1861. if (!engine->freewheeling) {
  1862. return 0;
  1863. }
  1864. if (engine->driver == NULL) {
  1865. jack_error ("cannot start freewheeling without a driver!");
  1866. return -1;
  1867. }
  1868. if (!engine->freewheeling) {
  1869. VERBOSE (engine, "stop freewheel when not freewheeling\n");
  1870. return 0;
  1871. }
  1872. /* tell the freewheel thread to stop, and wait for it
  1873. to exit.
  1874. */
  1875. engine->freewheeling = 0;
  1876. VERBOSE (engine, "freewheeling stopped, waiting for thread\n");
  1877. pthread_join (engine->freewheel_thread, &ftstatus);
  1878. VERBOSE (engine, "freewheel thread has returned\n");
  1879. /* tell everyone we've stopped */
  1880. event.type = StopFreewheel;
  1881. jack_deliver_event_to_all (engine, &event);
  1882. /* restart the driver */
  1883. if (engine->driver->start (engine->driver)) {
  1884. jack_error ("could not restart driver after freewheeling");
  1885. return -1;
  1886. }
  1887. return 0;
  1888. }
  1889. static int
  1890. jack_run_one_cycle (jack_engine_t *engine, jack_nframes_t nframes,
  1891. float delayed_usecs)
  1892. {
  1893. jack_driver_t* driver = engine->driver;
  1894. int ret = -1;
  1895. static int consecutive_excessive_delays = 0;
  1896. #define WORK_SCALE 1.0f
  1897. if (engine->control->real_time &&
  1898. engine->spare_usecs &&
  1899. ((WORK_SCALE * engine->spare_usecs) <= delayed_usecs)) {
  1900. fprintf (stderr, "delay of %.3f usecs exceeds estimated spare"
  1901. " time of %.3f; restart ...\n",
  1902. delayed_usecs, WORK_SCALE * engine->spare_usecs);
  1903. if (++consecutive_excessive_delays > 10) {
  1904. jack_error ("too many consecutive interrupt delays "
  1905. "... engine pausing");
  1906. return -1; /* will exit the thread loop */
  1907. }
  1908. jack_engine_notify_clients_about_delay (engine);
  1909. return 0;
  1910. } else {
  1911. consecutive_excessive_delays = 0;
  1912. }
  1913. if (jack_try_lock_graph (engine)) {
  1914. /* engine can't run. just throw away an entire cycle */
  1915. driver->null_cycle (driver, nframes);
  1916. return 0;
  1917. }
  1918. if (!engine->freewheeling) {
  1919. DEBUG("waiting for driver read\n");
  1920. if (driver->read (driver, nframes)) {
  1921. goto unlock;
  1922. }
  1923. }
  1924. DEBUG("run process\n");
  1925. if (jack_engine_process (engine, nframes) == 0) {
  1926. if (!engine->freewheeling) {
  1927. if (driver->write (driver, nframes)) {
  1928. goto unlock;
  1929. }
  1930. }
  1931. } else {
  1932. JSList *node;
  1933. DEBUG ("engine process cycle failed");
  1934. /* we are already late, or something else went wrong,
  1935. so it can't hurt to check the existence of all
  1936. clients.
  1937. */
  1938. for (node = engine->clients; node;
  1939. node = jack_slist_next (node)) {
  1940. jack_client_internal_t *client =
  1941. (jack_client_internal_t *) node->data;
  1942. if (client->control->type == ClientExternal) {
  1943. if (kill (client->control->pid, 0)) {
  1944. VERBOSE(engine,
  1945. "client %s has died/exited\n",
  1946. client->control->name);
  1947. client->error++;
  1948. }
  1949. }
  1950. DEBUG ("client %s errors = %d", client->control->name,
  1951. client->error);
  1952. }
  1953. }
  1954. jack_engine_post_process (engine);
  1955. jack_inc_frame_time (engine, nframes);
  1956. ret = 0;
  1957. unlock:
  1958. jack_unlock_graph (engine);
  1959. DEBUG("cycle finished, status = %d", ret);
  1960. return ret;
  1961. }
  1962. static void
  1963. jack_engine_driver_exit (jack_engine_t* engine)
  1964. {
  1965. /* tell anyone waiting that the driver exited. */
  1966. kill (engine->wait_pid, SIGUSR2);
  1967. engine->driver = NULL;
  1968. }
  1969. static int
  1970. jack_run_cycle (jack_engine_t *engine, jack_nframes_t nframes,
  1971. float delayed_usecs)
  1972. {
  1973. jack_nframes_t left;
  1974. jack_nframes_t b_size = engine->control->buffer_size;
  1975. if (engine->verbose) {
  1976. if (nframes != b_size) {
  1977. VERBOSE (engine,
  1978. "late driver wakeup: nframes to process = %"
  1979. PRIu32 ".\n", nframes);
  1980. }
  1981. }
  1982. /* run as many cycles as it takes to consume nframes */
  1983. for (left = nframes; left >= b_size; left -= b_size) {
  1984. if (jack_run_one_cycle (engine, b_size, delayed_usecs)) {
  1985. jack_error ("cycle execution failure, exiting");
  1986. return EIO;
  1987. }
  1988. }
  1989. return 0;
  1990. }
  1991. void
  1992. jack_engine_delete (jack_engine_t *engine)
  1993. {
  1994. int i;
  1995. if (engine == NULL)
  1996. return;
  1997. engine->control->engine_ok = 0; /* tell clients we're going away */
  1998. if (engine->driver) {
  1999. jack_driver_t* driver = engine->driver;
  2000. VERBOSE (engine, "stopping driver\n");
  2001. driver->stop (driver);
  2002. VERBOSE (engine, "detaching driver\n");
  2003. driver->detach (driver, engine);
  2004. VERBOSE (engine, "unloading driver\n");
  2005. jack_driver_unload (driver);
  2006. engine->driver = NULL;
  2007. }
  2008. VERBOSE (engine, "freeing shared port segments\n");
  2009. for (i = 0; i < engine->control->n_port_types; ++i) {
  2010. jack_release_shm (&engine->port_segment[i]);
  2011. jack_destroy_shm (&engine->port_segment[i]);
  2012. }
  2013. /* stop the other engine threads */
  2014. VERBOSE (engine, "stopping server thread\n");
  2015. pthread_cancel (engine->server_thread);
  2016. pthread_join (engine->server_thread, NULL);
  2017. #ifndef JACK_USE_MACH_THREADS
  2018. /* Cancel the watchdog thread and wait for it to terminate.
  2019. *
  2020. * The watchdog thread is not used on MacOSX since CoreAudio
  2021. * drivers already contain a similar mechanism.
  2022. */
  2023. if (engine->control->real_time) {
  2024. VERBOSE (engine, "stopping watchdog thread\n");
  2025. pthread_cancel (engine->watchdog_thread);
  2026. pthread_join (engine->watchdog_thread, NULL);
  2027. }
  2028. #endif
  2029. /* free engine control shm segment */
  2030. engine->control = NULL;
  2031. VERBOSE (engine, "freeing engine shared memory\n");
  2032. jack_release_shm (&engine->control_shm);
  2033. jack_destroy_shm (&engine->control_shm);
  2034. VERBOSE (engine, "max usecs: %.3f, ", engine->max_usecs);
  2035. VERBOSE (engine, "engine deleted\n");
  2036. free (engine);
  2037. }
  2038. /* Set up the engine's client internal and control structures for both
  2039. * internal and external clients. */
  2040. static jack_client_internal_t *
  2041. jack_setup_client_control (jack_engine_t *engine, int fd,
  2042. jack_client_connect_request_t *req)
  2043. {
  2044. jack_client_internal_t *client;
  2045. client = (jack_client_internal_t *)
  2046. malloc (sizeof (jack_client_internal_t));
  2047. client->request_fd = fd;
  2048. client->event_fd = -1;
  2049. client->ports = 0;
  2050. client->fed_by = 0;
  2051. client->execution_order = UINT_MAX;
  2052. client->next_client = NULL;
  2053. client->handle = NULL;
  2054. client->finish = NULL;
  2055. client->error = 0;
  2056. if (req->type != ClientExternal) {
  2057. client->control = (jack_client_control_t *)
  2058. malloc (sizeof (jack_client_control_t));
  2059. } else {
  2060. char shm_name[PATH_MAX+1];
  2061. snprintf (shm_name, sizeof (shm_name), "/jack-c-%s", req->name);
  2062. if (jack_shmalloc (shm_name,
  2063. sizeof (jack_client_control_t),
  2064. &client->control_shm)) {
  2065. jack_error ("cannot create client control block for %s",
  2066. req->name);
  2067. free (client);
  2068. return 0;
  2069. }
  2070. if (jack_attach_shm (&client->control_shm)) {
  2071. jack_error ("cannot attach to client control block "
  2072. "for %s (%s)", req->name, strerror (errno));
  2073. jack_destroy_shm (&client->control_shm);
  2074. free (client);
  2075. return 0;
  2076. }
  2077. client->control = (jack_client_control_t *)
  2078. jack_shm_addr (&client->control_shm);
  2079. }
  2080. client->control->type = req->type;
  2081. client->control->active = 0;
  2082. client->control->dead = FALSE;
  2083. client->control->timed_out = 0;
  2084. client->control->id = engine->next_client_id++;
  2085. strcpy ((char *) client->control->name, req->name);
  2086. client->subgraph_start_fd = -1;
  2087. client->subgraph_wait_fd = -1;
  2088. client->control->process = NULL;
  2089. client->control->process_arg = NULL;
  2090. client->control->bufsize = NULL;
  2091. client->control->bufsize_arg = NULL;
  2092. client->control->srate = NULL;
  2093. client->control->srate_arg = NULL;
  2094. client->control->xrun = NULL;
  2095. client->control->xrun_arg = NULL;
  2096. client->control->port_register = NULL;
  2097. client->control->port_register_arg = NULL;
  2098. client->control->graph_order = NULL;
  2099. client->control->graph_order_arg = NULL;
  2100. jack_transport_client_new (client);
  2101. #ifdef JACK_USE_MACH_THREADS
  2102. /* specific resources for server/client real-time thread
  2103. * communication */
  2104. allocate_mach_serverport(engine, client);
  2105. client->running = FALSE;
  2106. #endif
  2107. if (req->type == ClientInternal) {
  2108. if (jack_load_client (engine, client, req->object_path)) {
  2109. jack_error ("cannot dynamically load client from"
  2110. " \"%s\"", req->object_path);
  2111. jack_client_delete (engine, client);
  2112. return NULL;
  2113. }
  2114. }
  2115. return client;
  2116. }
  2117. static void
  2118. jack_port_clear_connections (jack_engine_t *engine,
  2119. jack_port_internal_t *port)
  2120. {
  2121. JSList *node, *next;
  2122. for (node = port->connections; node; ) {
  2123. next = jack_slist_next (node);
  2124. jack_port_disconnect_internal (
  2125. engine, ((jack_connection_internal_t *)
  2126. node->data)->source,
  2127. ((jack_connection_internal_t *)
  2128. node->data)->destination,
  2129. FALSE);
  2130. node = next;
  2131. }
  2132. jack_slist_free (port->connections);
  2133. port->connections = 0;
  2134. }
  2135. static void
  2136. jack_zombify_client (jack_engine_t *engine, jack_client_internal_t *client)
  2137. {
  2138. VERBOSE (engine, "removing client \"%s\" from the processing chain\n",
  2139. client->control->name);
  2140. /* caller must hold the client_lock */
  2141. /* this stops jack_deliver_event() from doing anything */
  2142. client->control->dead = TRUE;
  2143. jack_client_disconnect (engine, client);
  2144. jack_client_do_deactivate (engine, client, FALSE);
  2145. }
  2146. static void
  2147. jack_remove_client (jack_engine_t *engine, jack_client_internal_t *client)
  2148. {
  2149. unsigned int i;
  2150. JSList *node;
  2151. /* caller must hold the client_lock */
  2152. VERBOSE (engine, "removing client \"%s\"\n", client->control->name);
  2153. /* if its not already a zombie, make it so */
  2154. if (!client->control->dead) {
  2155. jack_zombify_client (engine, client);
  2156. }
  2157. /* try to force the server thread to return from poll */
  2158. /* JOQ: why do this for internal clients? */
  2159. close (client->event_fd);
  2160. close (client->request_fd);
  2161. if (client->control->type == ClientExternal) {
  2162. /* rearrange the pollfd array so that things work right the
  2163. next time we go into poll(2).
  2164. */
  2165. for (i = 0; i < engine->pfd_max; i++) {
  2166. if (engine->pfd[i].fd == client->request_fd) {
  2167. if (i+1 < engine->pfd_max) {
  2168. memmove (&engine->pfd[i],
  2169. &engine->pfd[i+1],
  2170. sizeof (struct pollfd)
  2171. * (engine->pfd_max - i));
  2172. }
  2173. engine->pfd_max--;
  2174. }
  2175. }
  2176. }
  2177. for (node = engine->clients; node; node = jack_slist_next (node)) {
  2178. if (((jack_client_internal_t *) node->data)->control->id
  2179. == client->control->id) {
  2180. engine->clients =
  2181. jack_slist_remove_link (engine->clients, node);
  2182. jack_slist_free_1 (node);
  2183. break;
  2184. }
  2185. }
  2186. jack_client_delete (engine, client);
  2187. /* ignore the driver, which counts as a client. */
  2188. if (engine->temporary && (jack_slist_length(engine->clients) <= 1)) {
  2189. exit(0);
  2190. }
  2191. }
  2192. static void
  2193. jack_client_delete (jack_engine_t *engine, jack_client_internal_t *client)
  2194. {
  2195. if (jack_client_is_internal (client)) {
  2196. jack_client_unload (client);
  2197. free (client->control->private_client);
  2198. free ((void *) client->control);
  2199. } else {
  2200. /* release the client segment, mark it for
  2201. destruction, and free up the shm registry
  2202. information so that it can be reused.
  2203. */
  2204. jack_release_shm (&client->control_shm);
  2205. jack_destroy_shm (&client->control_shm);
  2206. }
  2207. free (client);
  2208. }
  2209. jack_client_internal_t *
  2210. jack_client_by_name (jack_engine_t *engine, const char *name)
  2211. {
  2212. jack_client_internal_t *client = NULL;
  2213. JSList *node;
  2214. jack_lock_graph (engine);
  2215. for (node = engine->clients; node; node = jack_slist_next (node)) {
  2216. if (strcmp ((const char *) ((jack_client_internal_t *)
  2217. node->data)->control->name,
  2218. name) == 0) {
  2219. client = (jack_client_internal_t *) node->data;
  2220. break;
  2221. }
  2222. }
  2223. jack_unlock_graph (engine);
  2224. return client;
  2225. }
  2226. jack_client_internal_t *
  2227. jack_client_internal_by_id (jack_engine_t *engine, jack_client_id_t id)
  2228. {
  2229. jack_client_internal_t *client = NULL;
  2230. JSList *node;
  2231. /* call tree ***MUST HOLD*** the graph lock */
  2232. for (node = engine->clients; node; node = jack_slist_next (node)) {
  2233. if (((jack_client_internal_t *) node->data)->control->id
  2234. == id) {
  2235. client = (jack_client_internal_t *) node->data;
  2236. break;
  2237. }
  2238. }
  2239. return client;
  2240. }
  2241. static void
  2242. jack_deliver_event_to_all (jack_engine_t *engine, jack_event_t *event)
  2243. {
  2244. JSList *node;
  2245. jack_lock_graph (engine);
  2246. for (node = engine->clients; node; node = jack_slist_next (node)) {
  2247. jack_deliver_event (engine,
  2248. (jack_client_internal_t *) node->data,
  2249. event);
  2250. }
  2251. jack_unlock_graph (engine);
  2252. }
  2253. static int
  2254. jack_deliver_event (jack_engine_t *engine, jack_client_internal_t *client,
  2255. jack_event_t *event)
  2256. {
  2257. char status;
  2258. /* caller must hold the graph lock */
  2259. DEBUG ("delivering event (type %d)", event->type);
  2260. /* we are not RT-constrained here, so use kill(2) to beef up
  2261. our check on a client's continued well-being
  2262. */
  2263. if (client->control->dead ||
  2264. (client->control->type == ClientExternal && kill (client->control->pid, 0))) {
  2265. DEBUG ("client %s is dead - no event sent", client->control->name);
  2266. return 0;
  2267. }
  2268. DEBUG ("client %s is still alive", client->control->name);
  2269. if (jack_client_is_internal (client)) {
  2270. switch (event->type) {
  2271. case PortConnected:
  2272. case PortDisconnected:
  2273. jack_client_handle_port_connection
  2274. (client->control->private_client, event);
  2275. break;
  2276. case BufferSizeChange:
  2277. jack_client_invalidate_port_buffers
  2278. (client->control->private_client);
  2279. if (client->control->bufsize) {
  2280. client->control->bufsize
  2281. (event->x.n,
  2282. client->control->bufsize_arg);
  2283. }
  2284. break;
  2285. case SampleRateChange:
  2286. if (client->control->srate) {
  2287. client->control->srate
  2288. (event->x.n,
  2289. client->control->srate_arg);
  2290. }
  2291. break;
  2292. case GraphReordered:
  2293. if (client->control->graph_order) {
  2294. client->control->graph_order
  2295. (client->control->graph_order_arg);
  2296. }
  2297. break;
  2298. case XRun:
  2299. if (client->control->xrun) {
  2300. client->control->xrun
  2301. (client->control->xrun_arg);
  2302. }
  2303. break;
  2304. default:
  2305. /* internal clients don't need to know */
  2306. break;
  2307. }
  2308. } else {
  2309. if (client->control->active) {
  2310. /* there's a thread waiting for events, so
  2311. * it's worth telling the client */
  2312. DEBUG ("engine writing on event fd");
  2313. if (write (client->event_fd, event, sizeof (*event))
  2314. != sizeof (*event)) {
  2315. jack_error ("cannot send event to client [%s]"
  2316. " (%s)", client->control->name,
  2317. strerror (errno));
  2318. client->error++;
  2319. }
  2320. DEBUG ("engine reading from event fd");
  2321. if (!client->error &&
  2322. (read (client->event_fd, &status, sizeof (status))
  2323. != sizeof (status))) {
  2324. jack_error ("cannot read event response from "
  2325. "client [%s] (%s)",
  2326. client->control->name,
  2327. strerror (errno));
  2328. client->error++;
  2329. }
  2330. if (status != 0) {
  2331. jack_error ("bad status for client event "
  2332. "handling (type = %d)",
  2333. event->type);
  2334. client->error++;
  2335. }
  2336. }
  2337. }
  2338. DEBUG ("event delivered");
  2339. return 0;
  2340. }
  2341. int
  2342. jack_rechain_graph (jack_engine_t *engine)
  2343. {
  2344. JSList *node, *next;
  2345. unsigned long n;
  2346. int err = 0;
  2347. jack_client_internal_t *client, *subgraph_client, *next_client;
  2348. jack_event_t event;
  2349. int upstream_is_jackd;
  2350. jack_clear_fifos (engine);
  2351. subgraph_client = 0;
  2352. VERBOSE(engine, "++ jack_rechain_graph():\n");
  2353. event.type = GraphReordered;
  2354. for (n = 0, node = engine->clients, next = NULL; node; node = next) {
  2355. next = jack_slist_next (node);
  2356. if (((jack_client_internal_t *) node->data)->control->active) {
  2357. client = (jack_client_internal_t *) node->data;
  2358. /* find the next active client. its ok for
  2359. * this to be NULL */
  2360. while (next) {
  2361. if (((jack_client_internal_t *)
  2362. next->data)->control->active) {
  2363. break;
  2364. }
  2365. next = jack_slist_next (next);
  2366. };
  2367. if (next == NULL) {
  2368. next_client = NULL;
  2369. } else {
  2370. next_client = (jack_client_internal_t *)
  2371. next->data;
  2372. }
  2373. client->execution_order = n;
  2374. client->next_client = next_client;
  2375. if (jack_client_is_internal (client)) {
  2376. /* break the chain for the current
  2377. * subgraph. the server will wait for
  2378. * chain on the nth FIFO, and will
  2379. * then execute this internal
  2380. * client. */
  2381. if (subgraph_client) {
  2382. subgraph_client->subgraph_wait_fd =
  2383. jack_get_fifo_fd (engine, n);
  2384. VERBOSE (engine, "client %s: wait_fd="
  2385. "%d, execution_order="
  2386. "%lu.\n",
  2387. subgraph_client->
  2388. control->name,
  2389. subgraph_client->
  2390. subgraph_wait_fd, n);
  2391. n++;
  2392. }
  2393. VERBOSE (engine, "client %s: internal "
  2394. "client, execution_order="
  2395. "%lu.\n",
  2396. client->control->name, n);
  2397. /* this does the right thing for
  2398. * internal clients too
  2399. */
  2400. jack_deliver_event (engine, client, &event);
  2401. subgraph_client = 0;
  2402. } else {
  2403. if (subgraph_client == NULL) {
  2404. /* start a new subgraph. the
  2405. * engine will start the chain
  2406. * by writing to the nth
  2407. * FIFO.
  2408. */
  2409. subgraph_client = client;
  2410. subgraph_client->subgraph_start_fd =
  2411. jack_get_fifo_fd (engine, n);
  2412. VERBOSE (engine, "client %s: "
  2413. "start_fd=%d, execution"
  2414. "_order=%lu.\n",
  2415. subgraph_client->
  2416. control->name,
  2417. subgraph_client->
  2418. subgraph_start_fd, n);
  2419. /* this external client after this will have
  2420. jackd as its upstream connection.
  2421. */
  2422. upstream_is_jackd = 1;
  2423. }
  2424. else {
  2425. VERBOSE (engine, "client %s: in"
  2426. " subgraph after %s, "
  2427. "execution_order="
  2428. "%lu.\n",
  2429. client->control->name,
  2430. subgraph_client->
  2431. control->name, n);
  2432. subgraph_client->subgraph_wait_fd = -1;
  2433. /* this external client after this will have
  2434. another client as its upstream connection.
  2435. */
  2436. upstream_is_jackd = 0;
  2437. }
  2438. /* make sure fifo for 'n + 1' exists
  2439. * before issuing client reorder
  2440. */
  2441. (void) jack_get_fifo_fd(
  2442. engine, client->execution_order + 1);
  2443. event.x.n = client->execution_order;
  2444. event.y.n = upstream_is_jackd;
  2445. jack_deliver_event (engine, client, &event);
  2446. n++;
  2447. }
  2448. }
  2449. }
  2450. if (subgraph_client) {
  2451. subgraph_client->subgraph_wait_fd =
  2452. jack_get_fifo_fd (engine, n);
  2453. VERBOSE (engine, "client %s: wait_fd=%d, "
  2454. "execution_order=%lu (last client).\n",
  2455. subgraph_client->control->name,
  2456. subgraph_client->subgraph_wait_fd, n);
  2457. }
  2458. VERBOSE (engine, "-- jack_rechain_graph()\n");
  2459. return err;
  2460. }
  2461. static void
  2462. jack_trace_terminal (jack_client_internal_t *c1, jack_client_internal_t *rbase)
  2463. {
  2464. jack_client_internal_t *c2;
  2465. /* make a copy of the existing list of routes that feed
  2466. c1. this provides us with an atomic snapshot of c1's
  2467. "fed-by" state, which will be modified as we progress ...
  2468. */
  2469. JSList *existing;
  2470. JSList *node;
  2471. if (c1->fed_by == NULL) {
  2472. return;
  2473. }
  2474. existing = jack_slist_copy (c1->fed_by);
  2475. /* for each route that feeds c1, recurse, marking it as
  2476. feeding rbase as well.
  2477. */
  2478. for (node = existing; node; node = jack_slist_next (node)) {
  2479. c2 = (jack_client_internal_t *) node->data;
  2480. /* c2 is a route that feeds c1 which somehow feeds
  2481. base. mark base as being fed by c2, but don't do it
  2482. more than once.
  2483. */
  2484. if (c2 != rbase && c2 != c1) {
  2485. if (jack_slist_find (rbase->fed_by, c2) == NULL) {
  2486. rbase->fed_by =
  2487. jack_slist_prepend (rbase->fed_by, c2);
  2488. }
  2489. /* FIXME: if c2->fed_by is not up-to-date, we
  2490. may end up recursing infinitely
  2491. (kaiv)
  2492. */
  2493. if (jack_slist_find (c2->fed_by, c1) == NULL) {
  2494. /* now recurse, so that we can mark
  2495. base as being fed by all routes
  2496. that feed c2
  2497. */
  2498. jack_trace_terminal (c2, rbase);
  2499. }
  2500. }
  2501. }
  2502. jack_slist_free (existing);
  2503. }
  2504. static int
  2505. jack_client_sort (jack_client_internal_t *a, jack_client_internal_t *b)
  2506. {
  2507. if (jack_slist_find (a->fed_by, b)) {
  2508. if (jack_slist_find (b->fed_by, a)) {
  2509. /* feedback loop: if `a' is the driver
  2510. client, let that execute first.
  2511. */
  2512. if (a->control->type == ClientDriver) {
  2513. /* b comes after a */
  2514. return -1;
  2515. }
  2516. }
  2517. /* a comes after b */
  2518. return 1;
  2519. } else if (jack_slist_find (b->fed_by, a)) {
  2520. if (jack_slist_find (a->fed_by, b)) {
  2521. /* feedback loop: if `b' is the driver
  2522. client, let that execute first.
  2523. */
  2524. if (b->control->type == ClientDriver) {
  2525. /* b comes before a */
  2526. return 1;
  2527. }
  2528. }
  2529. /* b comes after a */
  2530. return -1;
  2531. } else {
  2532. /* we don't care */
  2533. return 0;
  2534. }
  2535. }
  2536. static int
  2537. jack_client_feeds (jack_client_internal_t *might,
  2538. jack_client_internal_t *target)
  2539. {
  2540. JSList *pnode, *cnode;
  2541. /* Check every port of `might' for an outbound connection to
  2542. * `target' */
  2543. for (pnode = might->ports; pnode; pnode = jack_slist_next (pnode)) {
  2544. jack_port_internal_t *port;
  2545. port = (jack_port_internal_t *) pnode->data;
  2546. for (cnode = port->connections; cnode;
  2547. cnode = jack_slist_next (cnode)) {
  2548. jack_connection_internal_t *c;
  2549. c = (jack_connection_internal_t *) cnode->data;
  2550. if (c->source->shared->client_id
  2551. == might->control->id &&
  2552. c->destination->shared->client_id
  2553. == target->control->id) {
  2554. return 1;
  2555. }
  2556. }
  2557. }
  2558. return 0;
  2559. }
  2560. static jack_nframes_t
  2561. jack_get_port_total_latency (jack_engine_t *engine,
  2562. jack_port_internal_t *port, int hop_count,
  2563. int toward_port)
  2564. {
  2565. JSList *node;
  2566. jack_nframes_t latency;
  2567. jack_nframes_t max_latency = 0;
  2568. /* call tree must hold engine->client_lock. */
  2569. latency = port->shared->latency;
  2570. /* we don't prevent cyclic graphs, so we have to do something
  2571. to bottom out in the event that they are created.
  2572. */
  2573. if (hop_count > 8) {
  2574. return latency;
  2575. }
  2576. for (node = port->connections; node; node = jack_slist_next (node)) {
  2577. jack_nframes_t this_latency;
  2578. jack_connection_internal_t *connection;
  2579. connection = (jack_connection_internal_t *) node->data;
  2580. if ((toward_port &&
  2581. (connection->source->shared == port->shared)) ||
  2582. (!toward_port &&
  2583. (connection->destination->shared == port->shared))) {
  2584. continue;
  2585. }
  2586. /* if we're a destination in the connection, recurse
  2587. on the source to get its total latency
  2588. */
  2589. if (connection->destination == port) {
  2590. if (connection->source->shared->flags
  2591. & JackPortIsTerminal) {
  2592. this_latency = connection->source->
  2593. shared->latency;
  2594. } else {
  2595. this_latency =
  2596. jack_get_port_total_latency (
  2597. engine, connection->source,
  2598. hop_count + 1,
  2599. toward_port);
  2600. }
  2601. } else {
  2602. /* "port" is the source, so get the latency of
  2603. * the destination */
  2604. if (connection->destination->shared->flags
  2605. & JackPortIsTerminal) {
  2606. this_latency = connection->destination->
  2607. shared->latency;
  2608. } else {
  2609. this_latency =
  2610. jack_get_port_total_latency (
  2611. engine,
  2612. connection->destination,
  2613. hop_count + 1,
  2614. toward_port);
  2615. }
  2616. }
  2617. if (this_latency > max_latency) {
  2618. max_latency = this_latency;
  2619. }
  2620. }
  2621. return latency + max_latency;
  2622. }
  2623. static void
  2624. jack_compute_all_port_total_latencies (jack_engine_t *engine)
  2625. {
  2626. jack_port_shared_t *shared = engine->control->ports;
  2627. unsigned int i;
  2628. int toward_port;
  2629. for (i = 0; i < engine->control->port_max; i++) {
  2630. if (shared[i].in_use) {
  2631. if (shared[i].flags & JackPortIsOutput) {
  2632. toward_port = FALSE;
  2633. } else {
  2634. toward_port = TRUE;
  2635. }
  2636. shared[i].total_latency =
  2637. jack_get_port_total_latency (
  2638. engine, &engine->internal_ports[i],
  2639. 0, toward_port);
  2640. }
  2641. }
  2642. }
  2643. /**
  2644. * Sorts the network of clients using the following
  2645. * algorithm:
  2646. *
  2647. * 1) figure out who is connected to whom:
  2648. *
  2649. * foreach client1
  2650. * foreach input port
  2651. * foreach client2
  2652. * foreach output port
  2653. * if client1->input port connected to client2->output port
  2654. * mark client1 fed by client 2
  2655. *
  2656. * 2) trace the connections as terminal arcs in the graph so that
  2657. * if client A feeds client B who feeds client C, mark client C
  2658. * as fed by client A as well as client B, and so forth.
  2659. *
  2660. * 3) now sort according to whether or not client1->fed_by (client2) is true.
  2661. * if the condition is true, client2 must execute before client1
  2662. *
  2663. */
  2664. static void
  2665. jack_sort_graph (jack_engine_t *engine)
  2666. {
  2667. JSList *node, *onode;
  2668. jack_client_internal_t *client;
  2669. jack_client_internal_t *oclient;
  2670. /* called, obviously, must hold engine->client_lock */
  2671. for (node = engine->clients; node; node = jack_slist_next (node)) {
  2672. client = (jack_client_internal_t *) node->data;
  2673. jack_slist_free (client->fed_by);
  2674. client->fed_by = 0;
  2675. for (onode = engine->clients; onode;
  2676. onode = jack_slist_next (onode)) {
  2677. oclient = (jack_client_internal_t *) onode->data;
  2678. if (jack_client_feeds (oclient, client)) {
  2679. client->fed_by =
  2680. jack_slist_prepend (client->fed_by,
  2681. oclient);
  2682. }
  2683. }
  2684. }
  2685. for (node = engine->clients; node; node = jack_slist_next (node)) {
  2686. jack_trace_terminal ((jack_client_internal_t *) node->data,
  2687. (jack_client_internal_t *) node->data);
  2688. }
  2689. engine->clients = jack_slist_sort (engine->clients,
  2690. (JCompareFunc) jack_client_sort);
  2691. jack_compute_all_port_total_latencies (engine);
  2692. jack_rechain_graph (engine);
  2693. }
  2694. /**
  2695. * Dumps current engine configuration to stderr.
  2696. */
  2697. void jack_dump_configuration(jack_engine_t *engine, int take_lock)
  2698. {
  2699. JSList *clientnode, *portnode, *connectionnode;
  2700. jack_client_internal_t *client;
  2701. jack_client_control_t *ctl;
  2702. jack_port_internal_t *port;
  2703. jack_connection_internal_t* connection;
  2704. int n, m, o;
  2705. fprintf(stderr, "engine.c: <-- dump begins -->\n");
  2706. if (take_lock) {
  2707. jack_lock_graph (engine);
  2708. }
  2709. for (n = 0, clientnode = engine->clients; clientnode;
  2710. clientnode = jack_slist_next (clientnode)) {
  2711. client = (jack_client_internal_t *) clientnode->data;
  2712. ctl = client->control;
  2713. fprintf (stderr, "client #%d: %s (type: %d, process? %s, fed"
  2714. " by %d clients) start=%d wait=%d\n",
  2715. ++n,
  2716. ctl->name,
  2717. ctl->type,
  2718. ctl->process ? "yes" : "no",
  2719. jack_slist_length(client->fed_by),
  2720. client->subgraph_start_fd,
  2721. client->subgraph_wait_fd);
  2722. for(m = 0, portnode = client->ports; portnode;
  2723. portnode = jack_slist_next (portnode)) {
  2724. port = (jack_port_internal_t *) portnode->data;
  2725. fprintf(stderr, "\t port #%d: %s\n", ++m,
  2726. port->shared->name);
  2727. for(o = 0, connectionnode = port->connections;
  2728. connectionnode;
  2729. connectionnode =
  2730. jack_slist_next (connectionnode)) {
  2731. connection = (jack_connection_internal_t *)
  2732. connectionnode->data;
  2733. fprintf(stderr, "\t\t connection #%d: %s %s\n",
  2734. ++o,
  2735. (port->shared->flags
  2736. & JackPortIsInput)? "<-": "->",
  2737. (port->shared->flags & JackPortIsInput)?
  2738. connection->source->shared->name:
  2739. connection->destination->shared->name);
  2740. }
  2741. }
  2742. }
  2743. if (take_lock) {
  2744. jack_unlock_graph (engine);
  2745. }
  2746. fprintf(stderr, "engine.c: <-- dump ends -->\n");
  2747. }
  2748. static int
  2749. jack_port_do_connect (jack_engine_t *engine,
  2750. const char *source_port,
  2751. const char *destination_port)
  2752. {
  2753. jack_connection_internal_t *connection;
  2754. jack_port_internal_t *srcport, *dstport;
  2755. jack_port_id_t src_id, dst_id;
  2756. jack_client_internal_t *client;
  2757. JSList *it;
  2758. if ((srcport = jack_get_port_by_name (engine, source_port)) == NULL) {
  2759. jack_error ("unknown source port in attempted connection [%s]",
  2760. source_port);
  2761. return -1;
  2762. }
  2763. if ((dstport = jack_get_port_by_name (engine, destination_port))
  2764. == NULL) {
  2765. jack_error ("unknown destination port in attempted connection"
  2766. " [%s]", destination_port);
  2767. return -1;
  2768. }
  2769. if ((dstport->shared->flags & JackPortIsInput) == 0) {
  2770. jack_error ("destination port in attempted connection of"
  2771. " %s and %s is not an input port",
  2772. source_port, destination_port);
  2773. return -1;
  2774. }
  2775. if ((srcport->shared->flags & JackPortIsOutput) == 0) {
  2776. jack_error ("source port in attempted connection of %s and"
  2777. " %s is not an output port",
  2778. source_port, destination_port);
  2779. return -1;
  2780. }
  2781. if (srcport->shared->locked) {
  2782. jack_error ("source port %s is locked against connection"
  2783. " changes", source_port);
  2784. return -1;
  2785. }
  2786. if (dstport->shared->locked) {
  2787. jack_error ("destination port %s is locked against connection"
  2788. " changes", destination_port);
  2789. return -1;
  2790. }
  2791. if (srcport->shared->ptype_id != dstport->shared->ptype_id) {
  2792. jack_error ("ports used in attemped connection are not of "
  2793. "the same data type");
  2794. return -1;
  2795. }
  2796. if ((client = jack_client_internal_by_id (engine,
  2797. srcport->shared->client_id))
  2798. == 0) {
  2799. jack_error ("unknown client set as owner of port - "
  2800. "cannot connect");
  2801. return -1;
  2802. }
  2803. if (!client->control->active) {
  2804. jack_error ("cannot connect ports owned by inactive clients;"
  2805. " \"%s\" is not active", client->control->name);
  2806. return -1;
  2807. }
  2808. if ((client = jack_client_internal_by_id (engine,
  2809. dstport->shared->client_id))
  2810. == 0) {
  2811. jack_error ("unknown client set as owner of port - cannot "
  2812. "connect");
  2813. return -1;
  2814. }
  2815. if (!client->control->active) {
  2816. jack_error ("cannot connect ports owned by inactive clients;"
  2817. " \"%s\" is not active", client->control->name);
  2818. return -1;
  2819. }
  2820. for (it = srcport->connections; it; it = it->next) {
  2821. if (((jack_connection_internal_t *)it->data)->destination
  2822. == dstport) {
  2823. return EEXIST;
  2824. }
  2825. }
  2826. connection = (jack_connection_internal_t *)
  2827. malloc (sizeof (jack_connection_internal_t));
  2828. connection->source = srcport;
  2829. connection->destination = dstport;
  2830. src_id = srcport->shared->id;
  2831. dst_id = dstport->shared->id;
  2832. jack_lock_graph (engine);
  2833. if (dstport->connections && !dstport->shared->has_mixdown) {
  2834. jack_port_type_info_t *port_type =
  2835. jack_port_type_info (engine, dstport);
  2836. jack_error ("cannot make multiple connections to a port of"
  2837. " type [%s]", port_type->type_name);
  2838. free (connection);
  2839. jack_unlock_graph (engine);
  2840. return -1;
  2841. } else {
  2842. VERBOSE (engine, "connect %s and %s\n",
  2843. srcport->shared->name,
  2844. dstport->shared->name);
  2845. dstport->connections =
  2846. jack_slist_prepend (dstport->connections, connection);
  2847. srcport->connections =
  2848. jack_slist_prepend (srcport->connections, connection);
  2849. jack_sort_graph (engine);
  2850. DEBUG ("actually sorted the graph...");
  2851. jack_send_connection_notification (engine,
  2852. srcport->shared->client_id,
  2853. src_id, dst_id, TRUE);
  2854. jack_send_connection_notification (engine,
  2855. dstport->shared->client_id,
  2856. dst_id, src_id, TRUE);
  2857. }
  2858. jack_unlock_graph (engine);
  2859. return 0;
  2860. }
  2861. int
  2862. jack_port_disconnect_internal (jack_engine_t *engine,
  2863. jack_port_internal_t *srcport,
  2864. jack_port_internal_t *dstport,
  2865. int sort_graph)
  2866. {
  2867. JSList *node;
  2868. jack_connection_internal_t *connect;
  2869. int ret = -1;
  2870. jack_port_id_t src_id, dst_id;
  2871. /* call tree **** MUST HOLD **** engine->client_lock. */
  2872. for (node = srcport->connections; node;
  2873. node = jack_slist_next (node)) {
  2874. connect = (jack_connection_internal_t *) node->data;
  2875. if (connect->source == srcport &&
  2876. connect->destination == dstport) {
  2877. VERBOSE (engine, "DIS-connect %s and %s\n",
  2878. srcport->shared->name,
  2879. dstport->shared->name);
  2880. srcport->connections =
  2881. jack_slist_remove (srcport->connections,
  2882. connect);
  2883. dstport->connections =
  2884. jack_slist_remove (dstport->connections,
  2885. connect);
  2886. src_id = srcport->shared->id;
  2887. dst_id = dstport->shared->id;
  2888. /* this is a bit harsh, but it basically says
  2889. that if we actually do a disconnect, and
  2890. its the last one, then make sure that any
  2891. input monitoring is turned off on the
  2892. srcport. this isn't ideal for all
  2893. situations, but it works better for most of
  2894. them.
  2895. */
  2896. if (srcport->connections == NULL) {
  2897. srcport->shared->monitor_requests = 0;
  2898. }
  2899. jack_send_connection_notification (
  2900. engine, srcport->shared->client_id, src_id,
  2901. dst_id, FALSE);
  2902. jack_send_connection_notification (
  2903. engine, dstport->shared->client_id, dst_id,
  2904. src_id, FALSE);
  2905. free (connect);
  2906. ret = 0;
  2907. break;
  2908. }
  2909. }
  2910. if (sort_graph) {
  2911. jack_sort_graph (engine);
  2912. }
  2913. return ret;
  2914. }
  2915. static int
  2916. jack_port_do_disconnect_all (jack_engine_t *engine,
  2917. jack_port_id_t port_id)
  2918. {
  2919. if (port_id >= engine->control->port_max) {
  2920. jack_error ("illegal port ID in attempted disconnection [%"
  2921. PRIu32 "]", port_id);
  2922. return -1;
  2923. }
  2924. VERBOSE (engine, "clear connections for %s\n",
  2925. engine->internal_ports[port_id].shared->name);
  2926. jack_lock_graph (engine);
  2927. jack_port_clear_connections (engine, &engine->internal_ports[port_id]);
  2928. jack_sort_graph (engine);
  2929. jack_unlock_graph (engine);
  2930. return 0;
  2931. }
  2932. static int
  2933. jack_port_do_disconnect (jack_engine_t *engine,
  2934. const char *source_port,
  2935. const char *destination_port)
  2936. {
  2937. jack_port_internal_t *srcport, *dstport;
  2938. int ret = -1;
  2939. if ((srcport = jack_get_port_by_name (engine, source_port)) == NULL) {
  2940. jack_error ("unknown source port in attempted disconnection"
  2941. " [%s]", source_port);
  2942. return -1;
  2943. }
  2944. if ((dstport = jack_get_port_by_name (engine, destination_port))
  2945. == NULL) {
  2946. jack_error ("unknown destination port in attempted connection"
  2947. " [%s]", destination_port);
  2948. return -1;
  2949. }
  2950. jack_lock_graph (engine);
  2951. ret = jack_port_disconnect_internal (engine, srcport, dstport, TRUE);
  2952. jack_unlock_graph (engine);
  2953. return ret;
  2954. }
  2955. static int
  2956. jack_get_fifo_fd (jack_engine_t *engine, unsigned int which_fifo)
  2957. {
  2958. /* caller must hold client_lock */
  2959. char path[PATH_MAX+1];
  2960. struct stat statbuf;
  2961. snprintf (path, sizeof (path), "%s-%d", engine->fifo_prefix,
  2962. which_fifo);
  2963. DEBUG ("%s", path);
  2964. if (stat (path, &statbuf)) {
  2965. if (errno == ENOENT) {
  2966. if (mkfifo(path, 0666) < 0){
  2967. jack_error ("cannot create inter-client FIFO"
  2968. " [%s] (%s)\n", path,
  2969. strerror (errno));
  2970. return -1;
  2971. }
  2972. } else {
  2973. jack_error ("cannot check on FIFO %d\n", which_fifo);
  2974. return -1;
  2975. }
  2976. } else {
  2977. if (!S_ISFIFO(statbuf.st_mode)) {
  2978. jack_error ("FIFO %d (%s) already exists, but is not"
  2979. " a FIFO!\n", which_fifo, path);
  2980. return -1;
  2981. }
  2982. }
  2983. if (which_fifo >= engine->fifo_size) {
  2984. unsigned int i;
  2985. engine->fifo = (int *)
  2986. realloc (engine->fifo,
  2987. sizeof (int) * engine->fifo_size + 16);
  2988. for (i = engine->fifo_size; i < engine->fifo_size + 16; i++) {
  2989. engine->fifo[i] = -1;
  2990. }
  2991. engine->fifo_size += 16;
  2992. }
  2993. if (engine->fifo[which_fifo] < 0) {
  2994. if ((engine->fifo[which_fifo] =
  2995. open (path, O_RDWR|O_CREAT|O_NONBLOCK, 0666)) < 0) {
  2996. jack_error ("cannot open fifo [%s] (%s)", path,
  2997. strerror (errno));
  2998. return -1;
  2999. }
  3000. DEBUG ("opened engine->fifo[%d] == %d (%s)",
  3001. which_fifo, engine->fifo[which_fifo], path);
  3002. }
  3003. return engine->fifo[which_fifo];
  3004. }
  3005. static void
  3006. jack_clear_fifos (jack_engine_t *engine)
  3007. {
  3008. /* caller must hold client_lock */
  3009. unsigned int i;
  3010. char buf[16];
  3011. /* this just drains the existing FIFO's of any data left in
  3012. them by aborted clients, etc. there is only ever going to
  3013. be 0, 1 or 2 bytes in them, but we'll allow for up to 16.
  3014. */
  3015. for (i = 0; i < engine->fifo_size; i++) {
  3016. if (engine->fifo[i] >= 0) {
  3017. int nread = read (engine->fifo[i], buf, sizeof (buf));
  3018. if (nread < 0 && errno != EAGAIN) {
  3019. jack_error ("clear fifo[%d] error: %s",
  3020. i, strerror (errno));
  3021. }
  3022. }
  3023. }
  3024. }
  3025. static int
  3026. jack_use_driver (jack_engine_t *engine, jack_driver_t *driver)
  3027. {
  3028. if (engine->driver) {
  3029. engine->driver->detach (engine->driver, engine);
  3030. engine->driver = 0;
  3031. }
  3032. if (driver) {
  3033. if (driver->attach (driver, engine))
  3034. return -1;
  3035. engine->rolling_interval =
  3036. jack_rolling_interval (driver->period_usecs);
  3037. }
  3038. engine->driver = driver;
  3039. return 0;
  3040. }
  3041. /* PORT RELATED FUNCTIONS */
  3042. static jack_port_id_t
  3043. jack_get_free_port (jack_engine_t *engine)
  3044. {
  3045. jack_port_id_t i;
  3046. pthread_mutex_lock (&engine->port_lock);
  3047. for (i = 0; i < engine->port_max; i++) {
  3048. if (engine->control->ports[i].in_use == 0) {
  3049. engine->control->ports[i].in_use = 1;
  3050. break;
  3051. }
  3052. }
  3053. pthread_mutex_unlock (&engine->port_lock);
  3054. if (i == engine->port_max) {
  3055. return (jack_port_id_t) -1;
  3056. }
  3057. return i;
  3058. }
  3059. static void
  3060. jack_port_release (jack_engine_t *engine, jack_port_internal_t *port)
  3061. {
  3062. pthread_mutex_lock (&engine->port_lock);
  3063. port->shared->in_use = 0;
  3064. if (port->buffer_info) {
  3065. jack_port_buffer_list_t *blist =
  3066. jack_port_buffer_list (engine, port);
  3067. pthread_mutex_lock (&blist->lock);
  3068. blist->freelist =
  3069. jack_slist_prepend (blist->freelist,
  3070. port->buffer_info);
  3071. port->buffer_info = NULL;
  3072. pthread_mutex_unlock (&blist->lock);
  3073. }
  3074. pthread_mutex_unlock (&engine->port_lock);
  3075. }
  3076. jack_port_internal_t *
  3077. jack_get_port_internal_by_name (jack_engine_t *engine, const char *name)
  3078. {
  3079. jack_port_id_t id;
  3080. pthread_mutex_lock (&engine->port_lock);
  3081. for (id = 0; id < engine->port_max; id++) {
  3082. if (strcmp (engine->control->ports[id].name, name) == 0) {
  3083. break;
  3084. }
  3085. }
  3086. pthread_mutex_unlock (&engine->port_lock);
  3087. if (id != engine->port_max) {
  3088. return &engine->internal_ports[id];
  3089. } else {
  3090. return NULL;
  3091. }
  3092. }
  3093. int
  3094. jack_port_do_register (jack_engine_t *engine, jack_request_t *req)
  3095. {
  3096. jack_port_id_t port_id;
  3097. jack_port_shared_t *shared;
  3098. jack_port_internal_t *port;
  3099. jack_client_internal_t *client;
  3100. unsigned long i;
  3101. for (i = 0; i < engine->control->n_port_types; ++i) {
  3102. if (strcmp (req->x.port_info.type,
  3103. engine->control->port_types[i].type_name) == 0) {
  3104. break;
  3105. }
  3106. }
  3107. if (i == engine->control->n_port_types) {
  3108. jack_error ("cannot register a port of type \"%s\"",
  3109. req->x.port_info.type);
  3110. return -1;
  3111. }
  3112. jack_lock_graph (engine);
  3113. if ((client = jack_client_internal_by_id (engine,
  3114. req->x.port_info.client_id))
  3115. == NULL) {
  3116. jack_error ("unknown client id in port registration request");
  3117. return -1;
  3118. }
  3119. jack_unlock_graph (engine);
  3120. if ((port_id = jack_get_free_port (engine)) == (jack_port_id_t) -1) {
  3121. jack_error ("no ports available!");
  3122. return -1;
  3123. }
  3124. shared = &engine->control->ports[port_id];
  3125. strcpy (shared->name, req->x.port_info.name);
  3126. shared->ptype_id = engine->control->port_types[i].ptype_id;
  3127. shared->client_id = req->x.port_info.client_id;
  3128. shared->flags = req->x.port_info.flags;
  3129. shared->latency = 0;
  3130. shared->monitor_requests = 0;
  3131. shared->locked = 0;
  3132. port = &engine->internal_ports[port_id];
  3133. port->shared = shared;
  3134. port->connections = 0;
  3135. port->buffer_info = NULL;
  3136. if (jack_port_assign_buffer (engine, port)) {
  3137. jack_error ("cannot assign buffer for port");
  3138. return -1;
  3139. }
  3140. jack_lock_graph (engine);
  3141. client->ports = jack_slist_prepend (client->ports, port);
  3142. jack_port_registration_notify (engine, port_id, TRUE);
  3143. jack_unlock_graph (engine);
  3144. VERBOSE (engine, "registered port %s, offset = %u\n",
  3145. shared->name, (unsigned int)shared->offset);
  3146. req->x.port_info.port_id = port_id;
  3147. return 0;
  3148. }
  3149. int
  3150. jack_port_do_unregister (jack_engine_t *engine, jack_request_t *req)
  3151. {
  3152. jack_client_internal_t *client;
  3153. jack_port_shared_t *shared;
  3154. jack_port_internal_t *port;
  3155. if (req->x.port_info.port_id < 0 ||
  3156. req->x.port_info.port_id > engine->port_max) {
  3157. jack_error ("invalid port ID %" PRIu32
  3158. " in unregister request",
  3159. req->x.port_info.port_id);
  3160. return -1;
  3161. }
  3162. shared = &engine->control->ports[req->x.port_info.port_id];
  3163. if (shared->client_id != req->x.port_info.client_id) {
  3164. jack_error ("Client %" PRIu32
  3165. " is not allowed to remove port %s",
  3166. req->x.port_info.client_id, shared->name);
  3167. return -1;
  3168. }
  3169. jack_lock_graph (engine);
  3170. if ((client = jack_client_internal_by_id (engine, shared->client_id))
  3171. == NULL) {
  3172. jack_error ("unknown client id in port registration request");
  3173. jack_unlock_graph (engine);
  3174. return -1;
  3175. }
  3176. port = &engine->internal_ports[req->x.port_info.port_id];
  3177. jack_port_clear_connections (engine, port);
  3178. jack_port_release (engine,
  3179. &engine->internal_ports[req->x.port_info.port_id]);
  3180. client->ports = jack_slist_remove (client->ports, port);
  3181. jack_port_registration_notify (engine, req->x.port_info.port_id,
  3182. FALSE);
  3183. jack_unlock_graph (engine);
  3184. return 0;
  3185. }
  3186. int
  3187. jack_do_get_port_connections (jack_engine_t *engine, jack_request_t *req,
  3188. int reply_fd)
  3189. {
  3190. jack_port_internal_t *port;
  3191. JSList *node;
  3192. unsigned int i;
  3193. int ret = -1;
  3194. int internal = FALSE;
  3195. jack_lock_graph (engine);
  3196. port = &engine->internal_ports[req->x.port_info.port_id];
  3197. DEBUG ("Getting connections for port '%s'.", port->shared->name);
  3198. req->x.port_connections.nports = jack_slist_length (port->connections);
  3199. req->status = 0;
  3200. /* figure out if this is an internal or external client */
  3201. for (node = engine->clients; node; node = jack_slist_next (node)) {
  3202. if (((jack_client_internal_t *) node->data)->request_fd
  3203. == reply_fd) {
  3204. internal = jack_client_is_internal(
  3205. (jack_client_internal_t *) node->data);
  3206. break;
  3207. }
  3208. }
  3209. if (!internal) {
  3210. if (write (reply_fd, req, sizeof (*req))
  3211. < (ssize_t) sizeof (req)) {
  3212. jack_error ("cannot write GetPortConnections result "
  3213. "to client via fd = %d (%s)",
  3214. reply_fd, strerror (errno));
  3215. goto out;
  3216. }
  3217. } else {
  3218. req->x.port_connections.ports = (const char **)
  3219. malloc (sizeof (char *)
  3220. * req->x.port_connections.nports);
  3221. }
  3222. if (req->type == GetPortConnections) {
  3223. for (i = 0, node = port->connections; node;
  3224. node = jack_slist_next (node), ++i) {
  3225. jack_port_id_t port_id;
  3226. if (((jack_connection_internal_t *) node->data)->source
  3227. == port) {
  3228. port_id = ((jack_connection_internal_t *)
  3229. node->data)->destination->shared->id;
  3230. } else {
  3231. port_id = ((jack_connection_internal_t *)
  3232. node->data)->source->shared->id;
  3233. }
  3234. if (internal) {
  3235. /* internal client asking for
  3236. * names. store in malloc'ed space,
  3237. * client frees
  3238. */
  3239. req->x.port_connections.ports[i] =
  3240. engine->control->ports[port_id].name;
  3241. } else {
  3242. /* external client asking for
  3243. * names. we write the port id's to
  3244. * the reply fd.
  3245. */
  3246. if (write (reply_fd, &port_id,
  3247. sizeof (port_id))
  3248. < (ssize_t) sizeof (port_id)) {
  3249. jack_error ("cannot write port id "
  3250. "to client");
  3251. goto out;
  3252. }
  3253. }
  3254. }
  3255. }
  3256. ret = 0;
  3257. out:
  3258. req->status = ret;
  3259. jack_unlock_graph (engine);
  3260. return ret;
  3261. }
  3262. void
  3263. jack_port_registration_notify (jack_engine_t *engine,
  3264. jack_port_id_t port_id, int yn)
  3265. {
  3266. jack_event_t event;
  3267. jack_client_internal_t *client;
  3268. JSList *node;
  3269. event.type = (yn ? PortRegistered : PortUnregistered);
  3270. event.x.port_id = port_id;
  3271. for (node = engine->clients; node; node = jack_slist_next (node)) {
  3272. client = (jack_client_internal_t *) node->data;
  3273. if (!client->control->active) {
  3274. continue;
  3275. }
  3276. if (client->control->port_register) {
  3277. if (jack_deliver_event (engine, client, &event)) {
  3278. jack_error ("cannot send port registration"
  3279. " notification to %s (%s)",
  3280. client->control->name,
  3281. strerror (errno));
  3282. }
  3283. }
  3284. }
  3285. }
  3286. int
  3287. jack_port_assign_buffer (jack_engine_t *engine, jack_port_internal_t *port)
  3288. {
  3289. jack_port_buffer_list_t *blist =
  3290. jack_port_buffer_list (engine, port);
  3291. jack_port_buffer_info_t *bi;
  3292. if (port->shared->flags & JackPortIsInput) {
  3293. port->shared->offset = 0;
  3294. return 0;
  3295. }
  3296. pthread_mutex_lock (&blist->lock);
  3297. if (blist->freelist == NULL) {
  3298. jack_port_type_info_t *port_type =
  3299. jack_port_type_info (engine, port);
  3300. jack_error ("all %s port buffers in use!",
  3301. port_type->type_name);
  3302. pthread_mutex_unlock (&blist->lock);
  3303. return -1;
  3304. }
  3305. bi = (jack_port_buffer_info_t *) blist->freelist->data;
  3306. blist->freelist = jack_slist_remove (blist->freelist, bi);
  3307. port->shared->offset = bi->offset;
  3308. port->buffer_info = bi;
  3309. pthread_mutex_unlock (&blist->lock);
  3310. return 0;
  3311. }
  3312. static jack_port_internal_t *
  3313. jack_get_port_by_name (jack_engine_t *engine, const char *name)
  3314. {
  3315. jack_port_id_t id;
  3316. /* Note the potential race on "in_use". Other design
  3317. elements prevent this from being a problem.
  3318. */
  3319. for (id = 0; id < engine->port_max; id++) {
  3320. if (engine->control->ports[id].in_use &&
  3321. strcmp (engine->control->ports[id].name, name) == 0) {
  3322. return &engine->internal_ports[id];
  3323. }
  3324. }
  3325. return NULL;
  3326. }
  3327. static int
  3328. jack_send_connection_notification (jack_engine_t *engine,
  3329. jack_client_id_t client_id,
  3330. jack_port_id_t self_id,
  3331. jack_port_id_t other_id, int connected)
  3332. {
  3333. jack_client_internal_t *client;
  3334. jack_event_t event;
  3335. if ((client = jack_client_internal_by_id (engine, client_id)) == NULL) {
  3336. jack_error ("no such client %" PRIu32
  3337. " during connection notification", client_id);
  3338. return -1;
  3339. }
  3340. if (client->control->active) {
  3341. event.type = (connected ? PortConnected : PortDisconnected);
  3342. event.x.self_id = self_id;
  3343. event.y.other_id = other_id;
  3344. if (jack_deliver_event (engine, client, &event)) {
  3345. jack_error ("cannot send port connection notification"
  3346. " to client %s (%s)",
  3347. client->control->name, strerror (errno));
  3348. return -1;
  3349. }
  3350. }
  3351. return 0;
  3352. }