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.

4121 lines
99KB

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