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.

361 lines
8.1KB

  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 <jack/engine.h>
  25. #include <jack/internal.h>
  26. #include <jack/driver.h>
  27. static sigset_t signals;
  28. static jack_engine_t *engine = 0;
  29. static int jackd_pid;
  30. static int realtime = 0;
  31. static int realtime_priority = 10;
  32. static int with_fork = 1;
  33. static int verbose = 0;
  34. static int asio_mode = 0;
  35. typedef struct {
  36. pid_t pid;
  37. int argc;
  38. char **argv;
  39. } waiter_arg_t;
  40. static void
  41. signal_handler (int sig)
  42. {
  43. fprintf (stderr, "jackd: signal %d received\n", sig);
  44. kill (jackd_pid, SIGTERM);
  45. }
  46. static void
  47. posix_me_harder (void)
  48. {
  49. /* what's this for?
  50. POSIX says that signals are delivered like this:
  51. * if a thread has blocked that signal, it is not
  52. a candidate to receive the signal.
  53. * of all threads not blocking the signal, pick
  54. one at random, and deliver the signal.
  55. this means that a simple-minded multi-threaded
  56. program can expect to get POSIX signals delivered
  57. randomly to any one of its threads,
  58. here, we block all signals that we think we
  59. might receive and want to catch. all "child"
  60. threads will inherit this setting. if we
  61. create a thread that calls sigwait() on the
  62. same set of signals, implicitly unblocking
  63. all those signals. any of those signals that
  64. are delivered to the process will be delivered
  65. to that thread, and that thread alone. this
  66. makes cleanup for a signal-driven exit much
  67. easier, since we know which thread is doing
  68. it and more importantly, we are free to
  69. call async-unsafe functions, because the
  70. code is executing in normal thread context
  71. after a return from sigwait().
  72. */
  73. sigemptyset (&signals);
  74. sigaddset(&signals, SIGHUP);
  75. sigaddset(&signals, SIGINT);
  76. sigaddset(&signals, SIGQUIT);
  77. sigaddset(&signals, SIGILL);
  78. sigaddset(&signals, SIGTRAP);
  79. sigaddset(&signals, SIGABRT);
  80. sigaddset(&signals, SIGIOT);
  81. sigaddset(&signals, SIGFPE);
  82. sigaddset(&signals, SIGPIPE);
  83. sigaddset(&signals, SIGTERM);
  84. sigaddset(&signals, SIGUSR1);
  85. /* this can make debugging a pain, but it also makes
  86. segv-exits cleanup_files after themselves rather than
  87. leaving the audio thread active. i still
  88. find it truly wierd that _exit() or whatever is done
  89. by the default SIGSEGV handler does not
  90. cancel all threads in a process, but what
  91. else can we do?
  92. */
  93. sigaddset(&signals, SIGSEGV);
  94. /* all child threads will inherit this mask */
  95. pthread_sigmask (SIG_BLOCK, &signals, 0);
  96. }
  97. static void *
  98. jack_engine_waiter_thread (void *arg)
  99. {
  100. waiter_arg_t *warg = (waiter_arg_t *) arg;
  101. jack_driver_t *driver;
  102. pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  103. if ((engine = jack_engine_new (realtime, realtime_priority, verbose)) == 0) {
  104. fprintf (stderr, "cannot create engine\n");
  105. kill (warg->pid, SIGTERM);
  106. return 0;
  107. }
  108. if (warg->argc) {
  109. if ((driver = jack_driver_load (warg->argc, warg->argv)) == 0) {
  110. fprintf (stderr, "cannot load driver module %s\n", warg->argv[0]);
  111. kill (warg->pid, SIGTERM);
  112. return 0;
  113. }
  114. jack_use_driver (engine, driver);
  115. }
  116. if (asio_mode) {
  117. jack_set_asio_mode (engine, TRUE);
  118. }
  119. if (jack_run (engine)) {
  120. fprintf (stderr, "cannot start main JACK thread\n");
  121. kill (warg->pid, SIGTERM);
  122. return 0;
  123. }
  124. jack_wait (engine);
  125. fprintf (stderr, "telling signal thread that the engine is done\n");
  126. kill (warg->pid, SIGHUP);
  127. return 0; /* nobody cares what this returns */
  128. }
  129. static void
  130. jack_main (int argc, char **argv)
  131. {
  132. int sig;
  133. int err;
  134. pthread_t waiter_thread;
  135. waiter_arg_t warg;
  136. pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  137. posix_me_harder ();
  138. /* what we'd really like to do here is to be able to
  139. wait for either the engine to stop or a POSIX signal,
  140. whichever arrives sooner. but there is no mechanism
  141. to do that, so instead we create a thread to wait
  142. for the engine to finish, and here we stop and wait
  143. for any (reasonably likely) POSIX signal.
  144. if the engine finishes first, the waiter thread will
  145. tell us about it via a signal.
  146. if a signal arrives, we'll stop the engine and then
  147. exit.
  148. in normal operation, our parent process will be waiting
  149. for us and will cleanup.
  150. */
  151. warg.pid = getpid();
  152. warg.argc = argc;
  153. warg.argv = argv;
  154. if (pthread_create (&waiter_thread, 0, jack_engine_waiter_thread, &warg)) {
  155. fprintf (stderr, "jackd: cannot create engine waiting thread\n");
  156. return;
  157. }
  158. /* Note: normal operation has with_fork == 1 */
  159. if (with_fork) {
  160. /* let the parent handle SIGINT */
  161. sigdelset (&signals, SIGINT);
  162. }
  163. while(1) {
  164. err = sigwait (&signals, &sig);
  165. if (sig == SIGUSR1) {
  166. jack_dump_configuration(engine, 1);
  167. } else {
  168. /* continue to kill engine */
  169. break;
  170. }
  171. }
  172. pthread_cancel (waiter_thread);
  173. jack_engine_delete (engine);
  174. return;
  175. }
  176. static void usage ()
  177. {
  178. fprintf (stderr, "\
  179. usage: jackd [ --asio OR -a ]
  180. [ --realtime OR -R [ --realtime-priority OR -P priority ] ]
  181. [ --verbose OR -v ]
  182. [ --tmpdir OR -D directory-for-temporary-files ]
  183. -d driver [ ... driver args ... ]
  184. ");
  185. }
  186. int
  187. main (int argc, char *argv[])
  188. {
  189. const char *options = "ad:D:P:vhRF";
  190. struct option long_options[] =
  191. {
  192. { "asio", 0, 0, 'a' },
  193. { "driver", 1, 0, 'd' },
  194. { "tmpdir", 1, 0, 'D' },
  195. { "verbose", 0, 0, 'v' },
  196. { "help", 0, 0, 'h' },
  197. { "realtime", 0, 0, 'R' },
  198. { "realtime-priority", 1, 0, 'P' },
  199. { "spoon", 0, 0, 'F' },
  200. { 0, 0, 0, 0 }
  201. };
  202. int option_index;
  203. int opt;
  204. int seen_driver = 0;
  205. char *driver_name = 0;
  206. char **driver_args = 0;
  207. int driver_nargs = 1;
  208. int i;
  209. opterr = 0;
  210. while (!seen_driver && (opt = getopt_long (argc, argv, options, long_options, &option_index)) != EOF) {
  211. switch (opt) {
  212. case 'a':
  213. asio_mode = TRUE;
  214. break;
  215. case 'D':
  216. jack_set_temp_dir (optarg);
  217. break;
  218. case 'd':
  219. seen_driver = 1;
  220. driver_name = optarg;
  221. break;
  222. case 'v':
  223. verbose = 1;
  224. break;
  225. case 'F':
  226. with_fork = 0;
  227. break;
  228. case 'P':
  229. realtime_priority = atoi (optarg);
  230. break;
  231. case 'R':
  232. realtime = 1;
  233. break;
  234. default:
  235. fprintf (stderr, "unknown option character %c\n", optopt);
  236. /*fallthru*/
  237. case 'h':
  238. usage();
  239. return -1;
  240. }
  241. }
  242. if (!seen_driver) {
  243. usage ();
  244. exit (1);
  245. }
  246. if (optind < argc) {
  247. driver_nargs = 1 + argc - optind;
  248. } else {
  249. driver_nargs = 1;
  250. }
  251. driver_args = (char **) malloc (sizeof (char *) * driver_nargs);
  252. driver_args[0] = driver_name;
  253. for (i = 1; i < driver_nargs; i++) {
  254. driver_args[i] = argv[optind++];
  255. }
  256. printf ( "jackd " VERSION "\n"
  257. "Copyright 2001-2002 Paul Davis and others.\n"
  258. "jackd comes with ABSOLUTELY NO WARRANTY\n"
  259. "This is free software, and you are welcome to redistribute it\n"
  260. "under certain conditions; see the file COPYING for details\n\n");
  261. if (!with_fork) {
  262. /* This is really here so that we can run gdb easily */
  263. jack_main (driver_nargs, driver_args);
  264. } else {
  265. int pid = fork ();
  266. if (pid < 0) {
  267. fprintf (stderr, "could not fork jack server (%s)", strerror (errno));
  268. exit (1);
  269. } else if (pid == 0) {
  270. jack_main (driver_nargs, driver_args);
  271. } else {
  272. jackd_pid = pid;
  273. signal (SIGHUP, signal_handler);
  274. signal (SIGINT, signal_handler);
  275. signal (SIGQUIT, signal_handler);
  276. signal (SIGTERM, signal_handler);
  277. waitpid (pid, NULL, 0);
  278. }
  279. }
  280. jack_cleanup_shm ();
  281. jack_cleanup_files ();
  282. return 0;
  283. }