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.

1032 lines
25KB

  1. /* -*- mode: c; c-file-style: "bsd"; -*- */
  2. /*
  3. Copyright (C) 2001-2005 Paul Davis
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. */
  16. #include <config.h>
  17. #include <stdio.h>
  18. #include <ctype.h>
  19. #include <signal.h>
  20. #include <getopt.h>
  21. #include <sys/types.h>
  22. #include <sys/shm.h>
  23. #include <sys/wait.h>
  24. #include <string.h>
  25. #include <errno.h>
  26. #include <stdlib.h>
  27. #include <dirent.h>
  28. #include <dlfcn.h>
  29. #include <jack/midiport.h>
  30. #include <jack/intclient.h>
  31. #include <jack/uuid.h>
  32. #include "engine.h"
  33. #include "internal.h"
  34. #include "driver.h"
  35. #include "shm.h"
  36. #include "driver_parse.h"
  37. #include "messagebuffer.h"
  38. #include "clientengine.h"
  39. #ifdef USE_CAPABILITIES
  40. #include <sys/stat.h>
  41. /* capgetp and capsetp are linux only extensions, not posix */
  42. #undef _POSIX_SOURCE
  43. #include <sys/capability.h>
  44. #include "start.h"
  45. static struct stat pipe_stat;
  46. #endif /* USE_CAPABILITIES */
  47. static JSList *drivers = NULL;
  48. static sigset_t signals;
  49. static jack_engine_t *engine = NULL;
  50. static char *server_name = NULL;
  51. static int realtime = 1;
  52. static int realtime_priority = 10;
  53. static int do_mlock = 1;
  54. static int temporary = 0;
  55. static int verbose = 0;
  56. static int client_timeout = 0; /* msecs; if zero, use period size. */
  57. static unsigned int port_max = 256;
  58. static int do_unlock = 0;
  59. static jack_nframes_t frame_time_offset = 0;
  60. static int nozombies = 0;
  61. static int timeout_count_threshold = 0;
  62. extern int sanitycheck(int, int);
  63. static jack_driver_desc_t *
  64. jack_find_driver_descriptor(const char * name);
  65. static void
  66. do_nothing_handler (int sig)
  67. {
  68. /* this is used by the child (active) process, but it never
  69. gets called unless we are already shutting down after
  70. another signal.
  71. */
  72. char buf[64];
  73. snprintf (buf, sizeof(buf),
  74. "received signal %d during shutdown (ignored)\n", sig);
  75. write (1, buf, strlen (buf));
  76. }
  77. static void
  78. jack_load_internal_clients (JSList* load_list)
  79. {
  80. JSList * node;
  81. for (node = load_list; node; node = jack_slist_next (node)) {
  82. char* str = (char*)node->data;
  83. jack_request_t req;
  84. char* colon = strchr (str, ':');
  85. char* slash = strchr (str, '/');
  86. char* client_name = NULL;
  87. char* path = NULL;
  88. char* args = NULL;
  89. char* rest = NULL;
  90. int free_path = 0;
  91. int free_name = 0;
  92. size_t len;
  93. /* possible argument forms:
  94. client-name:client-type/args
  95. client-type/args
  96. client-name:client-type
  97. client-type
  98. client-name is the desired JACK client name.
  99. client-type is basically the name of the DLL/DSO without any suffix.
  100. args is a string whose contents will be passed to the client as
  101. it is instantiated
  102. */
  103. if ((slash == NULL && colon == NULL) || (slash && colon && colon > slash)) {
  104. /* client-type */
  105. client_name = str;
  106. path = client_name;
  107. } else if (slash && colon) {
  108. /* client-name:client-type/args */
  109. len = colon - str;
  110. if (len) {
  111. /* add 1 to leave space for a NULL */
  112. client_name = (char*)malloc (len + 1);
  113. free_name = 1;
  114. memcpy (client_name, str, len);
  115. client_name[len] = '\0';
  116. }
  117. len = slash - (colon + 1);
  118. if (len) {
  119. /* add 1 to leave space for a NULL */
  120. path = (char*)malloc (len + 1);
  121. free_path = 1;
  122. memcpy (path, colon + 1, len);
  123. path[len] = '\0';
  124. } else {
  125. path = client_name;
  126. }
  127. rest = slash + 1;
  128. len = strlen (rest);
  129. if (len) {
  130. /* add 1 to leave space for a NULL */
  131. args = (char*)malloc (len + 1);
  132. memcpy (args, rest, len);
  133. args[len] = '\0';
  134. }
  135. } else if (slash && colon == NULL) {
  136. /* client-type/args */
  137. len = slash - str;
  138. if (len) {
  139. /* add 1 to leave space for a NULL */
  140. path = (char*)malloc (len + 1);
  141. free_path = 1;
  142. memcpy (path, str, len);
  143. path[len] = '\0';
  144. }
  145. rest = slash + 1;
  146. len = strlen (rest);
  147. if (len) {
  148. /* add 1 to leave space for a NULL */
  149. args = (char*)malloc (len + 1);
  150. memcpy (args, rest, len);
  151. args[len] = '\0';
  152. }
  153. } else {
  154. /* client-name:client-type */
  155. len = colon - str;
  156. if (len) {
  157. /* add 1 to leave space for a NULL */
  158. client_name = (char*)malloc (len + 1);
  159. free_name = 1;
  160. memcpy (client_name, str, len);
  161. client_name[len] = '\0';
  162. path = colon + 1;
  163. }
  164. }
  165. if (client_name == NULL || path == NULL) {
  166. fprintf (stderr, "incorrect format for internal client specification (%s)\n", str);
  167. exit (1);
  168. }
  169. memset (&req, 0, sizeof(req));
  170. req.type = IntClientLoad;
  171. req.x.intclient.options = 0;
  172. strncpy (req.x.intclient.name, client_name, sizeof(req.x.intclient.name));
  173. strncpy (req.x.intclient.path, path, sizeof(req.x.intclient.path));
  174. if (args) {
  175. strncpy (req.x.intclient.init, args, sizeof(req.x.intclient.init));
  176. } else {
  177. req.x.intclient.init[0] = '\0';
  178. }
  179. pthread_mutex_lock (&engine->request_lock);
  180. jack_intclient_load_request (engine, &req);
  181. pthread_mutex_unlock (&engine->request_lock);
  182. if (free_name) {
  183. free (client_name);
  184. }
  185. if (free_path) {
  186. free (path);
  187. }
  188. if (args) {
  189. free (args);
  190. }
  191. }
  192. }
  193. static int
  194. jack_main (jack_driver_desc_t * driver_desc, JSList * driver_params, JSList * slave_names, JSList * load_list)
  195. {
  196. int sig;
  197. int i;
  198. sigset_t allsignals;
  199. struct sigaction action;
  200. int waiting;
  201. JSList * node;
  202. /* ensure that we are in our own process group so that
  203. kill (SIG, -pgrp) does the right thing.
  204. */
  205. setsid ();
  206. pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  207. /* what's this for?
  208. POSIX says that signals are delivered like this:
  209. * if a thread has blocked that signal, it is not
  210. a candidate to receive the signal.
  211. * of all threads not blocking the signal, pick
  212. one at random, and deliver the signal.
  213. this means that a simple-minded multi-threaded program can
  214. expect to get POSIX signals delivered randomly to any one
  215. of its threads,
  216. here, we block all signals that we think we might receive
  217. and want to catch. all "child" threads will inherit this
  218. setting. if we create a thread that calls sigwait() on the
  219. same set of signals, implicitly unblocking all those
  220. signals. any of those signals that are delivered to the
  221. process will be delivered to that thread, and that thread
  222. alone. this makes cleanup for a signal-driven exit much
  223. easier, since we know which thread is doing it and more
  224. importantly, we are free to call async-unsafe functions,
  225. because the code is executing in normal thread context
  226. after a return from sigwait().
  227. */
  228. sigemptyset (&signals);
  229. sigaddset (&signals, SIGHUP);
  230. sigaddset (&signals, SIGINT);
  231. sigaddset (&signals, SIGQUIT);
  232. sigaddset (&signals, SIGPIPE);
  233. sigaddset (&signals, SIGTERM);
  234. sigaddset (&signals, SIGUSR1);
  235. sigaddset (&signals, SIGUSR2);
  236. /* all child threads will inherit this mask unless they
  237. * explicitly reset it
  238. */
  239. pthread_sigmask (SIG_BLOCK, &signals, 0);
  240. if (!realtime && client_timeout == 0) {
  241. client_timeout = 500; /* 0.5 sec; usable when non realtime. */
  242. }
  243. /* get the engine/driver started */
  244. if ((engine = jack_engine_new (realtime, realtime_priority,
  245. do_mlock, do_unlock, server_name,
  246. temporary, verbose, client_timeout,
  247. port_max, getpid (), frame_time_offset,
  248. nozombies, timeout_count_threshold, drivers)) == 0) {
  249. jack_error ("cannot create engine");
  250. return -1;
  251. }
  252. jack_info ("loading driver ..");
  253. if (jack_engine_load_driver (engine, driver_desc, driver_params)) {
  254. jack_error ("cannot load driver module %s",
  255. driver_desc->name);
  256. goto error;
  257. }
  258. for (node = slave_names; node; node = jack_slist_next (node)) {
  259. char *sl_name = node->data;
  260. jack_driver_desc_t *sl_desc = jack_find_driver_descriptor (sl_name);
  261. if (sl_desc) {
  262. jack_engine_load_slave_driver (engine, sl_desc, NULL);
  263. }
  264. }
  265. if (jack_drivers_start (engine) != 0) {
  266. jack_error ("cannot start driver");
  267. goto error;
  268. }
  269. jack_load_internal_clients (load_list);
  270. /* install a do-nothing handler because otherwise pthreads
  271. behaviour is undefined when we enter sigwait.
  272. */
  273. sigfillset (&allsignals);
  274. action.sa_handler = do_nothing_handler;
  275. action.sa_mask = allsignals;
  276. action.sa_flags = SA_RESTART | SA_RESETHAND;
  277. for (i = 1; i < NSIG; i++) {
  278. if (sigismember (&signals, i)) {
  279. sigaction (i, &action, 0);
  280. }
  281. }
  282. if (verbose) {
  283. jack_info ("%d waiting for signals", getpid ());
  284. }
  285. waiting = TRUE;
  286. while (waiting) {
  287. sigwait (&signals, &sig);
  288. jack_info ("jack main caught signal %d", sig);
  289. switch (sig) {
  290. case SIGUSR1:
  291. jack_dump_configuration (engine, 1);
  292. break;
  293. case SIGUSR2:
  294. /* driver exit */
  295. waiting = FALSE;
  296. break;
  297. default:
  298. waiting = FALSE;
  299. break;
  300. }
  301. }
  302. if (sig != SIGSEGV) {
  303. /* unblock signals so we can see them during shutdown.
  304. this will help prod developers not to lose sight of
  305. bugs that cause segfaults etc. during shutdown.
  306. */
  307. sigprocmask (SIG_UNBLOCK, &signals, 0);
  308. }
  309. jack_engine_delete (engine);
  310. return 1;
  311. error:
  312. jack_engine_delete (engine);
  313. return -1;
  314. }
  315. static jack_driver_desc_t *
  316. jack_drivers_get_descriptor (JSList * drivers, const char * sofile)
  317. {
  318. jack_driver_desc_t * descriptor, * other_descriptor;
  319. JackDriverDescFunction so_get_descriptor;
  320. JSList * node;
  321. void * dlhandle;
  322. char * filename;
  323. const char * dlerr;
  324. int err;
  325. char* driver_dir;
  326. if ((driver_dir = getenv ("JACK_DRIVER_DIR")) == 0) {
  327. driver_dir = ADDON_DIR;
  328. }
  329. filename = malloc (strlen (driver_dir) + 1 + strlen (sofile) + 1);
  330. sprintf (filename, "%s/%s", driver_dir, sofile);
  331. if (verbose) {
  332. jack_info ("getting driver descriptor from %s", filename);
  333. }
  334. if ((dlhandle = dlopen (filename, RTLD_NOW | RTLD_GLOBAL)) == NULL) {
  335. jack_error ("could not open driver .so '%s': %s\n", filename, dlerror ());
  336. free (filename);
  337. return NULL;
  338. }
  339. so_get_descriptor = (JackDriverDescFunction)
  340. dlsym (dlhandle, "driver_get_descriptor");
  341. if ((dlerr = dlerror ()) != NULL) {
  342. jack_error ("%s", dlerr);
  343. dlclose (dlhandle);
  344. free (filename);
  345. return NULL;
  346. }
  347. if ((descriptor = so_get_descriptor ()) == NULL) {
  348. jack_error ("driver from '%s' returned NULL descriptor\n", filename);
  349. dlclose (dlhandle);
  350. free (filename);
  351. return NULL;
  352. }
  353. if ((err = dlclose (dlhandle)) != 0) {
  354. jack_error ("error closing driver .so '%s': %s\n", filename, dlerror ());
  355. }
  356. /* check it doesn't exist already */
  357. for (node = drivers; node; node = jack_slist_next (node)) {
  358. other_descriptor = (jack_driver_desc_t*)node->data;
  359. if (strcmp (descriptor->name, other_descriptor->name) == 0) {
  360. jack_error ("the drivers in '%s' and '%s' both have the name '%s'; using the first\n",
  361. other_descriptor->file, filename, other_descriptor->name);
  362. /* FIXME: delete the descriptor */
  363. free (filename);
  364. return NULL;
  365. }
  366. }
  367. snprintf (descriptor->file, sizeof(descriptor->file), "%s", filename);
  368. free (filename);
  369. return descriptor;
  370. }
  371. static JSList *
  372. jack_drivers_load ()
  373. {
  374. struct dirent * dir_entry;
  375. DIR * dir_stream;
  376. const char * ptr;
  377. int err;
  378. JSList * driver_list = NULL;
  379. jack_driver_desc_t * desc;
  380. char* driver_dir;
  381. if ((driver_dir = getenv ("JACK_DRIVER_DIR")) == 0) {
  382. driver_dir = ADDON_DIR;
  383. }
  384. /* search through the driver_dir and add get descriptors
  385. from the .so files in it */
  386. dir_stream = opendir (driver_dir);
  387. if (!dir_stream) {
  388. jack_error ("could not open driver directory %s: %s\n",
  389. driver_dir, strerror (errno));
  390. return NULL;
  391. }
  392. while ( (dir_entry = readdir (dir_stream)) ) {
  393. /* check the filename is of the right format */
  394. if (strncmp ("jack_", dir_entry->d_name, 5) != 0) {
  395. continue;
  396. }
  397. ptr = strrchr (dir_entry->d_name, '.');
  398. if (!ptr) {
  399. continue;
  400. }
  401. ptr++;
  402. if (strncmp ("so", ptr, 2) != 0) {
  403. continue;
  404. }
  405. desc = jack_drivers_get_descriptor (drivers, dir_entry->d_name);
  406. if (desc) {
  407. driver_list = jack_slist_append (driver_list, desc);
  408. }
  409. }
  410. err = closedir (dir_stream);
  411. if (err) {
  412. jack_error ("error closing driver directory %s: %s\n",
  413. driver_dir, strerror (errno));
  414. }
  415. if (!driver_list) {
  416. jack_error ("could not find any drivers in %s!\n", driver_dir);
  417. return NULL;
  418. }
  419. return driver_list;
  420. }
  421. static void copyright (FILE* file)
  422. {
  423. fprintf (file, "jackd " VERSION "\n"
  424. "Copyright 2001-2009 Paul Davis, Stephane Letz, Jack O'Quinn, Torben Hohn and others.\n"
  425. "jackd comes with ABSOLUTELY NO WARRANTY\n"
  426. "This is free software, and you are welcome to redistribute it\n"
  427. "under certain conditions; see the file COPYING for details\n\n");
  428. }
  429. static void usage (FILE *file)
  430. {
  431. copyright (file);
  432. fprintf (file, "\n"
  433. "usage: jackd [ server options ] -d backend [ ... backend options ... ]\n"
  434. " (see the manual page for jackd for a complete list of options)\n\n"
  435. #ifdef __APPLE__
  436. " Available backends may include: coreaudio, dummy, net, portaudio.\n\n"
  437. #else
  438. " Available backends may include: alsa, dummy, freebob, firewire, net, oss, sun, portaudio or sndio.\n\n"
  439. #endif
  440. " jackd -d backend --help\n"
  441. " to display options for each backend\n\n");
  442. }
  443. static jack_driver_desc_t *
  444. jack_find_driver_descriptor (const char * name)
  445. {
  446. jack_driver_desc_t * desc = 0;
  447. JSList * node;
  448. for (node = drivers; node; node = jack_slist_next (node)) {
  449. desc = (jack_driver_desc_t*)node->data;
  450. if (strcmp (desc->name, name) != 0) {
  451. desc = NULL;
  452. } else {
  453. break;
  454. }
  455. }
  456. return desc;
  457. }
  458. static void
  459. jack_cleanup_files (const char *server_name)
  460. {
  461. DIR *dir;
  462. struct dirent *dirent;
  463. char dir_name[PATH_MAX + 1] = "";
  464. jack_server_dir (server_name, dir_name);
  465. /* On termination, we remove all files that jackd creates so
  466. * subsequent attempts to start jackd will not believe that an
  467. * instance is already running. If the server crashes or is
  468. * terminated with SIGKILL, this is not possible. So, cleanup
  469. * is also attempted when jackd starts.
  470. *
  471. * There are several tricky issues. First, the previous JACK
  472. * server may have run for a different user ID, so its files
  473. * may be inaccessible. This is handled by using a separate
  474. * JACK_TMP_DIR subdirectory for each user. Second, there may
  475. * be other servers running with different names. Each gets
  476. * its own subdirectory within the per-user directory. The
  477. * current process has already registered as `server_name', so
  478. * we know there is no other server actively using that name.
  479. */
  480. /* nothing to do if the server directory does not exist */
  481. if ((dir = opendir (dir_name)) == NULL) {
  482. return;
  483. }
  484. /* unlink all the files in this directory, they are mine */
  485. while ((dirent = readdir (dir)) != NULL) {
  486. char fullpath[PATH_MAX + 1];
  487. if ((strcmp (dirent->d_name, ".") == 0)
  488. || (strcmp (dirent->d_name, "..") == 0)) {
  489. continue;
  490. }
  491. snprintf (fullpath, sizeof(fullpath), "%s/%s",
  492. dir_name, dirent->d_name);
  493. if (unlink (fullpath)) {
  494. jack_error ("cannot unlink `%s' (%s)", fullpath,
  495. strerror (errno));
  496. }
  497. }
  498. closedir (dir);
  499. /* now, delete the per-server subdirectory, itself */
  500. if (rmdir (dir_name)) {
  501. jack_error ("cannot remove `%s' (%s)", dir_name,
  502. strerror (errno));
  503. }
  504. /* finally, delete the per-user subdirectory, if empty */
  505. if (rmdir (jack_user_dir ())) {
  506. if (errno != ENOTEMPTY) {
  507. jack_error ("cannot remove `%s' (%s)",
  508. jack_user_dir (), strerror (errno));
  509. }
  510. }
  511. }
  512. static void
  513. maybe_use_capabilities ()
  514. {
  515. #ifdef USE_CAPABILITIES
  516. int status;
  517. /* check to see if there is a pipe in the right descriptor */
  518. if ((status = fstat (PIPE_WRITE_FD, &pipe_stat)) == 0 &&
  519. S_ISFIFO (pipe_stat.st_mode)) {
  520. /* tell jackstart we are up and running */
  521. char c = 1;
  522. if (write (PIPE_WRITE_FD, &c, 1) != 1) {
  523. jack_error ("cannot write to jackstart sync "
  524. "pipe %d (%s)", PIPE_WRITE_FD,
  525. strerror (errno));
  526. }
  527. if (close (PIPE_WRITE_FD) != 0) {
  528. jack_error ("jackd: error on startup pipe close: %s",
  529. strerror (errno));
  530. } else {
  531. /* wait for jackstart process to set our capabilities */
  532. if (wait (&status) == -1) {
  533. jack_error ("jackd: wait for startup "
  534. "process exit failed");
  535. }
  536. if (!WIFEXITED (status) || WEXITSTATUS (status)) {
  537. jack_error ("jackd: jackstart did not "
  538. "exit cleanly");
  539. exit (1);
  540. }
  541. }
  542. }
  543. #endif /* USE_CAPABILITIES */
  544. }
  545. int
  546. main (int argc, char *argv[])
  547. {
  548. jack_driver_desc_t * desc;
  549. int replace_registry = 0;
  550. int do_sanity_checks = 1;
  551. int show_version = 0;
  552. #ifdef HAVE_ZITA_BRIDGE_DEPS
  553. const char *options = "A:d:P:uvshVrRZTFlI:t:mM:n:Np:c:X:C:";
  554. #else
  555. const char *options = "d:P:uvshVrRZTFlI:t:mM:n:Np:c:X:C:";
  556. #endif
  557. struct option long_options[] =
  558. {
  559. /* keep ordered by single-letter option code */
  560. #ifdef HAVE_ZITA_BRIDGE_DEPS
  561. { "alsa-add", 1, 0, 'A' },
  562. #endif
  563. { "clock-source", 1, 0, 'c' },
  564. { "driver", 1, 0, 'd' },
  565. { "help", 0, 0, 'h' },
  566. { "tmpdir-location", 0, 0, 'l' },
  567. { "internal-client", 0, 0, 'I' },
  568. { "no-mlock", 0, 0, 'm' },
  569. { "midi-bufsize", 1, 0, 'M' },
  570. { "name", 1, 0, 'n' },
  571. { "no-sanity-checks", 0, 0, 'N' },
  572. { "port-max", 1, 0, 'p' },
  573. { "realtime-priority", 1, 0, 'P' },
  574. { "no-realtime", 0, 0, 'r' },
  575. { "realtime", 0, 0, 'R' },
  576. { "replace-registry", 0, &replace_registry, 0 },
  577. { "silent", 0, 0, 's' },
  578. { "sync", 0, 0, 'S' },
  579. { "timeout", 1, 0, 't' },
  580. { "temporary", 0, 0, 'T' },
  581. { "unlock", 0, 0, 'u' },
  582. { "version", 0, 0, 'V' },
  583. { "verbose", 0, 0, 'v' },
  584. { "slave-driver", 1, 0, 'X' },
  585. { "nozombies", 0, 0, 'Z' },
  586. { "timeout-thres", 2, 0, 'C' },
  587. { 0, 0, 0, 0 }
  588. };
  589. int opt = 0;
  590. int option_index = 0;
  591. int seen_driver = 0;
  592. char *driver_name = NULL;
  593. char **driver_args = NULL;
  594. JSList * driver_params;
  595. JSList * slave_drivers = NULL;
  596. JSList * load_list = NULL;
  597. size_t midi_buffer_size = 0;
  598. int driver_nargs = 1;
  599. int i;
  600. int rc;
  601. #ifdef HAVE_ZITA_BRIDGE_DEPS
  602. const char* alsa_add_client_name_playback = "zalsa_out";
  603. const char* alsa_add_client_name_capture = "zalsa_in";
  604. char alsa_add_args[64];
  605. char* dirstr;
  606. #endif
  607. setvbuf (stdout, NULL, _IOLBF, 0);
  608. maybe_use_capabilities ();
  609. opterr = 0;
  610. while (!seen_driver &&
  611. (opt = getopt_long (argc, argv, options,
  612. long_options, &option_index)) != EOF) {
  613. switch (opt) {
  614. #ifdef HAVE_ZITA_BRIDGE_DEPS
  615. case 'A':
  616. /* add a new internal client named after the ALSA device name
  617. given as optarg, using the last character 'p' or 'c' to
  618. indicate playback or capture. If there isn't one,
  619. assume capture (common case: USB mics etc.)
  620. */
  621. if ((dirstr = strstr (optarg, "%p")) != NULL && dirstr == (optarg + strlen (optarg) - 2)) {
  622. snprintf (alsa_add_args, sizeof(alsa_add_args), "%.*s_play:%s/-dhw:%.*s",
  623. (int)strlen (optarg) - 2, optarg,
  624. alsa_add_client_name_playback,
  625. (int)strlen (optarg) - 2, optarg);
  626. load_list = jack_slist_append (load_list, strdup (alsa_add_args));
  627. } else if ((dirstr = strstr (optarg, "%c")) != NULL && dirstr == (optarg + strlen (optarg) - 2)) {
  628. snprintf (alsa_add_args, sizeof(alsa_add_args), "%.*s_rec:%s/-dhw:%.*s",
  629. (int)strlen (optarg) - 2, optarg,
  630. alsa_add_client_name_capture,
  631. (int)strlen (optarg) - 2, optarg);
  632. load_list = jack_slist_append (load_list, strdup (alsa_add_args));
  633. } else {
  634. snprintf (alsa_add_args, sizeof(alsa_add_args), "%s_play:%s/-dhw:%s",
  635. optarg,
  636. alsa_add_client_name_playback,
  637. optarg);
  638. load_list = jack_slist_append (load_list, strdup (alsa_add_args));
  639. snprintf (alsa_add_args, sizeof(alsa_add_args), "%s_rec:%s/-dhw:%s",
  640. optarg,
  641. alsa_add_client_name_capture,
  642. optarg);
  643. load_list = jack_slist_append (load_list, strdup (alsa_add_args));
  644. }
  645. break;
  646. #endif
  647. case 'c':
  648. if (tolower (optarg[0]) == 'h') {
  649. clock_source = JACK_TIMER_HPET;
  650. } else if (tolower (optarg[0]) == 'c') {
  651. /* For backwards compatibility with scripts,
  652. * allow the user to request the cycle clock
  653. * on the command line, but use the system
  654. * clock instead
  655. */
  656. clock_source = JACK_TIMER_SYSTEM_CLOCK;
  657. } else if (tolower (optarg[0]) == 's') {
  658. clock_source = JACK_TIMER_SYSTEM_CLOCK;
  659. } else {
  660. usage (stderr);
  661. return -1;
  662. }
  663. break;
  664. case 'C':
  665. if (optarg) {
  666. timeout_count_threshold = atoi (optarg);
  667. } else {
  668. timeout_count_threshold = 250;
  669. }
  670. break;
  671. case 'd':
  672. seen_driver = optind + 1;
  673. driver_name = optarg;
  674. break;
  675. case 'D':
  676. frame_time_offset = JACK_MAX_FRAMES - atoi (optarg);
  677. break;
  678. case 'l':
  679. /* special flag to allow libjack to determine jackd's idea of where tmpdir is */
  680. printf("%s\n", DEFAULT_TMP_DIR);
  681. exit (0);
  682. case 'I':
  683. load_list = jack_slist_append (load_list, optarg);
  684. break;
  685. case 'm':
  686. do_mlock = 0;
  687. break;
  688. case 'M':
  689. midi_buffer_size = (unsigned int)atol (optarg);
  690. break;
  691. case 'n':
  692. server_name = optarg;
  693. break;
  694. case 'N':
  695. do_sanity_checks = 0;
  696. break;
  697. case 'p':
  698. port_max = (unsigned int)atol (optarg);
  699. break;
  700. case 'P':
  701. realtime_priority = atoi (optarg);
  702. break;
  703. case 'r':
  704. realtime = 0;
  705. break;
  706. case 'R':
  707. /* this is now the default */
  708. realtime = 1;
  709. break;
  710. case 's':
  711. jack_set_error_function (silent_jack_error_callback);
  712. break;
  713. case 'S':
  714. /* this option is for jack2 only (synchronous mode) */
  715. break;
  716. case 'T':
  717. temporary = 1;
  718. break;
  719. case 't':
  720. client_timeout = atoi (optarg);
  721. break;
  722. case 'u':
  723. do_unlock = 1;
  724. break;
  725. case 'v':
  726. verbose = 1;
  727. break;
  728. case 'V':
  729. show_version = 1;
  730. break;
  731. case 'X':
  732. slave_drivers = jack_slist_append (slave_drivers, optarg);
  733. break;
  734. case 'Z':
  735. nozombies = 1;
  736. break;
  737. default:
  738. jack_error ("Unknown option character %c",
  739. optopt);
  740. /*fallthru*/
  741. case 'h':
  742. usage (stdout);
  743. return -1;
  744. }
  745. }
  746. if (show_version) {
  747. printf ( "jackd version " VERSION
  748. " tmpdir " DEFAULT_TMP_DIR
  749. " protocol " PROTOCOL_VERSION
  750. "\n");
  751. return 0;
  752. }
  753. copyright (stdout);
  754. if (do_sanity_checks && (0 < sanitycheck (realtime, FALSE))) {
  755. return -1;
  756. }
  757. if (!seen_driver) {
  758. usage (stderr);
  759. exit (1);
  760. }
  761. /* DIRTY HACK needed to pick up -X supplied as part of ALSA driver args. This is legacy
  762. hack to make control apps like qjackctl based on the < 0.124 command line interface
  763. continue to work correctly.
  764. If -X seq was given as part of the driver args, load the ALSA MIDI slave driver.
  765. */
  766. for (i = seen_driver; i < argc; ++i) {
  767. if (strcmp (argv[i], "-X") == 0) {
  768. if (argc >= i + 2) {
  769. if (strcmp (argv[i + 1], "seq") == 0) {
  770. slave_drivers = jack_slist_append (slave_drivers, "alsa_midi");
  771. }
  772. }
  773. break;
  774. } else if (strcmp (argv[i], "-Xseq") == 0) {
  775. slave_drivers = jack_slist_append (slave_drivers, "alsa_midi");
  776. break;
  777. }
  778. }
  779. drivers = jack_drivers_load ();
  780. if (!drivers) {
  781. fprintf (stderr, "jackd: no drivers found; exiting\n");
  782. exit (1);
  783. }
  784. if (midi_buffer_size != 0) {
  785. jack_port_type_info_t* port_type = &jack_builtin_port_types[JACK_MIDI_PORT_TYPE];
  786. port_type->buffer_size = midi_buffer_size * jack_midi_internal_event_size ();
  787. port_type->buffer_scale_factor = -1;
  788. if (verbose) {
  789. fprintf (stderr, "Set MIDI buffer size to %u bytes\n", port_type->buffer_size);
  790. }
  791. }
  792. desc = jack_find_driver_descriptor (driver_name);
  793. if (!desc) {
  794. fprintf (stderr, "jackd: unknown driver '%s'\n", driver_name);
  795. exit (1);
  796. }
  797. if (optind < argc) {
  798. driver_nargs = 1 + argc - optind;
  799. } else {
  800. driver_nargs = 1;
  801. }
  802. if (driver_nargs == 0) {
  803. fprintf (stderr, "No driver specified ... hmm. JACK won't do"
  804. " anything when run like this.\n");
  805. return -1;
  806. }
  807. driver_args = (char**)malloc (sizeof(char *) * driver_nargs);
  808. driver_args[0] = driver_name;
  809. for (i = 1; i < driver_nargs; i++)
  810. driver_args[i] = argv[optind++];
  811. if (jack_parse_driver_params (desc, driver_nargs,
  812. driver_args, &driver_params)) {
  813. exit (0);
  814. }
  815. if (server_name == NULL) {
  816. server_name = jack_default_server_name ();
  817. }
  818. rc = jack_register_server (server_name, replace_registry);
  819. switch (rc) {
  820. case EEXIST:
  821. fprintf (stderr, "`%s' server already active\n", server_name);
  822. exit (1);
  823. case ENOSPC:
  824. fprintf (stderr, "too many servers already active\n");
  825. exit (2);
  826. case ENOMEM:
  827. fprintf (stderr, "no access to shm registry\n");
  828. exit (3);
  829. default:
  830. if (verbose) {
  831. fprintf (stderr, "server `%s' registered\n",
  832. server_name);
  833. }
  834. }
  835. /* clean up shared memory and files from any previous
  836. * instance of this server name */
  837. jack_cleanup_shm ();
  838. jack_cleanup_files (server_name);
  839. /* run the server engine until it terminates */
  840. jack_main (desc, driver_params, slave_drivers, load_list);
  841. /* clean up shared memory and files from this server instance */
  842. if (verbose) {
  843. fprintf (stderr, "cleaning up shared memory\n");
  844. }
  845. jack_cleanup_shm ();
  846. if (verbose) {
  847. fprintf (stderr, "cleaning up files\n");
  848. }
  849. jack_cleanup_files (server_name);
  850. if (verbose) {
  851. fprintf (stderr, "unregistering server `%s'\n", server_name);
  852. }
  853. jack_unregister_server (server_name);
  854. exit (0);
  855. }