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.

569 lines
13KB

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