Jackdmp.cpp

00001 /*
00002 Copyright (C) 2001 Paul Davis 
00003 Copyright (C) 2004-2006 Grame
00004 
00005 This program is free software; you can redistribute it and/or modify
00006 it under the terms of the GNU General Public License as published by
00007 the Free Software Foundation; either version 2 of the License, or
00008 (at your option) any later version.
00009 
00010 This program is distributed in the hope that it will be useful,
00011 but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 GNU General Public License for more details.
00014 
00015 You should have received a copy of the GNU General Public License
00016 along with this program; if not, write to the Free Software
00017 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00018 
00019 */
00020 
00021 #include <iostream>
00022 #include <assert.h>
00023 #include <signal.h>
00024 #include <pwd.h>
00025 #include <sys/types.h>
00026 #include <dirent.h>
00027 #include <getopt.h>
00028 
00029 #include "JackServer.h"
00030 #include "JackConstants.h"
00031 #include "driver_interface.h"
00032 #include "driver_parse.h"
00033 #include "JackDriverLoader.h"
00034 #include "jslist.h"
00035 #include "JackError.h"
00036 #include "shm.h"
00037 #include "jack.h"
00038 
00039 using namespace Jack;
00040 
00041 static JackServer* fServer;
00042 static char* server_name = NULL;
00043 static int realtime_priority = 10;
00044 static int do_mlock = 1;
00045 static unsigned int port_max = 128;
00046 static int realtime = 0;
00047 static int loopback = 0;
00048 static int temporary = 0;
00049 static int client_timeout = 0; /* msecs; if zero, use period size. */
00050 static int do_unlock = 0;
00051 static JSList* drivers = NULL;
00052 
00053 static sigset_t signals;
00054 
00055 #define DEFAULT_TMP_DIR "/tmp"
00056 char* jack_tmpdir = DEFAULT_TMP_DIR;
00057 
00058 static void silent_jack_error_callback(const char *desc)
00059 {}
00060 
00061 static void copyright(FILE* file)
00062 {
00063     fprintf(file, "jackdmp " VERSION "\n"
00064              "Copyright 2001-2005 Paul Davis and others.\n"
00065              "Copyright 2004-2007 Grame.\n"
00066              "jackdmp comes with ABSOLUTELY NO WARRANTY\n"
00067              "This is free software, and you are welcome to redistribute it\n"
00068              "under certain conditions; see the file COPYING for details\n");
00069 }
00070 
00071 static void usage(FILE* file)
00072 {
00073     copyright(file);
00074     fprintf(file, "\n"
00075              "usage: jackdmp [ --realtime OR -R [ --realtime-priority OR -P priority ] ]\n"
00076              // "               [ --name OR -n server-name ]\n"
00077              // "               [ --no-mlock OR -m ]\n"
00078              // "               [ --unlock OR -u ]\n"
00079              "               [ --timeout OR -t client-timeout-in-msecs ]\n"
00080              "               [ --loopback OR -L loopback-port-number ]\n"
00081              // "               [ --port-max OR -p maximum-number-of-ports]\n"
00082              "               [ --verbose OR -v ]\n"
00083                          "               [ --replace-registry OR -r ]\n"
00084              "               [ --silent OR -s ]\n"
00085              "               [ --sync OR -S ]\n"
00086                          "               [ --temporary OR -T ]\n"
00087              "               [ --version OR -V ]\n"
00088              "         -d driver [ ... driver args ... ]\n"
00089              "             where driver can be `alsa', `coreaudio', 'portaudio' or `dummy'\n"
00090              "       jackdmp -d driver --help\n"
00091              "             to display options for each driver\n\n");
00092 }
00093 
00094 
00095 static void DoNothingHandler(int sig)
00096 {
00097     /* this is used by the child (active) process, but it never
00098        gets called unless we are already shutting down after
00099        another signal.
00100     */
00101     char buf[64];
00102     snprintf(buf, sizeof(buf), "received signal %d during shutdown(ignored)\n", sig);
00103     write(1, buf, strlen(buf));
00104 }
00105 
00106 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)
00107 {
00108     JackLog("Jackdmp: sync = %ld timeout = %ld rt = %ld priority = %ld verbose = %ld \n", sync, time_out_ms, rt, priority, verbose);
00109     fServer = new JackServer(sync, temporary, time_out_ms, rt, priority, loopback, verbose);
00110     int res = fServer->Open(driver_desc, driver_params);
00111     return (res < 0) ? res : fServer->Start();
00112 }
00113 
00114 static int JackStop()
00115 {
00116     fServer->Stop();
00117     fServer->Close();
00118     JackLog("Jackdmp: server close\n");
00119     delete fServer;
00120     JackLog("Jackdmp: delete server\n");
00121     return 0;
00122 }
00123 
00124 static int JackDelete()
00125 {
00126     delete fServer;
00127     JackLog("Jackdmp: delete server\n");
00128     return 0;
00129 }
00130 
00131 static void FilterSIGPIPE()
00132 {
00133     sigset_t set;
00134     sigemptyset(&set);
00135     sigaddset(&set, SIGPIPE);
00136     //sigprocmask(SIG_BLOCK, &set, 0);
00137     pthread_sigmask(SIG_BLOCK, &set, 0);
00138 }
00139 
00140 static char* jack_default_server_name(void)
00141 {
00142     char* server_name;
00143     if ((server_name = getenv("JACK_DEFAULT_SERVER")) == NULL)
00144         server_name = "jackdmp_default";
00145     return server_name;
00146 }
00147 
00148 /* returns the name of the per-user subdirectory of jack_tmpdir */
00149 static char* jack_user_dir(void)
00150 {
00151     static char user_dir[PATH_MAX] = "";
00152 
00153     /* format the path name on the first call */
00154     if (user_dir[0] == '\0') {
00155         snprintf(user_dir, sizeof(user_dir), "%s/jack-%d",
00156                   jack_tmpdir, getuid());
00157     }
00158 
00159     return user_dir;
00160 }
00161 
00162 /* returns the name of the per-server subdirectory of jack_user_dir() */
00163 
00164 static char* get_jack_server_dir(const char* toto)
00165 {
00166     static char server_dir[PATH_MAX] = "";
00167 
00168     // format the path name on the first call
00169     if (server_dir[0] == '\0') {
00170         snprintf(server_dir, sizeof(server_dir), "%s/%s",
00171                   jack_user_dir(), server_name);
00172     }
00173 
00174     return server_dir;
00175 }
00176 
00177 static void
00178 jack_cleanup_files(const char *server_name)
00179 {
00180     DIR *dir;
00181     struct dirent *dirent;
00182     char *dir_name = get_jack_server_dir(server_name);
00183 
00184     /* On termination, we remove all files that jackd creates so
00185      * subsequent attempts to start jackd will not believe that an
00186      * instance is already running.  If the server crashes or is
00187      * terminated with SIGKILL, this is not possible.  So, cleanup
00188      * is also attempted when jackd starts.
00189      *
00190      * There are several tricky issues.  First, the previous JACK
00191      * server may have run for a different user ID, so its files
00192      * may be inaccessible.  This is handled by using a separate
00193      * JACK_TMP_DIR subdirectory for each user.  Second, there may
00194      * be other servers running with different names.  Each gets
00195      * its own subdirectory within the per-user directory.  The
00196      * current process has already registered as `server_name', so
00197      * we know there is no other server actively using that name.
00198      */
00199 
00200     /* nothing to do if the server directory does not exist */
00201     if ((dir = opendir(dir_name)) == NULL) {
00202         return ;
00203     }
00204 
00205     /* unlink all the files in this directory, they are mine */
00206     while ((dirent = readdir(dir)) != NULL) {
00207 
00208         char fullpath[PATH_MAX];
00209 
00210         if ((strcmp(dirent->d_name, ".") == 0)
00211                 || (strcmp(dirent->d_name, "..") == 0)) {
00212             continue;
00213         }
00214 
00215         snprintf(fullpath, sizeof(fullpath), "%s/%s",
00216                   dir_name, dirent->d_name);
00217 
00218         if (unlink(fullpath)) {
00219             jack_error("cannot unlink `%s' (%s)", fullpath,
00220                         strerror(errno));
00221         }
00222     }
00223 
00224     closedir(dir);
00225 
00226     /* now, delete the per-server subdirectory, itself */
00227     if (rmdir(dir_name)) {
00228         jack_error("cannot remove `%s' (%s)", dir_name,
00229                     strerror(errno));
00230     }
00231 
00232     /* finally, delete the per-user subdirectory, if empty */
00233     if (rmdir(jack_user_dir())) {
00234         if (errno != ENOTEMPTY) {
00235             jack_error("cannot remove `%s' (%s)",
00236                         jack_user_dir(), strerror(errno));
00237         }
00238     }
00239 }
00240 
00241 int main(int argc, char* argv[])
00242 {
00243     int sig;
00244     sigset_t allsignals;
00245     struct sigaction action;
00246     int waiting;
00247 
00248     jack_driver_desc_t* driver_desc;
00249     const char *options = "-ad:P:uvrshVRL:STFl:t:mn:p:";
00250     struct option long_options[] = {
00251                                        { "driver", 1, 0, 'd' },
00252                                        { "verbose", 0, 0, 'v' },
00253                                        { "help", 0, 0, 'h' },
00254                                        { "port-max", 1, 0, 'p' },
00255                                        { "no-mlock", 0, 0, 'm' },
00256                                        { "name", 0, 0, 'n' },
00257                                        { "unlock", 0, 0, 'u' },
00258                                        { "realtime", 0, 0, 'R' },
00259                                                                            { "replace-registry", 0, 0, 'r' },
00260                                        { "loopback", 0, 0, 'L' },
00261                                        { "realtime-priority", 1, 0, 'P' },
00262                                        { "timeout", 1, 0, 't' },
00263                                        { "temporary", 0, 0, 'T' },
00264                                        { "version", 0, 0, 'V' },
00265                                        { "silent", 0, 0, 's' },
00266                                        { "sync", 0, 0, 'S' },
00267                                        { 0, 0, 0, 0 }
00268                                    };
00269     int opt = 0;
00270     int option_index = 0;
00271     int seen_driver = 0;
00272     char *driver_name = NULL;
00273     char **driver_args = NULL;
00274     JSList* driver_params;
00275     int driver_nargs = 1;
00276     int show_version = 0;
00277         int replace_registry = 0;
00278     int sync = 0;
00279     int rc, i;
00280 
00281     opterr = 0;
00282     while (!seen_driver &&
00283             (opt = getopt_long(argc, argv, options,
00284                                long_options, &option_index)) != EOF) {
00285         switch (opt) {
00286 
00287             case 'd':
00288                 seen_driver = 1;
00289                 driver_name = optarg;
00290                 break;
00291 
00292             case 'v':
00293                 jack_verbose = 1;
00294                 break;
00295 
00296             case 's':
00297                 jack_set_error_function(silent_jack_error_callback);
00298                 break;
00299 
00300             case 'S':
00301                 sync = 1;
00302                 break;
00303 
00304             case 'n':
00305                 server_name = optarg;
00306                 break;
00307 
00308             case 'm':
00309                 do_mlock = 0;
00310                 break;
00311 
00312             case 'p':
00313                 port_max = (unsigned int)atol(optarg);
00314                 break;
00315 
00316             case 'P':
00317                 realtime_priority = atoi(optarg);
00318                 break;
00319                         
00320                         case 'r':
00321                                 replace_registry = 1;
00322                                 break;
00323                                 
00324             case 'R':
00325                 realtime = 1;
00326                 break;
00327 
00328             case 'L':
00329                 loopback = atoi(optarg);
00330                 break;
00331 
00332             case 'T':
00333                 temporary = 1;
00334                 break;
00335 
00336             case 't':
00337                 client_timeout = atoi(optarg);
00338                 break;
00339 
00340             case 'u':
00341                 do_unlock = 1;
00342                 break;
00343 
00344             case 'V':
00345                 show_version = 1;
00346                 break;
00347 
00348             default:
00349                 fprintf(stderr, "unknown option character %c\n",
00350                         optopt);
00351                 /*fallthru*/
00352             case 'h':
00353                 usage(stdout);
00354                 return -1;
00355         }
00356     }
00357 
00358     /*
00359     if (show_version) {
00360         printf ( "jackd version " VERSION 
00361                         " tmpdir " DEFAULT_TMP_DIR 
00362                         " protocol " PROTOCOL_VERSION
00363                         "\n");
00364         return -1;
00365     }
00366     */
00367 
00368     if (!seen_driver) {
00369         usage(stderr);
00370         exit(1);
00371     }
00372 
00373     drivers = jack_drivers_load(drivers);
00374     if (!drivers) {
00375         fprintf(stderr, "jackdmp: no drivers found; exiting\n");
00376         exit(1);
00377     }
00378 
00379     driver_desc = jack_find_driver_descriptor(drivers, driver_name);
00380     if (!driver_desc) {
00381         fprintf(stderr, "jackdmp: unknown driver '%s'\n", driver_name);
00382         exit(1);
00383     }
00384 
00385     if (optind < argc) {
00386         driver_nargs = 1 + argc - optind;
00387     } else {
00388         driver_nargs = 1;
00389     }
00390 
00391     if (driver_nargs == 0) {
00392         fprintf(stderr, "No driver specified ... hmm. JACK won't do"
00393                  " anything when run like this.\n");
00394         return -1;
00395     }
00396 
00397     driver_args = (char **) malloc(sizeof(char *) * driver_nargs);
00398     driver_args[0] = driver_name;
00399 
00400     for (i = 1; i < driver_nargs; i++) {
00401         driver_args[i] = argv[optind++];
00402     }
00403 
00404     if (jack_parse_driver_params(driver_desc, driver_nargs,
00405                                   driver_args, &driver_params)) {
00406         exit(0);
00407     }
00408 
00409     if (server_name == NULL)
00410         server_name = jack_default_server_name();
00411 
00412     copyright(stdout);
00413 
00414     rc = jack_register_server(server_name, replace_registry);
00415     switch (rc) {
00416         case EEXIST:
00417             fprintf(stderr, "`%s' server already active\n", server_name);
00418             exit(1);
00419         case ENOSPC:
00420             fprintf(stderr, "too many servers already active\n");
00421             exit(2);
00422         case ENOMEM:
00423             fprintf(stderr, "no access to shm registry\n");
00424             exit(3);
00425         default:
00426             if (jack_verbose)
00427                 fprintf(stderr, "server `%s' registered\n", server_name);
00428     }
00429 
00430     /* clean up shared memory and files from any previous
00431      * instance of this server name */
00432     jack_cleanup_shm();
00433     jack_cleanup_files(server_name);
00434 
00435     pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
00436 
00437     sigemptyset(&signals);
00438     sigaddset(&signals, SIGHUP);
00439     sigaddset(&signals, SIGINT);
00440     sigaddset(&signals, SIGQUIT);
00441     sigaddset(&signals, SIGPIPE);
00442     sigaddset(&signals, SIGTERM);
00443     sigaddset(&signals, SIGUSR1);
00444     sigaddset(&signals, SIGUSR2);
00445 
00446     // all child threads will inherit this mask unless they
00447     // explicitly reset it
00448 
00449     FilterSIGPIPE();
00450     pthread_sigmask(SIG_BLOCK, &signals, 0);
00451 
00452     if (!realtime && client_timeout == 0)
00453         client_timeout = 500; /* 0.5 sec; usable when non realtime. */
00454 
00455     int res = JackStart(driver_desc, driver_params, sync, temporary, client_timeout, realtime, realtime_priority, loopback, jack_verbose);
00456     if (res < 0) {
00457         jack_error("Cannot start server... exit");
00458         JackDelete();
00459         return 0;
00460     }
00461         
00462         /*
00463         jack_client_t* c1 = my_jack_internal_client_new("c1", "inprocess.so", "untitled");
00464         jack_client_t* c2 = my_jack_internal_client_new("c2", "inprocess.so", "untitled");
00465         jack_client_t* c3 = my_jack_internal_client_new("c3", "inprocess.so", "untitled");
00466         jack_client_t* c4 = my_jack_internal_client_new("c4", "inprocess.so", "untitled");
00467         jack_client_t* c5 = my_jack_internal_client_new("c5", "inprocess.so", "untitled");
00468         */
00469 
00470     /*
00471     For testing purpose...
00472     InternalMetro* client1  = new InternalMetro(1200, 0.4, 20, 80, "metro1");
00473     InternalMetro* client2  = new InternalMetro(600, 0.4, 20, 150, "metro2");
00474     InternalMetro* client3  = new InternalMetro(1000, 0.4, 20, 110, "metro3");
00475     InternalMetro* client4  = new InternalMetro(1200, 0.4, 20, 80, "metro4");
00476     InternalMetro* client5  = new InternalMetro(1500, 0.4, 20, 60, "metro5");
00477     InternalMetro* client6  = new InternalMetro(1200, 0.4, 20, 84, "metro6");
00478     InternalMetro* client7  = new InternalMetro(600, 0.4, 20, 160, "metro7");
00479     InternalMetro* client8  = new InternalMetro(1000, 0.4, 20, 113, "metro8");
00480     InternalMetro* client9  = new InternalMetro(1200, 0.4, 20, 84, "metro9");
00481     InternalMetro* client10  = new InternalMetro(1500, 0.4, 20, 70, "metro10");
00482     */
00483 
00484     // install a do-nothing handler because otherwise pthreads
00485     // behaviour is undefined when we enter sigwait.
00486 
00487     sigfillset(&allsignals);
00488     action.sa_handler = DoNothingHandler;
00489     action.sa_mask = allsignals;
00490     action.sa_flags = SA_RESTART | SA_RESETHAND;
00491 
00492     for (i = 1; i < NSIG; i++) {
00493         if (sigismember(&signals, i)) {
00494             sigaction(i, &action, 0);
00495         }
00496     }
00497 
00498     waiting = TRUE;
00499 
00500     while (waiting) {
00501         sigwait(&signals, &sig);
00502 
00503         fprintf(stderr, "jack main caught signal %d\n", sig);
00504 
00505         switch (sig) {
00506             case SIGUSR1:
00507                 //jack_dump_configuration(engine, 1);
00508                 break;
00509             case SIGUSR2:
00510                 // driver exit
00511                 waiting = FALSE;
00512                 break;
00513             default:
00514                 waiting = FALSE;
00515                 break;
00516         }
00517     }
00518 
00519     if (sig != SIGSEGV) {
00520         // unblock signals so we can see them during shutdown.
00521         // this will help prod developers not to lose sight of
00522         // bugs that cause segfaults etc. during shutdown.
00523         sigprocmask(SIG_UNBLOCK, &signals, 0);
00524     }
00525         
00526         /*
00527         my_jack_internal_client_close(c1);
00528         my_jack_internal_client_close(c2);
00529         my_jack_internal_client_close(c3);
00530         my_jack_internal_client_close(c4);
00531         my_jack_internal_client_close(c5);
00532         */
00533         
00534     JackStop();
00535 
00536     jack_cleanup_shm();
00537     jack_cleanup_files(server_name);
00538     jack_unregister_server(server_name);
00539 
00540     return 1;
00541 }

Generated on Mon Oct 15 17:11:14 2007 for Jackdmp by  doxygen 1.5.1