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.

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