JackTools.cpp

00001 /*
00002   Copyright (C) 2001 Paul Davis
00003   
00004   This program is free software; you can redistribute it and/or modify
00005   it under the terms of the GNU Lesser General Public License as published by
00006   the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
00013   
00014   You should have received a copy of the GNU Lesser General Public License
00015   along with this program; if not, write to the Free Software 
00016   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017 
00018 */
00019 
00020 #include "JackTools.h"
00021 #include "JackError.h"
00022 #include <stdlib.h>
00023 
00024 #ifdef WIN32
00025 #include <process.h>
00026 #endif
00027 
00028 namespace Jack
00029 {
00030 
00031 #define DEFAULT_TMP_DIR "/tmp"
00032 char* jack_tmpdir = (char*)DEFAULT_TMP_DIR;
00033 
00034 int JackTools::GetPID() 
00035 {
00036 #ifdef WIN32
00037         return  _getpid();
00038 #else
00039         return getpid();
00040 #endif
00041 }
00042 
00043 int JackTools::GetUID() 
00044 {
00045 #ifdef WIN32
00046         return  _getpid();
00047         //#error "No getuid function available"
00048 #else
00049         return getuid();
00050 #endif
00051 }
00052 
00053 const char* JackTools::DefaultServerName()
00054 {
00055     const char* server_name;
00056     if ((server_name = getenv("JACK_DEFAULT_SERVER")) == NULL)
00057         server_name = "default";
00058     return server_name;
00059 }
00060 
00061 /* returns the name of the per-user subdirectory of jack_tmpdir */
00062 #ifdef WIN32
00063 
00064 char* JackTools::UserDir()
00065 {
00066         return "";
00067 }
00068 
00069 char* JackTools::ServerDir(const char* server_name, char* server_dir)
00070 {
00071         return "";
00072 }
00073 
00074 void JackTools::CleanupFiles(const char* server_name)
00075 {
00076 
00077 }
00078 
00079 int JackTools::GetTmpdir()
00080 {
00081         return 0;
00082 }
00083 
00084 #else
00085 char* JackTools::UserDir()
00086 {
00087         static char user_dir[PATH_MAX + 1] = "";
00088 
00089         /* format the path name on the first call */
00090         if (user_dir[0] == '\0') {
00091                 if (getenv ("JACK_PROMISCUOUS_SERVER")) {
00092                         snprintf(user_dir, sizeof(user_dir), "%s/jack", jack_tmpdir);
00093                 } else {
00094                         snprintf(user_dir, sizeof(user_dir), "%s/jack-%d", jack_tmpdir, GetUID());
00095                 }
00096         }
00097 
00098         return user_dir;
00099 }
00100 
00101 /* returns the name of the per-server subdirectory of jack_user_dir() */
00102 char* JackTools::ServerDir(const char* server_name, char* server_dir)
00103 {
00104         /* format the path name into the suppled server_dir char array,
00105          * assuming that server_dir is at least as large as PATH_MAX+1 */
00106 
00107         snprintf(server_dir, PATH_MAX + 1, "%s/%s", UserDir(), server_name);
00108         return server_dir;
00109 }
00110 
00111 void JackTools::CleanupFiles(const char* server_name)
00112 {
00113     DIR* dir;
00114         struct dirent *dirent;
00115         char dir_name[PATH_MAX + 1] = "";
00116         ServerDir(server_name, dir_name);
00117 
00118         /* On termination, we remove all files that jackd creates so
00119          * subsequent attempts to start jackd will not believe that an
00120          * instance is already running. If the server crashes or is
00121          * terminated with SIGKILL, this is not possible. So, cleanup
00122          * is also attempted when jackd starts.
00123          *
00124          * There are several tricky issues. First, the previous JACK
00125          * server may have run for a different user ID, so its files
00126          * may be inaccessible. This is handled by using a separate
00127          * JACK_TMP_DIR subdirectory for each user. Second, there may
00128          * be other servers running with different names. Each gets
00129          * its own subdirectory within the per-user directory. The
00130          * current process has already registered as `server_name', so
00131          * we know there is no other server actively using that name.
00132          */
00133 
00134         /* nothing to do if the server directory does not exist */
00135         if ((dir = opendir(dir_name)) == NULL) {
00136                 return;
00137         }
00138 
00139         /* unlink all the files in this directory, they are mine */
00140         while ((dirent = readdir(dir)) != NULL) {
00141 
00142                 char fullpath[PATH_MAX + 1];
00143 
00144                 if ((strcmp(dirent->d_name, ".") == 0) || (strcmp (dirent->d_name, "..") == 0)) {
00145                         continue;
00146                 }
00147 
00148                 snprintf(fullpath, sizeof(fullpath), "%s/%s", dir_name, dirent->d_name);
00149 
00150                 if (unlink(fullpath)) {
00151                         jack_error("cannot unlink `%s' (%s)", fullpath, strerror(errno));
00152                 }
00153         } 
00154 
00155         closedir(dir);
00156 
00157         /* now, delete the per-server subdirectory, itself */
00158         if (rmdir(dir_name)) {
00159                 jack_error("cannot remove `%s' (%s)", dir_name, strerror(errno));
00160         }
00161 
00162         /* finally, delete the per-user subdirectory, if empty */
00163         if (rmdir(UserDir())) {
00164                 if (errno != ENOTEMPTY) {
00165                         jack_error("cannot remove `%s' (%s)", UserDir(), strerror(errno));
00166                 }
00167         }
00168 }
00169 
00170 int JackTools::GetTmpdir()
00171 {
00172         FILE* in;
00173         size_t len;
00174         char buf[PATH_MAX + 2]; /* allow tmpdir to live anywhere, plus newline, plus null */
00175 
00176         if ((in = popen("jackd -l", "r")) == NULL) {
00177                 return -1;
00178         }
00179 
00180         if (fgets(buf, sizeof(buf), in) == NULL) {
00181                 fclose(in);
00182                 return -1;
00183         }
00184 
00185         len = strlen(buf);
00186 
00187         if (buf[len - 1] != '\n') {
00188                 /* didn't get a whole line */
00189                 fclose(in);
00190                 return -1;
00191         }
00192 
00193         jack_tmpdir = (char *)malloc(len);
00194         memcpy(jack_tmpdir, buf, len - 1);
00195         jack_tmpdir[len - 1] = '\0';
00196         
00197         fclose(in);
00198         return 0;
00199 }
00200 #endif
00201 
00202 void JackTools::RewriteName(const char* name, char* new_name)
00203 {
00204         int i;
00205         for (i = 0; i < strlen(name); i++) {
00206                 if ((name[i] == '/') || (name[i] == '\\'))
00207                         new_name[i] = '_';
00208                 else
00209                         new_name[i] = name[i];
00210         }       
00211         new_name[i] = '\0';
00212 }
00213 
00214 
00215 }

Generated on Thu Feb 14 11:16:02 2008 for Jackdmp by  doxygen 1.5.1