jack2 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

303 lines
8.0KB

  1. /*
  2. Copyright (C) 2006-2008 Grame
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as published by
  5. the Free Software Foundation; either version 2.1 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. */
  15. #include "JackConstants.h"
  16. #include "JackDriverLoader.h"
  17. #include "JackTools.h"
  18. #include "JackError.h"
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <assert.h>
  22. #include <signal.h>
  23. #ifdef WIN32
  24. #include <process.h>
  25. #endif
  26. using namespace std;
  27. namespace Jack {
  28. void JackTools::KillServer()
  29. {
  30. #ifdef WIN32
  31. raise(SIGINT);
  32. #else
  33. kill(GetPID(), SIGINT);
  34. #endif
  35. }
  36. void JackTools::ThrowJackNetException()
  37. {
  38. throw JackNetException();
  39. }
  40. int JackTools::MkDir(const char* path)
  41. {
  42. #ifdef WIN32
  43. return CreateDirectory(path, NULL) == 0;
  44. #else
  45. return mkdir(path, 0777) != 0;
  46. #endif
  47. }
  48. #define DEFAULT_TMP_DIR "/tmp"
  49. char* jack_tmpdir = (char*)DEFAULT_TMP_DIR;
  50. int JackTools::GetPID()
  51. {
  52. #ifdef WIN32
  53. return _getpid();
  54. #else
  55. return getpid();
  56. #endif
  57. }
  58. int JackTools::GetUID()
  59. {
  60. #ifdef WIN32
  61. return _getpid();
  62. //#error "No getuid function available"
  63. #else
  64. return geteuid();
  65. #endif
  66. }
  67. const char* JackTools::DefaultServerName()
  68. {
  69. const char* server_name;
  70. if ((server_name = getenv("JACK_DEFAULT_SERVER")) == NULL) {
  71. server_name = JACK_DEFAULT_SERVER_NAME;
  72. }
  73. return server_name;
  74. }
  75. /* returns the name of the per-user subdirectory of jack_tmpdir */
  76. #ifdef WIN32
  77. char* JackTools::UserDir()
  78. {
  79. return "";
  80. }
  81. char* JackTools::ServerDir(const char* server_name, char* server_dir)
  82. {
  83. return "";
  84. }
  85. void JackTools::CleanupFiles(const char* server_name) {}
  86. int JackTools::GetTmpdir()
  87. {
  88. return 0;
  89. }
  90. #else
  91. char* JackTools::UserDir()
  92. {
  93. static char user_dir[JACK_PATH_MAX + 1] = "";
  94. /* format the path name on the first call */
  95. if (user_dir[0] == '\0') {
  96. if (getenv ("JACK_PROMISCUOUS_SERVER")) {
  97. snprintf(user_dir, sizeof(user_dir), "%s/jack", jack_tmpdir);
  98. } else {
  99. snprintf(user_dir, sizeof(user_dir), "%s/jack-%d", jack_tmpdir, GetUID());
  100. }
  101. }
  102. return user_dir;
  103. }
  104. /* returns the name of the per-server subdirectory of jack_user_dir() */
  105. char* JackTools::ServerDir(const char* server_name, char* server_dir)
  106. {
  107. /* format the path name into the suppled server_dir char array,
  108. * 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;
  111. }
  112. void JackTools::CleanupFiles(const char* server_name)
  113. {
  114. DIR* dir;
  115. struct dirent *dirent;
  116. char dir_name[JACK_PATH_MAX + 1] = "";
  117. ServerDir(server_name, dir_name);
  118. /* On termination, we remove all files that jackd creates so
  119. * subsequent attempts to start jackd will not believe that an
  120. * instance is already running. If the server crashes or is
  121. * terminated with SIGKILL, this is not possible. So, cleanup
  122. * is also attempted when jackd starts.
  123. *
  124. * There are several tricky issues. First, the previous JACK
  125. * server may have run for a different user ID, so its files
  126. * may be inaccessible. This is handled by using a separate
  127. * JACK_TMP_DIR subdirectory for each user. Second, there may
  128. * be other servers running with different names. Each gets
  129. * its own subdirectory within the per-user directory. The
  130. * current process has already registered as `server_name', so
  131. * we know there is no other server actively using that name.
  132. */
  133. /* nothing to do if the server directory does not exist */
  134. if ((dir = opendir(dir_name)) == NULL) {
  135. return;
  136. }
  137. /* unlink all the files in this directory, they are mine */
  138. while ((dirent = readdir(dir)) != NULL) {
  139. char fullpath[JACK_PATH_MAX + 1];
  140. if ((strcmp(dirent->d_name, ".") == 0) || (strcmp (dirent->d_name, "..") == 0)) {
  141. continue;
  142. }
  143. snprintf(fullpath, sizeof(fullpath), "%s/%s", dir_name, dirent->d_name);
  144. if (unlink(fullpath)) {
  145. jack_error("cannot unlink `%s' (%s)", fullpath, strerror(errno));
  146. }
  147. }
  148. closedir(dir);
  149. /* now, delete the per-server subdirectory, itself */
  150. if (rmdir(dir_name)) {
  151. jack_error("cannot remove `%s' (%s)", dir_name, strerror(errno));
  152. }
  153. /* finally, delete the per-user subdirectory, if empty */
  154. if (rmdir(UserDir())) {
  155. if (errno != ENOTEMPTY) {
  156. jack_error("cannot remove `%s' (%s)", UserDir(), strerror(errno));
  157. }
  158. }
  159. }
  160. int JackTools::GetTmpdir()
  161. {
  162. FILE* in;
  163. size_t len;
  164. char buf[JACK_PATH_MAX + 2]; /* allow tmpdir to live anywhere, plus newline, plus null */
  165. if ((in = popen("jackd -l", "r")) == NULL) {
  166. return -1;
  167. }
  168. if (fgets(buf, sizeof(buf), in) == NULL) {
  169. pclose(in);
  170. return -1;
  171. }
  172. len = strlen(buf);
  173. if (buf[len - 1] != '\n') {
  174. /* didn't get a whole line */
  175. pclose(in);
  176. return -1;
  177. }
  178. jack_tmpdir = (char *)malloc(len);
  179. memcpy(jack_tmpdir, buf, len - 1);
  180. jack_tmpdir[len - 1] = '\0';
  181. pclose(in);
  182. return 0;
  183. }
  184. #endif
  185. void JackTools::RewriteName(const char* name, char* new_name)
  186. {
  187. size_t i;
  188. for (i = 0; i < strlen(name); i++) {
  189. if ((name[i] == '/') || (name[i] == '\\')) {
  190. new_name[i] = '_';
  191. } else {
  192. new_name[i] = name[i];
  193. }
  194. }
  195. new_name[i] = '\0';
  196. }
  197. #ifdef WIN32
  198. void BuildClientPath(char* path_to_so, int path_len, const char* so_name)
  199. {
  200. snprintf(path_to_so, path_len, ADDON_DIR "/%s.dll", so_name);
  201. }
  202. void PrintLoadError(const char* so_name)
  203. {
  204. // Retrieve the system error message for the last-error code
  205. LPVOID lpMsgBuf;
  206. LPVOID lpDisplayBuf;
  207. DWORD dw = GetLastError();
  208. FormatMessage(
  209. FORMAT_MESSAGE_ALLOCATE_BUFFER |
  210. FORMAT_MESSAGE_FROM_SYSTEM |
  211. FORMAT_MESSAGE_IGNORE_INSERTS,
  212. NULL,
  213. dw,
  214. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  215. (LPTSTR) &lpMsgBuf,
  216. 0, NULL );
  217. // Display the error message and exit the process
  218. lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT,
  219. (lstrlen((LPCTSTR)lpMsgBuf) + lstrlen((LPCTSTR)so_name) + 40) * sizeof(TCHAR));
  220. _snprintf((LPTSTR)lpDisplayBuf, LocalSize(lpDisplayBuf) / sizeof(TCHAR),
  221. TEXT("error loading %s err = %s"), so_name, lpMsgBuf);
  222. jack_error((LPCTSTR)lpDisplayBuf);
  223. LocalFree(lpMsgBuf);
  224. LocalFree(lpDisplayBuf);
  225. }
  226. #else
  227. void PrintLoadError(const char* so_name)
  228. {
  229. jack_log("error loading %s err = %s", so_name, dlerror());
  230. }
  231. void BuildClientPath(char* path_to_so, int path_len, const char* so_name)
  232. {
  233. const char* internal_dir;
  234. if ((internal_dir = getenv("JACK_INTERNAL_DIR")) == 0) {
  235. if ((internal_dir = getenv("JACK_DRIVER_DIR")) == 0) {
  236. internal_dir = ADDON_DIR;
  237. }
  238. }
  239. snprintf(path_to_so, path_len, "%s/%s.so", internal_dir, so_name);
  240. }
  241. #endif
  242. } // end of namespace