Cleanup thread code.
[jack2.git] / common / JackTools.cpp
blobcc8c67c39fdee05e3043560848ce564103e42a6b
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 #define DEFAULT_TMP_DIR "/tmp"
37 char* jack_tmpdir = (char*)DEFAULT_TMP_DIR;
39 int JackTools::GetPID()
41 #ifdef WIN32
42 return _getpid();
43 #else
44 return getpid();
45 #endif
48 int JackTools::GetUID()
50 #ifdef WIN32
51 return _getpid();
52 //#error "No getuid function available"
53 #else
54 return getuid();
55 #endif
58 const char* JackTools::DefaultServerName()
60 const char* server_name;
61 if ((server_name = getenv("JACK_DEFAULT_SERVER")) == NULL)
62 server_name = JACK_DEFAULT_SERVER_NAME;
63 return server_name;
66 /* returns the name of the per-user subdirectory of jack_tmpdir */
67 #ifdef WIN32
69 char* JackTools::UserDir()
71 return "";
74 char* JackTools::ServerDir(const char* server_name, char* server_dir)
76 return "";
79 void JackTools::CleanupFiles(const char* server_name) {}
81 int JackTools::GetTmpdir()
83 return 0;
86 #else
87 char* JackTools::UserDir()
89 static char user_dir[JACK_PATH_MAX + 1] = "";
91 /* format the path name on the first call */
92 if (user_dir[0] == '\0') {
93 if (getenv ("JACK_PROMISCUOUS_SERVER")) {
94 snprintf(user_dir, sizeof(user_dir), "%s/jack", jack_tmpdir);
95 } else {
96 snprintf(user_dir, sizeof(user_dir), "%s/jack-%d", jack_tmpdir, GetUID());
100 return user_dir;
103 /* returns the name of the per-server subdirectory of jack_user_dir() */
104 char* JackTools::ServerDir(const char* server_name, char* server_dir)
106 /* format the path name into the suppled server_dir char array,
107 * assuming that server_dir is at least as large as JACK_PATH_MAX + 1 */
109 snprintf(server_dir, JACK_PATH_MAX + 1, "%s/%s", UserDir(), server_name);
110 return server_dir;
113 void JackTools::CleanupFiles(const char* server_name)
115 DIR* dir;
116 struct dirent *dirent;
117 char dir_name[JACK_PATH_MAX + 1] = "";
118 ServerDir(server_name, dir_name);
120 /* On termination, we remove all files that jackd creates so
121 * subsequent attempts to start jackd will not believe that an
122 * instance is already running. If the server crashes or is
123 * terminated with SIGKILL, this is not possible. So, cleanup
124 * is also attempted when jackd starts.
126 * There are several tricky issues. First, the previous JACK
127 * server may have run for a different user ID, so its files
128 * may be inaccessible. This is handled by using a separate
129 * JACK_TMP_DIR subdirectory for each user. Second, there may
130 * be other servers running with different names. Each gets
131 * its own subdirectory within the per-user directory. The
132 * current process has already registered as `server_name', so
133 * we know there is no other server actively using that name.
136 /* nothing to do if the server directory does not exist */
137 if ((dir = opendir(dir_name)) == NULL) {
138 return;
141 /* unlink all the files in this directory, they are mine */
142 while ((dirent = readdir(dir)) != NULL) {
144 char fullpath[JACK_PATH_MAX + 1];
146 if ((strcmp(dirent->d_name, ".") == 0) || (strcmp (dirent->d_name, "..") == 0)) {
147 continue;
150 snprintf(fullpath, sizeof(fullpath), "%s/%s", dir_name, dirent->d_name);
152 if (unlink(fullpath)) {
153 jack_error("cannot unlink `%s' (%s)", fullpath, strerror(errno));
157 closedir(dir);
159 /* now, delete the per-server subdirectory, itself */
160 if (rmdir(dir_name)) {
161 jack_error("cannot remove `%s' (%s)", dir_name, strerror(errno));
164 /* finally, delete the per-user subdirectory, if empty */
165 if (rmdir(UserDir())) {
166 if (errno != ENOTEMPTY) {
167 jack_error("cannot remove `%s' (%s)", UserDir(), strerror(errno));
172 int JackTools::GetTmpdir()
174 FILE* in;
175 size_t len;
176 char buf[JACK_PATH_MAX + 2]; /* allow tmpdir to live anywhere, plus newline, plus null */
178 if ((in = popen("jackd -l", "r")) == NULL) {
179 return -1;
182 if (fgets(buf, sizeof(buf), in) == NULL) {
183 fclose(in);
184 return -1;
187 len = strlen(buf);
189 if (buf[len - 1] != '\n') {
190 /* didn't get a whole line */
191 fclose(in);
192 return -1;
195 jack_tmpdir = (char *)malloc(len);
196 memcpy(jack_tmpdir, buf, len - 1);
197 jack_tmpdir[len - 1] = '\0';
199 fclose(in);
200 return 0;
202 #endif
204 void JackTools::RewriteName(const char* name, char* new_name)
206 size_t i;
207 for (i = 0; i < strlen(name); i++) {
208 if ((name[i] == '/') || (name[i] == '\\'))
209 new_name[i] = '_';
210 else
211 new_name[i] = name[i];
213 new_name[i] = '\0';
216 #ifdef WIN32
218 void BuildClientPath(char* path_to_so, int path_len, const char* so_name)
220 snprintf(path_to_so, path_len, ADDON_DIR "/%s.dll", so_name);
223 void PrintLoadError(const char* so_name)
225 // Retrieve the system error message for the last-error code
226 LPVOID lpMsgBuf;
227 LPVOID lpDisplayBuf;
228 DWORD dw = GetLastError();
230 FormatMessage(
231 FORMAT_MESSAGE_ALLOCATE_BUFFER |
232 FORMAT_MESSAGE_FROM_SYSTEM |
233 FORMAT_MESSAGE_IGNORE_INSERTS,
234 NULL,
236 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
237 (LPTSTR) &lpMsgBuf,
238 0, NULL );
240 // Display the error message and exit the process
241 lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT,
242 (lstrlen((LPCTSTR)lpMsgBuf) + lstrlen((LPCTSTR)so_name) + 40) * sizeof(TCHAR));
243 _snprintf((LPTSTR)lpDisplayBuf, LocalSize(lpDisplayBuf) / sizeof(TCHAR),
244 TEXT("error loading %s err = %s"), so_name, lpMsgBuf);
246 jack_error((LPCTSTR)lpDisplayBuf);
248 LocalFree(lpMsgBuf);
249 LocalFree(lpDisplayBuf);
252 #else
254 void PrintLoadError(const char* so_name)
256 jack_log("error loading %s err = %s", so_name, dlerror());
259 void BuildClientPath(char* path_to_so, int path_len, const char* so_name)
261 const char* internal_dir;
262 if ((internal_dir = getenv("JACK_INTERNAL_DIR")) == 0) {
263 if ((internal_dir = getenv("JACK_DRIVER_DIR")) == 0) {
264 internal_dir = ADDON_DIR;
268 snprintf(path_to_so, path_len, "%s/%s.so", internal_dir, so_name);
271 #endif
273 } // end of namespace