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.

219 lines
5.4KB

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