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.

445 lines
10KB

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