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.

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