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.

1217 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. client->ports = 0;
  56. }
  57. /* Modified for new client execution order algorithm.
  58. * FA 08/2015
  59. */
  60. int
  61. jack_client_do_deactivate (jack_engine_t *engine,
  62. jack_client_internal_t *client, int sort_graph)
  63. {
  64. /* caller must hold engine->client_lock and must have checked for and/or
  65. * cleared all connections held by client.
  66. */
  67. VERBOSE (engine, "+++ deactivate %s", client->control->name);
  68. client->control->active = FALSE;
  69. jack_transport_client_exit (engine, client);
  70. if (!jack_client_is_internal (client) &&
  71. engine->external_client_cnt > 0) {
  72. engine->external_client_cnt--;
  73. }
  74. if (sort_graph) {
  75. engine->execlist_request = EXECLIST_ORDER | EXECLIST_CYCLE;
  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. /* Modified for new client execution order algorithm.
  274. * FA 08/2015
  275. */
  276. void
  277. jack_remove_clients (jack_engine_t* engine, int* exit_freewheeling_when_done)
  278. {
  279. JSList *tmp, *node;
  280. int need_sort = FALSE;
  281. jack_client_internal_t *client;
  282. /* CALLER MUST HOLD GRAPH LOCK */
  283. VERBOSE (engine, "++ Removing failed clients ...");
  284. /* remove all dead clients */
  285. for (node = engine->clients; node; ) {
  286. tmp = jack_slist_next (node);
  287. client = (jack_client_internal_t*)node->data;
  288. VERBOSE (engine, "client %s error status %d", client->control->name, client->error);
  289. if (client->error) {
  290. if (engine->freewheeling && jack_uuid_compare (client->control->uuid, engine->fwclient) == 0) {
  291. VERBOSE (engine, "freewheeling client has errors");
  292. *exit_freewheeling_when_done = 1;
  293. }
  294. /* if we have a communication problem with the
  295. client, remove it. otherwise, turn it into
  296. a zombie. the client will/should realize
  297. this and will close its sockets. then
  298. we'll end up back here again and will
  299. finally remove the client.
  300. */
  301. if (client->error >= JACK_ERROR_WITH_SOCKETS) {
  302. VERBOSE (engine, "removing failed "
  303. "client %s state = %s errors"
  304. " = %d",
  305. client->control->name,
  306. jack_client_state_name (client),
  307. client->error);
  308. jack_remove_client (engine,
  309. (jack_client_internal_t*)
  310. node->data);
  311. } else {
  312. VERBOSE (engine, "client failure: "
  313. "client %s state = %s errors"
  314. " = %d",
  315. client->control->name,
  316. jack_client_state_name (client),
  317. client->error);
  318. if (!engine->nozombies) {
  319. jack_zombify_client (engine,
  320. (jack_client_internal_t*)
  321. node->data);
  322. client->error = 0;
  323. }
  324. }
  325. need_sort = TRUE;
  326. }
  327. node = tmp;
  328. }
  329. if (need_sort) {
  330. engine->execlist_request = EXECLIST_ORDER | EXECLIST_CYCLE;
  331. jack_sort_graph (engine);
  332. }
  333. jack_engine_reset_rolling_usecs (engine);
  334. VERBOSE (engine, "-- Removing failed clients ...");
  335. }
  336. jack_client_internal_t *
  337. jack_client_by_name (jack_engine_t *engine, const char *name)
  338. {
  339. jack_client_internal_t *client = NULL;
  340. JSList *node;
  341. jack_rdlock_graph (engine);
  342. for (node = engine->clients; node; node = jack_slist_next (node)) {
  343. if (strcmp ((const char*)((jack_client_internal_t*)
  344. node->data)->control->name,
  345. name) == 0) {
  346. client = (jack_client_internal_t*)node->data;
  347. break;
  348. }
  349. }
  350. jack_unlock_graph (engine);
  351. return client;
  352. }
  353. static int
  354. jack_client_id_by_name (jack_engine_t *engine, const char *name, jack_uuid_t id)
  355. {
  356. JSList *node;
  357. int ret = -1;
  358. jack_uuid_clear (&id);
  359. jack_rdlock_graph (engine);
  360. for (node = engine->clients; node; node = jack_slist_next (node)) {
  361. if (strcmp ((const char*)((jack_client_internal_t*)
  362. node->data)->control->name,
  363. name) == 0) {
  364. jack_client_internal_t *client =
  365. (jack_client_internal_t*)node->data;
  366. jack_uuid_copy (&id, client->control->uuid);
  367. ret = 0;
  368. break;
  369. }
  370. }
  371. jack_unlock_graph (engine);
  372. return ret;
  373. }
  374. jack_client_internal_t *
  375. jack_client_internal_by_id (jack_engine_t *engine, jack_uuid_t id)
  376. {
  377. jack_client_internal_t *client = NULL;
  378. JSList *node;
  379. /* call tree ***MUST HOLD*** the graph lock */
  380. for (node = engine->clients; node; node = jack_slist_next (node)) {
  381. if (jack_uuid_compare (((jack_client_internal_t*)node->data)->control->uuid, id) == 0) {
  382. client = (jack_client_internal_t*)node->data;
  383. break;
  384. }
  385. }
  386. return client;
  387. }
  388. int
  389. jack_client_name_reserved ( jack_engine_t *engine, const char *name )
  390. {
  391. JSList *node;
  392. for (node = engine->reserved_client_names; node; node = jack_slist_next (node)) {
  393. jack_reserved_name_t *reservation = (jack_reserved_name_t*)node->data;
  394. if (!strcmp (reservation->name, name)) {
  395. return 1;
  396. }
  397. }
  398. return 0;
  399. }
  400. /* generate a unique client name
  401. *
  402. * returns 0 if successful, updates name in place
  403. */
  404. static inline int
  405. jack_generate_unique_name (jack_engine_t *engine, char *name)
  406. {
  407. int tens, ones;
  408. int length = strlen (name);
  409. if (length > JACK_CLIENT_NAME_SIZE - 4) {
  410. jack_error ("%s exists and is too long to make unique", name);
  411. return 1; /* failure */
  412. }
  413. /* generate a unique name by appending "-01".."-99" */
  414. name[length++] = '-';
  415. tens = length++;
  416. ones = length++;
  417. name[tens] = '0';
  418. name[ones] = '1';
  419. name[length] = '\0';
  420. while (jack_client_by_name (engine, name) || jack_client_name_reserved ( engine, name )) {
  421. if (name[ones] == '9') {
  422. if (name[tens] == '9') {
  423. jack_error ("client %s has 99 extra"
  424. " instances already", name);
  425. return 1; /* give up */
  426. }
  427. name[tens]++;
  428. name[ones] = '0';
  429. } else {
  430. name[ones]++;
  431. }
  432. }
  433. return 0;
  434. }
  435. static int
  436. jack_client_name_invalid (jack_engine_t *engine, char *name,
  437. jack_options_t options, jack_status_t *status)
  438. {
  439. /* Since this is always called from the server thread, no
  440. * other new client will be created at the same time. So,
  441. * testing a name for uniqueness is valid here. When called
  442. * from jack_engine_load_driver() this is not strictly true,
  443. * but that seems to be adequately serialized due to engine
  444. * startup. There are no other clients at that point, anyway.
  445. */
  446. if (jack_client_by_name (engine, name) || jack_client_name_reserved (engine, name )) {
  447. *status |= JackNameNotUnique;
  448. if (options & JackUseExactName) {
  449. jack_error ("cannot create new client; %s already"
  450. " exists", name);
  451. *status |= JackFailure;
  452. return TRUE;
  453. }
  454. if (jack_generate_unique_name (engine, name)) {
  455. *status |= JackFailure;
  456. return TRUE;
  457. }
  458. }
  459. return FALSE;
  460. }
  461. /* Set up the engine's client internal and control structures for both
  462. * internal and external clients. */
  463. /* Modified for new client execution order algorithm.
  464. * FA 08/2015
  465. */
  466. static jack_client_internal_t *
  467. jack_setup_client_control (jack_engine_t *engine, int fd, ClientType type, const char *name, jack_uuid_t uuid)
  468. {
  469. jack_client_internal_t *client;
  470. client = (jack_client_internal_t*)
  471. malloc (sizeof(jack_client_internal_t));
  472. client->request_fd = fd;
  473. client->event_fd = -1;
  474. client->ports = 0;
  475. client->feedlist = 0;
  476. client->depcount = 0;
  477. client->depcheck = 0;
  478. client->execlist_next = 0;
  479. client->execution_order = UINT_MAX;
  480. client->handle = NULL;
  481. client->finish = NULL;
  482. client->error = 0;
  483. client->private_client = NULL;
  484. if (type != ClientExternal) {
  485. client->control = (jack_client_control_t*)
  486. malloc (sizeof(jack_client_control_t));
  487. } else {
  488. if (jack_shmalloc (sizeof(jack_client_control_t),
  489. &client->control_shm)) {
  490. jack_error ("cannot create client control block for %s",
  491. name);
  492. free (client);
  493. return 0;
  494. }
  495. if (jack_attach_shm (&client->control_shm)) {
  496. jack_error ("cannot attach to client control block "
  497. "for %s (%s)", name, strerror (errno));
  498. jack_destroy_shm (&client->control_shm);
  499. free (client);
  500. return 0;
  501. }
  502. client->control = (jack_client_control_t*)
  503. jack_shm_addr (&client->control_shm);
  504. }
  505. client->control->type = type;
  506. client->control->active = 0;
  507. client->control->dead = FALSE;
  508. client->control->timed_out = 0;
  509. if (jack_uuid_empty (uuid)) {
  510. client->control->uuid = jack_client_uuid_generate ();
  511. } else {
  512. jack_uuid_copy (&client->control->uuid, uuid);
  513. }
  514. strcpy ((char*)client->control->name, name);
  515. client->subgraph_start_fd = -1;
  516. client->subgraph_wait_fd = -1;
  517. client->session_reply_pending = FALSE;
  518. client->control->process_cbset = FALSE;
  519. client->control->bufsize_cbset = FALSE;
  520. client->control->srate_cbset = FALSE;
  521. client->control->xrun_cbset = FALSE;
  522. client->control->port_register_cbset = FALSE;
  523. client->control->port_connect_cbset = FALSE;
  524. client->control->graph_order_cbset = FALSE;
  525. client->control->client_register_cbset = FALSE;
  526. client->control->thread_cb_cbset = FALSE;
  527. client->control->session_cbset = FALSE;
  528. client->control->property_cbset = FALSE;
  529. client->control->latency_cbset = FALSE;
  530. #if 0
  531. if (type != ClientExternal) {
  532. client->process = NULL;
  533. client->process_arg = NULL;
  534. client->bufsize = NULL;
  535. client->bufsize_arg = NULL;
  536. client->srate = NULL;
  537. client->srate_arg = NULL;
  538. client->xrun = NULL;
  539. client->xrun_arg = NULL;
  540. client->port_register = NULL;
  541. client->port_register_arg = NULL;
  542. client->port_connect = NULL;
  543. client->port_connect_arg = NULL;
  544. client->graph_order = NULL;
  545. client->graph_order_arg = NULL;
  546. client->client_register = NULL;
  547. client->client_register_arg = NULL;
  548. client->thread_cb = NULL;
  549. client->thread_cb_arg = NULL;
  550. }
  551. #endif
  552. jack_transport_client_new (client);
  553. #ifdef JACK_USE_MACH_THREADS
  554. /* specific resources for server/client real-time thread
  555. * communication */
  556. allocate_mach_serverport (engine, client);
  557. client->running = FALSE;
  558. #endif
  559. return client;
  560. }
  561. static void
  562. jack_ensure_uuid_unique (jack_engine_t *engine, jack_uuid_t uuid)
  563. {
  564. JSList *node;
  565. if (jack_uuid_empty (uuid)) {
  566. return;
  567. }
  568. jack_lock_graph (engine);
  569. for (node = engine->clients; node; node = jack_slist_next (node)) {
  570. jack_client_internal_t *client = (jack_client_internal_t*)node->data;
  571. if (jack_uuid_compare (client->control->uuid, uuid) == 0) {
  572. jack_uuid_clear (&uuid);
  573. }
  574. }
  575. jack_unlock_graph (engine);
  576. }
  577. /* set up all types of clients */
  578. static jack_client_internal_t *
  579. setup_client (jack_engine_t *engine, ClientType type, char *name,
  580. jack_uuid_t uuid,
  581. jack_options_t options, jack_status_t *status, int client_fd,
  582. const char *object_path, const char *object_data)
  583. {
  584. /* called with the request_lock */
  585. jack_client_internal_t *client;
  586. char bufx[64];
  587. /* validate client name, generate a unique one if appropriate */
  588. if (jack_client_name_invalid (engine, name, options, status)) {
  589. return NULL;
  590. }
  591. jack_ensure_uuid_unique (engine, uuid);
  592. /* create a client struct for this name */
  593. if ((client = jack_setup_client_control (engine, client_fd,
  594. type, name, uuid)) == NULL) {
  595. *status |= (JackFailure | JackInitFailure);
  596. jack_error ("cannot create new client object");
  597. return NULL;
  598. }
  599. /* only for internal clients, driver is already loaded */
  600. if (type == ClientInternal) {
  601. if (jack_load_client (engine, client, object_path)) {
  602. jack_error ("cannot dynamically load client from"
  603. " \"%s\"", object_path);
  604. jack_client_delete (engine, client);
  605. *status |= (JackFailure | JackLoadFailure);
  606. return NULL;
  607. }
  608. }
  609. jack_uuid_unparse (client->control->uuid, bufx);
  610. VERBOSE (engine, "new client: %s, uuid = %s"
  611. " type %d @ %p fd = %d",
  612. client->control->name, bufx,
  613. type, client->control, client_fd);
  614. if (jack_client_is_internal (client)) {
  615. // XXX: do i need to lock the graph here ?
  616. // i moved this one up in the init process, lets see what happens.
  617. /* Internal clients need to make regular JACK API
  618. * calls, which need a jack_client_t structure.
  619. * Create one here.
  620. */
  621. client->private_client =
  622. jack_client_alloc_internal (client->control, engine);
  623. /* Set up the pointers necessary for the request
  624. * system to work. The client is in the same address
  625. * space */
  626. client->private_client->deliver_request = internal_client_request;
  627. client->private_client->deliver_arg = engine;
  628. }
  629. /* add new client to the clients list */
  630. jack_lock_graph (engine);
  631. engine->clients = jack_slist_prepend (engine->clients, client);
  632. jack_engine_reset_rolling_usecs (engine);
  633. if (jack_client_is_internal (client)) {
  634. jack_unlock_graph (engine);
  635. /* Call its initialization function. This function
  636. * may make requests of its own, so we temporarily
  637. * release and then reacquire the request_lock. */
  638. if (client->control->type == ClientInternal) {
  639. pthread_mutex_unlock (&engine->request_lock);
  640. if (client->initialize (client->private_client,
  641. object_data)) {
  642. /* failed: clean up client data */
  643. VERBOSE (engine,
  644. "%s jack_initialize() failed!",
  645. client->control->name);
  646. jack_lock_graph (engine);
  647. jack_remove_client (engine, client);
  648. jack_unlock_graph (engine);
  649. *status |= (JackFailure | JackInitFailure);
  650. client = NULL;
  651. //JOQ: not clear that all allocated
  652. //storage has been cleaned up properly.
  653. }
  654. pthread_mutex_lock (&engine->request_lock);
  655. }
  656. } else {
  657. jack_unlock_graph (engine);
  658. }
  659. return client;
  660. }
  661. jack_client_internal_t *
  662. jack_create_driver_client (jack_engine_t *engine, char *name)
  663. {
  664. jack_client_connect_request_t req;
  665. jack_status_t status;
  666. jack_client_internal_t *client;
  667. jack_uuid_t empty_uuid = JACK_UUID_EMPTY_INITIALIZER;
  668. VALGRIND_MEMSET (&empty_uuid, 0, sizeof(empty_uuid));
  669. snprintf (req.name, sizeof(req.name), "%s", name);
  670. jack_uuid_clear (&empty_uuid);
  671. pthread_mutex_lock (&engine->request_lock);
  672. client = setup_client (engine, ClientDriver, name, empty_uuid, JackUseExactName,
  673. &status, -1, NULL, NULL);
  674. pthread_mutex_unlock (&engine->request_lock);
  675. return client;
  676. }
  677. static jack_status_t
  678. handle_unload_client (jack_engine_t *engine, jack_uuid_t id)
  679. {
  680. /* called *without* the request_lock */
  681. jack_client_internal_t *client;
  682. jack_status_t status = (JackNoSuchClient | JackFailure);
  683. jack_lock_graph (engine);
  684. if ((client = jack_client_internal_by_id (engine, id))) {
  685. VERBOSE (engine, "unloading client \"%s\"",
  686. client->control->name);
  687. if (client->control->type != ClientInternal) {
  688. status = JackFailure | JackInvalidOption;
  689. } else {
  690. jack_remove_client (engine, client);
  691. status = 0;
  692. }
  693. }
  694. jack_unlock_graph (engine);
  695. return status;
  696. }
  697. static char *
  698. jack_get_reserved_name (jack_engine_t *engine, jack_uuid_t uuid)
  699. {
  700. JSList *node;
  701. for (node = engine->reserved_client_names; node; node = jack_slist_next (node)) {
  702. jack_reserved_name_t *reservation = (jack_reserved_name_t*)node->data;
  703. if (jack_uuid_compare (reservation->uuid, uuid) != 0) {
  704. char *retval = strdup (reservation->name);
  705. free (reservation);
  706. engine->reserved_client_names =
  707. jack_slist_remove (engine->reserved_client_names, reservation);
  708. return retval;
  709. }
  710. }
  711. return 0;
  712. }
  713. int
  714. jack_client_create (jack_engine_t *engine, int client_fd)
  715. {
  716. /* called *without* the request_lock */
  717. jack_client_internal_t *client;
  718. jack_client_connect_request_t req;
  719. jack_client_connect_result_t res;
  720. ssize_t nbytes;
  721. res.status = 0;
  722. VALGRIND_MEMSET (&res, 0, sizeof(res));
  723. nbytes = read (client_fd, &req, sizeof(req));
  724. if (nbytes == 0) { /* EOF? */
  725. jack_error ("cannot read connection request from client (%s)", strerror (errno));
  726. return -1;
  727. }
  728. /* First verify protocol version (first field of request), if
  729. * present, then make sure request has the expected length. */
  730. if ((nbytes < sizeof(req.protocol_v))
  731. || (req.protocol_v != jack_protocol_version)
  732. || (nbytes != sizeof(req))) {
  733. /* JACK protocol incompatibility */
  734. res.status |= (JackFailure | JackVersionError);
  735. jack_error ("JACK protocol mismatch (%d vs %d)", req.protocol_v, jack_protocol_version);
  736. if (write (client_fd, &res, sizeof(res)) != sizeof(res)) {
  737. jack_error ("cannot write client connection response");
  738. }
  739. return -1;
  740. }
  741. if (!req.load) { /* internal client close? */
  742. int rc = -1;
  743. jack_uuid_t id = JACK_UUID_EMPTY_INITIALIZER;
  744. if (jack_client_id_by_name (engine, req.name, id) == 0) {
  745. rc = handle_unload_client (engine, id);
  746. }
  747. /* close does not send a reply */
  748. return rc;
  749. }
  750. pthread_mutex_lock (&engine->request_lock);
  751. if (!jack_uuid_empty (req.uuid)) {
  752. char *res_name = jack_get_reserved_name (engine, req.uuid);
  753. if (res_name) {
  754. snprintf (req.name, sizeof(req.name), "%s", res_name);
  755. free (res_name);
  756. }
  757. }
  758. client = setup_client (engine, req.type, req.name, req.uuid,
  759. req.options, &res.status, client_fd,
  760. req.object_path, req.object_data);
  761. pthread_mutex_unlock (&engine->request_lock);
  762. if (client == NULL) {
  763. res.status |= JackFailure; /* just making sure */
  764. return -1;
  765. }
  766. res.client_shm_index = client->control_shm.index;
  767. res.engine_shm_index = engine->control_shm.index;
  768. res.realtime = engine->control->real_time;
  769. res.realtime_priority = engine->rtpriority - 1;
  770. strncpy (res.name, req.name, sizeof(res.name));
  771. #ifdef JACK_USE_MACH_THREADS
  772. /* Mach port number for server/client communication */
  773. res.portnum = client->portnum;
  774. #endif
  775. if (jack_client_is_internal (client)) {
  776. /* the ->control pointers are for an internal client
  777. so we know they are the right sized pointers
  778. for this server. however, to keep the result
  779. structure the same size for both 32 and 64 bit
  780. clients/servers, the result structure stores
  781. them as 64 bit integer, so we have to do a slightly
  782. forced cast here.
  783. */
  784. res.client_control = (uint64_t)((intptr_t)client->control);
  785. res.engine_control = (uint64_t)((intptr_t)engine->control);
  786. } else {
  787. strcpy (res.fifo_prefix, engine->fifo_prefix);
  788. }
  789. if (write (client_fd, &res, sizeof(res)) != sizeof(res)) {
  790. jack_error ("cannot write connection response to client");
  791. jack_lock_graph (engine);
  792. client->control->dead = 1;
  793. jack_remove_client (engine, client);
  794. jack_unlock_graph (engine);
  795. return -1;
  796. }
  797. if (jack_client_is_internal (client)) {
  798. close (client_fd);
  799. }
  800. jack_client_registration_notify (engine, (const char*)client->control->name, 1);
  801. return 0;
  802. }
  803. /* Modified for new client execution order algorithm.
  804. * FA 08/2015
  805. */
  806. int
  807. jack_client_activate (jack_engine_t *engine, jack_uuid_t id)
  808. {
  809. jack_client_internal_t *client;
  810. JSList *node;
  811. int ret = -1;
  812. int i;
  813. jack_event_t event;
  814. VALGRIND_MEMSET (&event, 0, sizeof(event));
  815. jack_lock_graph (engine);
  816. if ((client = jack_client_internal_by_id (engine, id))) {
  817. client->control->active = TRUE;
  818. jack_transport_activate (engine, client);
  819. /* we call this to make sure the FIFO is
  820. * built+ready by the time the client needs
  821. * it. we don't care about the return value at
  822. * this point.
  823. */
  824. jack_get_fifo_fd (engine, ++engine->external_client_cnt);
  825. engine->execlist_request = EXECLIST_ORDER;
  826. jack_sort_graph (engine);
  827. for (i = 0; i < engine->control->n_port_types; ++i) {
  828. event.type = AttachPortSegment;
  829. event.y.ptid = i;
  830. jack_deliver_event (engine, client, &event);
  831. }
  832. event.type = BufferSizeChange;
  833. event.x.n = engine->control->buffer_size;
  834. jack_deliver_event (engine, client, &event);
  835. // send delayed notifications for ports.
  836. for (node = client->ports; node; node = jack_slist_next (node)) {
  837. jack_port_internal_t *port = (jack_port_internal_t*)node->data;
  838. jack_port_registration_notify (engine, port->shared->id, TRUE);
  839. }
  840. ret = 0;
  841. }
  842. jack_unlock_graph (engine);
  843. return ret;
  844. }
  845. int
  846. jack_client_deactivate (jack_engine_t *engine, jack_uuid_t id)
  847. {
  848. JSList *node;
  849. int ret = -1;
  850. jack_lock_graph (engine);
  851. for (node = engine->clients; node; node = jack_slist_next (node)) {
  852. jack_client_internal_t *client =
  853. (jack_client_internal_t*)node->data;
  854. if (jack_uuid_compare (client->control->uuid, id) == 0) {
  855. JSList *portnode;
  856. jack_port_internal_t *port;
  857. for (portnode = client->ports; portnode;
  858. portnode = jack_slist_next (portnode)) {
  859. port = (jack_port_internal_t*)portnode->data;
  860. jack_port_clear_connections (engine, port);
  861. }
  862. ret = jack_client_do_deactivate (engine, client, TRUE);
  863. break;
  864. }
  865. }
  866. jack_unlock_graph (engine);
  867. return ret;
  868. }
  869. int
  870. jack_mark_client_socket_error (jack_engine_t *engine, int fd)
  871. {
  872. /* CALLER MUST HOLD GRAPH LOCK */
  873. jack_client_internal_t *client = 0;
  874. JSList *node;
  875. for (node = engine->clients; node; node = jack_slist_next (node)) {
  876. if (jack_client_is_internal ((jack_client_internal_t*)
  877. node->data)) {
  878. continue;
  879. }
  880. if (((jack_client_internal_t*)node->data)->request_fd == fd) {
  881. client = (jack_client_internal_t*)node->data;
  882. break;
  883. }
  884. }
  885. if (client) {
  886. VERBOSE (engine, "marking client %s with SOCKET error state = "
  887. "%s errors = %d", client->control->name,
  888. jack_client_state_name (client),
  889. client->error);
  890. client->error += JACK_ERROR_WITH_SOCKETS;
  891. }
  892. return 0;
  893. }
  894. void
  895. jack_client_delete (jack_engine_t *engine, jack_client_internal_t *client)
  896. {
  897. jack_uuid_t uuid = JACK_UUID_EMPTY_INITIALIZER;
  898. jack_uuid_copy (&uuid, client->control->uuid);
  899. jack_client_registration_notify (engine, (const char*)client->control->name, 0);
  900. jack_remove_properties (NULL, uuid);
  901. /* have to do the notification ourselves, since the client argument
  902. to jack_remove_properties() was NULL
  903. */
  904. jack_property_change_notify (engine, PropertyDeleted, uuid, NULL);
  905. if (jack_client_is_internal (client)) {
  906. free (client->private_client);
  907. free ((void*)client->control);
  908. } else {
  909. /* release the client segment, mark it for
  910. destruction, and free up the shm registry
  911. information so that it can be reused.
  912. */
  913. jack_release_shm (&client->control_shm);
  914. jack_destroy_shm (&client->control_shm);
  915. }
  916. free (client);
  917. }
  918. void
  919. jack_intclient_handle_request (jack_engine_t *engine, jack_request_t *req)
  920. {
  921. jack_client_internal_t *client;
  922. req->status = 0;
  923. if ((client = jack_client_by_name (engine, req->x.intclient.name))) {
  924. jack_uuid_copy (&req->x.intclient.uuid, client->control->uuid);
  925. } else {
  926. req->status |= (JackNoSuchClient | JackFailure);
  927. }
  928. }
  929. void
  930. jack_intclient_load_request (jack_engine_t *engine, jack_request_t *req)
  931. {
  932. /* called with the request_lock */
  933. jack_client_internal_t *client;
  934. jack_status_t status = 0;
  935. jack_uuid_t empty_uuid = JACK_UUID_EMPTY_INITIALIZER;
  936. VERBOSE (engine, "load internal client %s from %s, init `%s', "
  937. "options: 0x%x", req->x.intclient.name,
  938. req->x.intclient.path, req->x.intclient.init,
  939. req->x.intclient.options);
  940. jack_uuid_clear (&empty_uuid);
  941. client = setup_client (engine, ClientInternal, req->x.intclient.name, empty_uuid,
  942. req->x.intclient.options | JackUseExactName, &status, -1,
  943. req->x.intclient.path, req->x.intclient.init);
  944. if (client == NULL) {
  945. status |= JackFailure; /* just making sure */
  946. jack_uuid_clear (&req->x.intclient.uuid);
  947. VERBOSE (engine, "load failed, status = 0x%x", status);
  948. } else {
  949. jack_uuid_copy (&req->x.intclient.uuid, client->control->uuid);
  950. }
  951. req->status = status;
  952. }
  953. void
  954. jack_intclient_name_request (jack_engine_t *engine, jack_request_t *req)
  955. {
  956. jack_client_internal_t *client;
  957. jack_rdlock_graph (engine);
  958. if ((client = jack_client_internal_by_id (engine,
  959. req->x.intclient.uuid))) {
  960. strncpy ((char*)req->x.intclient.name,
  961. (char*)client->control->name,
  962. sizeof(req->x.intclient.name));
  963. req->status = 0;
  964. } else {
  965. req->status = (JackNoSuchClient | JackFailure);
  966. }
  967. jack_unlock_graph (engine);
  968. }
  969. void
  970. jack_intclient_unload_request (jack_engine_t *engine, jack_request_t *req)
  971. {
  972. /* Called with the request_lock, but we need to call
  973. * handle_unload_client() *without* it. */
  974. if (!jack_uuid_empty (req->x.intclient.uuid)) {
  975. /* non-empty UUID */
  976. pthread_mutex_unlock (&engine->request_lock);
  977. req->status = handle_unload_client (engine, req->x.intclient.uuid);
  978. pthread_mutex_lock (&engine->request_lock);
  979. } else {
  980. VERBOSE (engine, "invalid unload request");
  981. req->status = JackFailure;
  982. }
  983. }