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.

308 lines
7.2KB

  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 char *alsa_pcm_name = "default";
  31. static nframes_t frames_per_interrupt = 64;
  32. static nframes_t srate = 48000;
  33. static int realtime = 0;
  34. static int realtime_priority = 10;
  35. static int with_fork = 1;
  36. static void
  37. signal_handler (int sig)
  38. {
  39. fprintf (stderr, "jackd: signal %d received\n", sig);
  40. kill (jackd_pid, SIGTERM);
  41. }
  42. static void
  43. posix_me_harder (void)
  44. {
  45. /* what's this for?
  46. POSIX says that signals are delivered like this:
  47. * if a thread has blocked that signal, it is not
  48. a candidate to receive the signal.
  49. * of all threads not blocking the signal, pick
  50. one at random, and deliver the signal.
  51. this means that a simple-minded multi-threaded
  52. program can expect to get POSIX signals delivered
  53. randomly to any one of its threads,
  54. here, we block all signals that we think we
  55. might receive and want to catch. all "child"
  56. threads will inherit this setting. if we
  57. create a thread that calls sigwait() on the
  58. same set of signals, implicitly unblocking
  59. all those signals. any of those signals that
  60. are delivered to the process will be delivered
  61. to that thread, and that thread alone. this
  62. makes cleanup for a signal-driven exit much
  63. easier, since we know which thread is doing
  64. it and more importantly, we are free to
  65. call async-unsafe functions, because the
  66. code is executing in normal thread context
  67. after a return from sigwait().
  68. */
  69. sigemptyset (&signals);
  70. sigaddset(&signals, SIGHUP);
  71. sigaddset(&signals, SIGINT);
  72. sigaddset(&signals, SIGQUIT);
  73. sigaddset(&signals, SIGILL);
  74. sigaddset(&signals, SIGTRAP);
  75. sigaddset(&signals, SIGABRT);
  76. sigaddset(&signals, SIGIOT);
  77. sigaddset(&signals, SIGFPE);
  78. sigaddset(&signals, SIGPIPE);
  79. sigaddset(&signals, SIGTERM);
  80. /* this can make debugging a pain, but it also makes
  81. segv-exits cleanup_files after themselves rather than
  82. leaving the audio thread active. i still
  83. find it truly wierd that _exit() or whatever is done
  84. by the default SIGSEGV handler does not
  85. cancel all threads in a process, but what
  86. else can we do?
  87. */
  88. sigaddset(&signals, SIGSEGV);
  89. /* all child threads will inherit this mask */
  90. pthread_sigmask (SIG_BLOCK, &signals, 0);
  91. }
  92. static void *
  93. jack_engine_waiter_thread (void *arg)
  94. {
  95. pid_t signal_pid = (pid_t) arg;
  96. jack_driver_t *driver;
  97. pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  98. if ((engine = jack_engine_new (realtime, realtime_priority)) == 0) {
  99. fprintf (stderr, "cannot create engine\n");
  100. kill (signal_pid, SIGTERM);
  101. return 0;
  102. }
  103. if ((driver = jack_driver_load (ADDON_DIR "/jack_alsa.so", alsa_pcm_name, frames_per_interrupt, srate)) == 0) {
  104. fprintf (stderr, "cannot load ALSA driver module\n");
  105. kill (signal_pid, SIGTERM);
  106. return 0;
  107. }
  108. jack_use_driver (engine, driver);
  109. if (jack_run (engine)) {
  110. fprintf (stderr, "cannot start main JACK thread\n");
  111. kill (signal_pid, SIGTERM);
  112. return 0;
  113. }
  114. jack_wait (engine);
  115. fprintf (stderr, "telling signal thread that the engine is done\n");
  116. kill (signal_pid, SIGHUP);
  117. return 0; /* nobody cares what this returns */
  118. }
  119. static void
  120. jack_main ()
  121. {
  122. int sig;
  123. int err;
  124. pthread_t waiter_thread;
  125. pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  126. posix_me_harder ();
  127. /* what we'd really like to do here is to be able to
  128. wait for either the engine to stop or a POSIX signal,
  129. whichever arrives sooner. but there is no mechanism
  130. to do that, so instead we create a thread to wait
  131. for the engine to finish, and here we stop and wait
  132. for any (reasonably likely) POSIX signal.
  133. if the engine finishes first, the waiter thread will
  134. tell us about it via a signal.
  135. if a signal arrives, we'll stop the engine and then
  136. exit.
  137. in normal operation, our parent process will be waiting
  138. for us and will cleanup.
  139. */
  140. if (pthread_create (&waiter_thread, 0, jack_engine_waiter_thread, (void *) getpid())) {
  141. fprintf (stderr, "jackd: cannot create engine waiting thread\n");
  142. return;
  143. }
  144. /* Note: normal operation has with_fork == 1 */
  145. if (with_fork) {
  146. /* let the parent handle SIGINT */
  147. sigdelset (&signals, SIGINT);
  148. }
  149. err = sigwait (&signals, &sig);
  150. fprintf (stderr, "signal waiter: exiting due to signal %d\n", sig);
  151. pthread_cancel (waiter_thread);
  152. jack_engine_delete (engine);
  153. return;
  154. }
  155. static void usage ()
  156. {
  157. fprintf (stderr,
  158. "usage: jackd [ --device OR -d ALSA-PCM-device ]
  159. [ --srate OR -r sample-rate ]
  160. [ --frames-per-interrupt OR -p frames_per_interrupt ]
  161. [ --realtime OR -R [ --realtime-priority OR -P priority ] ]
  162. [ --spoon OR -F ] (don't fork)
  163. ");
  164. }
  165. int
  166. main (int argc, char *argv[])
  167. {
  168. const char *options = "hd:r:p:RP:FD:";
  169. struct option long_options[] =
  170. {
  171. { "tmpdir", 1, 0, 'D' },
  172. { "device", 1, 0, 'd' },
  173. { "srate", 1, 0, 'r' },
  174. { "frames-per-interrupt", 1, 0, 'p' },
  175. { "help", 0, 0, 'h' },
  176. { "realtime", 0, 0, 'R' },
  177. { "realtime-priority", 1, 0, 'P' },
  178. { "spoon", 0, 0, 'F' },
  179. { 0, 0, 0, 0 }
  180. };
  181. int option_index;
  182. int opt;
  183. opterr = 0;
  184. while ((opt = getopt_long (argc, argv, options, long_options, &option_index)) != EOF) {
  185. switch (opt) {
  186. case 'D':
  187. jack_set_temp_dir (optarg);
  188. break;
  189. case 'd':
  190. alsa_pcm_name = optarg;
  191. break;
  192. case 'r':
  193. srate = atoi (optarg);
  194. break;
  195. case 'p':
  196. frames_per_interrupt = atoi (optarg);
  197. break;
  198. case 'F':
  199. with_fork = 0;
  200. break;
  201. case 'P':
  202. realtime_priority = atoi (optarg);
  203. break;
  204. case 'R':
  205. realtime = 1;
  206. break;
  207. case 'h':
  208. default:
  209. fprintf (stderr, "unknown option character %c\n", opt);
  210. usage();
  211. return -1;
  212. }
  213. }
  214. if (!with_fork) {
  215. /* This is really here so that we can run gdb easily */
  216. jack_main ();
  217. } else {
  218. int pid = fork ();
  219. if (pid < 0) {
  220. fprintf (stderr, "could not fork jack server (%s)", strerror (errno));
  221. exit (1);
  222. } else if (pid == 0) {
  223. jack_main ();
  224. } else {
  225. jackd_pid = pid;
  226. signal (SIGHUP, signal_handler);
  227. signal (SIGINT, signal_handler);
  228. signal (SIGQUIT, signal_handler);
  229. signal (SIGTERM, signal_handler);
  230. waitpid (pid, NULL, 0);
  231. }
  232. }
  233. jack_cleanup_shm ();
  234. jack_cleanup_files ();
  235. return 0;
  236. }