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.

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