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.

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