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.

216 lines
6.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 <stdlib.h>
  19. #include <stdio.h>
  20. #include <assert.h>
  21. #ifdef WIN32
  22. #include <process.h>
  23. #endif
  24. using namespace std;
  25. namespace Jack {
  26. #define DEFAULT_TMP_DIR "/tmp"
  27. char* jack_tmpdir = (char*)DEFAULT_TMP_DIR;
  28. int JackTools::GetPID()
  29. {
  30. #ifdef WIN32
  31. return _getpid();
  32. #else
  33. return getpid();
  34. #endif
  35. }
  36. int JackTools::GetUID()
  37. {
  38. #ifdef WIN32
  39. return _getpid();
  40. //#error "No getuid function available"
  41. #else
  42. return getuid();
  43. #endif
  44. }
  45. const char* JackTools::DefaultServerName()
  46. {
  47. const char* server_name;
  48. if ((server_name = getenv("JACK_DEFAULT_SERVER")) == NULL)
  49. server_name = JACK_DEFAULT_SERVER_NAME;
  50. return server_name;
  51. }
  52. /* returns the name of the per-user subdirectory of jack_tmpdir */
  53. #ifdef WIN32
  54. char* JackTools::UserDir()
  55. {
  56. return "";
  57. }
  58. char* JackTools::ServerDir(const char* server_name, char* server_dir)
  59. {
  60. return "";
  61. }
  62. void JackTools::CleanupFiles(const char* server_name) {}
  63. int JackTools::GetTmpdir()
  64. {
  65. return 0;
  66. }
  67. #else
  68. char* JackTools::UserDir()
  69. {
  70. static char user_dir[JACK_PATH_MAX + 1] = "";
  71. /* format the path name on the first call */
  72. if (user_dir[0] == '\0') {
  73. if (getenv ("JACK_PROMISCUOUS_SERVER")) {
  74. snprintf(user_dir, sizeof(user_dir), "%s/jack", jack_tmpdir);
  75. } else {
  76. snprintf(user_dir, sizeof(user_dir), "%s/jack-%d", jack_tmpdir, GetUID());
  77. }
  78. }
  79. return user_dir;
  80. }
  81. /* returns the name of the per-server subdirectory of jack_user_dir() */
  82. char* JackTools::ServerDir(const char* server_name, char* server_dir)
  83. {
  84. /* format the path name into the suppled server_dir char array,
  85. * assuming that server_dir is at least as large as JACK_PATH_MAX + 1 */
  86. snprintf(server_dir, JACK_PATH_MAX + 1, "%s/%s", UserDir(), server_name);
  87. return server_dir;
  88. }
  89. void JackTools::CleanupFiles(const char* server_name)
  90. {
  91. DIR* dir;
  92. struct dirent *dirent;
  93. char dir_name[JACK_PATH_MAX + 1] = "";
  94. ServerDir(server_name, dir_name);
  95. /* On termination, we remove all files that jackd creates so
  96. * subsequent attempts to start jackd will not believe that an
  97. * instance is already running. If the server crashes or is
  98. * terminated with SIGKILL, this is not possible. So, cleanup
  99. * is also attempted when jackd starts.
  100. *
  101. * There are several tricky issues. First, the previous JACK
  102. * server may have run for a different user ID, so its files
  103. * may be inaccessible. This is handled by using a separate
  104. * JACK_TMP_DIR subdirectory for each user. Second, there may
  105. * be other servers running with different names. Each gets
  106. * its own subdirectory within the per-user directory. The
  107. * current process has already registered as `server_name', so
  108. * we know there is no other server actively using that name.
  109. */
  110. /* nothing to do if the server directory does not exist */
  111. if ((dir = opendir(dir_name)) == NULL) {
  112. return;
  113. }
  114. /* unlink all the files in this directory, they are mine */
  115. while ((dirent = readdir(dir)) != NULL) {
  116. char fullpath[JACK_PATH_MAX + 1];
  117. if ((strcmp(dirent->d_name, ".") == 0) || (strcmp (dirent->d_name, "..") == 0)) {
  118. continue;
  119. }
  120. snprintf(fullpath, sizeof(fullpath), "%s/%s", dir_name, dirent->d_name);
  121. if (unlink(fullpath)) {
  122. jack_error("cannot unlink `%s' (%s)", fullpath, strerror(errno));
  123. }
  124. }
  125. closedir(dir);
  126. /* now, delete the per-server subdirectory, itself */
  127. if (rmdir(dir_name)) {
  128. jack_error("cannot remove `%s' (%s)", dir_name, strerror(errno));
  129. }
  130. /* finally, delete the per-user subdirectory, if empty */
  131. if (rmdir(UserDir())) {
  132. if (errno != ENOTEMPTY) {
  133. jack_error("cannot remove `%s' (%s)", UserDir(), strerror(errno));
  134. }
  135. }
  136. }
  137. int JackTools::GetTmpdir() {
  138. FILE* in;
  139. size_t len;
  140. char buf[JACK_PATH_MAX + 2]; /* allow tmpdir to live anywhere, plus newline, plus null */
  141. if ((in = popen("jackd -l", "r")) == NULL) {
  142. return -1;
  143. }
  144. if (fgets(buf, sizeof(buf), in) == NULL) {
  145. fclose(in);
  146. return -1;
  147. }
  148. len = strlen(buf);
  149. if (buf[len - 1] != '\n') {
  150. /* didn't get a whole line */
  151. fclose(in);
  152. return -1;
  153. }
  154. jack_tmpdir = (char *)malloc(len);
  155. memcpy(jack_tmpdir, buf, len - 1);
  156. jack_tmpdir[len - 1] = '\0';
  157. fclose(in);
  158. return 0;
  159. }
  160. #endif
  161. void JackTools::RewriteName(const char* name, char* new_name) {
  162. size_t i;
  163. for (i = 0; i < strlen(name); i++) {
  164. if ((name[i] == '/') || (name[i] == '\\'))
  165. new_name[i] = '_';
  166. else
  167. new_name[i] = name[i];
  168. }
  169. new_name[i] = '\0';
  170. }
  171. }