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.

444 lines
10KB

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