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.

962 lines
24KB

  1. /* -*- mode: c; c-file-style: "bsd"; -*- */
  2. /*
  3. * Client creation and destruction interfaces for JACK engine.
  4. *
  5. * Copyright (C) 2001-2003 Paul Davis
  6. * Copyright (C) 2004 Jack O'Quin
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. *
  22. * $Id$
  23. */
  24. #include <config.h>
  25. #include <errno.h>
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include <jack/internal.h>
  29. #include <jack/engine.h>
  30. #include <jack/messagebuffer.h>
  31. #include <jack/version.h>
  32. #include <sysdeps/poll.h>
  33. #include <sysdeps/ipc.h>
  34. #include "clientengine.h"
  35. #include "transengine.h"
  36. #define JACK_ERROR_WITH_SOCKETS 10000000
  37. static void
  38. jack_client_disconnect_ports (jack_engine_t *engine,
  39. jack_client_internal_t *client)
  40. {
  41. JSList *node;
  42. jack_port_internal_t *port;
  43. /* call tree **** MUST HOLD *** engine->client_lock */
  44. for (node = client->ports; node; node = jack_slist_next (node)) {
  45. port = (jack_port_internal_t *) node->data;
  46. jack_port_clear_connections (engine, port);
  47. jack_port_registration_notify (engine, port->shared->id, FALSE);
  48. jack_port_release (engine, port);
  49. }
  50. jack_slist_free (client->ports);
  51. jack_slist_free (client->truefeeds);
  52. jack_slist_free (client->sortfeeds);
  53. client->truefeeds = 0;
  54. client->sortfeeds = 0;
  55. client->ports = 0;
  56. }
  57. int
  58. jack_client_do_deactivate (jack_engine_t *engine,
  59. jack_client_internal_t *client, int sort_graph)
  60. {
  61. /* caller must hold engine->client_lock and must have checked for and/or
  62. * cleared all connections held by client. */
  63. client->control->active = FALSE;
  64. jack_transport_client_exit (engine, client);
  65. if (!jack_client_is_internal (client) &&
  66. engine->external_client_cnt > 0) {
  67. engine->external_client_cnt--;
  68. }
  69. if (sort_graph) {
  70. jack_sort_graph (engine);
  71. }
  72. return 0;
  73. }
  74. static void
  75. jack_zombify_client (jack_engine_t *engine, jack_client_internal_t *client)
  76. {
  77. VERBOSE (engine, "removing client \"%s\" from the processing chain\n",
  78. client->control->name);
  79. /* caller must hold the client_lock */
  80. /* this stops jack_deliver_event() from doing anything */
  81. client->control->dead = TRUE;
  82. jack_client_disconnect_ports (engine, client);
  83. jack_client_do_deactivate (engine, client, FALSE);
  84. }
  85. static void
  86. jack_remove_client (jack_engine_t *engine, jack_client_internal_t *client)
  87. {
  88. /* called *without* the request_lock */
  89. unsigned int i;
  90. JSList *node;
  91. /* caller must hold the client_lock */
  92. VERBOSE (engine, "removing client \"%s\"\n", client->control->name);
  93. /* if its not already a zombie, make it so */
  94. if (!client->control->dead) {
  95. jack_zombify_client (engine, client);
  96. }
  97. if (client->control->type == ClientExternal) {
  98. /* try to force the server thread to return from poll */
  99. close (client->event_fd);
  100. close (client->request_fd);
  101. /* rearrange the pollfd array so that things work right the
  102. next time we go into poll(2).
  103. */
  104. for (i = 0; i < engine->pfd_max; i++) {
  105. if (engine->pfd[i].fd == client->request_fd) {
  106. if (i+1 < engine->pfd_max) {
  107. memmove (&engine->pfd[i],
  108. &engine->pfd[i+1],
  109. sizeof (struct pollfd)
  110. * (engine->pfd_max - i));
  111. }
  112. engine->pfd_max--;
  113. }
  114. }
  115. }
  116. for (node = engine->clients; node; node = jack_slist_next (node)) {
  117. if (((jack_client_internal_t *) node->data)->control->id
  118. == client->control->id) {
  119. engine->clients =
  120. jack_slist_remove_link (engine->clients, node);
  121. jack_slist_free_1 (node);
  122. break;
  123. }
  124. }
  125. jack_client_delete (engine, client);
  126. /* ignore the driver, which counts as a client. */
  127. if (engine->temporary && (jack_slist_length(engine->clients) <= 1)) {
  128. exit(0);
  129. }
  130. }
  131. void
  132. jack_remove_clients (jack_engine_t* engine)
  133. {
  134. JSList *tmp, *node;
  135. int need_sort = FALSE;
  136. jack_client_internal_t *client;
  137. /* remove all dead clients */
  138. for (node = engine->clients; node; ) {
  139. tmp = jack_slist_next (node);
  140. client = (jack_client_internal_t *) node->data;
  141. if (client->error) {
  142. /* if we have a communication problem with the
  143. client, remove it. otherwise, turn it into
  144. a zombie. the client will/should realize
  145. this and will close its sockets. then
  146. we'll end up back here again and will
  147. finally remove the client.
  148. */
  149. if (client->error >= JACK_ERROR_WITH_SOCKETS) {
  150. VERBOSE (engine, "removing failed "
  151. "client %s state = %s errors"
  152. " = %d\n",
  153. client->control->name,
  154. client_state_names[
  155. client->control->state
  156. ],
  157. client->error);
  158. jack_remove_client (engine,
  159. (jack_client_internal_t *)
  160. node->data);
  161. } else {
  162. VERBOSE (engine, "client failure: "
  163. "client %s state = %s errors"
  164. " = %d\n",
  165. client->control->name,
  166. client_state_names[
  167. client->control->state
  168. ],
  169. client->error);
  170. jack_zombify_client (engine,
  171. (jack_client_internal_t *)
  172. node->data);
  173. client->error = 0;
  174. }
  175. need_sort = TRUE;
  176. }
  177. node = tmp;
  178. }
  179. if (need_sort) {
  180. jack_sort_graph (engine);
  181. }
  182. jack_engine_reset_rolling_usecs (engine);
  183. }
  184. static int
  185. jack_load_client (jack_engine_t *engine, jack_client_internal_t *client,
  186. const char *so_name)
  187. {
  188. const char *errstr;
  189. char path_to_so[PATH_MAX+1];
  190. snprintf (path_to_so, sizeof (path_to_so), ADDON_DIR "/%s.so", so_name);
  191. client->handle = dlopen (path_to_so, RTLD_NOW|RTLD_GLOBAL);
  192. if (client->handle == 0) {
  193. if ((errstr = dlerror ()) != 0) {
  194. jack_error ("%s", errstr);
  195. } else {
  196. jack_error ("bizarre error loading %s", so_name);
  197. }
  198. return -1;
  199. }
  200. client->initialize = dlsym (client->handle, "jack_initialize");
  201. if ((errstr = dlerror ()) != 0) {
  202. jack_error ("%s has no initialize() function\n", so_name);
  203. dlclose (client->handle);
  204. client->handle = 0;
  205. return -1;
  206. }
  207. client->finish = (void (*)(void *)) dlsym (client->handle,
  208. "jack_finish");
  209. if ((errstr = dlerror ()) != 0) {
  210. jack_error ("%s has no finish() function", so_name);
  211. dlclose (client->handle);
  212. client->handle = 0;
  213. return -1;
  214. }
  215. return 0;
  216. }
  217. static void
  218. jack_client_unload (jack_client_internal_t *client)
  219. {
  220. if (client->handle) {
  221. if (client->finish) {
  222. client->finish (client->control->process_arg);
  223. }
  224. dlclose (client->handle);
  225. }
  226. }
  227. static jack_client_internal_t *
  228. jack_client_by_name (jack_engine_t *engine, const char *name)
  229. {
  230. jack_client_internal_t *client = NULL;
  231. JSList *node;
  232. jack_lock_graph (engine);
  233. for (node = engine->clients; node; node = jack_slist_next (node)) {
  234. if (strcmp ((const char *) ((jack_client_internal_t *)
  235. node->data)->control->name,
  236. name) == 0) {
  237. client = (jack_client_internal_t *) node->data;
  238. break;
  239. }
  240. }
  241. jack_unlock_graph (engine);
  242. return client;
  243. }
  244. static jack_client_id_t
  245. jack_client_id_by_name (jack_engine_t *engine, const char *name)
  246. {
  247. jack_client_id_t id = 0; /* NULL client ID */
  248. JSList *node;
  249. jack_lock_graph (engine);
  250. for (node = engine->clients; node; node = jack_slist_next (node)) {
  251. if (strcmp ((const char *) ((jack_client_internal_t *)
  252. node->data)->control->name,
  253. name) == 0) {
  254. jack_client_internal_t *client =
  255. (jack_client_internal_t *) node->data;
  256. id = client->control->id;
  257. break;
  258. }
  259. }
  260. jack_unlock_graph (engine);
  261. return id;
  262. }
  263. jack_client_internal_t *
  264. jack_client_internal_by_id (jack_engine_t *engine, jack_client_id_t id)
  265. {
  266. jack_client_internal_t *client = NULL;
  267. JSList *node;
  268. /* call tree ***MUST HOLD*** the graph lock */
  269. for (node = engine->clients; node; node = jack_slist_next (node)) {
  270. if (((jack_client_internal_t *) node->data)->control->id
  271. == id) {
  272. client = (jack_client_internal_t *) node->data;
  273. break;
  274. }
  275. }
  276. return client;
  277. }
  278. /* generate a unique client name
  279. *
  280. * returns 0 if successful, updates name in place
  281. */
  282. static inline int
  283. jack_generate_unique_name (jack_engine_t *engine, char *name)
  284. {
  285. int tens, ones;
  286. int length = strlen (name);
  287. if (length > JACK_CLIENT_NAME_SIZE - 4) {
  288. jack_error ("%s exists and is too long to make unique", name);
  289. return 1; /* failure */
  290. }
  291. /* generate a unique name by appending "-01".."-99" */
  292. name[length++] = '-';
  293. tens = length++;
  294. ones = length++;
  295. name[tens] = '0';
  296. name[ones] = '1';
  297. name[length] = '\0';
  298. while (jack_client_by_name (engine, name)) {
  299. if (name[ones] == '9') {
  300. if (name[tens] == '9') {
  301. jack_error ("client %s has 99 extra"
  302. " instances already", name);
  303. return 1; /* give up */
  304. }
  305. name[tens]++;
  306. name[ones] = '0';
  307. } else {
  308. name[ones]++;
  309. }
  310. }
  311. return 0;
  312. }
  313. static int
  314. jack_client_name_invalid (jack_engine_t *engine, char *name,
  315. jack_options_t options, jack_status_t *status)
  316. {
  317. /* Since this is always called from the server thread, no
  318. * other new client will be created at the same time. So,
  319. * testing a name for uniqueness is valid here. When called
  320. * from jack_engine_load_driver() this is not strictly true,
  321. * but that seems to be adequately serialized due to engine
  322. * startup. There are no other clients at that point, anyway.
  323. */
  324. if (jack_client_by_name (engine, name)) {
  325. *status |= JackNameNotUnique;
  326. if (options & JackUseExactName) {
  327. jack_error ("cannot create new client; %s already"
  328. " exists", name);
  329. *status |= JackFailure;
  330. return TRUE;
  331. }
  332. if (jack_generate_unique_name(engine, name)) {
  333. *status |= JackFailure;
  334. return TRUE;
  335. }
  336. }
  337. return FALSE;
  338. }
  339. /* Set up the engine's client internal and control structures for both
  340. * internal and external clients. */
  341. static jack_client_internal_t *
  342. jack_setup_client_control (jack_engine_t *engine, int fd,
  343. ClientType type, const char *name)
  344. {
  345. jack_client_internal_t *client;
  346. client = (jack_client_internal_t *)
  347. malloc (sizeof (jack_client_internal_t));
  348. client->request_fd = fd;
  349. client->event_fd = -1;
  350. client->ports = 0;
  351. client->truefeeds = 0;
  352. client->sortfeeds = 0;
  353. client->execution_order = UINT_MAX;
  354. client->next_client = NULL;
  355. client->handle = NULL;
  356. client->finish = NULL;
  357. client->error = 0;
  358. if (type != ClientExternal) {
  359. client->control = (jack_client_control_t *)
  360. malloc (sizeof (jack_client_control_t));
  361. } else {
  362. char shm_name[PATH_MAX+1];
  363. snprintf (shm_name, sizeof (shm_name), "jack-c-%s", name);
  364. if (jack_shmalloc (shm_name,
  365. sizeof (jack_client_control_t),
  366. &client->control_shm)) {
  367. jack_error ("cannot create client control block for %s",
  368. name);
  369. free (client);
  370. return 0;
  371. }
  372. if (jack_attach_shm (&client->control_shm)) {
  373. jack_error ("cannot attach to client control block "
  374. "for %s (%s)", name, strerror (errno));
  375. jack_destroy_shm (&client->control_shm);
  376. free (client);
  377. return 0;
  378. }
  379. client->control = (jack_client_control_t *)
  380. jack_shm_addr (&client->control_shm);
  381. }
  382. client->control->type = type;
  383. client->control->active = 0;
  384. client->control->dead = FALSE;
  385. client->control->timed_out = 0;
  386. client->control->id = engine->next_client_id++;
  387. strcpy ((char *) client->control->name, name);
  388. client->subgraph_start_fd = -1;
  389. client->subgraph_wait_fd = -1;
  390. client->control->process = NULL;
  391. client->control->process_arg = NULL;
  392. client->control->bufsize = NULL;
  393. client->control->bufsize_arg = NULL;
  394. client->control->srate = NULL;
  395. client->control->srate_arg = NULL;
  396. client->control->xrun = NULL;
  397. client->control->xrun_arg = NULL;
  398. client->control->port_register = NULL;
  399. client->control->port_register_arg = NULL;
  400. client->control->graph_order = NULL;
  401. client->control->graph_order_arg = NULL;
  402. jack_transport_client_new (client);
  403. #ifdef JACK_USE_MACH_THREADS
  404. /* specific resources for server/client real-time thread
  405. * communication */
  406. allocate_mach_serverport(engine, client);
  407. client->running = FALSE;
  408. #endif
  409. return client;
  410. }
  411. /* set up all types of clients */
  412. static jack_client_internal_t *
  413. setup_client (jack_engine_t *engine, ClientType type, char *name,
  414. jack_options_t options, jack_status_t *status, int client_fd,
  415. const char *object_path, const char *object_data)
  416. {
  417. /* called with the request_lock */
  418. jack_client_internal_t *client;
  419. /* validate client name, generate a unique one if appropriate */
  420. if (jack_client_name_invalid (engine, name, options, status))
  421. return NULL;
  422. /* create a client struct for this name */
  423. if ((client = jack_setup_client_control (engine, client_fd,
  424. type, name)) == NULL) {
  425. *status |= (JackFailure|JackInitFailure);
  426. jack_error ("cannot create new client object");
  427. return NULL;
  428. }
  429. /* only for internal clients, driver is already loaded */
  430. if (type == ClientInternal) {
  431. if (jack_load_client (engine, client, object_path)) {
  432. jack_error ("cannot dynamically load client from"
  433. " \"%s\"", object_path);
  434. jack_client_delete (engine, client);
  435. *status |= (JackFailure|JackLoadFailure);
  436. return NULL;
  437. }
  438. }
  439. VERBOSE (engine, "new client: %s, id = %" PRIu32
  440. " type %d @ %p fd = %d\n",
  441. client->control->name, client->control->id,
  442. type, client->control, client_fd);
  443. if (jack_client_is_internal(client)) {
  444. /* Set up the pointers necessary for the request
  445. * system to work. The client is in the same address
  446. * space */
  447. client->control->deliver_request = internal_client_request;
  448. client->control->deliver_arg = engine;
  449. }
  450. /* add new client to the clients list */
  451. jack_lock_graph (engine);
  452. engine->clients = jack_slist_prepend (engine->clients, client);
  453. jack_engine_reset_rolling_usecs (engine);
  454. if (jack_client_is_internal(client)) {
  455. /* Internal clients need to make regular JACK API
  456. * calls, which need a jack_client_t structure.
  457. * Create one here.
  458. */
  459. client->control->private_client =
  460. jack_client_alloc_internal (client->control, engine);
  461. jack_unlock_graph (engine);
  462. /* Call its initialization function. This function
  463. * may make requests of its own, so we temporarily
  464. * release and then reacquire the request_lock. */
  465. if (client->control->type == ClientInternal) {
  466. pthread_mutex_unlock (&engine->request_lock);
  467. if (client->initialize (client->control->private_client,
  468. object_data)) {
  469. /* failed: clean up client data */
  470. VERBOSE (engine,
  471. "%s jack_initialize() failed!\n",
  472. client->control->name);
  473. jack_lock_graph (engine);
  474. jack_remove_client (engine, client);
  475. jack_unlock_graph (engine);
  476. *status |= (JackFailure|JackInitFailure);
  477. client = NULL;
  478. //JOQ: not clear that all allocated
  479. //storage has been cleaned up properly.
  480. }
  481. pthread_mutex_lock (&engine->request_lock);
  482. }
  483. } else { /* external client */
  484. if (engine->pfd_max >= engine->pfd_size) {
  485. engine->pfd = (struct pollfd *)
  486. realloc (engine->pfd, sizeof (struct pollfd)
  487. * (engine->pfd_size + 16));
  488. engine->pfd_size += 16;
  489. }
  490. engine->pfd[engine->pfd_max].fd = client->request_fd;
  491. engine->pfd[engine->pfd_max].events =
  492. POLLIN|POLLPRI|POLLERR|POLLHUP|POLLNVAL;
  493. engine->pfd_max++;
  494. jack_unlock_graph (engine);
  495. }
  496. return client;
  497. }
  498. jack_client_internal_t *
  499. jack_create_driver_client (jack_engine_t *engine, char *name)
  500. {
  501. jack_client_connect_request_t req;
  502. jack_status_t status;
  503. jack_client_internal_t *client;
  504. snprintf (req.name, sizeof (req.name), "%s", name);
  505. pthread_mutex_lock (&engine->request_lock);
  506. client = setup_client (engine, ClientDriver, name, JackUseExactName,
  507. &status, -1, NULL, NULL);
  508. pthread_mutex_unlock (&engine->request_lock);
  509. return client;
  510. }
  511. static jack_status_t
  512. handle_unload_client (jack_engine_t *engine, jack_client_id_t id)
  513. {
  514. /* called *without* the request_lock */
  515. jack_client_internal_t *client;
  516. jack_status_t status = (JackNoSuchClient|JackFailure);
  517. jack_lock_graph (engine);
  518. if ((client = jack_client_internal_by_id (engine, id))) {
  519. VERBOSE (engine, "unloading client \"%s\"\n",
  520. client->control->name);
  521. jack_remove_client (engine, client);
  522. status = 0;
  523. }
  524. jack_unlock_graph (engine);
  525. return status;
  526. }
  527. int
  528. jack_client_create (jack_engine_t *engine, int client_fd)
  529. {
  530. /* called *without* the request_lock */
  531. jack_client_internal_t *client;
  532. jack_client_connect_request_t req;
  533. jack_client_connect_result_t res;
  534. ssize_t nbytes;
  535. res.status = 0;
  536. nbytes = read (client_fd, &req, sizeof (req));
  537. if (nbytes == 0) { /* EOF? */
  538. jack_error ("cannot read connection request from client");
  539. return -1;
  540. }
  541. /* First verify protocol version (first field of request), if
  542. * present, then make sure request has the expected length. */
  543. if ((nbytes < sizeof (req.protocol_v))
  544. || (req.protocol_v != jack_protocol_version)
  545. || (nbytes != sizeof (req))) {
  546. /* JACK protocol incompatibility */
  547. res.status |= (JackFailure|JackVersionError);
  548. jack_error ("JACK protocol mismatch");
  549. if (write (client_fd, &res, sizeof (res)) != sizeof (res)) {
  550. jack_error ("cannot write client connection response");
  551. }
  552. return -1;
  553. }
  554. if (!req.load) { /* internal client close? */
  555. int rc = -1;
  556. jack_client_id_t id;
  557. if ((id = jack_client_id_by_name(engine, req.name))) {
  558. rc = handle_unload_client (engine, id);
  559. }
  560. /* close does not send a reply */
  561. return rc;
  562. }
  563. pthread_mutex_lock (&engine->request_lock);
  564. client = setup_client (engine, req.type, req.name,
  565. req.options, &res.status, client_fd,
  566. req.object_path, req.object_data);
  567. pthread_mutex_unlock (&engine->request_lock);
  568. if (client == NULL) {
  569. res.status |= JackFailure; /* just making sure */
  570. return -1;
  571. }
  572. res.client_shm = client->control_shm;
  573. res.engine_shm = engine->control_shm;
  574. res.realtime = engine->control->real_time;
  575. res.realtime_priority = engine->rtpriority - 1;
  576. strncpy (res.name, req.name, sizeof(res.name));
  577. #ifdef JACK_USE_MACH_THREADS
  578. /* Mach port number for server/client communication */
  579. res.portnum = client->portnum;
  580. #endif
  581. if (jack_client_is_internal(client)) {
  582. res.client_control = client->control;
  583. res.engine_control = engine->control;
  584. } else {
  585. strcpy (res.fifo_prefix, engine->fifo_prefix);
  586. }
  587. if (write (client_fd, &res, sizeof (res)) != sizeof (res)) {
  588. jack_error ("cannot write connection response to client");
  589. jack_client_delete (engine, client);
  590. return -1;
  591. }
  592. if (jack_client_is_internal (client)) {
  593. close (client_fd);
  594. }
  595. return 0;
  596. }
  597. int
  598. jack_client_activate (jack_engine_t *engine, jack_client_id_t id)
  599. {
  600. jack_client_internal_t *client;
  601. JSList *node;
  602. int ret = -1;
  603. jack_lock_graph (engine);
  604. for (node = engine->clients; node; node = jack_slist_next (node)) {
  605. if (((jack_client_internal_t *) node->data)->control->id
  606. == id) {
  607. client = (jack_client_internal_t *) node->data;
  608. client->control->active = TRUE;
  609. jack_transport_activate(engine, client);
  610. /* we call this to make sure the FIFO is
  611. * built+ready by the time the client needs
  612. * it. we don't care about the return value at
  613. * this point.
  614. */
  615. jack_get_fifo_fd (engine,
  616. ++engine->external_client_cnt);
  617. jack_sort_graph (engine);
  618. ret = 0;
  619. break;
  620. }
  621. }
  622. jack_unlock_graph (engine);
  623. return ret;
  624. }
  625. int
  626. jack_client_deactivate (jack_engine_t *engine, jack_client_id_t id)
  627. {
  628. JSList *node;
  629. int ret = -1;
  630. jack_lock_graph (engine);
  631. for (node = engine->clients; node; node = jack_slist_next (node)) {
  632. jack_client_internal_t *client =
  633. (jack_client_internal_t *) node->data;
  634. if (client->control->id == id) {
  635. JSList *portnode;
  636. jack_port_internal_t *port;
  637. for (portnode = client->ports; portnode;
  638. portnode = jack_slist_next (portnode)) {
  639. port = (jack_port_internal_t *) portnode->data;
  640. jack_port_clear_connections (engine, port);
  641. }
  642. ret = jack_client_do_deactivate (engine, client, TRUE);
  643. break;
  644. }
  645. }
  646. jack_unlock_graph (engine);
  647. return ret;
  648. }
  649. int
  650. jack_client_disconnect (jack_engine_t *engine, int fd)
  651. {
  652. jack_client_internal_t *client = 0;
  653. JSList *node;
  654. #ifndef DEFER_CLIENT_REMOVE_TO_AUDIO_THREAD
  655. jack_lock_graph (engine);
  656. for (node = engine->clients; node; node = jack_slist_next (node)) {
  657. if (jack_client_is_internal((jack_client_internal_t *)
  658. node->data)) {
  659. continue;
  660. }
  661. if (((jack_client_internal_t *) node->data)->request_fd == fd) {
  662. client = (jack_client_internal_t *) node->data;
  663. break;
  664. }
  665. }
  666. if (client) {
  667. VERBOSE (engine, "removing disconnected client %s state = "
  668. "%s errors = %d\n", client->control->name,
  669. client_state_names[client->control->state],
  670. client->error);
  671. jack_remove_client(engine, client);
  672. jack_sort_graph (engine);
  673. }
  674. jack_unlock_graph (engine);
  675. #else /* DEFER_CLIENT_REMOVE_TO_AUDIO_THREAD */
  676. jack_lock_graph (engine);
  677. for (node = engine->clients; node; node = jack_slist_next (node)) {
  678. if (jack_client_is_internal((jack_client_internal_t *)
  679. node->data)) {
  680. continue;
  681. }
  682. if (((jack_client_internal_t *) node->data)->request_fd == fd) {
  683. client = (jack_client_internal_t *) node->data;
  684. if (client->error < JACK_ERROR_WITH_SOCKETS) {
  685. client->error += JACK_ERROR_WITH_SOCKETS;
  686. }
  687. break;
  688. }
  689. }
  690. jack_unlock_graph (engine);
  691. #endif /* DEFER_CLIENT_REMOVE_TO_AUDIO_THREAD */
  692. return 0;
  693. }
  694. void
  695. jack_client_delete (jack_engine_t *engine, jack_client_internal_t *client)
  696. {
  697. if (jack_client_is_internal (client)) {
  698. jack_client_unload (client);
  699. free (client->control->private_client);
  700. free ((void *) client->control);
  701. } else {
  702. /* release the client segment, mark it for
  703. destruction, and free up the shm registry
  704. information so that it can be reused.
  705. */
  706. jack_release_shm (&client->control_shm);
  707. jack_destroy_shm (&client->control_shm);
  708. }
  709. free (client);
  710. }
  711. void
  712. jack_intclient_handle_request (jack_engine_t *engine, jack_request_t *req)
  713. {
  714. jack_client_internal_t *client;
  715. req->status = 0;
  716. if ((client = jack_client_by_name (engine, req->x.intclient.name))) {
  717. req->x.intclient.id = client->control->id;
  718. } else {
  719. req->status |= (JackNoSuchClient|JackFailure);
  720. }
  721. }
  722. void
  723. jack_intclient_load_request (jack_engine_t *engine, jack_request_t *req)
  724. {
  725. /* called with the request_lock */
  726. jack_client_internal_t *client;
  727. jack_status_t status = 0;
  728. VERBOSE (engine, "load internal client %s from %s, init `%s', "
  729. "options: 0x%x\n", req->x.intclient.name,
  730. req->x.intclient.path, req->x.intclient.init,
  731. req->x.intclient.options);
  732. client = setup_client (engine, ClientInternal, req->x.intclient.name,
  733. req->x.intclient.options, &status, -1,
  734. req->x.intclient.path, req->x.intclient.init);
  735. if (client == NULL) {
  736. status |= JackFailure; /* just making sure */
  737. req->x.intclient.id = 0;
  738. VERBOSE (engine, "load failed, status = 0x%x\n", status);
  739. } else {
  740. req->x.intclient.id = client->control->id;
  741. }
  742. req->status = status;
  743. }
  744. void
  745. jack_intclient_name_request (jack_engine_t *engine, jack_request_t *req)
  746. {
  747. jack_client_internal_t *client;
  748. jack_lock_graph (engine);
  749. if ((client = jack_client_internal_by_id (engine,
  750. req->x.intclient.id))) {
  751. strncpy ((char *) req->x.intclient.name,
  752. (char *) client->control->name,
  753. sizeof (req->x.intclient.name));
  754. req->status = 0;
  755. } else {
  756. req->status = (JackNoSuchClient|JackFailure);
  757. }
  758. jack_unlock_graph (engine);
  759. }
  760. void
  761. jack_intclient_unload_request (jack_engine_t *engine, jack_request_t *req)
  762. {
  763. /* Called with the request_lock, but we need to call
  764. * handle_unload_client() *without* it. */
  765. if (req->x.intclient.id) {
  766. pthread_mutex_unlock (&engine->request_lock);
  767. req->status =
  768. handle_unload_client (engine, req->x.intclient.id);
  769. pthread_mutex_lock (&engine->request_lock);
  770. } else {
  771. VERBOSE (engine, "invalid unload request\n");
  772. req->status = JackFailure;
  773. }
  774. }