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.

1132 lines
29KB

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