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