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