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.

1165 lines
31KB

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