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.

958 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. jack_client_state_name (client),
  155. client->error);
  156. jack_remove_client (engine,
  157. (jack_client_internal_t *)
  158. node->data);
  159. } else {
  160. VERBOSE (engine, "client failure: "
  161. "client %s state = %s errors"
  162. " = %d\n",
  163. client->control->name,
  164. jack_client_state_name (client),
  165. client->error);
  166. jack_zombify_client (engine,
  167. (jack_client_internal_t *)
  168. node->data);
  169. client->error = 0;
  170. }
  171. need_sort = TRUE;
  172. }
  173. node = tmp;
  174. }
  175. if (need_sort) {
  176. jack_sort_graph (engine);
  177. }
  178. jack_engine_reset_rolling_usecs (engine);
  179. }
  180. static int
  181. jack_load_client (jack_engine_t *engine, jack_client_internal_t *client,
  182. const char *so_name)
  183. {
  184. const char *errstr;
  185. char path_to_so[PATH_MAX+1];
  186. snprintf (path_to_so, sizeof (path_to_so), ADDON_DIR "/%s.so", so_name);
  187. client->handle = dlopen (path_to_so, RTLD_NOW|RTLD_GLOBAL);
  188. if (client->handle == 0) {
  189. if ((errstr = dlerror ()) != 0) {
  190. jack_error ("%s", errstr);
  191. } else {
  192. jack_error ("bizarre error loading %s", so_name);
  193. }
  194. return -1;
  195. }
  196. client->initialize = dlsym (client->handle, "jack_initialize");
  197. if ((errstr = dlerror ()) != 0) {
  198. jack_error ("%s has no initialize() function\n", so_name);
  199. dlclose (client->handle);
  200. client->handle = 0;
  201. return -1;
  202. }
  203. client->finish = (void (*)(void *)) dlsym (client->handle,
  204. "jack_finish");
  205. if ((errstr = dlerror ()) != 0) {
  206. jack_error ("%s has no finish() function", so_name);
  207. dlclose (client->handle);
  208. client->handle = 0;
  209. return -1;
  210. }
  211. return 0;
  212. }
  213. static void
  214. jack_client_unload (jack_client_internal_t *client)
  215. {
  216. if (client->handle) {
  217. if (client->finish) {
  218. client->finish (client->control->process_arg);
  219. }
  220. dlclose (client->handle);
  221. }
  222. }
  223. static jack_client_internal_t *
  224. jack_client_by_name (jack_engine_t *engine, const char *name)
  225. {
  226. jack_client_internal_t *client = NULL;
  227. JSList *node;
  228. jack_lock_graph (engine);
  229. for (node = engine->clients; node; node = jack_slist_next (node)) {
  230. if (strcmp ((const char *) ((jack_client_internal_t *)
  231. node->data)->control->name,
  232. name) == 0) {
  233. client = (jack_client_internal_t *) node->data;
  234. break;
  235. }
  236. }
  237. jack_unlock_graph (engine);
  238. return client;
  239. }
  240. static jack_client_id_t
  241. jack_client_id_by_name (jack_engine_t *engine, const char *name)
  242. {
  243. jack_client_id_t id = 0; /* NULL client ID */
  244. JSList *node;
  245. jack_lock_graph (engine);
  246. for (node = engine->clients; node; node = jack_slist_next (node)) {
  247. if (strcmp ((const char *) ((jack_client_internal_t *)
  248. node->data)->control->name,
  249. name) == 0) {
  250. jack_client_internal_t *client =
  251. (jack_client_internal_t *) node->data;
  252. id = client->control->id;
  253. break;
  254. }
  255. }
  256. jack_unlock_graph (engine);
  257. return id;
  258. }
  259. jack_client_internal_t *
  260. jack_client_internal_by_id (jack_engine_t *engine, jack_client_id_t id)
  261. {
  262. jack_client_internal_t *client = NULL;
  263. JSList *node;
  264. /* call tree ***MUST HOLD*** the graph lock */
  265. for (node = engine->clients; node; node = jack_slist_next (node)) {
  266. if (((jack_client_internal_t *) node->data)->control->id
  267. == id) {
  268. client = (jack_client_internal_t *) node->data;
  269. break;
  270. }
  271. }
  272. return client;
  273. }
  274. /* generate a unique client name
  275. *
  276. * returns 0 if successful, updates name in place
  277. */
  278. static inline int
  279. jack_generate_unique_name (jack_engine_t *engine, char *name)
  280. {
  281. int tens, ones;
  282. int length = strlen (name);
  283. if (length > JACK_CLIENT_NAME_SIZE - 4) {
  284. jack_error ("%s exists and is too long to make unique", name);
  285. return 1; /* failure */
  286. }
  287. /* generate a unique name by appending "-01".."-99" */
  288. name[length++] = '-';
  289. tens = length++;
  290. ones = length++;
  291. name[tens] = '0';
  292. name[ones] = '1';
  293. name[length] = '\0';
  294. while (jack_client_by_name (engine, name)) {
  295. if (name[ones] == '9') {
  296. if (name[tens] == '9') {
  297. jack_error ("client %s has 99 extra"
  298. " instances already", name);
  299. return 1; /* give up */
  300. }
  301. name[tens]++;
  302. name[ones] = '0';
  303. } else {
  304. name[ones]++;
  305. }
  306. }
  307. return 0;
  308. }
  309. static int
  310. jack_client_name_invalid (jack_engine_t *engine, char *name,
  311. jack_options_t options, jack_status_t *status)
  312. {
  313. /* Since this is always called from the server thread, no
  314. * other new client will be created at the same time. So,
  315. * testing a name for uniqueness is valid here. When called
  316. * from jack_engine_load_driver() this is not strictly true,
  317. * but that seems to be adequately serialized due to engine
  318. * startup. There are no other clients at that point, anyway.
  319. */
  320. if (jack_client_by_name (engine, name)) {
  321. *status |= JackNameNotUnique;
  322. if (options & JackUseExactName) {
  323. jack_error ("cannot create new client; %s already"
  324. " exists", name);
  325. *status |= JackFailure;
  326. return TRUE;
  327. }
  328. if (jack_generate_unique_name(engine, name)) {
  329. *status |= JackFailure;
  330. return TRUE;
  331. }
  332. }
  333. return FALSE;
  334. }
  335. /* Set up the engine's client internal and control structures for both
  336. * internal and external clients. */
  337. static jack_client_internal_t *
  338. jack_setup_client_control (jack_engine_t *engine, int fd,
  339. ClientType type, const char *name)
  340. {
  341. jack_client_internal_t *client;
  342. client = (jack_client_internal_t *)
  343. malloc (sizeof (jack_client_internal_t));
  344. client->request_fd = fd;
  345. client->event_fd = -1;
  346. client->ports = 0;
  347. client->truefeeds = 0;
  348. client->sortfeeds = 0;
  349. client->execution_order = UINT_MAX;
  350. client->next_client = NULL;
  351. client->handle = NULL;
  352. client->finish = NULL;
  353. client->error = 0;
  354. if (type != ClientExternal) {
  355. client->control = (jack_client_control_t *)
  356. malloc (sizeof (jack_client_control_t));
  357. } else {
  358. char shm_name[PATH_MAX+1];
  359. snprintf (shm_name, sizeof (shm_name), "jack-c-%s", name);
  360. if (jack_shmalloc (shm_name,
  361. sizeof (jack_client_control_t),
  362. &client->control_shm)) {
  363. jack_error ("cannot create client control block for %s",
  364. name);
  365. free (client);
  366. return 0;
  367. }
  368. if (jack_attach_shm (&client->control_shm)) {
  369. jack_error ("cannot attach to client control block "
  370. "for %s (%s)", name, strerror (errno));
  371. jack_destroy_shm (&client->control_shm);
  372. free (client);
  373. return 0;
  374. }
  375. client->control = (jack_client_control_t *)
  376. jack_shm_addr (&client->control_shm);
  377. }
  378. client->control->type = type;
  379. client->control->active = 0;
  380. client->control->dead = FALSE;
  381. client->control->timed_out = 0;
  382. client->control->id = engine->next_client_id++;
  383. strcpy ((char *) client->control->name, name);
  384. client->subgraph_start_fd = -1;
  385. client->subgraph_wait_fd = -1;
  386. client->control->process = NULL;
  387. client->control->process_arg = NULL;
  388. client->control->bufsize = NULL;
  389. client->control->bufsize_arg = NULL;
  390. client->control->srate = NULL;
  391. client->control->srate_arg = NULL;
  392. client->control->xrun = NULL;
  393. client->control->xrun_arg = NULL;
  394. client->control->port_register = NULL;
  395. client->control->port_register_arg = NULL;
  396. client->control->graph_order = NULL;
  397. client->control->graph_order_arg = NULL;
  398. jack_transport_client_new (client);
  399. #ifdef JACK_USE_MACH_THREADS
  400. /* specific resources for server/client real-time thread
  401. * communication */
  402. allocate_mach_serverport(engine, client);
  403. client->running = FALSE;
  404. #endif
  405. return client;
  406. }
  407. /* set up all types of clients */
  408. static jack_client_internal_t *
  409. setup_client (jack_engine_t *engine, ClientType type, char *name,
  410. jack_options_t options, jack_status_t *status, int client_fd,
  411. const char *object_path, const char *object_data)
  412. {
  413. /* called with the request_lock */
  414. jack_client_internal_t *client;
  415. /* validate client name, generate a unique one if appropriate */
  416. if (jack_client_name_invalid (engine, name, options, status))
  417. return NULL;
  418. /* create a client struct for this name */
  419. if ((client = jack_setup_client_control (engine, client_fd,
  420. type, name)) == NULL) {
  421. *status |= (JackFailure|JackInitFailure);
  422. jack_error ("cannot create new client object");
  423. return NULL;
  424. }
  425. /* only for internal clients, driver is already loaded */
  426. if (type == ClientInternal) {
  427. if (jack_load_client (engine, client, object_path)) {
  428. jack_error ("cannot dynamically load client from"
  429. " \"%s\"", object_path);
  430. jack_client_delete (engine, client);
  431. *status |= (JackFailure|JackLoadFailure);
  432. return NULL;
  433. }
  434. }
  435. VERBOSE (engine, "new client: %s, id = %" PRIu32
  436. " type %d @ %p fd = %d\n",
  437. client->control->name, client->control->id,
  438. type, client->control, client_fd);
  439. if (jack_client_is_internal(client)) {
  440. /* Set up the pointers necessary for the request
  441. * system to work. The client is in the same address
  442. * space */
  443. client->control->deliver_request = internal_client_request;
  444. client->control->deliver_arg = engine;
  445. }
  446. /* add new client to the clients list */
  447. jack_lock_graph (engine);
  448. engine->clients = jack_slist_prepend (engine->clients, client);
  449. jack_engine_reset_rolling_usecs (engine);
  450. if (jack_client_is_internal(client)) {
  451. /* Internal clients need to make regular JACK API
  452. * calls, which need a jack_client_t structure.
  453. * Create one here.
  454. */
  455. client->control->private_client =
  456. jack_client_alloc_internal (client->control, engine);
  457. jack_unlock_graph (engine);
  458. /* Call its initialization function. This function
  459. * may make requests of its own, so we temporarily
  460. * release and then reacquire the request_lock. */
  461. if (client->control->type == ClientInternal) {
  462. pthread_mutex_unlock (&engine->request_lock);
  463. if (client->initialize (client->control->private_client,
  464. object_data)) {
  465. /* failed: clean up client data */
  466. VERBOSE (engine,
  467. "%s jack_initialize() failed!\n",
  468. client->control->name);
  469. jack_lock_graph (engine);
  470. jack_remove_client (engine, client);
  471. jack_unlock_graph (engine);
  472. *status |= (JackFailure|JackInitFailure);
  473. client = NULL;
  474. //JOQ: not clear that all allocated
  475. //storage has been cleaned up properly.
  476. }
  477. pthread_mutex_lock (&engine->request_lock);
  478. }
  479. } else { /* external client */
  480. if (engine->pfd_max >= engine->pfd_size) {
  481. engine->pfd = (struct pollfd *)
  482. realloc (engine->pfd, sizeof (struct pollfd)
  483. * (engine->pfd_size + 16));
  484. engine->pfd_size += 16;
  485. }
  486. engine->pfd[engine->pfd_max].fd = client->request_fd;
  487. engine->pfd[engine->pfd_max].events =
  488. POLLIN|POLLPRI|POLLERR|POLLHUP|POLLNVAL;
  489. engine->pfd_max++;
  490. jack_unlock_graph (engine);
  491. }
  492. return client;
  493. }
  494. jack_client_internal_t *
  495. jack_create_driver_client (jack_engine_t *engine, char *name)
  496. {
  497. jack_client_connect_request_t req;
  498. jack_status_t status;
  499. jack_client_internal_t *client;
  500. snprintf (req.name, sizeof (req.name), "%s", name);
  501. pthread_mutex_lock (&engine->request_lock);
  502. client = setup_client (engine, ClientDriver, name, JackUseExactName,
  503. &status, -1, NULL, NULL);
  504. pthread_mutex_unlock (&engine->request_lock);
  505. return client;
  506. }
  507. static jack_status_t
  508. handle_unload_client (jack_engine_t *engine, jack_client_id_t id)
  509. {
  510. /* called *without* the request_lock */
  511. jack_client_internal_t *client;
  512. jack_status_t status = (JackNoSuchClient|JackFailure);
  513. jack_lock_graph (engine);
  514. if ((client = jack_client_internal_by_id (engine, id))) {
  515. VERBOSE (engine, "unloading client \"%s\"\n",
  516. client->control->name);
  517. jack_remove_client (engine, client);
  518. status = 0;
  519. }
  520. jack_unlock_graph (engine);
  521. return status;
  522. }
  523. int
  524. jack_client_create (jack_engine_t *engine, int client_fd)
  525. {
  526. /* called *without* the request_lock */
  527. jack_client_internal_t *client;
  528. jack_client_connect_request_t req;
  529. jack_client_connect_result_t res;
  530. ssize_t nbytes;
  531. res.status = 0;
  532. nbytes = read (client_fd, &req, sizeof (req));
  533. if (nbytes == 0) { /* EOF? */
  534. jack_error ("cannot read connection request from client");
  535. return -1;
  536. }
  537. /* First verify protocol version (first field of request), if
  538. * present, then make sure request has the expected length. */
  539. if ((nbytes < sizeof (req.protocol_v))
  540. || (req.protocol_v != jack_protocol_version)
  541. || (nbytes != sizeof (req))) {
  542. /* JACK protocol incompatibility */
  543. res.status |= (JackFailure|JackVersionError);
  544. jack_error ("JACK protocol mismatch");
  545. if (write (client_fd, &res, sizeof (res)) != sizeof (res)) {
  546. jack_error ("cannot write client connection response");
  547. }
  548. return -1;
  549. }
  550. if (!req.load) { /* internal client close? */
  551. int rc = -1;
  552. jack_client_id_t id;
  553. if ((id = jack_client_id_by_name(engine, req.name))) {
  554. rc = handle_unload_client (engine, id);
  555. }
  556. /* close does not send a reply */
  557. return rc;
  558. }
  559. pthread_mutex_lock (&engine->request_lock);
  560. client = setup_client (engine, req.type, req.name,
  561. req.options, &res.status, client_fd,
  562. req.object_path, req.object_data);
  563. pthread_mutex_unlock (&engine->request_lock);
  564. if (client == NULL) {
  565. res.status |= JackFailure; /* just making sure */
  566. return -1;
  567. }
  568. res.client_shm = client->control_shm;
  569. res.engine_shm = engine->control_shm;
  570. res.realtime = engine->control->real_time;
  571. res.realtime_priority = engine->rtpriority - 1;
  572. strncpy (res.name, req.name, sizeof(res.name));
  573. #ifdef JACK_USE_MACH_THREADS
  574. /* Mach port number for server/client communication */
  575. res.portnum = client->portnum;
  576. #endif
  577. if (jack_client_is_internal(client)) {
  578. res.client_control = client->control;
  579. res.engine_control = engine->control;
  580. } else {
  581. strcpy (res.fifo_prefix, engine->fifo_prefix);
  582. }
  583. if (write (client_fd, &res, sizeof (res)) != sizeof (res)) {
  584. jack_error ("cannot write connection response to client");
  585. jack_client_delete (engine, client);
  586. return -1;
  587. }
  588. if (jack_client_is_internal (client)) {
  589. close (client_fd);
  590. }
  591. return 0;
  592. }
  593. int
  594. jack_client_activate (jack_engine_t *engine, jack_client_id_t id)
  595. {
  596. jack_client_internal_t *client;
  597. JSList *node;
  598. int ret = -1;
  599. jack_lock_graph (engine);
  600. for (node = engine->clients; node; node = jack_slist_next (node)) {
  601. if (((jack_client_internal_t *) node->data)->control->id
  602. == id) {
  603. client = (jack_client_internal_t *) node->data;
  604. client->control->active = TRUE;
  605. jack_transport_activate(engine, client);
  606. /* we call this to make sure the FIFO is
  607. * built+ready by the time the client needs
  608. * it. we don't care about the return value at
  609. * this point.
  610. */
  611. jack_get_fifo_fd (engine,
  612. ++engine->external_client_cnt);
  613. jack_sort_graph (engine);
  614. ret = 0;
  615. break;
  616. }
  617. }
  618. jack_unlock_graph (engine);
  619. return ret;
  620. }
  621. int
  622. jack_client_deactivate (jack_engine_t *engine, jack_client_id_t id)
  623. {
  624. JSList *node;
  625. int ret = -1;
  626. jack_lock_graph (engine);
  627. for (node = engine->clients; node; node = jack_slist_next (node)) {
  628. jack_client_internal_t *client =
  629. (jack_client_internal_t *) node->data;
  630. if (client->control->id == id) {
  631. JSList *portnode;
  632. jack_port_internal_t *port;
  633. for (portnode = client->ports; portnode;
  634. portnode = jack_slist_next (portnode)) {
  635. port = (jack_port_internal_t *) portnode->data;
  636. jack_port_clear_connections (engine, port);
  637. }
  638. ret = jack_client_do_deactivate (engine, client, TRUE);
  639. break;
  640. }
  641. }
  642. jack_unlock_graph (engine);
  643. return ret;
  644. }
  645. int
  646. jack_client_disconnect (jack_engine_t *engine, int fd)
  647. {
  648. jack_client_internal_t *client = 0;
  649. JSList *node;
  650. #ifndef DEFER_CLIENT_REMOVE_TO_AUDIO_THREAD
  651. jack_lock_graph (engine);
  652. for (node = engine->clients; node; node = jack_slist_next (node)) {
  653. if (jack_client_is_internal((jack_client_internal_t *)
  654. node->data)) {
  655. continue;
  656. }
  657. if (((jack_client_internal_t *) node->data)->request_fd == fd) {
  658. client = (jack_client_internal_t *) node->data;
  659. break;
  660. }
  661. }
  662. if (client) {
  663. VERBOSE (engine, "removing disconnected client %s state = "
  664. "%s errors = %d\n", client->control->name,
  665. jack_client_state_name (client),
  666. client->error);
  667. jack_remove_client(engine, client);
  668. jack_sort_graph (engine);
  669. }
  670. jack_unlock_graph (engine);
  671. #else /* DEFER_CLIENT_REMOVE_TO_AUDIO_THREAD */
  672. jack_lock_graph (engine);
  673. for (node = engine->clients; node; node = jack_slist_next (node)) {
  674. if (jack_client_is_internal((jack_client_internal_t *)
  675. node->data)) {
  676. continue;
  677. }
  678. if (((jack_client_internal_t *) node->data)->request_fd == fd) {
  679. client = (jack_client_internal_t *) node->data;
  680. if (client->error < JACK_ERROR_WITH_SOCKETS) {
  681. client->error += JACK_ERROR_WITH_SOCKETS;
  682. }
  683. break;
  684. }
  685. }
  686. jack_unlock_graph (engine);
  687. #endif /* DEFER_CLIENT_REMOVE_TO_AUDIO_THREAD */
  688. return 0;
  689. }
  690. void
  691. jack_client_delete (jack_engine_t *engine, jack_client_internal_t *client)
  692. {
  693. if (jack_client_is_internal (client)) {
  694. jack_client_unload (client);
  695. free (client->control->private_client);
  696. free ((void *) client->control);
  697. } else {
  698. /* release the client segment, mark it for
  699. destruction, and free up the shm registry
  700. information so that it can be reused.
  701. */
  702. jack_release_shm (&client->control_shm);
  703. jack_destroy_shm (&client->control_shm);
  704. }
  705. free (client);
  706. }
  707. void
  708. jack_intclient_handle_request (jack_engine_t *engine, jack_request_t *req)
  709. {
  710. jack_client_internal_t *client;
  711. req->status = 0;
  712. if ((client = jack_client_by_name (engine, req->x.intclient.name))) {
  713. req->x.intclient.id = client->control->id;
  714. } else {
  715. req->status |= (JackNoSuchClient|JackFailure);
  716. }
  717. }
  718. void
  719. jack_intclient_load_request (jack_engine_t *engine, jack_request_t *req)
  720. {
  721. /* called with the request_lock */
  722. jack_client_internal_t *client;
  723. jack_status_t status = 0;
  724. VERBOSE (engine, "load internal client %s from %s, init `%s', "
  725. "options: 0x%x\n", req->x.intclient.name,
  726. req->x.intclient.path, req->x.intclient.init,
  727. req->x.intclient.options);
  728. client = setup_client (engine, ClientInternal, req->x.intclient.name,
  729. req->x.intclient.options, &status, -1,
  730. req->x.intclient.path, req->x.intclient.init);
  731. if (client == NULL) {
  732. status |= JackFailure; /* just making sure */
  733. req->x.intclient.id = 0;
  734. VERBOSE (engine, "load failed, status = 0x%x\n", status);
  735. } else {
  736. req->x.intclient.id = client->control->id;
  737. }
  738. req->status = status;
  739. }
  740. void
  741. jack_intclient_name_request (jack_engine_t *engine, jack_request_t *req)
  742. {
  743. jack_client_internal_t *client;
  744. jack_lock_graph (engine);
  745. if ((client = jack_client_internal_by_id (engine,
  746. req->x.intclient.id))) {
  747. strncpy ((char *) req->x.intclient.name,
  748. (char *) client->control->name,
  749. sizeof (req->x.intclient.name));
  750. req->status = 0;
  751. } else {
  752. req->status = (JackNoSuchClient|JackFailure);
  753. }
  754. jack_unlock_graph (engine);
  755. }
  756. void
  757. jack_intclient_unload_request (jack_engine_t *engine, jack_request_t *req)
  758. {
  759. /* Called with the request_lock, but we need to call
  760. * handle_unload_client() *without* it. */
  761. if (req->x.intclient.id) {
  762. pthread_mutex_unlock (&engine->request_lock);
  763. req->status =
  764. handle_unload_client (engine, req->x.intclient.id);
  765. pthread_mutex_lock (&engine->request_lock);
  766. } else {
  767. VERBOSE (engine, "invalid unload request\n");
  768. req->status = JackFailure;
  769. }
  770. }