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.

471 lines
10KB

  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. #ifdef USE_CAPABILITIES
  28. #include <sys/stat.h>
  29. /* capgetp and capsetp are linux only extensions, not posix */
  30. #undef _POSIX_SOURCE
  31. #include <sys/capability.h>
  32. #include <jack/start.h>
  33. static struct stat pipe_stat;
  34. #endif
  35. static sigset_t signals;
  36. static jack_engine_t *engine = 0;
  37. static int jackd_pid;
  38. static int realtime = 0;
  39. static int realtime_priority = 10;
  40. static int with_fork = 1;
  41. static int verbose = 0;
  42. static int asio_mode = 0;
  43. typedef struct {
  44. pid_t pid;
  45. int argc;
  46. char **argv;
  47. } waiter_arg_t;
  48. #define ILOWER 0
  49. #define IRANGE 3000
  50. int wait_times[IRANGE];
  51. int unders = 0;
  52. int overs = 0;
  53. int max_over = 0;
  54. int min_under = INT_MAX;
  55. #define WRANGE 3000
  56. int work_times[WRANGE];
  57. int work_overs = 0;
  58. int work_max = 0;
  59. void
  60. store_work_time (int howlong)
  61. {
  62. if (howlong < WRANGE) {
  63. work_times[howlong]++;
  64. } else {
  65. work_overs++;
  66. }
  67. if (work_max < howlong) {
  68. work_max = howlong;
  69. }
  70. }
  71. void
  72. show_work_times ()
  73. {
  74. int i;
  75. for (i = 0; i < WRANGE; ++i) {
  76. printf ("%d %d\n", i, work_times[i]);
  77. }
  78. printf ("work overs = %d\nmax = %d\n", work_overs, work_max);
  79. }
  80. void
  81. store_wait_time (int interval)
  82. {
  83. if (interval < ILOWER) {
  84. unders++;
  85. } else if (interval >= ILOWER + IRANGE) {
  86. overs++;
  87. } else {
  88. wait_times[interval-ILOWER]++;
  89. }
  90. if (interval > max_over) {
  91. max_over = interval;
  92. }
  93. if (interval < min_under) {
  94. min_under = interval;
  95. }
  96. }
  97. void
  98. show_wait_times ()
  99. {
  100. int i;
  101. for (i = 0; i < IRANGE; i++) {
  102. printf ("%d %d\n", i+ILOWER, wait_times[i]);
  103. }
  104. printf ("unders: %d\novers: %d\n", unders, overs);
  105. printf ("max: %d\nmin: %d\n", max_over, min_under);
  106. }
  107. static void
  108. signal_handler (int sig)
  109. {
  110. fprintf (stderr, "jackd: signal %d received\n", sig);
  111. kill (jackd_pid, SIGTERM);
  112. }
  113. static void
  114. posix_me_harder (void)
  115. {
  116. /* what's this for?
  117. POSIX says that signals are delivered like this:
  118. * if a thread has blocked that signal, it is not
  119. a candidate to receive the signal.
  120. * of all threads not blocking the signal, pick
  121. one at random, and deliver the signal.
  122. this means that a simple-minded multi-threaded
  123. program can expect to get POSIX signals delivered
  124. randomly to any one of its threads,
  125. here, we block all signals that we think we
  126. might receive and want to catch. all "child"
  127. threads will inherit this setting. if we
  128. create a thread that calls sigwait() on the
  129. same set of signals, implicitly unblocking
  130. all those signals. any of those signals that
  131. are delivered to the process will be delivered
  132. to that thread, and that thread alone. this
  133. makes cleanup for a signal-driven exit much
  134. easier, since we know which thread is doing
  135. it and more importantly, we are free to
  136. call async-unsafe functions, because the
  137. code is executing in normal thread context
  138. after a return from sigwait().
  139. */
  140. sigemptyset (&signals);
  141. sigaddset(&signals, SIGHUP);
  142. sigaddset(&signals, SIGINT);
  143. sigaddset(&signals, SIGQUIT);
  144. sigaddset(&signals, SIGILL);
  145. sigaddset(&signals, SIGTRAP);
  146. sigaddset(&signals, SIGABRT);
  147. sigaddset(&signals, SIGIOT);
  148. sigaddset(&signals, SIGFPE);
  149. sigaddset(&signals, SIGPIPE);
  150. sigaddset(&signals, SIGTERM);
  151. sigaddset(&signals, SIGUSR1);
  152. /* this can make debugging a pain, but it also makes
  153. segv-exits cleanup_files after themselves rather than
  154. leaving the audio thread active. i still
  155. find it truly wierd that _exit() or whatever is done
  156. by the default SIGSEGV handler does not
  157. cancel all threads in a process, but what
  158. else can we do?
  159. */
  160. sigaddset(&signals, SIGSEGV);
  161. /* all child threads will inherit this mask */
  162. pthread_sigmask (SIG_BLOCK, &signals, 0);
  163. }
  164. static void *
  165. jack_engine_waiter_thread (void *arg)
  166. {
  167. waiter_arg_t *warg = (waiter_arg_t *) arg;
  168. jack_driver_t *driver;
  169. pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  170. if ((engine = jack_engine_new (realtime, realtime_priority, verbose)) == 0) {
  171. fprintf (stderr, "cannot create engine\n");
  172. kill (warg->pid, SIGTERM);
  173. return 0;
  174. }
  175. if (warg->argc) {
  176. if ((driver = jack_driver_load (warg->argc, warg->argv)) == 0) {
  177. fprintf (stderr, "cannot load driver module %s\n", warg->argv[0]);
  178. kill (warg->pid, SIGTERM);
  179. return 0;
  180. }
  181. jack_use_driver (engine, driver);
  182. }
  183. if (asio_mode) {
  184. jack_set_asio_mode (engine, TRUE);
  185. }
  186. if (jack_run (engine)) {
  187. fprintf (stderr, "cannot start main JACK thread\n");
  188. kill (warg->pid, SIGTERM);
  189. return 0;
  190. }
  191. jack_wait (engine);
  192. fprintf (stderr, "telling signal thread that the engine is done\n");
  193. kill (warg->pid, SIGHUP);
  194. return 0; /* nobody cares what this returns */
  195. }
  196. static void
  197. jack_main (int argc, char **argv)
  198. {
  199. int sig;
  200. pthread_t waiter_thread;
  201. waiter_arg_t warg;
  202. pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  203. posix_me_harder ();
  204. /* what we'd really like to do here is to be able to
  205. wait for either the engine to stop or a POSIX signal,
  206. whichever arrives sooner. but there is no mechanism
  207. to do that, so instead we create a thread to wait
  208. for the engine to finish, and here we stop and wait
  209. for any (reasonably likely) POSIX signal.
  210. if the engine finishes first, the waiter thread will
  211. tell us about it via a signal.
  212. if a signal arrives, we'll stop the engine and then
  213. exit.
  214. in normal operation, our parent process will be waiting
  215. for us and will cleanup.
  216. */
  217. warg.pid = getpid();
  218. warg.argc = argc;
  219. warg.argv = argv;
  220. if (pthread_create (&waiter_thread, 0, jack_engine_waiter_thread, &warg)) {
  221. fprintf (stderr, "jackd: cannot create engine waiting thread\n");
  222. return;
  223. }
  224. /* Note: normal operation has with_fork == 1 */
  225. if (with_fork) {
  226. /* let the parent handle SIGINT */
  227. sigdelset (&signals, SIGINT);
  228. }
  229. if (verbose) {
  230. fprintf (stderr, "%d waiting for signals\n", getpid());
  231. }
  232. while(1) {
  233. sigwait (&signals, &sig);
  234. printf ("jack main caught signal %d\n", sig);
  235. if (sig == SIGUSR1) {
  236. jack_dump_configuration(engine, 1);
  237. } else {
  238. /* continue to kill engine */
  239. break;
  240. }
  241. }
  242. pthread_cancel (waiter_thread);
  243. jack_engine_delete (engine);
  244. return;
  245. }
  246. static void usage ()
  247. {
  248. fprintf (stderr, "\
  249. usage: jackd [ --asio OR -a ]
  250. [ --realtime OR -R [ --realtime-priority OR -P priority ] ]
  251. [ --verbose OR -v ]
  252. [ --tmpdir OR -D directory-for-temporary-files ]
  253. -d driver [ ... driver args ... ]
  254. ");
  255. }
  256. int
  257. main (int argc, char *argv[])
  258. {
  259. const char *options = "ad:D:P:vhRFl:";
  260. struct option long_options[] =
  261. {
  262. { "asio", 0, 0, 'a' },
  263. { "driver", 1, 0, 'd' },
  264. { "tmpdir", 1, 0, 'D' },
  265. { "verbose", 0, 0, 'v' },
  266. { "help", 0, 0, 'h' },
  267. { "realtime", 0, 0, 'R' },
  268. { "realtime-priority", 1, 0, 'P' },
  269. { "spoon", 0, 0, 'F' },
  270. { 0, 0, 0, 0 }
  271. };
  272. int option_index;
  273. int opt;
  274. int seen_driver = 0;
  275. char *driver_name = 0;
  276. char **driver_args = 0;
  277. int driver_nargs = 1;
  278. int i;
  279. #ifdef USE_CAPABILITIES
  280. int status;
  281. #endif
  282. #ifdef USE_CAPABILITIES
  283. /* check to see if there is a pipe in the right descriptor */
  284. if ((status = fstat (PIPE_WRITE_FD, &pipe_stat)) == 0 && S_ISFIFO(pipe_stat.st_mode)) {
  285. /* tell jackstart we are up and running */
  286. if (close(PIPE_WRITE_FD) != 0) {
  287. fprintf(stderr, "jackd: error on startup pipe close: %s\n", strerror (errno));
  288. } else {
  289. /* wait for jackstart process to set our capabilities */
  290. if (wait (&status) == -1) {
  291. fprintf (stderr, "jackd: wait for startup process exit failed\n");
  292. }
  293. if (!WIFEXITED (status) || WEXITSTATUS (status)) {
  294. fprintf(stderr, "jackd: jackstart did not exit cleanly\n");
  295. exit (1);
  296. }
  297. }
  298. }
  299. #endif
  300. opterr = 0;
  301. while (!seen_driver && (opt = getopt_long (argc, argv, options, long_options, &option_index)) != EOF) {
  302. switch (opt) {
  303. case 'a':
  304. asio_mode = TRUE;
  305. break;
  306. case 'D':
  307. jack_set_temp_dir (optarg);
  308. break;
  309. case 'd':
  310. seen_driver = 1;
  311. driver_name = optarg;
  312. break;
  313. case 'v':
  314. verbose = 1;
  315. break;
  316. case 'F':
  317. with_fork = 0;
  318. break;
  319. case 'P':
  320. realtime_priority = atoi (optarg);
  321. break;
  322. case 'R':
  323. realtime = 1;
  324. break;
  325. default:
  326. fprintf (stderr, "unknown option character %c\n", optopt);
  327. /*fallthru*/
  328. case 'h':
  329. usage();
  330. return -1;
  331. }
  332. }
  333. if (!seen_driver) {
  334. usage ();
  335. exit (1);
  336. }
  337. if (optind < argc) {
  338. driver_nargs = 1 + argc - optind;
  339. } else {
  340. driver_nargs = 1;
  341. }
  342. driver_args = (char **) malloc (sizeof (char *) * driver_nargs);
  343. driver_args[0] = driver_name;
  344. for (i = 1; i < driver_nargs; i++) {
  345. driver_args[i] = argv[optind++];
  346. }
  347. printf ( "jackd " VERSION "\n"
  348. "Copyright 2001-2002 Paul Davis and others.\n"
  349. "jackd comes with ABSOLUTELY NO WARRANTY\n"
  350. "This is free software, and you are welcome to redistribute it\n"
  351. "under certain conditions; see the file COPYING for details\n\n");
  352. if (!with_fork) {
  353. /* This is really here so that we can run gdb easily */
  354. jack_main (driver_nargs, driver_args);
  355. } else {
  356. int pid = fork ();
  357. if (pid < 0) {
  358. fprintf (stderr, "could not fork jack server (%s)", strerror (errno));
  359. exit (1);
  360. } else if (pid == 0) {
  361. jack_main (driver_nargs, driver_args);
  362. } else {
  363. jackd_pid = pid;
  364. signal (SIGHUP, signal_handler);
  365. signal (SIGINT, signal_handler);
  366. signal (SIGQUIT, signal_handler);
  367. signal (SIGTERM, signal_handler);
  368. waitpid (pid, NULL, 0);
  369. }
  370. }
  371. jack_cleanup_shm ();
  372. jack_cleanup_files ();
  373. return 0;
  374. }