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.

1018 lines
26KB

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