JackServerLaunch.cpp

00001 /*
00002 Copyright (C) 2001-2003 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 Lesser General Public License as published by
00007 the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
00014 
00015 You should have received a copy of the GNU Lesser General Public License
00016 along with this program; if not, write to the Free Software 
00017 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00018 
00019 */
00020 
00021 #include "JackChannel.h"
00022 #include "JackLibGlobals.h"
00023 #include "JackServerLaunch.h"
00024 
00025 using namespace Jack;
00026 
00027 #ifndef WIN32
00028 
00029 /* Exec the JACK server in this process.  Does not return. */
00030 static void start_server_aux(const char* server_name)
00031 {
00032         FILE* fp = 0;
00033         char filename[255];
00034         char arguments[255];
00035         char buffer[255];
00036         char* command = 0;
00037         size_t pos = 0;
00038         size_t result = 0;
00039         char** argv = 0;
00040         int i = 0;
00041         int good = 0;
00042         int ret;
00043         
00044         snprintf(filename, 255, "%s/.jackdrc", getenv("HOME"));
00045         fp = fopen(filename, "r");
00046 
00047         if (!fp) {
00048                 fp = fopen("/etc/jackdrc", "r");
00049         }
00050         /* if still not found, check old config name for backwards compatability */
00051         if (!fp) {
00052                 fp = fopen("/etc/jackd.conf", "r");
00053         }
00054 
00055         if (fp) {
00056                 arguments[0] = '\0';
00057                 ret = fscanf(fp, "%s", buffer);
00058                 while (ret != 0 && ret != EOF) {
00059                         strcat(arguments, buffer);
00060                         strcat(arguments, " ");
00061                         ret = fscanf(fp, "%s", buffer);
00062                 }
00063                 if (strlen(arguments) > 0) {
00064                         good = 1;
00065                 }
00066                 fclose(fp);
00067         }
00068 
00069         if (!good) {
00070                 command = JACK_LOCATION "/jackd";
00071                 strncpy(arguments, JACK_LOCATION "/jackd -T -d "JACK_DEFAULT_DRIVER, 255);
00072         } else {
00073                 result = strcspn(arguments, " ");
00074                 command = (char*)malloc(result + 1);
00075                 strncpy(command, arguments, result);
00076                 command[result] = '\0';
00077         }
00078 
00079         argv = (char**)malloc(255);
00080   
00081         while (1) {
00082                 /* insert -T and -nserver_name in front of arguments */
00083                 if (i == 1) {
00084                         argv[i] = (char*)malloc(strlen ("-T") + 1);
00085                         strcpy (argv[i++], "-T"); 
00086                         if (server_name) {
00087                                 size_t optlen = strlen("-n");
00088                                 char* buf = (char*)malloc(optlen + strlen(server_name) + 1);
00089                                 strcpy(buf, "-n");
00090                                 strcpy(buf + optlen, server_name);
00091                                 argv[i++] = buf;
00092                         }
00093                 }
00094 
00095                 result = strcspn(arguments + pos, " ");
00096                 if (result == 0) {
00097                         break;
00098                 }
00099                 argv[i] = (char*)malloc(result + 1);
00100                 strncpy(argv[i], arguments + pos, result);
00101                 argv[i][result] = '\0';
00102                 pos += result + 1;
00103                 ++i;
00104         }
00105         argv[i] = 0;
00106         execv(command, argv);
00107 
00108         /* If execv() succeeds, it does not return. There's no point
00109          * in calling jack_error() here in the child process. */
00110         fprintf(stderr, "exec of JACK server (command = \"%s\") failed: %s\n", command, strerror(errno));
00111 }
00112 
00113 static int start_server(const char* server_name, jack_options_t options)
00114 {
00115         if ((options & JackNoStartServer) || getenv("JACK_NO_START_SERVER")) {
00116                 return 1;
00117         }
00118 
00119         /* The double fork() forces the server to become a child of
00120          * init, which will always clean up zombie process state on
00121          * termination. This even works in cases where the server
00122          * terminates but this client does not.
00123          *
00124          * Since fork() is usually implemented using copy-on-write
00125          * virtual memory tricks, the overhead of the second fork() is
00126          * probably relatively small.
00127          */
00128         switch (fork()) {
00129                 case 0:                                 /* child process */
00130                         switch (fork()) {
00131                                 case 0:                 /* grandchild process */
00132                                         start_server_aux(server_name);
00133                                         _exit(99);      /* exec failed */
00134                                 case -1:
00135                                         _exit(98);
00136                                 default:
00137                                         _exit(0);
00138                         }
00139                 case -1:                        /* fork() error */
00140                         return 1;               /* failed to start server */
00141         }
00142 
00143         /* only the original parent process goes here */
00144         return 0;                       /* (probably) successful */
00145 }
00146 
00147 int server_connect(char* server_name)
00148 {
00149         JackClientChannelInterface* channel = JackGlobals::MakeClientChannel();
00150         int res = channel->ServerCheck(server_name);
00151         delete channel;
00152         return res;
00153 }
00154 
00155 int try_start_server(jack_varargs_t* va, jack_options_t options, jack_status_t* status)
00156 {
00157         if (server_connect(va->server_name) < 0) {
00158                 int trys;
00159                 if (start_server(va->server_name, options)) {
00160                         int my_status1 = *status | JackFailure | JackServerFailed;
00161                         *status = (jack_status_t)my_status1;
00162                         return -1;
00163                 }
00164                 trys = 5;
00165                 do {
00166                         sleep(1);
00167                         if (--trys < 0) {
00168                                 int my_status1 = *status | JackFailure | JackServerFailed;
00169                                 *status = (jack_status_t)my_status1;
00170                                 return -1;
00171                         }
00172                 } while (server_connect(va->server_name) < 0);
00173                 int my_status1 = *status | JackServerStarted;
00174                 *status = (jack_status_t)my_status1;
00175         }
00176         
00177         return 0;
00178 }
00179 
00180 #endif

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