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.

730 lines
18KB

  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. $Id$
  16. */
  17. #include <config.h>
  18. #include <stdio.h>
  19. #include <ctype.h>
  20. #include <signal.h>
  21. #include <getopt.h>
  22. #include <sys/types.h>
  23. #include <sys/shm.h>
  24. #include <sys/wait.h>
  25. #include <string.h>
  26. #include <errno.h>
  27. #include <stdlib.h>
  28. #include <dirent.h>
  29. #include <dlfcn.h>
  30. #include <jack/engine.h>
  31. #include <jack/internal.h>
  32. #include <jack/driver.h>
  33. #include <jack/shm.h>
  34. #include <jack/driver_parse.h>
  35. #include <jack/messagebuffer.h>
  36. #ifdef USE_CAPABILITIES
  37. #include <sys/stat.h>
  38. /* capgetp and capsetp are linux only extensions, not posix */
  39. #undef _POSIX_SOURCE
  40. #include <sys/capability.h>
  41. #include <jack/start.h>
  42. static struct stat pipe_stat;
  43. #endif /* USE_CAPABILITIES */
  44. static JSList *drivers = NULL;
  45. static sigset_t signals;
  46. static jack_engine_t *engine = NULL;
  47. static char *server_name = NULL;
  48. static int realtime = 0;
  49. static int realtime_priority = 10;
  50. static int do_mlock = 1;
  51. static int temporary = 0;
  52. static int verbose = 0;
  53. static int client_timeout = 0; /* msecs; if zero, use period size. */
  54. static unsigned int port_max = 256;
  55. static int do_unlock = 0;
  56. static jack_nframes_t frame_time_offset = 0;
  57. static void
  58. do_nothing_handler (int sig)
  59. {
  60. /* this is used by the child (active) process, but it never
  61. gets called unless we are already shutting down after
  62. another signal.
  63. */
  64. char buf[64];
  65. snprintf (buf, sizeof(buf),
  66. "received signal %d during shutdown (ignored)\n", sig);
  67. write (1, buf, strlen (buf));
  68. }
  69. static int
  70. jack_main (jack_driver_desc_t * driver_desc, JSList * driver_params)
  71. {
  72. int sig;
  73. int i;
  74. sigset_t allsignals;
  75. struct sigaction action;
  76. int waiting;
  77. /* ensure that we are in our own process group so that
  78. kill (SIG, -pgrp) does the right thing.
  79. */
  80. setsid ();
  81. pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  82. /* what's this for?
  83. POSIX says that signals are delivered like this:
  84. * if a thread has blocked that signal, it is not
  85. a candidate to receive the signal.
  86. * of all threads not blocking the signal, pick
  87. one at random, and deliver the signal.
  88. this means that a simple-minded multi-threaded program can
  89. expect to get POSIX signals delivered randomly to any one
  90. of its threads,
  91. here, we block all signals that we think we might receive
  92. and want to catch. all "child" threads will inherit this
  93. setting. if we create a thread that calls sigwait() on the
  94. same set of signals, implicitly unblocking all those
  95. signals. any of those signals that are delivered to the
  96. process will be delivered to that thread, and that thread
  97. alone. this makes cleanup for a signal-driven exit much
  98. easier, since we know which thread is doing it and more
  99. importantly, we are free to call async-unsafe functions,
  100. because the code is executing in normal thread context
  101. after a return from sigwait().
  102. */
  103. sigemptyset (&signals);
  104. sigaddset(&signals, SIGHUP);
  105. sigaddset(&signals, SIGINT);
  106. sigaddset(&signals, SIGQUIT);
  107. sigaddset(&signals, SIGPIPE);
  108. sigaddset(&signals, SIGTERM);
  109. sigaddset(&signals, SIGUSR1);
  110. sigaddset(&signals, SIGUSR2);
  111. /* all child threads will inherit this mask unless they
  112. * explicitly reset it
  113. */
  114. pthread_sigmask (SIG_BLOCK, &signals, 0);
  115. if (!realtime && client_timeout == 0)
  116. client_timeout = 500; /* 0.5 sec; usable when non realtime. */
  117. /* get the engine/driver started */
  118. if ((engine = jack_engine_new (realtime, realtime_priority,
  119. do_mlock, do_unlock, server_name,
  120. temporary, verbose, client_timeout,
  121. port_max, getpid(), frame_time_offset, drivers)) == 0) {
  122. fprintf (stderr, "cannot create engine\n");
  123. return -1;
  124. }
  125. fprintf (stderr, "loading driver ..\n");
  126. if (jack_engine_load_driver (engine, driver_desc, driver_params)) {
  127. fprintf (stderr, "cannot load driver module %s\n",
  128. driver_desc->name);
  129. goto error;
  130. }
  131. if (engine->driver->start (engine->driver) != 0) {
  132. jack_error ("cannot start driver");
  133. goto error;
  134. }
  135. /* install a do-nothing handler because otherwise pthreads
  136. behaviour is undefined when we enter sigwait.
  137. */
  138. sigfillset (&allsignals);
  139. action.sa_handler = do_nothing_handler;
  140. action.sa_mask = allsignals;
  141. action.sa_flags = SA_RESTART|SA_RESETHAND;
  142. for (i = 1; i < NSIG; i++) {
  143. if (sigismember (&signals, i)) {
  144. sigaction (i, &action, 0);
  145. }
  146. }
  147. if (verbose) {
  148. fprintf (stderr, "%d waiting for signals\n", getpid());
  149. }
  150. waiting = TRUE;
  151. while (waiting) {
  152. sigwait (&signals, &sig);
  153. fprintf (stderr, "jack main caught signal %d\n", sig);
  154. switch (sig) {
  155. case SIGUSR1:
  156. jack_dump_configuration(engine, 1);
  157. break;
  158. case SIGUSR2:
  159. /* driver exit */
  160. waiting = FALSE;
  161. break;
  162. default:
  163. waiting = FALSE;
  164. break;
  165. }
  166. }
  167. if (sig != SIGSEGV) {
  168. /* unblock signals so we can see them during shutdown.
  169. this will help prod developers not to lose sight of
  170. bugs that cause segfaults etc. during shutdown.
  171. */
  172. sigprocmask (SIG_UNBLOCK, &signals, 0);
  173. }
  174. jack_engine_delete (engine);
  175. return 1;
  176. error:
  177. jack_engine_delete (engine);
  178. return -1;
  179. }
  180. static jack_driver_desc_t *
  181. jack_drivers_get_descriptor (JSList * drivers, const char * sofile)
  182. {
  183. jack_driver_desc_t * descriptor, * other_descriptor;
  184. JackDriverDescFunction so_get_descriptor;
  185. JSList * node;
  186. void * dlhandle;
  187. char * filename;
  188. const char * dlerr;
  189. int err;
  190. char* driver_dir;
  191. if ((driver_dir = getenv("JACK_DRIVER_DIR")) == 0) {
  192. driver_dir = ADDON_DIR;
  193. }
  194. filename = malloc (strlen (driver_dir) + 1 + strlen (sofile) + 1);
  195. sprintf (filename, "%s/%s", driver_dir, sofile);
  196. if (verbose) {
  197. fprintf (stderr, "getting driver descriptor from %s\n", filename);
  198. }
  199. if ((dlhandle = dlopen (filename, RTLD_NOW|RTLD_GLOBAL)) == NULL) {
  200. jack_error ("could not open driver .so '%s': %s\n", filename, dlerror ());
  201. free (filename);
  202. return NULL;
  203. }
  204. so_get_descriptor = (JackDriverDescFunction)
  205. dlsym (dlhandle, "driver_get_descriptor");
  206. if ((dlerr = dlerror ()) != NULL) {
  207. fprintf(stderr, "%s\n", dlerr);
  208. dlclose (dlhandle);
  209. free (filename);
  210. return NULL;
  211. }
  212. if ((descriptor = so_get_descriptor ()) == NULL) {
  213. jack_error ("driver from '%s' returned NULL descriptor\n", filename);
  214. dlclose (dlhandle);
  215. free (filename);
  216. return NULL;
  217. }
  218. if ((err = dlclose (dlhandle)) != 0) {
  219. jack_error ("error closing driver .so '%s': %s\n", filename, dlerror ());
  220. }
  221. /* check it doesn't exist already */
  222. for (node = drivers; node; node = jack_slist_next (node)) {
  223. other_descriptor = (jack_driver_desc_t *) node->data;
  224. if (strcmp (descriptor->name, other_descriptor->name) == 0) {
  225. jack_error ("the drivers in '%s' and '%s' both have the name '%s'; using the first\n",
  226. other_descriptor->file, filename, other_descriptor->name);
  227. /* FIXME: delete the descriptor */
  228. free (filename);
  229. return NULL;
  230. }
  231. }
  232. snprintf (descriptor->file, sizeof(descriptor->file), "%s", filename);
  233. free (filename);
  234. return descriptor;
  235. }
  236. static JSList *
  237. jack_drivers_load ()
  238. {
  239. struct dirent * dir_entry;
  240. DIR * dir_stream;
  241. const char * ptr;
  242. int err;
  243. JSList * driver_list = NULL;
  244. jack_driver_desc_t * desc;
  245. char* driver_dir;
  246. if ((driver_dir = getenv("JACK_DRIVER_DIR")) == 0) {
  247. driver_dir = ADDON_DIR;
  248. }
  249. /* search through the driver_dir and add get descriptors
  250. from the .so files in it */
  251. dir_stream = opendir (driver_dir);
  252. if (!dir_stream) {
  253. jack_error ("could not open driver directory %s: %s\n",
  254. driver_dir, strerror (errno));
  255. return NULL;
  256. }
  257. while ( (dir_entry = readdir (dir_stream)) ) {
  258. /* check the filename is of the right format */
  259. if (strncmp ("jack_", dir_entry->d_name, 5) != 0) {
  260. continue;
  261. }
  262. ptr = strrchr (dir_entry->d_name, '.');
  263. if (!ptr) {
  264. continue;
  265. }
  266. ptr++;
  267. if (strncmp ("so", ptr, 2) != 0) {
  268. continue;
  269. }
  270. desc = jack_drivers_get_descriptor (drivers, dir_entry->d_name);
  271. if (desc) {
  272. driver_list = jack_slist_append (driver_list, desc);
  273. }
  274. }
  275. err = closedir (dir_stream);
  276. if (err) {
  277. jack_error ("error closing driver directory %s: %s\n",
  278. driver_dir, strerror (errno));
  279. }
  280. if (!driver_list) {
  281. jack_error ("could not find any drivers in %s!\n", driver_dir);
  282. return NULL;
  283. }
  284. return driver_list;
  285. }
  286. static void copyright (FILE* file)
  287. {
  288. fprintf (file, "jackd " VERSION "\n"
  289. "Copyright 2001-2005 Paul Davis and others.\n"
  290. "jackd comes with ABSOLUTELY NO WARRANTY\n"
  291. "This is free software, and you are welcome to redistribute it\n"
  292. "under certain conditions; see the file COPYING for details\n\n");
  293. }
  294. static void usage (FILE *file)
  295. {
  296. copyright (file);
  297. fprintf (file, "\n"
  298. "usage: jackd [ --realtime OR -R [ --realtime-priority OR -P priority ] ]\n"
  299. " [ --name OR -n server-name ]\n"
  300. " [ --no-mlock OR -m ]\n"
  301. " [ --unlock OR -u ]\n"
  302. " [ --timeout OR -t client-timeout-in-msecs ]\n"
  303. " [ --port-max OR -p maximum-number-of-ports]\n"
  304. " [ --debug-timer OR -D ]\n"
  305. " [ --verbose OR -v ]\n"
  306. " [ --clocksource OR -c [ c(ycle) | h(pet) | s(ystem) ]\n"
  307. " [ --silent OR -s ]\n"
  308. " [ --version OR -V ]\n"
  309. " -d backend [ ... backend args ... ]\n"
  310. " The backend can be `alsa', `coreaudio', `dummy',\n"
  311. " `freebob', `oss' or `portaudio'.\n\n"
  312. " jackd -d backend --help\n"
  313. " to display options for each backend\n\n");
  314. }
  315. static jack_driver_desc_t *
  316. jack_find_driver_descriptor (const char * name)
  317. {
  318. jack_driver_desc_t * desc = 0;
  319. JSList * node;
  320. for (node = drivers; node; node = jack_slist_next (node)) {
  321. desc = (jack_driver_desc_t *) node->data;
  322. if (strcmp (desc->name, name) != 0) {
  323. desc = NULL;
  324. } else {
  325. break;
  326. }
  327. }
  328. return desc;
  329. }
  330. static void
  331. jack_cleanup_files (const char *server_name)
  332. {
  333. DIR *dir;
  334. struct dirent *dirent;
  335. char dir_name[PATH_MAX+1] = "";
  336. jack_server_dir (server_name, dir_name);
  337. /* On termination, we remove all files that jackd creates so
  338. * subsequent attempts to start jackd will not believe that an
  339. * instance is already running. If the server crashes or is
  340. * terminated with SIGKILL, this is not possible. So, cleanup
  341. * is also attempted when jackd starts.
  342. *
  343. * There are several tricky issues. First, the previous JACK
  344. * server may have run for a different user ID, so its files
  345. * may be inaccessible. This is handled by using a separate
  346. * JACK_TMP_DIR subdirectory for each user. Second, there may
  347. * be other servers running with different names. Each gets
  348. * its own subdirectory within the per-user directory. The
  349. * current process has already registered as `server_name', so
  350. * we know there is no other server actively using that name.
  351. */
  352. /* nothing to do if the server directory does not exist */
  353. if ((dir = opendir (dir_name)) == NULL) {
  354. return;
  355. }
  356. /* unlink all the files in this directory, they are mine */
  357. while ((dirent = readdir (dir)) != NULL) {
  358. char fullpath[PATH_MAX+1];
  359. if ((strcmp (dirent->d_name, ".") == 0)
  360. || (strcmp (dirent->d_name, "..") == 0)) {
  361. continue;
  362. }
  363. snprintf (fullpath, sizeof (fullpath), "%s/%s",
  364. dir_name, dirent->d_name);
  365. if (unlink (fullpath)) {
  366. jack_error ("cannot unlink `%s' (%s)", fullpath,
  367. strerror (errno));
  368. }
  369. }
  370. closedir (dir);
  371. /* now, delete the per-server subdirectory, itself */
  372. if (rmdir (dir_name)) {
  373. jack_error ("cannot remove `%s' (%s)", dir_name,
  374. strerror (errno));
  375. }
  376. /* finally, delete the per-user subdirectory, if empty */
  377. if (rmdir (jack_user_dir ())) {
  378. if (errno != ENOTEMPTY) {
  379. jack_error ("cannot remove `%s' (%s)",
  380. jack_user_dir (), strerror (errno));
  381. }
  382. }
  383. }
  384. static void
  385. maybe_use_capabilities ()
  386. {
  387. #ifdef USE_CAPABILITIES
  388. int status;
  389. /* check to see if there is a pipe in the right descriptor */
  390. if ((status = fstat (PIPE_WRITE_FD, &pipe_stat)) == 0 &&
  391. S_ISFIFO(pipe_stat.st_mode)) {
  392. /* tell jackstart we are up and running */
  393. char c = 1;
  394. if (write (PIPE_WRITE_FD, &c, 1) != 1) {
  395. fprintf (stderr, "cannot write to jackstart sync "
  396. "pipe %d (%s)\n", PIPE_WRITE_FD,
  397. strerror (errno));
  398. }
  399. if (close(PIPE_WRITE_FD) != 0) {
  400. fprintf(stderr,
  401. "jackd: error on startup pipe close: %s\n",
  402. strerror (errno));
  403. } else {
  404. /* wait for jackstart process to set our capabilities */
  405. if (wait (&status) == -1) {
  406. fprintf (stderr, "jackd: wait for startup "
  407. "process exit failed\n");
  408. }
  409. if (!WIFEXITED (status) || WEXITSTATUS (status)) {
  410. fprintf(stderr, "jackd: jackstart did not "
  411. "exit cleanly\n");
  412. exit (1);
  413. }
  414. }
  415. }
  416. #endif /* USE_CAPABILITIES */
  417. }
  418. int
  419. main (int argc, char *argv[])
  420. {
  421. jack_driver_desc_t * desc;
  422. const char *options = "-ad:P:uvshVRTFlt:mn:p:c:";
  423. struct option long_options[] =
  424. {
  425. { "driver", 1, 0, 'd' },
  426. { "verbose", 0, 0, 'v' },
  427. { "help", 0, 0, 'h' },
  428. { "tmpdir-location", 0, 0, 'l' },
  429. { "port-max", 1, 0, 'p' },
  430. { "no-mlock", 0, 0, 'm' },
  431. { "name", 1, 0, 'n' },
  432. { "unlock", 0, 0, 'u' },
  433. { "realtime", 0, 0, 'R' },
  434. { "realtime-priority", 1, 0, 'P' },
  435. { "timeout", 1, 0, 't' },
  436. { "temporary", 0, 0, 'T' },
  437. { "version", 0, 0, 'V' },
  438. { "silent", 0, 0, 's' },
  439. { "clock-source", 1, 0, 'c' },
  440. { 0, 0, 0, 0 }
  441. };
  442. int opt = 0;
  443. int option_index = 0;
  444. int seen_driver = 0;
  445. char *driver_name = NULL;
  446. char **driver_args = NULL;
  447. JSList * driver_params;
  448. int driver_nargs = 1;
  449. int show_version = 0;
  450. int i;
  451. int rc;
  452. setvbuf (stdout, NULL, _IOLBF, 0);
  453. maybe_use_capabilities ();
  454. opterr = 0;
  455. while (!seen_driver &&
  456. (opt = getopt_long (argc, argv, options,
  457. long_options, &option_index)) != EOF) {
  458. switch (opt) {
  459. case 'c':
  460. if (tolower (optarg[0]) == 'h') {
  461. clock_source = JACK_TIMER_HPET;
  462. } else if (tolower (optarg[0]) == 'c') {
  463. clock_source = JACK_TIMER_CYCLE_COUNTER;
  464. } else if (tolower (optarg[0]) == 's') {
  465. clock_source = JACK_TIMER_SYSTEM_CLOCK;
  466. } else {
  467. usage (stderr);
  468. return -1;
  469. }
  470. break;
  471. case 'd':
  472. seen_driver = 1;
  473. driver_name = optarg;
  474. break;
  475. case 'D':
  476. frame_time_offset = JACK_MAX_FRAMES - atoi(optarg);
  477. break;
  478. case 'l':
  479. /* special flag to allow libjack to determine jackd's idea of where tmpdir is */
  480. printf ("%s\n", jack_tmpdir);
  481. exit (0);
  482. case 'm':
  483. do_mlock = 0;
  484. break;
  485. case 'n':
  486. server_name = optarg;
  487. break;
  488. case 'p':
  489. port_max = (unsigned int) atol (optarg);
  490. break;
  491. case 'P':
  492. realtime_priority = atoi (optarg);
  493. break;
  494. case 'R':
  495. realtime = 1;
  496. break;
  497. case 's':
  498. jack_set_error_function (silent_jack_error_callback);
  499. break;
  500. case 'T':
  501. temporary = 1;
  502. break;
  503. case 't':
  504. client_timeout = atoi (optarg);
  505. break;
  506. case 'u':
  507. do_unlock = 1;
  508. break;
  509. case 'v':
  510. verbose = 1;
  511. break;
  512. case 'V':
  513. show_version = 1;
  514. break;
  515. default:
  516. fprintf (stderr, "Unknown option character %c\n",
  517. optopt);
  518. /*fallthru*/
  519. case 'h':
  520. usage (stdout);
  521. return -1;
  522. }
  523. }
  524. if (show_version) {
  525. printf ( "jackd version " VERSION
  526. " tmpdir " DEFAULT_TMP_DIR
  527. " protocol " PROTOCOL_VERSION
  528. "\n");
  529. return -1;
  530. }
  531. if (realtime && (client_timeout >= JACKD_WATCHDOG_TIMEOUT)) {
  532. usage (stderr);
  533. fprintf (stderr, "In realtime mode (-R) the client timeout must be smaller than the watchdog timeout (%ims).\n", JACKD_WATCHDOG_TIMEOUT);
  534. exit (1);
  535. }
  536. if (!seen_driver) {
  537. usage (stderr);
  538. exit (1);
  539. }
  540. drivers = jack_drivers_load ();
  541. if (!drivers) {
  542. fprintf (stderr, "jackd: no drivers found; exiting\n");
  543. exit (1);
  544. }
  545. desc = jack_find_driver_descriptor (driver_name);
  546. if (!desc) {
  547. fprintf (stderr, "jackd: unknown driver '%s'\n", driver_name);
  548. exit (1);
  549. }
  550. if (optind < argc) {
  551. driver_nargs = 1 + argc - optind;
  552. } else {
  553. driver_nargs = 1;
  554. }
  555. if (driver_nargs == 0) {
  556. fprintf (stderr, "No driver specified ... hmm. JACK won't do"
  557. " anything when run like this.\n");
  558. return -1;
  559. }
  560. driver_args = (char **) malloc (sizeof (char *) * driver_nargs);
  561. driver_args[0] = driver_name;
  562. for (i = 1; i < driver_nargs; i++) {
  563. driver_args[i] = argv[optind++];
  564. }
  565. if (jack_parse_driver_params (desc, driver_nargs,
  566. driver_args, &driver_params)) {
  567. exit (0);
  568. }
  569. if (server_name == NULL)
  570. server_name = jack_default_server_name ();
  571. copyright (stdout);
  572. rc = jack_register_server (server_name);
  573. switch (rc) {
  574. case EEXIST:
  575. fprintf (stderr, "`%s' server already active\n", server_name);
  576. exit (1);
  577. case ENOSPC:
  578. fprintf (stderr, "too many servers already active\n");
  579. exit (2);
  580. case ENOMEM:
  581. fprintf (stderr, "no access to shm registry\n");
  582. exit (3);
  583. default:
  584. if (verbose)
  585. fprintf (stderr, "server `%s' registered\n",
  586. server_name);
  587. }
  588. /* clean up shared memory and files from any previous
  589. * instance of this server name */
  590. jack_cleanup_shm ();
  591. jack_cleanup_files (server_name);
  592. /* run the server engine until it terminates */
  593. jack_main (desc, driver_params);
  594. /* clean up shared memory and files from this server instance */
  595. if (verbose)
  596. fprintf (stderr, "cleaning up shared memory\n");
  597. jack_cleanup_shm ();
  598. if (verbose)
  599. fprintf (stderr, "cleaning up files\n");
  600. jack_cleanup_files (server_name);
  601. if (verbose)
  602. fprintf (stderr, "unregistering server `%s'\n", server_name);
  603. jack_unregister_server (server_name);
  604. exit (0);
  605. }