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.

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