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.

301 lines
6.7KB

  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 <dirent.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;
  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 void
  36. cleanup ()
  37. {
  38. DIR *dir;
  39. struct dirent *dirent;
  40. /* its important that we remove all files that jackd creates
  41. because otherwise subsequent attempts to start jackd will
  42. believe that an instance is already running.
  43. */
  44. if ((dir = opendir ("/tmp")) == NULL) {
  45. fprintf (stderr, "jackd(%li): cleanup - cannot open scratch directory (%s)\n", (long)getpid(), strerror (errno));
  46. return;
  47. }
  48. while ((dirent = readdir (dir)) != NULL) {
  49. if (strncmp (dirent->d_name, "jack-", 5) == 0) {
  50. unlink (dirent->d_name);
  51. }
  52. }
  53. closedir (dir);
  54. }
  55. static void
  56. signal_handler (int sig)
  57. {
  58. fprintf (stderr, "killing jackd at %d\n", jackd_pid);
  59. kill (jackd_pid, SIGTERM);
  60. exit (-sig);
  61. }
  62. static void
  63. catch_signals (void)
  64. {
  65. /* what's this for?
  66. this just makes sure that if we are using the fork
  67. approach to cleanup (see main()), the waiting
  68. process will catch common "interrupt" signals
  69. and terminate the real server appropriately.
  70. */
  71. signal (SIGHUP, signal_handler);
  72. signal (SIGINT, signal_handler);
  73. signal (SIGQUIT, signal_handler);
  74. signal (SIGTERM, signal_handler);
  75. }
  76. static void *
  77. signal_thread (void *arg)
  78. {
  79. int sig;
  80. int err;
  81. pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  82. err = sigwait (&signals, &sig);
  83. fprintf (stderr, "exiting due to signal %d\n", sig);
  84. jack_engine_delete (engine);
  85. cleanup ();
  86. exit (err);
  87. /*NOTREACHED*/
  88. return 0;
  89. }
  90. static int
  91. posix_me_harder (void)
  92. {
  93. pthread_t thread_id;
  94. /* what's this for?
  95. POSIX says that signals are delivered like this:
  96. * if a thread has blocked that signal, it is not
  97. a candidate to receive the signal.
  98. * of all threads not blocking the signal, pick
  99. one at random, and deliver the signal.
  100. this means that a simple-minded multi-threaded
  101. program can expect to get POSIX signals delivered
  102. to any of its threads.
  103. here, we block all signals that we think we
  104. might receive and want to catch. all later
  105. threads will inherit this setting. then we
  106. create a thread that calls sigwait() on the
  107. same set of signals, implicitly unblocking
  108. all those signals. any of those signals that
  109. are delivered to the process will be delivered
  110. to that thread, and that thread alone. this
  111. makes cleanup for a signal-driven exit much
  112. easier, since we know which thread is doing
  113. it and more importantly, we are free to
  114. call async-unsafe functions, because the
  115. code is executing in normal thread context
  116. after a return from sigwait().
  117. */
  118. sigemptyset (&signals);
  119. sigaddset(&signals, SIGHUP);
  120. sigaddset(&signals, SIGINT);
  121. sigaddset(&signals, SIGQUIT);
  122. sigaddset(&signals, SIGILL);
  123. sigaddset(&signals, SIGTRAP);
  124. sigaddset(&signals, SIGABRT);
  125. sigaddset(&signals, SIGIOT);
  126. sigaddset(&signals, SIGFPE);
  127. sigaddset(&signals, SIGPIPE);
  128. sigaddset(&signals, SIGTERM);
  129. /* this can make debugging a pain, but it also makes
  130. segv-exits cleanup after themselves rather than
  131. leaving the audio thread active. i still
  132. find it truly wierd that _exit() or whatever is done
  133. by the default SIGSEGV handler does not
  134. cancel all threads in a process, but what
  135. else can we do?
  136. */
  137. sigaddset(&signals, SIGSEGV);
  138. /* all child threads will inherit this mask */
  139. pthread_sigmask (SIG_BLOCK, &signals, 0);
  140. /* start a thread to wait for signals */
  141. if (pthread_create (&thread_id, 0, signal_thread, 0)) {
  142. fprintf (stderr, "cannot create signal catching thread");
  143. return -1;
  144. }
  145. pthread_detach (thread_id);
  146. return 0;
  147. }
  148. static void
  149. jack_main ()
  150. {
  151. jack_driver_t *driver;
  152. posix_me_harder ();
  153. if ((engine = jack_engine_new (realtime, realtime_priority)) == 0) {
  154. fprintf (stderr, "cannot create engine\n");
  155. return;
  156. }
  157. if ((driver = jack_driver_load (ADDON_DIR "/jack_alsa.so", alsa_pcm_name, frames_per_interrupt, srate)) == 0) {
  158. fprintf (stderr, "cannot load ALSA driver module\n");
  159. return;
  160. }
  161. jack_use_driver (engine, driver);
  162. jack_run (engine);
  163. jack_wait (engine);
  164. }
  165. static void usage ()
  166. {
  167. fprintf (stderr,
  168. "usage: jackd [ --device OR -d ALSA-PCM-device ]
  169. [ --srate OR -r sample-rate ]
  170. [ --frames-per-interrupt OR -p frames_per_interrupt ]
  171. [ --realtime OR -R [ --realtime-priority OR -P priority ] ]
  172. [ --spoon OR -F ] (don't fork)
  173. ");
  174. }
  175. int
  176. main (int argc, char *argv[])
  177. {
  178. const char *options = "hd:r:p:RP:F";
  179. struct option long_options[] =
  180. {
  181. { "device", 1, 0, 'd' },
  182. { "srate", 1, 0, 'r' },
  183. { "frames-per-interrupt", 1, 0, 'p' },
  184. { "help", 0, 0, 'h' },
  185. { "realtime", 0, 0, 'R' },
  186. { "realtime-priority", 1, 0, 'P' },
  187. { "spoon", 0, 0, 'F' },
  188. { 0, 0, 0, 0 }
  189. };
  190. int option_index;
  191. int opt;
  192. int no_fork = 0;
  193. opterr = 0;
  194. while ((opt = getopt_long (argc, argv, options, long_options, &option_index)) != EOF) {
  195. switch (opt) {
  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. no_fork = 1;
  207. break;
  208. case 'P':
  209. realtime_priority = atoi (optarg);
  210. break;
  211. case 'R':
  212. realtime = 1;
  213. break;
  214. case 'h':
  215. default:
  216. fprintf (stderr, "unknown option character %c\n", opt);
  217. usage();
  218. return -1;
  219. }
  220. }
  221. if (no_fork) {
  222. jack_main ();
  223. cleanup ();
  224. } else {
  225. int pid = fork ();
  226. if (pid < 0) {
  227. fprintf (stderr, "could not fork jack server (%s)", strerror (errno));
  228. exit (1);
  229. } else if (pid == 0) {
  230. jack_main ();
  231. } else {
  232. jackd_pid = pid;
  233. catch_signals ();
  234. waitpid (pid, NULL, 0);
  235. cleanup ();
  236. }
  237. }
  238. return 0;
  239. }