JackServerGlobals.cpp

00001 /*
00002 Copyright (C) 2005 Grame  
00003 
00004 This program is free software; you can redistribute it and/or modify
00005 it under the terms of the GNU General Public License as published by
00006 the Free Software Foundation; either version 2 of the License, or
00007 (at your option) any later version.
00008 
00009 This program is distributed in the hope that it will be useful,
00010 but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 GNU General Public License for more details.
00013 
00014 You should have received a copy of the GNU General Public License
00015 along with this program; if not, write to the Free Software
00016 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00017 
00018 */
00019 
00020 #ifdef WIN32 
00021 #pragma warning (disable : 4786)
00022 #endif
00023 
00024 #include "JackServerGlobals.h"
00025 #include "JackError.h"
00026 #include "shm.h"
00027 #include <getopt.h>
00028 
00029 #ifndef WIN32
00030         #include <dirent.h>
00031 #endif
00032 
00033 #define DEFAULT_TMP_DIR "/tmp"
00034 static char* jack_tmpdir = DEFAULT_TMP_DIR;
00035 static char* server_name = NULL;
00036 
00037 namespace Jack
00038 {
00039 
00040 long JackServerGlobals::fClientCount = 0;
00041 JackServer* JackServerGlobals::fServer = NULL;
00042 
00043 #ifndef WIN32
00044 
00045 static char* jack_default_server_name(void)
00046 {
00047     char* server_name;
00048     if ((server_name = getenv("JACK_DEFAULT_SERVER")) == NULL)
00049         server_name = "jackdmp_default";
00050     return server_name;
00051 }
00052 
00053 /* returns the name of the per-user subdirectory of jack_tmpdir */
00054 static char* jack_user_dir(void)
00055 {
00056     static char user_dir[PATH_MAX] = "";
00057 
00058     /* format the path name on the first call */
00059     if (user_dir[0] == '\0') {
00060         snprintf(user_dir, sizeof(user_dir), "%s/jack-%d", jack_tmpdir, getuid());
00061     }
00062 
00063     return user_dir;
00064 }
00065 
00066 /* returns the name of the per-server subdirectory of jack_user_dir() */
00067 
00068 static char* get_jack_server_dir(const char* toto)
00069 {
00070     static char server_dir[PATH_MAX] = "";
00071 
00072     // format the path name on the first call
00073     if (server_dir[0] == '\0') {
00074         snprintf(server_dir, sizeof (server_dir), "%s/%s", jack_user_dir(), server_name);
00075     }
00076 
00077     return server_dir;
00078 }
00079 
00080 static void
00081 jack_cleanup_files(const char *server_name)
00082 {
00083     DIR *dir;
00084     struct dirent *dirent;
00085     char *dir_name = get_jack_server_dir(server_name);
00086 
00087     /* On termination, we remove all files that jackd creates so
00088      * subsequent attempts to start jackd will not believe that an
00089      * instance is already running.  If the server crashes or is
00090      * terminated with SIGKILL, this is not possible.  So, cleanup
00091      * is also attempted when jackd starts.
00092      *
00093      * There are several tricky issues.  First, the previous JACK
00094      * server may have run for a different user ID, so its files
00095      * may be inaccessible.  This is handled by using a separate
00096      * JACK_TMP_DIR subdirectory for each user.  Second, there may
00097      * be other servers running with different names.  Each gets
00098      * its own subdirectory within the per-user directory.  The
00099      * current process has already registered as `server_name', so
00100      * we know there is no other server actively using that name.
00101      */
00102 
00103     /* nothing to do if the server directory does not exist */
00104     if ((dir = opendir(dir_name)) == NULL) {
00105         return ;
00106     }
00107 
00108     /* unlink all the files in this directory, they are mine */
00109     while ((dirent = readdir(dir)) != NULL) {
00110 
00111         char fullpath[PATH_MAX];
00112 
00113         if ((strcmp(dirent->d_name, ".") == 0)
00114                 || (strcmp(dirent->d_name, "..") == 0)) {
00115             continue;
00116         }
00117 
00118         snprintf(fullpath, sizeof (fullpath), "%s/%s", dir_name, dirent->d_name);
00119 
00120         if (unlink(fullpath)) {
00121             jack_error("cannot unlink `%s' (%s)", fullpath, strerror(errno));
00122         }
00123     }
00124 
00125     closedir(dir);
00126 
00127     /* now, delete the per-server subdirectory, itself */
00128     if (rmdir(dir_name)) {
00129         jack_error("cannot remove `%s' (%s)", dir_name,
00130                     strerror(errno));
00131     }
00132 
00133     /* finally, delete the per-user subdirectory, if empty */
00134     if (rmdir (jack_user_dir())) {
00135         if (errno != ENOTEMPTY) {
00136             jack_error("cannot remove `%s' (%s)",
00137                         jack_user_dir(), strerror(errno));
00138         }
00139     }
00140 }
00141 
00142 #endif
00143 
00144 int JackServerGlobals::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)
00145 {
00146     JackLog("Jackdmp: sync = %ld timeout = %ld rt = %ld priority = %ld verbose = %ld \n", sync, time_out_ms, rt, priority, verbose);
00147         fServer = new JackServer(sync, temporary, time_out_ms, rt, priority, loopback, verbose);
00148     int res = fServer->Open(driver_desc, driver_params);
00149     return (res < 0) ? res : fServer->Start();
00150 }
00151 
00152 int JackServerGlobals::JackStop()
00153 {
00154     fServer->Stop();
00155     fServer->Close();
00156     JackLog("Jackdmp: server close\n");
00157     delete fServer;
00158     JackLog("Jackdmp: delete server\n");
00159     return 0;
00160 }
00161 
00162 int JackServerGlobals::JackDelete()
00163 {
00164     delete fServer;
00165     JackLog("Jackdmp: delete server\n");
00166     return 0;
00167 }
00168 
00169 bool JackServerGlobals::Init()
00170 {
00171         if (fClientCount++ == 0) {
00172         
00173         JackLog("JackServerGlobals Init\n");
00174                 int realtime = 0;
00175                 int client_timeout = 0; /* msecs; if zero, use period size. */
00176                 int realtime_priority = 10;
00177                 int verbose_aux = 0;
00178                 int do_mlock = 1;
00179                 unsigned int port_max = 128;
00180                 int loopback = 0;
00181                 int do_unlock = 0;
00182                 int temporary = 0;
00183                 
00184                 jack_driver_desc_t* driver_desc;
00185                 const char *options = "-ad:P:uvshVRL:STFl:t:mn:p:";
00186                 static struct option long_options[] = {
00187                                                                                    { "driver", 1, 0, 'd'},
00188                                                                                    { "verbose", 0, 0, 'v' },
00189                                                                                    { "help", 0, 0, 'h' },
00190                                                                                    { "port-max", 1, 0, 'p' },
00191                                                                                    { "no-mlock", 0, 0, 'm' },
00192                                                                                    { "name", 0, 0, 'n' },
00193                                                                                    { "unlock", 0, 0, 'u' },
00194                                                                                    { "realtime", 0, 0, 'R' },
00195                                                                                    { "loopback", 0, 0, 'L' },
00196                                                                                    { "realtime-priority", 1, 0, 'P' },
00197                                                                                    { "timeout", 1, 0, 't' },
00198                                                                                    { "temporary", 0, 0, 'T' },
00199                                                                                    { "version", 0, 0, 'V' },
00200                                                                                    { "silent", 0, 0, 's' },
00201                                                                                    { "sync", 0, 0, 'S' },
00202                                                                                    { 0, 0, 0, 0 }
00203                                                                            };
00204                 int opt = 0;
00205                 int option_index = 0;
00206                 int seen_driver = 0;
00207                 char *driver_name = NULL;
00208                 char **driver_args = NULL;
00209                 JSList* driver_params;
00210                 int driver_nargs = 1;
00211                 JSList* drivers = NULL;
00212                 int show_version = 0;
00213                 int sync = 0;
00214                 int rc, i;
00215                 int ret;
00216                 
00217                 FILE* fp = 0;
00218                 char filename[255];
00219                 char buffer[255];
00220                 int argc = 0;
00221                 char* argv[32];
00222                 
00223                 snprintf(filename, 255, "%s/.jackdrc", getenv("HOME"));
00224                 fp = fopen(filename, "r");
00225 
00226                 if (!fp) {
00227                         fp = fopen("/etc/jackdrc", "r");
00228                 }
00229                 // if still not found, check old config name for backwards compatability 
00230                 if (!fp) {
00231                         fp = fopen("/etc/jackd.conf", "r");
00232                 }
00233 
00234                 argc = 0;
00235                 if (fp) {
00236                         ret = fscanf(fp, "%s", buffer);
00237                         while (ret != 0 && ret != EOF) {
00238                                 argv[argc] = (char*)malloc(64);
00239                                 strcpy(argv[argc], buffer);
00240                                 ret = fscanf(fp, "%s", buffer);
00241                                 argc++;
00242                         }
00243                         fclose(fp);
00244                 }
00245                 
00246                 /*
00247                 int argc = 15;
00248                 char* argv[] = {"jackdmp", "-R", "-v", "-d", "coreaudio", "-p", "512", "-d", "~:Aggregate:0", "-r", "48000", "-i", "2", "-o", "2" };
00249                 */
00250 
00251                 opterr = 0;
00252                 optind = 1; // Important : to reset argv parsing
00253                 
00254                 while (!seen_driver &&
00255                                 (opt = getopt_long(argc, argv, options, long_options, &option_index)) != EOF) {
00256                                 
00257                         switch (opt) {
00258 
00259                                 case 'd':
00260                                         seen_driver = 1;
00261                                         driver_name = optarg;
00262                                         break;
00263 
00264                                 case 'v':
00265                                         verbose_aux = 1;
00266                                         break;
00267 
00268                                 case 'S':
00269                                         sync = 1;
00270                                         break;
00271 
00272                                 case 'n':
00273                                         server_name = optarg;
00274                                         break;
00275 
00276                                 case 'm':
00277                                         do_mlock = 0;
00278                                         break;
00279 
00280                                 case 'p':
00281                                         port_max = (unsigned int)atol(optarg);
00282                                         break;
00283 
00284                                 case 'P':
00285                                         realtime_priority = atoi(optarg);
00286                                         break;
00287 
00288                                 case 'R':
00289                                         realtime = 1;
00290                                         break;
00291 
00292                                 case 'L':
00293                                         loopback = atoi(optarg);
00294                                         break;
00295 
00296                                 case 'T':
00297                                         temporary = 1;
00298                                         break;
00299 
00300                                 case 't':
00301                                         client_timeout = atoi(optarg);
00302                                         break;
00303 
00304                                 case 'u':
00305                                         do_unlock = 1;
00306                                         break;
00307 
00308                                 case 'V':
00309                                         show_version = 1;
00310                                         break;
00311 
00312                                 default:
00313                                         fprintf(stderr, "unknown option character %c\n", optopt);
00314                                         break;
00315                         }
00316                 }
00317                 
00318                 drivers = jack_drivers_load(drivers);
00319                 if (!drivers) {
00320                         fprintf(stderr, "jackdmp: no drivers found; exiting\n");
00321                         goto error;
00322                 }
00323 
00324                 driver_desc = jack_find_driver_descriptor(drivers, driver_name);
00325                 if (!driver_desc) {
00326                         fprintf(stderr, "jackdmp: unknown driver '%s'\n", driver_name);
00327                         goto error;
00328                 }
00329 
00330                 if (optind < argc) {
00331                         driver_nargs = 1 + argc - optind;
00332                 } else {
00333                         driver_nargs = 1;
00334                 }
00335 
00336                 if (driver_nargs == 0) {
00337                         fprintf(stderr, "No driver specified ... hmm. JACK won't do"
00338                                          " anything when run like this.\n");
00339                         goto error;
00340                 }
00341 
00342                 driver_args = (char**)malloc(sizeof(char *) * driver_nargs);
00343                 driver_args[0] = driver_name;
00344 
00345                 for (i = 1; i < driver_nargs; i++) {
00346                         driver_args[i] = argv[optind++];
00347                 }
00348 
00349                 if (jack_parse_driver_params(driver_desc, driver_nargs, driver_args, &driver_params)) {
00350                         goto error;
00351                 }
00352 
00353         #ifndef WIN32
00354                 if (server_name == NULL)
00355                         server_name = jack_default_server_name();
00356         #endif
00357 
00358                 rc = jack_register_server(server_name, false);
00359                 switch (rc) {
00360                         case EEXIST:
00361                                 fprintf(stderr, "`%s' server already active\n", server_name);
00362                                 goto error;
00363                         case ENOSPC:
00364                                 fprintf(stderr, "too many servers already active\n");
00365                                 goto error;
00366                         case ENOMEM:
00367                                 fprintf(stderr, "no access to shm registry\n");
00368                                 goto error;
00369                         default:
00370                                 if (jack_verbose)
00371                                         fprintf(stderr, "server `%s' registered\n", server_name);
00372                 }
00373 
00374                 /* clean up shared memory and files from any previous instance of this server name */
00375                 jack_cleanup_shm();
00376         #ifndef WIN32
00377                 jack_cleanup_files(server_name);
00378         #endif
00379 
00380                 if (!realtime && client_timeout == 0)
00381                         client_timeout = 500; /* 0.5 sec; usable when non realtime. */
00382                         
00383                 for (i = 0; i < argc; i++) {
00384                         free(argv[i]);
00385                 }
00386                 
00387                 int res = JackStart(driver_desc, driver_params, sync, temporary, client_timeout, realtime, realtime_priority, loopback, verbose_aux);
00388                 if (res < 0) {
00389                         jack_error("Cannot start server... exit");
00390                         JackDelete();
00391                         jack_cleanup_shm();
00392                 #ifndef WIN32
00393                         jack_cleanup_files(server_name);
00394                 #endif
00395                         jack_unregister_server(server_name);
00396                         goto error;
00397                 }
00398         }
00399         
00400         return true;
00401         
00402 error:
00403         fClientCount--;
00404         return false;
00405 }
00406 
00407 void JackServerGlobals::Destroy()
00408 {
00409     if (--fClientCount == 0) {
00410         JackLog("JackServerGlobals Destroy\n");
00411                 JackStop();
00412                 jack_cleanup_shm();
00413         #ifndef WIN32
00414                 jack_cleanup_files(server_name);
00415         #endif
00416                 jack_unregister_server(server_name);
00417     }
00418 }
00419 
00420 } // end of namespace
00421 
00422 

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