Fix merge conflicts.
[jack2.git] / common / JackTools.cpp
bloba7a7020eee031882b86b47720a79cffdb543c0c8
1 /*
2 Copyright (C) 2006-2008 Grame
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published by
6 the Free Software Foundation; either version 2.1 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 #include "JackConstants.h"
21 #include "JackDriverLoader.h"
22 #include "JackTools.h"
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <assert.h>
27 #ifdef WIN32
28 #include <process.h>
29 #endif
32 using namespace std;
34 namespace Jack {
36 void JackTools::KillServer()
38 #ifdef WIN32
39 exit(1);
40 #else
41 kill(GetPID(), SIGINT);
42 #endif
45 void JackTools::ThrowJackNetException()
47 throw JackNetException();
50 #define DEFAULT_TMP_DIR "/tmp"
51 char* jack_tmpdir = (char*)DEFAULT_TMP_DIR;
53 int JackTools::GetPID()
55 #ifdef WIN32
56 return _getpid();
57 #else
58 return getpid();
59 #endif
62 int JackTools::GetUID()
64 #ifdef WIN32
65 return _getpid();
66 //#error "No getuid function available"
67 #else
68 return getuid();
69 #endif
72 const char* JackTools::DefaultServerName()
74 const char* server_name;
75 if ((server_name = getenv("JACK_DEFAULT_SERVER")) == NULL)
76 server_name = JACK_DEFAULT_SERVER_NAME;
77 return server_name;
80 /* returns the name of the per-user subdirectory of jack_tmpdir */
81 #ifdef WIN32
83 char* JackTools::UserDir()
85 return "";
88 char* JackTools::ServerDir(const char* server_name, char* server_dir)
90 return "";
93 void JackTools::CleanupFiles(const char* server_name) {}
95 int JackTools::GetTmpdir()
97 return 0;
100 #else
101 char* JackTools::UserDir()
103 static char user_dir[JACK_PATH_MAX + 1] = "";
105 /* format the path name on the first call */
106 if (user_dir[0] == '\0') {
107 if (getenv ("JACK_PROMISCUOUS_SERVER")) {
108 snprintf(user_dir, sizeof(user_dir), "%s/jack", jack_tmpdir);
109 } else {
110 snprintf(user_dir, sizeof(user_dir), "%s/jack-%d", jack_tmpdir, GetUID());
114 return user_dir;
117 /* returns the name of the per-server subdirectory of jack_user_dir() */
118 char* JackTools::ServerDir(const char* server_name, char* server_dir)
120 /* format the path name into the suppled server_dir char array,
121 * assuming that server_dir is at least as large as JACK_PATH_MAX + 1 */
123 snprintf(server_dir, JACK_PATH_MAX + 1, "%s/%s", UserDir(), server_name);
124 return server_dir;
127 void JackTools::CleanupFiles(const char* server_name)
129 DIR* dir;
130 struct dirent *dirent;
131 char dir_name[JACK_PATH_MAX + 1] = "";
132 ServerDir(server_name, dir_name);
134 /* On termination, we remove all files that jackd creates so
135 * subsequent attempts to start jackd will not believe that an
136 * instance is already running. If the server crashes or is
137 * terminated with SIGKILL, this is not possible. So, cleanup
138 * is also attempted when jackd starts.
140 * There are several tricky issues. First, the previous JACK
141 * server may have run for a different user ID, so its files
142 * may be inaccessible. This is handled by using a separate
143 * JACK_TMP_DIR subdirectory for each user. Second, there may
144 * be other servers running with different names. Each gets
145 * its own subdirectory within the per-user directory. The
146 * current process has already registered as `server_name', so
147 * we know there is no other server actively using that name.
150 /* nothing to do if the server directory does not exist */
151 if ((dir = opendir(dir_name)) == NULL) {
152 return;
155 /* unlink all the files in this directory, they are mine */
156 while ((dirent = readdir(dir)) != NULL) {
158 char fullpath[JACK_PATH_MAX + 1];
160 if ((strcmp(dirent->d_name, ".") == 0) || (strcmp (dirent->d_name, "..") == 0)) {
161 continue;
164 snprintf(fullpath, sizeof(fullpath), "%s/%s", dir_name, dirent->d_name);
166 if (unlink(fullpath)) {
167 jack_error("cannot unlink `%s' (%s)", fullpath, strerror(errno));
171 closedir(dir);
173 /* now, delete the per-server subdirectory, itself */
174 if (rmdir(dir_name)) {
175 jack_error("cannot remove `%s' (%s)", dir_name, strerror(errno));
178 /* finally, delete the per-user subdirectory, if empty */
179 if (rmdir(UserDir())) {
180 if (errno != ENOTEMPTY) {
181 jack_error("cannot remove `%s' (%s)", UserDir(), strerror(errno));
186 int JackTools::GetTmpdir()
188 FILE* in;
189 size_t len;
190 char buf[JACK_PATH_MAX + 2]; /* allow tmpdir to live anywhere, plus newline, plus null */
192 if ((in = popen("jackd -l", "r")) == NULL) {
193 return -1;
196 if (fgets(buf, sizeof(buf), in) == NULL) {
197 fclose(in);
198 return -1;
201 len = strlen(buf);
203 if (buf[len - 1] != '\n') {
204 /* didn't get a whole line */
205 fclose(in);
206 return -1;
209 jack_tmpdir = (char *)malloc(len);
210 memcpy(jack_tmpdir, buf, len - 1);
211 jack_tmpdir[len - 1] = '\0';
213 fclose(in);
214 return 0;
216 #endif
218 void JackTools::RewriteName(const char* name, char* new_name)
220 size_t i;
221 for (i = 0; i < strlen(name); i++) {
222 if ((name[i] == '/') || (name[i] == '\\'))
223 new_name[i] = '_';
224 else
225 new_name[i] = name[i];
227 new_name[i] = '\0';
230 #ifdef WIN32
232 void BuildClientPath(char* path_to_so, int path_len, const char* so_name)
234 snprintf(path_to_so, path_len, ADDON_DIR "/%s.dll", so_name);
237 void PrintLoadError(const char* so_name)
239 // Retrieve the system error message for the last-error code
240 LPVOID lpMsgBuf;
241 LPVOID lpDisplayBuf;
242 DWORD dw = GetLastError();
244 FormatMessage(
245 FORMAT_MESSAGE_ALLOCATE_BUFFER |
246 FORMAT_MESSAGE_FROM_SYSTEM |
247 FORMAT_MESSAGE_IGNORE_INSERTS,
248 NULL,
250 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
251 (LPTSTR) &lpMsgBuf,
252 0, NULL );
254 // Display the error message and exit the process
255 lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT,
256 (lstrlen((LPCTSTR)lpMsgBuf) + lstrlen((LPCTSTR)so_name) + 40) * sizeof(TCHAR));
257 _snprintf((LPTSTR)lpDisplayBuf, LocalSize(lpDisplayBuf) / sizeof(TCHAR),
258 TEXT("error loading %s err = %s"), so_name, lpMsgBuf);
260 jack_error((LPCTSTR)lpDisplayBuf);
262 LocalFree(lpMsgBuf);
263 LocalFree(lpDisplayBuf);
266 #else
268 void PrintLoadError(const char* so_name)
270 jack_log("error loading %s err = %s", so_name, dlerror());
273 void BuildClientPath(char* path_to_so, int path_len, const char* so_name)
275 const char* internal_dir;
276 if ((internal_dir = getenv("JACK_INTERNAL_DIR")) == 0) {
277 if ((internal_dir = getenv("JACK_DRIVER_DIR")) == 0) {
278 internal_dir = ADDON_DIR;
282 snprintf(path_to_so, path_len, "%s/%s.so", internal_dir, so_name);
285 #endif
287 } // end of namespace