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.

542 lines
16KB

  1. /*
  2. Copyright (C) 2001 Paul Davis
  3. Copyright (C) 2004-2006 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 "shm.h"
  31. #include "jack.h"
  32. using namespace Jack;
  33. static JackServer* fServer;
  34. static char* server_name = NULL;
  35. static int realtime_priority = 10;
  36. static int do_mlock = 1;
  37. static unsigned int port_max = 128;
  38. static int realtime = 0;
  39. static int loopback = 0;
  40. static int temporary = 0;
  41. static int client_timeout = 0; /* msecs; if zero, use period size. */
  42. static int do_unlock = 0;
  43. static JSList* drivers = NULL;
  44. static sigset_t signals;
  45. #define DEFAULT_TMP_DIR "/tmp"
  46. char* jack_tmpdir = DEFAULT_TMP_DIR;
  47. static void silent_jack_error_callback(const char *desc)
  48. {}
  49. static void copyright(FILE* file)
  50. {
  51. fprintf(file, "jackdmp " VERSION "\n"
  52. "Copyright 2001-2005 Paul Davis and others.\n"
  53. "Copyright 2004-2007 Grame.\n"
  54. "jackdmp comes with ABSOLUTELY NO WARRANTY\n"
  55. "This is free software, and you are welcome to redistribute it\n"
  56. "under certain conditions; see the file COPYING for details\n");
  57. }
  58. static void usage(FILE* file)
  59. {
  60. copyright(file);
  61. fprintf(file, "\n"
  62. "usage: jackdmp [ --realtime OR -R [ --realtime-priority OR -P priority ] ]\n"
  63. // " [ --name OR -n server-name ]\n"
  64. // " [ --no-mlock OR -m ]\n"
  65. // " [ --unlock OR -u ]\n"
  66. " [ --timeout OR -t client-timeout-in-msecs ]\n"
  67. " [ --loopback OR -L loopback-port-number ]\n"
  68. // " [ --port-max OR -p maximum-number-of-ports]\n"
  69. " [ --verbose OR -v ]\n"
  70. " [ --replace-registry OR -r ]\n"
  71. " [ --silent OR -s ]\n"
  72. " [ --sync OR -S ]\n"
  73. " [ --temporary OR -T ]\n"
  74. " [ --version OR -V ]\n"
  75. " -d driver [ ... driver args ... ]\n"
  76. " where driver can be `alsa', `coreaudio', 'portaudio' or `dummy'\n"
  77. " jackdmp -d driver --help\n"
  78. " to display options for each driver\n\n");
  79. }
  80. static void DoNothingHandler(int sig)
  81. {
  82. /* this is used by the child (active) process, but it never
  83. gets called unless we are already shutting down after
  84. another signal.
  85. */
  86. char buf[64];
  87. snprintf(buf, sizeof(buf), "received signal %d during shutdown(ignored)\n", sig);
  88. write(1, buf, strlen(buf));
  89. }
  90. static int JackStart(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)
  91. {
  92. JackLog("Jackdmp: sync = %ld timeout = %ld rt = %ld priority = %ld verbose = %ld \n", sync, time_out_ms, rt, priority, verbose);
  93. fServer = new JackServer(sync, temporary, time_out_ms, rt, priority, loopback, verbose);
  94. int res = fServer->Open(driver_desc, driver_params);
  95. return (res < 0) ? res : fServer->Start();
  96. }
  97. static int JackStop()
  98. {
  99. fServer->Stop();
  100. fServer->Close();
  101. JackLog("Jackdmp: server close\n");
  102. delete fServer;
  103. JackLog("Jackdmp: delete server\n");
  104. return 0;
  105. }
  106. static int JackDelete()
  107. {
  108. delete fServer;
  109. JackLog("Jackdmp: delete server\n");
  110. return 0;
  111. }
  112. static void FilterSIGPIPE()
  113. {
  114. sigset_t set;
  115. sigemptyset(&set);
  116. sigaddset(&set, SIGPIPE);
  117. //sigprocmask(SIG_BLOCK, &set, 0);
  118. pthread_sigmask(SIG_BLOCK, &set, 0);
  119. }
  120. static char* jack_default_server_name(void)
  121. {
  122. char* server_name;
  123. if ((server_name = getenv("JACK_DEFAULT_SERVER")) == NULL)
  124. server_name = "jackdmp_default";
  125. return server_name;
  126. }
  127. /* returns the name of the per-user subdirectory of jack_tmpdir */
  128. static char* jack_user_dir(void)
  129. {
  130. static char user_dir[PATH_MAX] = "";
  131. /* format the path name on the first call */
  132. if (user_dir[0] == '\0') {
  133. snprintf(user_dir, sizeof(user_dir), "%s/jack-%d",
  134. jack_tmpdir, getuid());
  135. }
  136. return user_dir;
  137. }
  138. /* returns the name of the per-server subdirectory of jack_user_dir() */
  139. static char* get_jack_server_dir(const char* toto)
  140. {
  141. static char server_dir[PATH_MAX] = "";
  142. // format the path name on the first call
  143. if (server_dir[0] == '\0') {
  144. snprintf(server_dir, sizeof(server_dir), "%s/%s",
  145. jack_user_dir(), server_name);
  146. }
  147. return server_dir;
  148. }
  149. static void
  150. jack_cleanup_files(const char *server_name)
  151. {
  152. DIR *dir;
  153. struct dirent *dirent;
  154. char *dir_name = get_jack_server_dir(server_name);
  155. /* On termination, we remove all files that jackd creates so
  156. * subsequent attempts to start jackd will not believe that an
  157. * instance is already running. If the server crashes or is
  158. * terminated with SIGKILL, this is not possible. So, cleanup
  159. * is also attempted when jackd starts.
  160. *
  161. * There are several tricky issues. First, the previous JACK
  162. * server may have run for a different user ID, so its files
  163. * may be inaccessible. This is handled by using a separate
  164. * JACK_TMP_DIR subdirectory for each user. Second, there may
  165. * be other servers running with different names. Each gets
  166. * its own subdirectory within the per-user directory. The
  167. * current process has already registered as `server_name', so
  168. * we know there is no other server actively using that name.
  169. */
  170. /* nothing to do if the server directory does not exist */
  171. if ((dir = opendir(dir_name)) == NULL) {
  172. return ;
  173. }
  174. /* unlink all the files in this directory, they are mine */
  175. while ((dirent = readdir(dir)) != NULL) {
  176. char fullpath[PATH_MAX];
  177. if ((strcmp(dirent->d_name, ".") == 0)
  178. || (strcmp(dirent->d_name, "..") == 0)) {
  179. continue;
  180. }
  181. snprintf(fullpath, sizeof(fullpath), "%s/%s",
  182. dir_name, dirent->d_name);
  183. if (unlink(fullpath)) {
  184. jack_error("cannot unlink `%s' (%s)", fullpath,
  185. strerror(errno));
  186. }
  187. }
  188. closedir(dir);
  189. /* now, delete the per-server subdirectory, itself */
  190. if (rmdir(dir_name)) {
  191. jack_error("cannot remove `%s' (%s)", dir_name,
  192. strerror(errno));
  193. }
  194. /* finally, delete the per-user subdirectory, if empty */
  195. if (rmdir(jack_user_dir())) {
  196. if (errno != ENOTEMPTY) {
  197. jack_error("cannot remove `%s' (%s)",
  198. jack_user_dir(), strerror(errno));
  199. }
  200. }
  201. }
  202. int main(int argc, char* argv[])
  203. {
  204. int sig;
  205. sigset_t allsignals;
  206. struct sigaction action;
  207. int waiting;
  208. jack_driver_desc_t* driver_desc;
  209. const char *options = "-ad:P:uvrshVRL:STFl:t:mn:p:";
  210. struct option long_options[] = {
  211. { "driver", 1, 0, 'd' },
  212. { "verbose", 0, 0, 'v' },
  213. { "help", 0, 0, 'h' },
  214. { "port-max", 1, 0, 'p' },
  215. { "no-mlock", 0, 0, 'm' },
  216. { "name", 0, 0, 'n' },
  217. { "unlock", 0, 0, 'u' },
  218. { "realtime", 0, 0, 'R' },
  219. { "replace-registry", 0, 0, 'r' },
  220. { "loopback", 0, 0, 'L' },
  221. { "realtime-priority", 1, 0, 'P' },
  222. { "timeout", 1, 0, 't' },
  223. { "temporary", 0, 0, 'T' },
  224. { "version", 0, 0, 'V' },
  225. { "silent", 0, 0, 's' },
  226. { "sync", 0, 0, 'S' },
  227. { 0, 0, 0, 0 }
  228. };
  229. int opt = 0;
  230. int option_index = 0;
  231. int seen_driver = 0;
  232. char *driver_name = NULL;
  233. char **driver_args = NULL;
  234. JSList* driver_params;
  235. int driver_nargs = 1;
  236. int show_version = 0;
  237. int replace_registry = 0;
  238. int sync = 0;
  239. int rc, i;
  240. opterr = 0;
  241. while (!seen_driver &&
  242. (opt = getopt_long(argc, argv, options,
  243. long_options, &option_index)) != EOF) {
  244. switch (opt) {
  245. case 'd':
  246. seen_driver = 1;
  247. driver_name = optarg;
  248. break;
  249. case 'v':
  250. jack_verbose = 1;
  251. break;
  252. case 's':
  253. jack_set_error_function(silent_jack_error_callback);
  254. break;
  255. case 'S':
  256. sync = 1;
  257. break;
  258. case 'n':
  259. server_name = optarg;
  260. break;
  261. case 'm':
  262. do_mlock = 0;
  263. break;
  264. case 'p':
  265. port_max = (unsigned int)atol(optarg);
  266. break;
  267. case 'P':
  268. realtime_priority = atoi(optarg);
  269. break;
  270. case 'r':
  271. replace_registry = 1;
  272. break;
  273. case 'R':
  274. realtime = 1;
  275. break;
  276. case 'L':
  277. loopback = atoi(optarg);
  278. break;
  279. case 'T':
  280. temporary = 1;
  281. break;
  282. case 't':
  283. client_timeout = atoi(optarg);
  284. break;
  285. case 'u':
  286. do_unlock = 1;
  287. break;
  288. case 'V':
  289. show_version = 1;
  290. break;
  291. default:
  292. fprintf(stderr, "unknown option character %c\n",
  293. optopt);
  294. /*fallthru*/
  295. case 'h':
  296. usage(stdout);
  297. return -1;
  298. }
  299. }
  300. /*
  301. if (show_version) {
  302. printf ( "jackd version " VERSION
  303. " tmpdir " DEFAULT_TMP_DIR
  304. " protocol " PROTOCOL_VERSION
  305. "\n");
  306. return -1;
  307. }
  308. */
  309. if (!seen_driver) {
  310. usage(stderr);
  311. exit(1);
  312. }
  313. drivers = jack_drivers_load(drivers);
  314. if (!drivers) {
  315. fprintf(stderr, "jackdmp: no drivers found; exiting\n");
  316. exit(1);
  317. }
  318. driver_desc = jack_find_driver_descriptor(drivers, driver_name);
  319. if (!driver_desc) {
  320. fprintf(stderr, "jackdmp: unknown driver '%s'\n", driver_name);
  321. exit(1);
  322. }
  323. if (optind < argc) {
  324. driver_nargs = 1 + argc - optind;
  325. } else {
  326. driver_nargs = 1;
  327. }
  328. if (driver_nargs == 0) {
  329. fprintf(stderr, "No driver specified ... hmm. JACK won't do"
  330. " anything when run like this.\n");
  331. return -1;
  332. }
  333. driver_args = (char **) malloc(sizeof(char *) * driver_nargs);
  334. driver_args[0] = driver_name;
  335. for (i = 1; i < driver_nargs; i++) {
  336. driver_args[i] = argv[optind++];
  337. }
  338. if (jack_parse_driver_params(driver_desc, driver_nargs,
  339. driver_args, &driver_params)) {
  340. exit(0);
  341. }
  342. if (server_name == NULL)
  343. server_name = jack_default_server_name();
  344. copyright(stdout);
  345. rc = jack_register_server(server_name, replace_registry);
  346. switch (rc) {
  347. case EEXIST:
  348. fprintf(stderr, "`%s' server already active\n", server_name);
  349. exit(1);
  350. case ENOSPC:
  351. fprintf(stderr, "too many servers already active\n");
  352. exit(2);
  353. case ENOMEM:
  354. fprintf(stderr, "no access to shm registry\n");
  355. exit(3);
  356. default:
  357. if (jack_verbose)
  358. fprintf(stderr, "server `%s' registered\n", server_name);
  359. }
  360. /* clean up shared memory and files from any previous
  361. * instance of this server name */
  362. jack_cleanup_shm();
  363. jack_cleanup_files(server_name);
  364. pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  365. sigemptyset(&signals);
  366. sigaddset(&signals, SIGHUP);
  367. sigaddset(&signals, SIGINT);
  368. sigaddset(&signals, SIGQUIT);
  369. sigaddset(&signals, SIGPIPE);
  370. sigaddset(&signals, SIGTERM);
  371. sigaddset(&signals, SIGUSR1);
  372. sigaddset(&signals, SIGUSR2);
  373. // all child threads will inherit this mask unless they
  374. // explicitly reset it
  375. FilterSIGPIPE();
  376. pthread_sigmask(SIG_BLOCK, &signals, 0);
  377. if (!realtime && client_timeout == 0)
  378. client_timeout = 500; /* 0.5 sec; usable when non realtime. */
  379. int res = JackStart(driver_desc, driver_params, sync, temporary, client_timeout, realtime, realtime_priority, loopback, jack_verbose);
  380. if (res < 0) {
  381. jack_error("Cannot start server... exit");
  382. JackDelete();
  383. return 0;
  384. }
  385. /*
  386. jack_client_t* c1 = my_jack_internal_client_new("c1", "inprocess.so", "untitled");
  387. jack_client_t* c2 = my_jack_internal_client_new("c2", "inprocess.so", "untitled");
  388. jack_client_t* c3 = my_jack_internal_client_new("c3", "inprocess.so", "untitled");
  389. jack_client_t* c4 = my_jack_internal_client_new("c4", "inprocess.so", "untitled");
  390. jack_client_t* c5 = my_jack_internal_client_new("c5", "inprocess.so", "untitled");
  391. */
  392. /*
  393. For testing purpose...
  394. InternalMetro* client1 = new InternalMetro(1200, 0.4, 20, 80, "metro1");
  395. InternalMetro* client2 = new InternalMetro(600, 0.4, 20, 150, "metro2");
  396. InternalMetro* client3 = new InternalMetro(1000, 0.4, 20, 110, "metro3");
  397. InternalMetro* client4 = new InternalMetro(1200, 0.4, 20, 80, "metro4");
  398. InternalMetro* client5 = new InternalMetro(1500, 0.4, 20, 60, "metro5");
  399. InternalMetro* client6 = new InternalMetro(1200, 0.4, 20, 84, "metro6");
  400. InternalMetro* client7 = new InternalMetro(600, 0.4, 20, 160, "metro7");
  401. InternalMetro* client8 = new InternalMetro(1000, 0.4, 20, 113, "metro8");
  402. InternalMetro* client9 = new InternalMetro(1200, 0.4, 20, 84, "metro9");
  403. InternalMetro* client10 = new InternalMetro(1500, 0.4, 20, 70, "metro10");
  404. */
  405. // install a do-nothing handler because otherwise pthreads
  406. // behaviour is undefined when we enter sigwait.
  407. sigfillset(&allsignals);
  408. action.sa_handler = DoNothingHandler;
  409. action.sa_mask = allsignals;
  410. action.sa_flags = SA_RESTART | SA_RESETHAND;
  411. for (i = 1; i < NSIG; i++) {
  412. if (sigismember(&signals, i)) {
  413. sigaction(i, &action, 0);
  414. }
  415. }
  416. waiting = TRUE;
  417. while (waiting) {
  418. sigwait(&signals, &sig);
  419. fprintf(stderr, "jack main caught signal %d\n", sig);
  420. switch (sig) {
  421. case SIGUSR1:
  422. //jack_dump_configuration(engine, 1);
  423. break;
  424. case SIGUSR2:
  425. // driver exit
  426. waiting = FALSE;
  427. break;
  428. default:
  429. waiting = FALSE;
  430. break;
  431. }
  432. }
  433. if (sig != SIGSEGV) {
  434. // unblock signals so we can see them during shutdown.
  435. // this will help prod developers not to lose sight of
  436. // bugs that cause segfaults etc. during shutdown.
  437. sigprocmask(SIG_UNBLOCK, &signals, 0);
  438. }
  439. /*
  440. my_jack_internal_client_close(c1);
  441. my_jack_internal_client_close(c2);
  442. my_jack_internal_client_close(c3);
  443. my_jack_internal_client_close(c4);
  444. my_jack_internal_client_close(c5);
  445. */
  446. JackStop();
  447. jack_cleanup_shm();
  448. jack_cleanup_files(server_name);
  449. jack_unregister_server(server_name);
  450. return 1;
  451. }