jack2 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.

433 lines
13KB

  1. /*
  2. Copyright (C) 2001 Paul Davis
  3. Copyright (C) 2004-2008 Grame
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. */
  16. #include <iostream>
  17. #include <assert.h>
  18. #include <signal.h>
  19. #include <pwd.h>
  20. #include <sys/types.h>
  21. #include <dirent.h>
  22. #include <getopt.h>
  23. #include "JackServer.h"
  24. #include "JackConstants.h"
  25. #include "driver_interface.h"
  26. #include "driver_parse.h"
  27. #include "JackDriverLoader.h"
  28. #include "jslist.h"
  29. #include "JackError.h"
  30. #include "JackTools.h"
  31. #include "shm.h"
  32. #include "jack.h"
  33. #ifdef __APPLE_
  34. #include <CoreFoundation/CFNotificationCenter.h>
  35. #endif
  36. using namespace Jack;
  37. static JackServer* fServer;
  38. static char* server_name = NULL;
  39. static int realtime_priority = 10;
  40. static int do_mlock = 1;
  41. static unsigned int port_max = 128;
  42. static int realtime = 0;
  43. static int loopback = 0;
  44. static int temporary = 0;
  45. static int client_timeout = 0; /* msecs; if zero, use period size. */
  46. static int do_unlock = 0;
  47. static JSList* drivers = NULL;
  48. static sigset_t signals;
  49. static void silent_jack_error_callback(const char *desc)
  50. {}
  51. static void copyright(FILE* file)
  52. {
  53. fprintf(file, "jackdmp " VERSION "\n"
  54. "Copyright 2001-2005 Paul Davis and others.\n"
  55. "Copyright 2004-2008 Grame.\n"
  56. "jackdmp comes with ABSOLUTELY NO WARRANTY\n"
  57. "This is free software, and you are welcome to redistribute it\n"
  58. "under certain conditions; see the file COPYING for details\n");
  59. }
  60. static void usage(FILE* file)
  61. {
  62. copyright(file);
  63. fprintf(file, "\n"
  64. "usage: jackdmp [ --realtime OR -R [ --realtime-priority OR -P priority ] ]\n"
  65. " [ --name OR -n server-name ]\n"
  66. // " [ --no-mlock OR -m ]\n"
  67. // " [ --unlock OR -u ]\n"
  68. " [ --timeout OR -t client-timeout-in-msecs ]\n"
  69. " [ --loopback OR -L loopback-port-number ]\n"
  70. // " [ --port-max OR -p maximum-number-of-ports]\n"
  71. " [ --verbose OR -v ]\n"
  72. " [ --replace-registry OR -r ]\n"
  73. " [ --silent OR -s ]\n"
  74. " [ --sync OR -S ]\n"
  75. " [ --temporary OR -T ]\n"
  76. " [ --version OR -V ]\n"
  77. " -d driver [ ... driver args ... ]\n"
  78. " where driver can be `alsa', `coreaudio', 'portaudio' or `dummy'\n"
  79. " jackdmp -d driver --help\n"
  80. " to display options for each driver\n\n");
  81. }
  82. static void DoNothingHandler(int sig)
  83. {
  84. /* this is used by the child (active) process, but it never
  85. gets called unless we are already shutting down after
  86. another signal.
  87. */
  88. char buf[64];
  89. snprintf(buf, sizeof(buf), "received signal %d during shutdown(ignored)\n", sig);
  90. write(1, buf, strlen(buf));
  91. }
  92. static int JackStart(const char* server_name, jack_driver_desc_t* driver_desc, JSList* driver_params, int sync, int temporary, int time_out_ms, int rt, int priority, int loopback, int verbose)
  93. {
  94. JackLog("Jackdmp: sync = %ld timeout = %ld rt = %ld priority = %ld verbose = %ld \n", sync, time_out_ms, rt, priority, verbose);
  95. fServer = new JackServer(sync, temporary, time_out_ms, rt, priority, loopback, verbose, server_name);
  96. int res = fServer->Open(driver_desc, driver_params);
  97. return (res < 0) ? res : fServer->Start();
  98. }
  99. static int JackStop()
  100. {
  101. fServer->Stop();
  102. fServer->Close();
  103. JackLog("Jackdmp: server close\n");
  104. delete fServer;
  105. JackLog("Jackdmp: delete server\n");
  106. return 0;
  107. }
  108. static int JackDelete()
  109. {
  110. delete fServer;
  111. JackLog("Jackdmp: delete server\n");
  112. return 0;
  113. }
  114. static void FilterSIGPIPE()
  115. {
  116. sigset_t set;
  117. sigemptyset(&set);
  118. sigaddset(&set, SIGPIPE);
  119. //sigprocmask(SIG_BLOCK, &set, 0);
  120. pthread_sigmask(SIG_BLOCK, &set, 0);
  121. }
  122. int main(int argc, char* argv[])
  123. {
  124. int sig;
  125. sigset_t allsignals;
  126. struct sigaction action;
  127. int waiting;
  128. jack_driver_desc_t* driver_desc;
  129. const char *options = "-ad:P:uvrshVRL:STFl:t:mn:p:";
  130. struct option long_options[] = {
  131. { "driver", 1, 0, 'd' },
  132. { "verbose", 0, 0, 'v' },
  133. { "help", 0, 0, 'h' },
  134. { "port-max", 1, 0, 'p' },
  135. { "no-mlock", 0, 0, 'm' },
  136. { "name", 0, 0, 'n' },
  137. { "unlock", 0, 0, 'u' },
  138. { "realtime", 0, 0, 'R' },
  139. { "replace-registry", 0, 0, 'r' },
  140. { "loopback", 0, 0, 'L' },
  141. { "realtime-priority", 1, 0, 'P' },
  142. { "timeout", 1, 0, 't' },
  143. { "temporary", 0, 0, 'T' },
  144. { "version", 0, 0, 'V' },
  145. { "silent", 0, 0, 's' },
  146. { "sync", 0, 0, 'S' },
  147. { 0, 0, 0, 0 }
  148. };
  149. int opt = 0;
  150. int option_index = 0;
  151. int seen_driver = 0;
  152. char *driver_name = NULL;
  153. char **driver_args = NULL;
  154. JSList* driver_params;
  155. int driver_nargs = 1;
  156. int show_version = 0;
  157. int replace_registry = 0;
  158. int sync = 0;
  159. int rc, i;
  160. opterr = 0;
  161. while (!seen_driver &&
  162. (opt = getopt_long(argc, argv, options,
  163. long_options, &option_index)) != EOF) {
  164. switch (opt) {
  165. case 'd':
  166. seen_driver = 1;
  167. driver_name = optarg;
  168. break;
  169. case 'v':
  170. jack_verbose = 1;
  171. break;
  172. case 's':
  173. jack_set_error_function(silent_jack_error_callback);
  174. break;
  175. case 'S':
  176. sync = 1;
  177. break;
  178. case 'n':
  179. server_name = optarg;
  180. break;
  181. case 'm':
  182. do_mlock = 0;
  183. break;
  184. case 'p':
  185. port_max = (unsigned int)atol(optarg);
  186. break;
  187. case 'P':
  188. realtime_priority = atoi(optarg);
  189. break;
  190. case 'r':
  191. replace_registry = 1;
  192. break;
  193. case 'R':
  194. realtime = 1;
  195. break;
  196. case 'L':
  197. loopback = atoi(optarg);
  198. break;
  199. case 'T':
  200. temporary = 1;
  201. break;
  202. case 't':
  203. client_timeout = atoi(optarg);
  204. break;
  205. case 'u':
  206. do_unlock = 1;
  207. break;
  208. case 'V':
  209. show_version = 1;
  210. break;
  211. default:
  212. fprintf(stderr, "unknown option character %c\n",
  213. optopt);
  214. /*fallthru*/
  215. case 'h':
  216. usage(stdout);
  217. return -1;
  218. }
  219. }
  220. /*
  221. if (show_version) {
  222. printf ( "jackd version " VERSION
  223. " tmpdir " DEFAULT_TMP_DIR
  224. " protocol " PROTOCOL_VERSION
  225. "\n");
  226. return -1;
  227. }
  228. */
  229. if (!seen_driver) {
  230. usage(stderr);
  231. exit(1);
  232. }
  233. drivers = jack_drivers_load(drivers);
  234. if (!drivers) {
  235. fprintf(stderr, "jackdmp: no drivers found; exiting\n");
  236. exit(1);
  237. }
  238. driver_desc = jack_find_driver_descriptor(drivers, driver_name);
  239. if (!driver_desc) {
  240. fprintf(stderr, "jackdmp: unknown driver '%s'\n", driver_name);
  241. exit(1);
  242. }
  243. if (optind < argc) {
  244. driver_nargs = 1 + argc - optind;
  245. } else {
  246. driver_nargs = 1;
  247. }
  248. if (driver_nargs == 0) {
  249. fprintf(stderr, "No driver specified ... hmm. JACK won't do"
  250. " anything when run like this.\n");
  251. return -1;
  252. }
  253. driver_args = (char **) malloc(sizeof(char *) * driver_nargs);
  254. driver_args[0] = driver_name;
  255. for (i = 1; i < driver_nargs; i++) {
  256. driver_args[i] = argv[optind++];
  257. }
  258. if (jack_parse_driver_params(driver_desc, driver_nargs,
  259. driver_args, &driver_params)) {
  260. exit(0);
  261. }
  262. if (server_name == NULL)
  263. server_name = (char*)JackTools::DefaultServerName();
  264. copyright(stdout);
  265. rc = jack_register_server(server_name, replace_registry);
  266. switch (rc) {
  267. case EEXIST:
  268. fprintf(stderr, "`%s' server already active\n", server_name);
  269. exit(1);
  270. case ENOSPC:
  271. fprintf(stderr, "too many servers already active\n");
  272. exit(2);
  273. case ENOMEM:
  274. fprintf(stderr, "no access to shm registry\n");
  275. exit(3);
  276. default:
  277. if (jack_verbose)
  278. fprintf(stderr, "server `%s' registered\n", server_name);
  279. }
  280. /* clean up shared memory and files from any previous
  281. * instance of this server name */
  282. jack_cleanup_shm();
  283. JackTools::CleanupFiles(server_name);
  284. pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  285. sigemptyset(&signals);
  286. sigaddset(&signals, SIGHUP);
  287. sigaddset(&signals, SIGINT);
  288. sigaddset(&signals, SIGQUIT);
  289. sigaddset(&signals, SIGPIPE);
  290. sigaddset(&signals, SIGTERM);
  291. sigaddset(&signals, SIGUSR1);
  292. sigaddset(&signals, SIGUSR2);
  293. // all child threads will inherit this mask unless they
  294. // explicitly reset it
  295. FilterSIGPIPE();
  296. pthread_sigmask(SIG_BLOCK, &signals, 0);
  297. if (!realtime && client_timeout == 0)
  298. client_timeout = 500; /* 0.5 sec; usable when non realtime. */
  299. int res = JackStart(server_name, driver_desc, driver_params, sync, temporary, client_timeout, realtime, realtime_priority, loopback, jack_verbose);
  300. if (res < 0) {
  301. jack_error("Cannot start server... exit");
  302. JackDelete();
  303. return 0;
  304. }
  305. #ifdef __APPLE__
  306. CFStringRef ref = CFStringCreateWithCString(NULL, server_name, kCFStringEncodingMacRoman);
  307. // Send notification to be used in the JackRouter plugin
  308. CFNotificationCenterPostNotificationWithOptions(CFNotificationCenterGetDistributedCenter(),
  309. CFSTR("com.grame.jackserver.start"),
  310. ref,
  311. NULL,
  312. kCFNotificationDeliverImmediately | kCFNotificationPostToAllSessions);
  313. CFRelease(ref);
  314. #endif
  315. // install a do-nothing handler because otherwise pthreads
  316. // behaviour is undefined when we enter sigwait.
  317. sigfillset(&allsignals);
  318. action.sa_handler = DoNothingHandler;
  319. action.sa_mask = allsignals;
  320. action.sa_flags = SA_RESTART | SA_RESETHAND;
  321. for (i = 1; i < NSIG; i++) {
  322. if (sigismember(&signals, i)) {
  323. sigaction(i, &action, 0);
  324. }
  325. }
  326. waiting = TRUE;
  327. while (waiting) {
  328. sigwait(&signals, &sig);
  329. fprintf(stderr, "jack main caught signal %d\n", sig);
  330. switch (sig) {
  331. case SIGUSR1:
  332. //jack_dump_configuration(engine, 1);
  333. break;
  334. case SIGUSR2:
  335. // driver exit
  336. waiting = FALSE;
  337. break;
  338. default:
  339. waiting = FALSE;
  340. break;
  341. }
  342. }
  343. if (sig != SIGSEGV) {
  344. // unblock signals so we can see them during shutdown.
  345. // this will help prod developers not to lose sight of
  346. // bugs that cause segfaults etc. during shutdown.
  347. sigprocmask(SIG_UNBLOCK, &signals, 0);
  348. }
  349. JackStop();
  350. jack_cleanup_shm();
  351. JackTools::CleanupFiles(server_name);
  352. jack_unregister_server(server_name);
  353. #ifdef __APPLE__
  354. CFStringRef ref1 = CFStringCreateWithCString(NULL, server_name, kCFStringEncodingMacRoman);
  355. // Send notification to be used in the JackRouter plugin
  356. CFNotificationCenterPostNotificationWithOptions(CFNotificationCenterGetDistributedCenter(),
  357. CFSTR("com.grame.jackserver.stop"),
  358. ref1,
  359. NULL,
  360. kCFNotificationDeliverImmediately | kCFNotificationPostToAllSessions);
  361. CFRelease(ref1);
  362. #endif
  363. return 1;
  364. }