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.

589 lines
14KB

  1. /* -*- mode: c; c-file-style: "bsd"; -*- */
  2. /*
  3. Copyright (C) 2001-2003 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 <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/engine.h>
  30. #include <jack/internal.h>
  31. #include <jack/driver.h>
  32. #include <jack/shm.h>
  33. #include <jack/driver_parse.h>
  34. #ifdef USE_CAPABILITIES
  35. #include <sys/stat.h>
  36. /* capgetp and capsetp are linux only extensions, not posix */
  37. #undef _POSIX_SOURCE
  38. #include <sys/capability.h>
  39. #include <jack/start.h>
  40. static struct stat pipe_stat;
  41. #endif /* USE_CAPABILITIES */
  42. static JSList * drivers = NULL;
  43. static sigset_t signals;
  44. static jack_engine_t *engine = 0;
  45. static int realtime = 0;
  46. static int realtime_priority = 10;
  47. static int do_mlock = 1;
  48. static int temporary = 0;
  49. static int verbose = 0;
  50. static int client_timeout = 500; /* msecs */
  51. static unsigned int port_max = 128;
  52. static int do_unlock = 0;
  53. static void
  54. do_nothing_handler (int sig)
  55. {
  56. /* this is used by the child (active) process, but it never
  57. gets called unless we are already shutting down after
  58. another signal.
  59. */
  60. char buf[64];
  61. snprintf (buf, sizeof(buf),
  62. "received signal %d during shutdown (ignored)\n", sig);
  63. write (1, buf, strlen (buf));
  64. }
  65. static int
  66. jack_main (jack_driver_desc_t * driver_desc, JSList * driver_params)
  67. {
  68. int sig;
  69. int i;
  70. sigset_t allsignals;
  71. struct sigaction action;
  72. int waiting;
  73. /* ensure that we are in our own process group so that
  74. kill (SIG, -pgrp) does the right thing.
  75. */
  76. setsid ();
  77. pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  78. /* what's this for?
  79. POSIX says that signals are delivered like this:
  80. * if a thread has blocked that signal, it is not
  81. a candidate to receive the signal.
  82. * of all threads not blocking the signal, pick
  83. one at random, and deliver the signal.
  84. this means that a simple-minded multi-threaded program can
  85. expect to get POSIX signals delivered randomly to any one
  86. of its threads,
  87. here, we block all signals that we think we might receive
  88. and want to catch. all "child" threads will inherit this
  89. setting. if we create a thread that calls sigwait() on the
  90. same set of signals, implicitly unblocking all those
  91. signals. any of those signals that are delivered to the
  92. process will be delivered to that thread, and that thread
  93. alone. this makes cleanup for a signal-driven exit much
  94. easier, since we know which thread is doing it and more
  95. importantly, we are free to call async-unsafe functions,
  96. because the code is executing in normal thread context
  97. after a return from sigwait().
  98. */
  99. sigemptyset (&signals);
  100. sigaddset(&signals, SIGHUP);
  101. sigaddset(&signals, SIGINT);
  102. sigaddset(&signals, SIGQUIT);
  103. sigaddset(&signals, SIGPIPE);
  104. sigaddset(&signals, SIGTERM);
  105. sigaddset(&signals, SIGUSR1);
  106. sigaddset(&signals, SIGUSR2);
  107. /* all child threads will inherit this mask unless they
  108. * explicitly reset it
  109. */
  110. pthread_sigmask (SIG_BLOCK, &signals, 0);
  111. /* get the engine/driver started */
  112. if ((engine = jack_engine_new (realtime, realtime_priority,
  113. do_mlock, do_unlock,
  114. temporary, verbose, client_timeout, port_max,
  115. getpid(), drivers)) == 0) {
  116. fprintf (stderr, "cannot create engine\n");
  117. return -1;
  118. }
  119. fprintf (stderr, "loading driver ..\n");
  120. if (jack_engine_load_driver (engine, driver_desc, driver_params)) {
  121. fprintf (stderr, "cannot load driver module %s\n",
  122. driver_desc->name);
  123. return -1;
  124. }
  125. if (engine->driver->start (engine->driver) != 0) {
  126. jack_error ("cannot start driver");
  127. return -1;
  128. }
  129. /* install a do-nothing handler because otherwise pthreads
  130. behaviour is undefined when we enter sigwait.
  131. */
  132. sigfillset (&allsignals);
  133. action.sa_handler = do_nothing_handler;
  134. action.sa_mask = allsignals;
  135. action.sa_flags = SA_RESTART|SA_RESETHAND;
  136. for (i = 1; i < NSIG; i++) {
  137. if (sigismember (&signals, i)) {
  138. sigaction (i, &action, 0);
  139. }
  140. }
  141. if (verbose) {
  142. fprintf (stderr, "%d waiting for signals\n", getpid());
  143. }
  144. waiting = TRUE;
  145. while (waiting) {
  146. sigwait (&signals, &sig);
  147. fprintf (stderr, "jack main caught signal %d\n", sig);
  148. switch (sig) {
  149. case SIGUSR1:
  150. jack_dump_configuration(engine, 1);
  151. break;
  152. case SIGUSR2:
  153. /* driver exit */
  154. waiting = FALSE;
  155. break;
  156. default:
  157. waiting = FALSE;
  158. break;
  159. }
  160. }
  161. if (sig != SIGSEGV) {
  162. /* unblock signals so we can see them during shutdown.
  163. this will help prod developers not to lose sight of
  164. bugs that cause segfaults etc. during shutdown.
  165. */
  166. sigprocmask (SIG_UNBLOCK, &signals, 0);
  167. }
  168. jack_engine_delete (engine);
  169. return 1;
  170. }
  171. static jack_driver_desc_t *
  172. jack_drivers_get_descriptor (JSList * drivers, const char * sofile)
  173. {
  174. jack_driver_desc_t * descriptor, * other_descriptor;
  175. JackDriverDescFunction so_get_descriptor;
  176. JSList * node;
  177. void * dlhandle;
  178. char * filename;
  179. const char * dlerr;
  180. int err;
  181. char* driver_dir;
  182. if ((driver_dir = getenv("JACK_DRIVER_DIR")) == 0) {
  183. driver_dir = ADDON_DIR;
  184. }
  185. filename = malloc (strlen (driver_dir) + 1 + strlen (sofile) + 1);
  186. sprintf (filename, "%s/%s", driver_dir, sofile);
  187. if (verbose) {
  188. fprintf (stderr, "getting driver descriptor from %s\n", filename);
  189. }
  190. if ((dlhandle = dlopen (filename, RTLD_NOW|RTLD_GLOBAL)) == NULL) {
  191. jack_error ("could not open driver .so '%s': %s\n", filename, dlerror ());
  192. free (filename);
  193. return NULL;
  194. }
  195. so_get_descriptor = (JackDriverDescFunction)
  196. dlsym (dlhandle, "driver_get_descriptor");
  197. if ((dlerr = dlerror ()) != NULL) {
  198. fprintf(stderr, "%s\n", dlerr);
  199. dlclose (dlhandle);
  200. free (filename);
  201. return NULL;
  202. }
  203. if ((descriptor = so_get_descriptor ()) == NULL) {
  204. jack_error ("driver from '%s' returned NULL descriptor\n", filename);
  205. dlclose (dlhandle);
  206. free (filename);
  207. return NULL;
  208. }
  209. if ((err = dlclose (dlhandle)) != 0) {
  210. jack_error ("error closing driver .so '%s': %s\n", filename, dlerror ());
  211. }
  212. /* check it doesn't exist already */
  213. for (node = drivers; node; node = jack_slist_next (node)) {
  214. other_descriptor = (jack_driver_desc_t *) node->data;
  215. if (strcmp (descriptor->name, other_descriptor->name) == 0) {
  216. jack_error ("the drivers in '%s' and '%s' both have the name '%s'; using the first\n",
  217. other_descriptor->file, filename, other_descriptor->name);
  218. /* FIXME: delete the descriptor */
  219. free (filename);
  220. return NULL;
  221. }
  222. }
  223. strncpy (descriptor->file, filename, PATH_MAX);
  224. free (filename);
  225. return descriptor;
  226. }
  227. static JSList *
  228. jack_drivers_load ()
  229. {
  230. struct dirent * dir_entry;
  231. DIR * dir_stream;
  232. const char * ptr;
  233. int err;
  234. JSList * driver_list = NULL;
  235. jack_driver_desc_t * desc;
  236. char* driver_dir;
  237. if ((driver_dir = getenv("JACK_DRIVER_DIR")) == 0) {
  238. driver_dir = ADDON_DIR;
  239. }
  240. /* search through the driver_dir and add get descriptors
  241. from the .so files in it */
  242. dir_stream = opendir (driver_dir);
  243. if (!dir_stream) {
  244. jack_error ("could not open driver directory %s: %s\n", driver_dir, strerror (errno));
  245. return NULL;
  246. }
  247. while ( (dir_entry = readdir (dir_stream)) ) {
  248. /* check the filename is of the right format */
  249. if (strncmp ("jack_", dir_entry->d_name, 5) != 0) {
  250. continue;
  251. }
  252. ptr = strrchr (dir_entry->d_name, '.');
  253. if (!ptr) {
  254. continue;
  255. }
  256. ptr++;
  257. if (strncmp ("so", ptr, 2) != 0) {
  258. continue;
  259. }
  260. desc = jack_drivers_get_descriptor (drivers, dir_entry->d_name);
  261. if (desc) {
  262. driver_list = jack_slist_append (driver_list, desc);
  263. }
  264. }
  265. err = closedir (dir_stream);
  266. if (err) {
  267. jack_error ("error closing driver directory %s: %s\n", driver_dir, strerror (errno));
  268. }
  269. if (!driver_list) {
  270. jack_error ("could not find any drivers in %s!\n", driver_dir);
  271. return NULL;
  272. }
  273. return driver_list;
  274. }
  275. static void copyright (FILE* file)
  276. {
  277. fprintf (file, "jackd " VERSION "\n"
  278. "Copyright 2001-2003 Paul Davis and others.\n"
  279. "jackd comes with ABSOLUTELY NO WARRANTY\n"
  280. "This is free software, and you are welcome to redistribute it\n"
  281. "under certain conditions; see the file COPYING for details\n\n");
  282. }
  283. static void usage (FILE *file)
  284. {
  285. copyright (file);
  286. fprintf (file, "\n"
  287. "usage: jackd [ --realtime OR -R [ --realtime-priority OR -P priority ] ]\n"
  288. " [ --no-mlock OR -m ]\n"
  289. " [ --unlock OR -u ]\n"
  290. " [ --timeout OR -t client-timeout-in-msecs ]\n"
  291. " [ --port-max OR -p maximum-number-of-ports]\n"
  292. " [ --verbose OR -v ]\n"
  293. " [ --silent OR -s ]\n"
  294. " [ --version OR -V ]\n"
  295. " -d driver [ ... driver args ... ]\n"
  296. " where driver can be `alsa', `coreaudio', `dummy',\n"
  297. " `oss' or `portaudio'\n\n"
  298. " jackd -d driver --help\n"
  299. " to display options for each driver\n\n");
  300. }
  301. static jack_driver_desc_t *
  302. jack_find_driver_descriptor (const char * name)
  303. {
  304. jack_driver_desc_t * desc = 0;
  305. JSList * node;
  306. for (node = drivers; node; node = jack_slist_next (node)) {
  307. desc = (jack_driver_desc_t *) node->data;
  308. if (strcmp (desc->name, name) != 0) {
  309. desc = NULL;
  310. } else {
  311. break;
  312. }
  313. }
  314. return desc;
  315. }
  316. int
  317. main (int argc, char *argv[])
  318. {
  319. jack_driver_desc_t * desc;
  320. const char *options = "-ad:P:uvshVRTFl:t:mp:";
  321. struct option long_options[] =
  322. {
  323. { "driver", 1, 0, 'd' },
  324. { "verbose", 0, 0, 'v' },
  325. { "help", 0, 0, 'h' },
  326. { "port-max", 1, 0, 'p' },
  327. { "no-mlock", 0, 0, 'm' },
  328. { "unlock", 0, 0, 'u' },
  329. { "realtime", 0, 0, 'R' },
  330. { "realtime-priority", 1, 0, 'P' },
  331. { "timeout", 1, 0, 't' },
  332. { "temporary", 0, 0, 'T' },
  333. { "version", 0, 0, 'V' },
  334. { "silent", 0, 0, 's' },
  335. { 0, 0, 0, 0 }
  336. };
  337. int opt = 0;
  338. int option_index = 0;
  339. int seen_driver = 0;
  340. char *driver_name = 0;
  341. char **driver_args = 0;
  342. JSList * driver_params;
  343. int driver_nargs = 1;
  344. int show_version = 0;
  345. int i;
  346. #ifdef USE_CAPABILITIES
  347. int status;
  348. #endif
  349. setvbuf (stdout, NULL, _IOLBF, 0);
  350. #ifdef USE_CAPABILITIES
  351. /* check to see if there is a pipe in the right descriptor */
  352. if ((status = fstat (PIPE_WRITE_FD, &pipe_stat)) == 0 &&
  353. S_ISFIFO(pipe_stat.st_mode)) {
  354. /* tell jackstart we are up and running */
  355. char c = 1;
  356. if (write (PIPE_WRITE_FD, &c, 1) != 1) {
  357. fprintf (stderr, "cannot write to jackstart sync "
  358. "pipe %d (%s)\n", PIPE_WRITE_FD,
  359. strerror (errno));
  360. }
  361. if (close(PIPE_WRITE_FD) != 0) {
  362. fprintf(stderr,
  363. "jackd: error on startup pipe close: %s\n",
  364. strerror (errno));
  365. } else {
  366. /* wait for jackstart process to set our capabilities */
  367. if (wait (&status) == -1) {
  368. fprintf (stderr, "jackd: wait for startup "
  369. "process exit failed\n");
  370. }
  371. if (!WIFEXITED (status) || WEXITSTATUS (status)) {
  372. fprintf(stderr, "jackd: jackstart did not "
  373. "exit cleanly\n");
  374. exit (1);
  375. }
  376. }
  377. }
  378. #endif /* USE_CAPABILITIES */
  379. opterr = 0;
  380. while (!seen_driver &&
  381. (opt = getopt_long (argc, argv, options,
  382. long_options, &option_index)) != EOF) {
  383. switch (opt) {
  384. case 'd':
  385. seen_driver = 1;
  386. driver_name = optarg;
  387. break;
  388. case 'v':
  389. verbose = 1;
  390. break;
  391. case 's':
  392. jack_set_error_function (silent_jack_error_callback);
  393. break;
  394. case 'm':
  395. do_mlock = 0;
  396. break;
  397. case 'p':
  398. port_max = (unsigned int) atol (optarg);
  399. break;
  400. case 'P':
  401. realtime_priority = atoi (optarg);
  402. break;
  403. case 'R':
  404. realtime = 1;
  405. break;
  406. case 'T':
  407. temporary = 1;
  408. break;
  409. case 't':
  410. client_timeout = atoi (optarg);
  411. break;
  412. case 'u':
  413. do_unlock = 1;
  414. break;
  415. case 'V':
  416. show_version = 1;
  417. break;
  418. default:
  419. fprintf (stderr, "unknown option character %c\n", optopt);
  420. /*fallthru*/
  421. case 'h':
  422. usage (stdout);
  423. return -1;
  424. }
  425. }
  426. if (show_version) {
  427. #ifdef DEFAULT_TMP_DIR
  428. printf ( "jackd version " VERSION
  429. " tmpdir " DEFAULT_TMP_DIR
  430. " protocol " PROTOCOL_VERSION
  431. "\n");
  432. #else
  433. printf ( "jackd version " VERSION
  434. " tmpdir " "/tmp"
  435. " protocol " PROTOCOL_VERSION
  436. "\n");
  437. #endif
  438. return -1;
  439. }
  440. if (!seen_driver) {
  441. usage (stderr);
  442. exit (1);
  443. }
  444. drivers = jack_drivers_load ();
  445. if (!drivers)
  446. {
  447. fprintf (stderr, "jackd: no drivers found; exiting\n");
  448. exit (1);
  449. }
  450. desc = jack_find_driver_descriptor (driver_name);
  451. if (!desc)
  452. {
  453. fprintf (stderr, "jackd: unknown driver '%s'\n", driver_name);
  454. exit (1);
  455. }
  456. if (optind < argc) {
  457. driver_nargs = 1 + argc - optind;
  458. } else {
  459. driver_nargs = 1;
  460. }
  461. if (driver_nargs == 0) {
  462. fprintf (stderr, "No driver specified ... hmm. JACK won't do"
  463. " anything when run like this.\n");
  464. return -1;
  465. }
  466. driver_args = (char **) malloc (sizeof (char *) * driver_nargs);
  467. driver_args[0] = driver_name;
  468. for (i = 1; i < driver_nargs; i++) {
  469. driver_args[i] = argv[optind++];
  470. }
  471. i = jack_parse_driver_params (desc, driver_nargs, driver_args, &driver_params);
  472. if (i)
  473. exit (0);
  474. copyright (stdout);
  475. jack_cleanup_shm ();
  476. jack_cleanup_files ();
  477. jack_main (desc, driver_params);
  478. jack_cleanup_shm ();
  479. jack_cleanup_files ();
  480. exit (0);
  481. }