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.

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