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.

325 lines
7.6KB

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