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.

815 lines
20KB

  1. /* -*- mode: c; c-file-style: "bsd"; -*- */
  2. /*
  3. Copyright (C) 2001-2005 Paul Davis
  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 <config.h>
  17. #include <stdio.h>
  18. #include <ctype.h>
  19. #include <signal.h>
  20. #include <getopt.h>
  21. #include <sys/types.h>
  22. #include <sys/shm.h>
  23. #include <sys/wait.h>
  24. #include <string.h>
  25. #include <errno.h>
  26. #include <stdlib.h>
  27. #include <dirent.h>
  28. #include <dlfcn.h>
  29. #include <jack/midiport.h>
  30. #include "engine.h"
  31. #include "internal.h"
  32. #include "driver.h"
  33. #include "shm.h"
  34. #include "driver_parse.h"
  35. #include "messagebuffer.h"
  36. #ifdef USE_CAPABILITIES
  37. #include <sys/stat.h>
  38. /* capgetp and capsetp are linux only extensions, not posix */
  39. #undef _POSIX_SOURCE
  40. #include <sys/capability.h>
  41. #include "start.h"
  42. static struct stat pipe_stat;
  43. #endif /* USE_CAPABILITIES */
  44. static JSList *drivers = NULL;
  45. static sigset_t signals;
  46. static jack_engine_t *engine = NULL;
  47. static char *server_name = NULL;
  48. static int realtime = 1;
  49. static int realtime_priority = 10;
  50. static int do_mlock = 1;
  51. static int temporary = 0;
  52. static int verbose = 0;
  53. static int client_timeout = 0; /* msecs; if zero, use period size. */
  54. static unsigned int port_max = 256;
  55. static int do_unlock = 0;
  56. static jack_nframes_t frame_time_offset = 0;
  57. static int nozombies = 0;
  58. static int timeout_count_threshold = 0;
  59. extern int sanitycheck (int, int);
  60. static jack_driver_desc_t *
  61. jack_find_driver_descriptor (const char * name);
  62. static void
  63. do_nothing_handler (int sig)
  64. {
  65. /* this is used by the child (active) process, but it never
  66. gets called unless we are already shutting down after
  67. another signal.
  68. */
  69. char buf[64];
  70. snprintf (buf, sizeof(buf),
  71. "received signal %d during shutdown (ignored)\n", sig);
  72. write (1, buf, strlen (buf));
  73. }
  74. static int
  75. jack_main (jack_driver_desc_t * driver_desc, JSList * driver_params, JSList * slave_names)
  76. {
  77. int sig;
  78. int i;
  79. sigset_t allsignals;
  80. struct sigaction action;
  81. int waiting;
  82. JSList * node;
  83. /* ensure that we are in our own process group so that
  84. kill (SIG, -pgrp) does the right thing.
  85. */
  86. setsid ();
  87. pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  88. /* what's this for?
  89. POSIX says that signals are delivered like this:
  90. * if a thread has blocked that signal, it is not
  91. a candidate to receive the signal.
  92. * of all threads not blocking the signal, pick
  93. one at random, and deliver the signal.
  94. this means that a simple-minded multi-threaded program can
  95. expect to get POSIX signals delivered randomly to any one
  96. of its threads,
  97. here, we block all signals that we think we might receive
  98. and want to catch. all "child" threads will inherit this
  99. setting. if we create a thread that calls sigwait() on the
  100. same set of signals, implicitly unblocking all those
  101. signals. any of those signals that are delivered to the
  102. process will be delivered to that thread, and that thread
  103. alone. this makes cleanup for a signal-driven exit much
  104. easier, since we know which thread is doing it and more
  105. importantly, we are free to call async-unsafe functions,
  106. because the code is executing in normal thread context
  107. after a return from sigwait().
  108. */
  109. sigemptyset (&signals);
  110. sigaddset(&signals, SIGHUP);
  111. sigaddset(&signals, SIGINT);
  112. sigaddset(&signals, SIGQUIT);
  113. sigaddset(&signals, SIGPIPE);
  114. sigaddset(&signals, SIGTERM);
  115. sigaddset(&signals, SIGUSR1);
  116. sigaddset(&signals, SIGUSR2);
  117. /* all child threads will inherit this mask unless they
  118. * explicitly reset it
  119. */
  120. pthread_sigmask (SIG_BLOCK, &signals, 0);
  121. if (!realtime && client_timeout == 0)
  122. client_timeout = 500; /* 0.5 sec; usable when non realtime. */
  123. /* get the engine/driver started */
  124. if ((engine = jack_engine_new (realtime, realtime_priority,
  125. do_mlock, do_unlock, server_name,
  126. temporary, verbose, client_timeout,
  127. port_max, getpid(), frame_time_offset,
  128. nozombies, timeout_count_threshold, drivers)) == 0) {
  129. jack_error ("cannot create engine");
  130. return -1;
  131. }
  132. jack_info ("loading driver ..");
  133. if (jack_engine_load_driver (engine, driver_desc, driver_params)) {
  134. jack_error ("cannot load driver module %s",
  135. driver_desc->name);
  136. goto error;
  137. }
  138. for (node=slave_names; node; node=jack_slist_next(node)) {
  139. char *sl_name = node->data;
  140. jack_driver_desc_t *sl_desc = jack_find_driver_descriptor(sl_name);
  141. if (sl_desc) {
  142. jack_engine_load_slave_driver(engine, sl_desc, NULL);
  143. }
  144. }
  145. if (jack_drivers_start (engine) != 0) {
  146. jack_error ("cannot start driver");
  147. goto error;
  148. }
  149. /* install a do-nothing handler because otherwise pthreads
  150. behaviour is undefined when we enter sigwait.
  151. */
  152. sigfillset (&allsignals);
  153. action.sa_handler = do_nothing_handler;
  154. action.sa_mask = allsignals;
  155. action.sa_flags = SA_RESTART|SA_RESETHAND;
  156. for (i = 1; i < NSIG; i++) {
  157. if (sigismember (&signals, i)) {
  158. sigaction (i, &action, 0);
  159. }
  160. }
  161. if (verbose) {
  162. jack_info ("%d waiting for signals", getpid());
  163. }
  164. waiting = TRUE;
  165. while (waiting) {
  166. sigwait (&signals, &sig);
  167. jack_info ("jack main caught signal %d", sig);
  168. switch (sig) {
  169. case SIGUSR1:
  170. jack_dump_configuration(engine, 1);
  171. break;
  172. case SIGUSR2:
  173. /* driver exit */
  174. waiting = FALSE;
  175. break;
  176. default:
  177. waiting = FALSE;
  178. break;
  179. }
  180. }
  181. if (sig != SIGSEGV) {
  182. /* unblock signals so we can see them during shutdown.
  183. this will help prod developers not to lose sight of
  184. bugs that cause segfaults etc. during shutdown.
  185. */
  186. sigprocmask (SIG_UNBLOCK, &signals, 0);
  187. }
  188. jack_engine_delete (engine);
  189. return 1;
  190. error:
  191. jack_engine_delete (engine);
  192. return -1;
  193. }
  194. static jack_driver_desc_t *
  195. jack_drivers_get_descriptor (JSList * drivers, const char * sofile)
  196. {
  197. jack_driver_desc_t * descriptor, * other_descriptor;
  198. JackDriverDescFunction so_get_descriptor;
  199. JSList * node;
  200. void * dlhandle;
  201. char * filename;
  202. const char * dlerr;
  203. int err;
  204. char* driver_dir;
  205. if ((driver_dir = getenv("JACK_DRIVER_DIR")) == 0) {
  206. driver_dir = ADDON_DIR;
  207. }
  208. filename = malloc (strlen (driver_dir) + 1 + strlen (sofile) + 1);
  209. sprintf (filename, "%s/%s", driver_dir, sofile);
  210. if (verbose) {
  211. jack_info ("getting driver descriptor from %s", filename);
  212. }
  213. if ((dlhandle = dlopen (filename, RTLD_NOW|RTLD_GLOBAL)) == NULL) {
  214. jack_error ("could not open driver .so '%s': %s\n", filename, dlerror ());
  215. free (filename);
  216. return NULL;
  217. }
  218. so_get_descriptor = (JackDriverDescFunction)
  219. dlsym (dlhandle, "driver_get_descriptor");
  220. if ((dlerr = dlerror ()) != NULL) {
  221. jack_error("%s", dlerr);
  222. dlclose (dlhandle);
  223. free (filename);
  224. return NULL;
  225. }
  226. if ((descriptor = so_get_descriptor ()) == NULL) {
  227. jack_error ("driver from '%s' returned NULL descriptor\n", filename);
  228. dlclose (dlhandle);
  229. free (filename);
  230. return NULL;
  231. }
  232. if ((err = dlclose (dlhandle)) != 0) {
  233. jack_error ("error closing driver .so '%s': %s\n", filename, dlerror ());
  234. }
  235. /* check it doesn't exist already */
  236. for (node = drivers; node; node = jack_slist_next (node)) {
  237. other_descriptor = (jack_driver_desc_t *) node->data;
  238. if (strcmp (descriptor->name, other_descriptor->name) == 0) {
  239. jack_error ("the drivers in '%s' and '%s' both have the name '%s'; using the first\n",
  240. other_descriptor->file, filename, other_descriptor->name);
  241. /* FIXME: delete the descriptor */
  242. free (filename);
  243. return NULL;
  244. }
  245. }
  246. snprintf (descriptor->file, sizeof(descriptor->file), "%s", filename);
  247. free (filename);
  248. return descriptor;
  249. }
  250. static JSList *
  251. jack_drivers_load ()
  252. {
  253. struct dirent * dir_entry;
  254. DIR * dir_stream;
  255. const char * ptr;
  256. int err;
  257. JSList * driver_list = NULL;
  258. jack_driver_desc_t * desc;
  259. char* driver_dir;
  260. if ((driver_dir = getenv("JACK_DRIVER_DIR")) == 0) {
  261. driver_dir = ADDON_DIR;
  262. }
  263. /* search through the driver_dir and add get descriptors
  264. from the .so files in it */
  265. dir_stream = opendir (driver_dir);
  266. if (!dir_stream) {
  267. jack_error ("could not open driver directory %s: %s\n",
  268. driver_dir, strerror (errno));
  269. return NULL;
  270. }
  271. while ( (dir_entry = readdir (dir_stream)) ) {
  272. /* check the filename is of the right format */
  273. if (strncmp ("jack_", dir_entry->d_name, 5) != 0) {
  274. continue;
  275. }
  276. ptr = strrchr (dir_entry->d_name, '.');
  277. if (!ptr) {
  278. continue;
  279. }
  280. ptr++;
  281. if (strncmp ("so", ptr, 2) != 0) {
  282. continue;
  283. }
  284. desc = jack_drivers_get_descriptor (drivers, dir_entry->d_name);
  285. if (desc) {
  286. driver_list = jack_slist_append (driver_list, desc);
  287. }
  288. }
  289. err = closedir (dir_stream);
  290. if (err) {
  291. jack_error ("error closing driver directory %s: %s\n",
  292. driver_dir, strerror (errno));
  293. }
  294. if (!driver_list) {
  295. jack_error ("could not find any drivers in %s!\n", driver_dir);
  296. return NULL;
  297. }
  298. return driver_list;
  299. }
  300. static void copyright (FILE* file)
  301. {
  302. fprintf (file, "jackd " VERSION "\n"
  303. "Copyright 2001-2009 Paul Davis, Stephane Letz, Jack O'Quinn, Torben Hohn and others.\n"
  304. "jackd comes with ABSOLUTELY NO WARRANTY\n"
  305. "This is free software, and you are welcome to redistribute it\n"
  306. "under certain conditions; see the file COPYING for details\n\n");
  307. }
  308. static void usage (FILE *file)
  309. {
  310. copyright (file);
  311. fprintf (file, "\n"
  312. "usage: jackd [ --no-realtime OR -r ]\n"
  313. " [ --realtime OR -R [ --realtime-priority OR -P priority ] ]\n"
  314. " (the two previous arguments are mutually exclusive. The default is --realtime)\n"
  315. " [ --name OR -n server-name ]\n"
  316. " [ --no-mlock OR -m ]\n"
  317. " [ --unlock OR -u ]\n"
  318. " [ --timeout OR -t client-timeout-in-msecs ]\n"
  319. " [ --port-max OR -p maximum-number-of-ports]\n"
  320. " [ --debug-timer OR -D ]\n"
  321. " [ --no-sanity-checks OR -N ]\n"
  322. " [ --verbose OR -v ]\n"
  323. " [ --clocksource OR -c [ c(ycle) | h(pet) | s(ystem) ]\n"
  324. " [ --replace-registry ]\n"
  325. " [ --silent OR -s ]\n"
  326. " [ --version OR -V ]\n"
  327. " [ --nozombies OR -Z ]\n"
  328. " -d backend [ ... backend args ... ]\n"
  329. #ifdef __APPLE__
  330. " Available backends may include: coreaudio, dummy, net, portaudio.\n\n"
  331. #else
  332. " Available backends may include: alsa, dummy, freebob, firewire, net, oss, sun, or portaudio.\n\n"
  333. #endif
  334. " jackd -d backend --help\n"
  335. " to display options for each backend\n\n");
  336. }
  337. static jack_driver_desc_t *
  338. jack_find_driver_descriptor (const char * name)
  339. {
  340. jack_driver_desc_t * desc = 0;
  341. JSList * node;
  342. for (node = drivers; node; node = jack_slist_next (node)) {
  343. desc = (jack_driver_desc_t *) node->data;
  344. if (strcmp (desc->name, name) != 0) {
  345. desc = NULL;
  346. } else {
  347. break;
  348. }
  349. }
  350. return desc;
  351. }
  352. static void
  353. jack_cleanup_files (const char *server_name)
  354. {
  355. DIR *dir;
  356. struct dirent *dirent;
  357. char dir_name[PATH_MAX+1] = "";
  358. jack_server_dir (server_name, dir_name);
  359. /* On termination, we remove all files that jackd creates so
  360. * subsequent attempts to start jackd will not believe that an
  361. * instance is already running. If the server crashes or is
  362. * terminated with SIGKILL, this is not possible. So, cleanup
  363. * is also attempted when jackd starts.
  364. *
  365. * There are several tricky issues. First, the previous JACK
  366. * server may have run for a different user ID, so its files
  367. * may be inaccessible. This is handled by using a separate
  368. * JACK_TMP_DIR subdirectory for each user. Second, there may
  369. * be other servers running with different names. Each gets
  370. * its own subdirectory within the per-user directory. The
  371. * current process has already registered as `server_name', so
  372. * we know there is no other server actively using that name.
  373. */
  374. /* nothing to do if the server directory does not exist */
  375. if ((dir = opendir (dir_name)) == NULL) {
  376. return;
  377. }
  378. /* unlink all the files in this directory, they are mine */
  379. while ((dirent = readdir (dir)) != NULL) {
  380. char fullpath[PATH_MAX+1];
  381. if ((strcmp (dirent->d_name, ".") == 0)
  382. || (strcmp (dirent->d_name, "..") == 0)) {
  383. continue;
  384. }
  385. snprintf (fullpath, sizeof (fullpath), "%s/%s",
  386. dir_name, dirent->d_name);
  387. if (unlink (fullpath)) {
  388. jack_error ("cannot unlink `%s' (%s)", fullpath,
  389. strerror (errno));
  390. }
  391. }
  392. closedir (dir);
  393. /* now, delete the per-server subdirectory, itself */
  394. if (rmdir (dir_name)) {
  395. jack_error ("cannot remove `%s' (%s)", dir_name,
  396. strerror (errno));
  397. }
  398. /* finally, delete the per-user subdirectory, if empty */
  399. if (rmdir (jack_user_dir ())) {
  400. if (errno != ENOTEMPTY) {
  401. jack_error ("cannot remove `%s' (%s)",
  402. jack_user_dir (), strerror (errno));
  403. }
  404. }
  405. }
  406. static void
  407. maybe_use_capabilities ()
  408. {
  409. #ifdef USE_CAPABILITIES
  410. int status;
  411. /* check to see if there is a pipe in the right descriptor */
  412. if ((status = fstat (PIPE_WRITE_FD, &pipe_stat)) == 0 &&
  413. S_ISFIFO(pipe_stat.st_mode)) {
  414. /* tell jackstart we are up and running */
  415. char c = 1;
  416. if (write (PIPE_WRITE_FD, &c, 1) != 1) {
  417. jack_error ("cannot write to jackstart sync "
  418. "pipe %d (%s)", PIPE_WRITE_FD,
  419. strerror (errno));
  420. }
  421. if (close(PIPE_WRITE_FD) != 0) {
  422. jack_error("jackd: error on startup pipe close: %s",
  423. strerror (errno));
  424. } else {
  425. /* wait for jackstart process to set our capabilities */
  426. if (wait (&status) == -1) {
  427. jack_error ("jackd: wait for startup "
  428. "process exit failed");
  429. }
  430. if (!WIFEXITED (status) || WEXITSTATUS (status)) {
  431. jack_error ("jackd: jackstart did not "
  432. "exit cleanly");
  433. exit (1);
  434. }
  435. }
  436. }
  437. #endif /* USE_CAPABILITIES */
  438. }
  439. int
  440. main (int argc, char *argv[])
  441. {
  442. jack_driver_desc_t * desc;
  443. int replace_registry = 0;
  444. int do_sanity_checks = 1;
  445. int show_version = 0;
  446. const char *options = "-d:P:uvshVrRZTFlt:mM:n:Np:c:X:C:";
  447. struct option long_options[] =
  448. {
  449. /* keep ordered by single-letter option code */
  450. { "clock-source", 1, 0, 'c' },
  451. { "driver", 1, 0, 'd' },
  452. { "help", 0, 0, 'h' },
  453. { "tmpdir-location", 0, 0, 'l' },
  454. { "no-mlock", 0, 0, 'm' },
  455. { "midi-bufsize", 1, 0, 'M' },
  456. { "name", 1, 0, 'n' },
  457. { "no-sanity-checks", 0, 0, 'N' },
  458. { "port-max", 1, 0, 'p' },
  459. { "realtime-priority", 1, 0, 'P' },
  460. { "no-realtime", 0, 0, 'r' },
  461. { "realtime", 0, 0, 'R' },
  462. { "replace-registry", 0, &replace_registry, 0 },
  463. { "silent", 0, 0, 's' },
  464. { "sync", 0, 0, 'S' },
  465. { "timeout", 1, 0, 't' },
  466. { "temporary", 0, 0, 'T' },
  467. { "unlock", 0, 0, 'u' },
  468. { "version", 0, 0, 'V' },
  469. { "verbose", 0, 0, 'v' },
  470. { "slave-driver", 1, 0, 'X' },
  471. { "nozombies", 0, 0, 'Z' },
  472. { "timeout-thres", 2, 0, 'C' },
  473. { 0, 0, 0, 0 }
  474. };
  475. int opt = 0;
  476. int option_index = 0;
  477. int seen_driver = 0;
  478. char *driver_name = NULL;
  479. char **driver_args = NULL;
  480. JSList * driver_params;
  481. JSList * slave_drivers = NULL;
  482. size_t midi_buffer_size = 0;
  483. int driver_nargs = 1;
  484. int i;
  485. int rc;
  486. setvbuf (stdout, NULL, _IOLBF, 0);
  487. maybe_use_capabilities ();
  488. opterr = 0;
  489. while (!seen_driver &&
  490. (opt = getopt_long (argc, argv, options,
  491. long_options, &option_index)) != EOF) {
  492. switch (opt) {
  493. case 'c':
  494. if (tolower (optarg[0]) == 'h') {
  495. clock_source = JACK_TIMER_HPET;
  496. } else if (tolower (optarg[0]) == 'c') {
  497. clock_source = JACK_TIMER_CYCLE_COUNTER;
  498. } else if (tolower (optarg[0]) == 's') {
  499. clock_source = JACK_TIMER_SYSTEM_CLOCK;
  500. } else {
  501. usage (stderr);
  502. return -1;
  503. }
  504. break;
  505. case 'C':
  506. if (optarg)
  507. timeout_count_threshold = atoi (optarg);
  508. else
  509. timeout_count_threshold = 250;
  510. break;
  511. case 'd':
  512. seen_driver = 1;
  513. driver_name = optarg;
  514. break;
  515. case 'D':
  516. frame_time_offset = JACK_MAX_FRAMES - atoi(optarg);
  517. break;
  518. case 'l':
  519. /* special flag to allow libjack to determine jackd's idea of where tmpdir is */
  520. printf ("%s\n", jack_tmpdir);
  521. exit (0);
  522. case 'm':
  523. do_mlock = 0;
  524. break;
  525. case 'M':
  526. midi_buffer_size = (unsigned int) atol (optarg);
  527. break;
  528. case 'n':
  529. server_name = optarg;
  530. break;
  531. case 'N':
  532. do_sanity_checks = 0;
  533. break;
  534. case 'p':
  535. port_max = (unsigned int) atol (optarg);
  536. break;
  537. case 'P':
  538. realtime_priority = atoi (optarg);
  539. break;
  540. case 'r':
  541. realtime = 0;
  542. break;
  543. case 'R':
  544. /* this is now the default */
  545. realtime = 1;
  546. break;
  547. case 's':
  548. jack_set_error_function (silent_jack_error_callback);
  549. break;
  550. case 'S':
  551. /* this option is for jack2 only (synchronous mode) */
  552. break;
  553. case 'T':
  554. temporary = 1;
  555. break;
  556. case 't':
  557. client_timeout = atoi (optarg);
  558. break;
  559. case 'u':
  560. do_unlock = 1;
  561. break;
  562. case 'v':
  563. verbose = 1;
  564. break;
  565. case 'V':
  566. show_version = 1;
  567. break;
  568. case 'X':
  569. slave_drivers = jack_slist_append(slave_drivers, optarg);
  570. break;
  571. case 'Z':
  572. nozombies = 1;
  573. break;
  574. default:
  575. jack_error ("Unknown option character %c",
  576. optopt);
  577. /*fallthru*/
  578. case 'h':
  579. usage (stdout);
  580. return -1;
  581. }
  582. }
  583. if (show_version) {
  584. printf ( "jackd version " VERSION
  585. " tmpdir " DEFAULT_TMP_DIR
  586. " protocol " PROTOCOL_VERSION
  587. "\n");
  588. return 0;
  589. }
  590. copyright (stdout);
  591. if (do_sanity_checks && (0 < sanitycheck (realtime, (clock_source == JACK_TIMER_CYCLE_COUNTER)))) {
  592. return -1;
  593. }
  594. if (realtime && (client_timeout >= JACKD_WATCHDOG_TIMEOUT)) {
  595. usage (stderr);
  596. fprintf (stderr, "In realtime mode (-R) the client timeout must be smaller than the watchdog timeout (%ims).\n", JACKD_WATCHDOG_TIMEOUT);
  597. exit (1);
  598. }
  599. if (!seen_driver) {
  600. usage (stderr);
  601. exit (1);
  602. }
  603. drivers = jack_drivers_load ();
  604. if (!drivers) {
  605. fprintf (stderr, "jackd: no drivers found; exiting\n");
  606. exit (1);
  607. }
  608. if (midi_buffer_size != 0) {
  609. jack_port_type_info_t* port_type = &jack_builtin_port_types[JACK_MIDI_PORT_TYPE];
  610. port_type->buffer_size = midi_buffer_size * jack_midi_internal_event_size ();
  611. port_type->buffer_scale_factor = -1;
  612. if (verbose) {
  613. fprintf (stderr, "Set MIDI buffer size to %u bytes\n", port_type->buffer_size);
  614. }
  615. }
  616. desc = jack_find_driver_descriptor (driver_name);
  617. if (!desc) {
  618. fprintf (stderr, "jackd: unknown driver '%s'\n", driver_name);
  619. exit (1);
  620. }
  621. if (optind < argc) {
  622. driver_nargs = 1 + argc - optind;
  623. } else {
  624. driver_nargs = 1;
  625. }
  626. if (driver_nargs == 0) {
  627. fprintf (stderr, "No driver specified ... hmm. JACK won't do"
  628. " anything when run like this.\n");
  629. return -1;
  630. }
  631. driver_args = (char **) malloc (sizeof (char *) * driver_nargs);
  632. driver_args[0] = driver_name;
  633. for (i = 1; i < driver_nargs; i++) {
  634. driver_args[i] = argv[optind++];
  635. }
  636. if (jack_parse_driver_params (desc, driver_nargs,
  637. driver_args, &driver_params)) {
  638. exit (0);
  639. }
  640. if (server_name == NULL)
  641. server_name = jack_default_server_name ();
  642. rc = jack_register_server (server_name, replace_registry);
  643. switch (rc) {
  644. case EEXIST:
  645. fprintf (stderr, "`%s' server already active\n", server_name);
  646. exit (1);
  647. case ENOSPC:
  648. fprintf (stderr, "too many servers already active\n");
  649. exit (2);
  650. case ENOMEM:
  651. fprintf (stderr, "no access to shm registry\n");
  652. exit (3);
  653. default:
  654. if (verbose)
  655. fprintf (stderr, "server `%s' registered\n",
  656. server_name);
  657. }
  658. /* clean up shared memory and files from any previous
  659. * instance of this server name */
  660. jack_cleanup_shm ();
  661. jack_cleanup_files (server_name);
  662. /* run the server engine until it terminates */
  663. jack_main (desc, driver_params, slave_drivers);
  664. /* clean up shared memory and files from this server instance */
  665. if (verbose)
  666. fprintf (stderr, "cleaning up shared memory\n");
  667. jack_cleanup_shm ();
  668. if (verbose)
  669. fprintf (stderr, "cleaning up files\n");
  670. jack_cleanup_files (server_name);
  671. if (verbose)
  672. fprintf (stderr, "unregistering server `%s'\n", server_name);
  673. jack_unregister_server (server_name);
  674. exit (0);
  675. }