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.

464 lines
11KB

  1. /*
  2. Copyright (C) 2001-2003 Paul Davis
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  14. $Id$
  15. */
  16. #include <stdio.h>
  17. #include <signal.h>
  18. #include <getopt.h>
  19. #include <sys/types.h>
  20. #include <sys/shm.h>
  21. #include <sys/wait.h>
  22. #include <string.h>
  23. #include <errno.h>
  24. #include <config.h>
  25. #include <jack/engine.h>
  26. #include <jack/internal.h>
  27. #include <jack/driver.h>
  28. #include <jack/shm.h>
  29. #ifdef USE_CAPABILITIES
  30. #include <sys/stat.h>
  31. /* capgetp and capsetp are linux only extensions, not posix */
  32. #undef _POSIX_SOURCE
  33. #include <sys/capability.h>
  34. #include <jack/start.h>
  35. static struct stat pipe_stat;
  36. #endif
  37. static sigset_t signals;
  38. static jack_engine_t *engine = 0;
  39. static int jackd_pid;
  40. static int realtime = 0;
  41. static int realtime_priority = 10;
  42. static int with_fork = 1;
  43. static int verbose = 0;
  44. static int asio_mode = 0;
  45. static int client_timeout = 500; /* msecs */
  46. typedef struct {
  47. pid_t pid;
  48. int argc;
  49. char **argv;
  50. } waiter_arg_t;
  51. static void
  52. signal_handler (int sig)
  53. {
  54. /* this is used by the parent (waiter) process */
  55. fprintf (stderr, "jackd: signal %d received\n", sig);
  56. kill (jackd_pid, SIGTERM);
  57. }
  58. static void
  59. do_nothing_handler (int sig)
  60. {
  61. /* this is used by the child (active) process, but it never
  62. gets called unless we are already shutting down
  63. after another signal.
  64. */
  65. char buf[64];
  66. snprintf (buf, sizeof(buf), "received signal %d during shutdown (ignored)\n", sig);
  67. write (1, buf, strlen (buf));
  68. }
  69. static void *
  70. jack_engine_waiter_thread (void *arg)
  71. {
  72. waiter_arg_t *warg = (waiter_arg_t *) arg;
  73. pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  74. if ((engine = jack_engine_new (realtime, realtime_priority, verbose, client_timeout)) == 0) {
  75. fprintf (stderr, "cannot create engine\n");
  76. kill (warg->pid, SIGTERM);
  77. return 0;
  78. }
  79. if (warg->argc) {
  80. fprintf (stderr, "loading driver ..\n");
  81. if (jack_engine_load_driver (engine, warg->argc, warg->argv)) {
  82. fprintf (stderr, "cannot load driver module %s\n", warg->argv[0]);
  83. kill (warg->pid, SIGTERM);
  84. return 0;
  85. }
  86. } else {
  87. fprintf (stderr, "No driver specified ... hmm. JACK won't do anything when run like this.\n");
  88. }
  89. if (asio_mode) {
  90. jack_set_asio_mode (engine, TRUE);
  91. }
  92. fprintf (stderr, "starting engine\n");
  93. if (jack_run (engine)) {
  94. fprintf (stderr, "cannot start main JACK thread\n");
  95. kill (warg->pid, SIGTERM);
  96. return 0;
  97. }
  98. jack_wait (engine);
  99. fprintf (stderr, "telling signal thread that the engine is done\n");
  100. kill (warg->pid, SIGHUP);
  101. return 0; /* nobody cares what this returns */
  102. }
  103. static void
  104. jack_main (int argc, char **argv)
  105. {
  106. int sig;
  107. int i;
  108. pthread_t waiter_thread;
  109. waiter_arg_t warg;
  110. sigset_t allsignals;
  111. struct sigaction action;
  112. /* remove any existing files from a previous instance */
  113. jack_cleanup_files ();
  114. pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  115. /* what's this for?
  116. POSIX says that signals are delivered like this:
  117. * if a thread has blocked that signal, it is not
  118. a candidate to receive the signal.
  119. * of all threads not blocking the signal, pick
  120. one at random, and deliver the signal.
  121. this means that a simple-minded multi-threaded
  122. program can expect to get POSIX signals delivered
  123. randomly to any one of its threads,
  124. here, we block all signals that we think we
  125. might receive and want to catch. all "child"
  126. threads will inherit this setting. if we
  127. create a thread that calls sigwait() on the
  128. same set of signals, implicitly unblocking
  129. all those signals. any of those signals that
  130. are delivered to the process will be delivered
  131. to that thread, and that thread alone. this
  132. makes cleanup for a signal-driven exit much
  133. easier, since we know which thread is doing
  134. it and more importantly, we are free to
  135. call async-unsafe functions, because the
  136. code is executing in normal thread context
  137. after a return from sigwait().
  138. */
  139. sigemptyset (&signals);
  140. sigaddset(&signals, SIGHUP);
  141. sigaddset(&signals, SIGINT);
  142. sigaddset(&signals, SIGQUIT);
  143. sigaddset(&signals, SIGILL);
  144. sigaddset(&signals, SIGTRAP);
  145. sigaddset(&signals, SIGABRT);
  146. sigaddset(&signals, SIGIOT);
  147. sigaddset(&signals, SIGFPE);
  148. sigaddset(&signals, SIGPIPE);
  149. sigaddset(&signals, SIGTERM);
  150. sigaddset(&signals, SIGUSR1);
  151. sigaddset(&signals, SIGSEGV);
  152. /* all child threads will inherit this mask unless they explicitly reset it */
  153. pthread_sigmask (SIG_BLOCK, &signals, 0);
  154. /* what we'd really like to do here is to be able to
  155. wait for either the engine to stop or a POSIX signal,
  156. whichever arrives sooner. but there is no mechanism
  157. to do that, so instead we create a thread to wait
  158. for the engine to finish, and here we stop and wait
  159. for any (reasonably likely) POSIX signal.
  160. if the engine finishes first, the waiter thread will
  161. tell us about it via a signal.
  162. if a signal arrives, we'll stop the engine and then
  163. exit.
  164. in normal operation, our parent process will be waiting
  165. for us and will cleanup.
  166. */
  167. warg.pid = getpid();
  168. warg.argc = argc;
  169. warg.argv = argv;
  170. if (pthread_create (&waiter_thread, 0, jack_engine_waiter_thread, &warg)) {
  171. fprintf (stderr, "jackd: cannot create engine waiting thread\n");
  172. return;
  173. }
  174. /* install a do-nothing handler because otherwise
  175. pthreads behaviour is undefined when we enter
  176. sigwait.
  177. */
  178. sigfillset (&allsignals);
  179. action.sa_handler = do_nothing_handler;
  180. action.sa_mask = allsignals;
  181. action.sa_flags = SA_RESTART|SA_RESETHAND;
  182. for (i = 1; i < NSIG; i++) {
  183. if (sigismember (&signals, i)) {
  184. sigaction (i, &action, 0);
  185. }
  186. }
  187. if (verbose) {
  188. fprintf (stderr, "%d waiting for signals\n", getpid());
  189. }
  190. while(1) {
  191. sigwait (&signals, &sig);
  192. fprintf (stderr, "jack main caught signal %d\n", sig);
  193. if (sig == SIGUSR1) {
  194. jack_dump_configuration(engine, 1);
  195. } else {
  196. /* continue to kill engine */
  197. break;
  198. }
  199. }
  200. if (sig != SIGSEGV) {
  201. /* unblock signals so we can see them during shutdown.
  202. this will help prod developers not to lose sight
  203. of bugs that cause segfaults etc. during shutdown.
  204. */
  205. sigprocmask (SIG_UNBLOCK, &signals, 0);
  206. }
  207. pthread_cancel (waiter_thread);
  208. jack_engine_delete (engine);
  209. return;
  210. }
  211. static void copyright (FILE* file)
  212. {
  213. fprintf (file, "jackd " VERSION "\n"
  214. "Copyright 2001-2003 Paul Davis and others.\n"
  215. "jackd comes with ABSOLUTELY NO WARRANTY\n"
  216. "This is free software, and you are welcome to redistribute it\n"
  217. "under certain conditions; see the file COPYING for details\n\n");
  218. }
  219. static void usage (FILE *file)
  220. {
  221. copyright (file);
  222. fprintf (file, "\n"
  223. "usage: jackd [ --asio OR -a ]\n"
  224. " [ --realtime OR -R [ --realtime-priority OR -P priority ] ]\n"
  225. " [ --timeout OR -t client-timeout-in-msecs ]\n"
  226. " [ --verbose OR -v ]\n"
  227. " [ --tmpdir OR -D directory-for-temporary-files ]\n"
  228. " [ --version OR -V ]\n"
  229. " -d driver [ ... driver args ... ]\n");
  230. }
  231. int
  232. main (int argc, char *argv[])
  233. {
  234. const char *options = "-ad:D:P:vhVRFl:t:";
  235. struct option long_options[] =
  236. {
  237. { "asio", 0, 0, 'a' },
  238. { "driver", 1, 0, 'd' },
  239. { "tmpdir", 1, 0, 'D' },
  240. { "verbose", 0, 0, 'v' },
  241. { "help", 0, 0, 'h' },
  242. { "realtime", 0, 0, 'R' },
  243. { "realtime-priority", 1, 0, 'P' },
  244. { "timeout", 1, 0, 't' },
  245. { "spoon", 0, 0, 'F' },
  246. { "version", 0, 0, 'V' },
  247. { 0, 0, 0, 0 }
  248. };
  249. int option_index;
  250. int opt;
  251. int seen_driver = 0;
  252. char *driver_name = 0;
  253. char **driver_args = 0;
  254. int driver_nargs = 1;
  255. int show_version = 0;
  256. int i;
  257. #ifdef USE_CAPABILITIES
  258. int status;
  259. #endif
  260. #ifdef USE_CAPABILITIES
  261. /* check to see if there is a pipe in the right descriptor */
  262. if ((status = fstat (PIPE_WRITE_FD, &pipe_stat)) == 0 && S_ISFIFO(pipe_stat.st_mode)) {
  263. /* tell jackstart we are up and running */
  264. char c = 1;
  265. if (write (PIPE_WRITE_FD, &c, 1) != 1) {
  266. fprintf (stderr, "cannot write to jackstart sync pipe %d (%s)\n", PIPE_WRITE_FD, strerror (errno));
  267. }
  268. if (close(PIPE_WRITE_FD) != 0) {
  269. fprintf(stderr, "jackd: error on startup pipe close: %s\n", strerror (errno));
  270. } else {
  271. /* wait for jackstart process to set our capabilities */
  272. if (wait (&status) == -1) {
  273. fprintf (stderr, "jackd: wait for startup process exit failed\n");
  274. }
  275. if (!WIFEXITED (status) || WEXITSTATUS (status)) {
  276. fprintf(stderr, "jackd: jackstart did not exit cleanly\n");
  277. exit (1);
  278. }
  279. }
  280. }
  281. #endif
  282. opterr = 0;
  283. while (!seen_driver && (opt = getopt_long (argc, argv, options, long_options, &option_index)) != EOF) {
  284. switch (opt) {
  285. case 'a':
  286. asio_mode = TRUE;
  287. break;
  288. case 'D':
  289. jack_set_server_dir (optarg);
  290. break;
  291. case 'd':
  292. seen_driver = 1;
  293. driver_name = optarg;
  294. break;
  295. case 'v':
  296. verbose = 1;
  297. break;
  298. case 'F':
  299. with_fork = 0;
  300. break;
  301. case 'P':
  302. realtime_priority = atoi (optarg);
  303. break;
  304. case 'R':
  305. realtime = 1;
  306. break;
  307. case 't':
  308. client_timeout = atoi (optarg);
  309. break;
  310. case 'V':
  311. show_version = 1;
  312. break;
  313. default:
  314. fprintf (stderr, "unknown option character %c\n", optopt);
  315. /*fallthru*/
  316. case 'h':
  317. usage (stdout);
  318. return -1;
  319. }
  320. }
  321. if (show_version) {
  322. printf ( "jackd version " VERSION "\n");
  323. return -1;
  324. }
  325. if (!seen_driver) {
  326. usage (stderr);
  327. exit (1);
  328. }
  329. if (optind < argc) {
  330. driver_nargs = 1 + argc - optind;
  331. } else {
  332. driver_nargs = 1;
  333. }
  334. driver_args = (char **) malloc (sizeof (char *) * driver_nargs);
  335. driver_args[0] = driver_name;
  336. for (i = 1; i < driver_nargs; i++) {
  337. driver_args[i] = argv[optind++];
  338. }
  339. copyright (stdout);
  340. if (!with_fork) {
  341. /* This is really here so that we can run gdb easily */
  342. jack_main (driver_nargs, driver_args);
  343. } else {
  344. int pid = fork ();
  345. if (pid < 0) {
  346. fprintf (stderr, "could not fork jack server (%s)", strerror (errno));
  347. exit (1);
  348. } else if (pid == 0) {
  349. jack_main (driver_nargs, driver_args);
  350. } else {
  351. jackd_pid = pid;
  352. signal (SIGHUP, signal_handler);
  353. signal (SIGINT, signal_handler);
  354. signal (SIGQUIT, signal_handler);
  355. signal (SIGTERM, signal_handler);
  356. waitpid (pid, NULL, 0);
  357. }
  358. }
  359. jack_cleanup_shm ();
  360. jack_cleanup_files ();
  361. return 0;
  362. }