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.

145 lines
3.9KB

  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. #include "JackTools.h"
  16. #include "JackError.h"
  17. #include <stdlib.h>
  18. namespace Jack
  19. {
  20. #define DEFAULT_TMP_DIR "/tmp"
  21. char* jack_tmpdir = DEFAULT_TMP_DIR;
  22. int JackTools::GetPID()
  23. {
  24. #ifdef WIN32
  25. return _getpid();
  26. #else
  27. return getpid();
  28. #endif
  29. }
  30. int JackTools::GetUID()
  31. {
  32. #ifdef WIN32
  33. //return _getpid();
  34. #error "No getuid function available"
  35. #else
  36. return getuid();
  37. #endif
  38. }
  39. char* JackTools::DefaultServerName()
  40. {
  41. char* server_name;
  42. if ((server_name = getenv("JACK_DEFAULT_SERVER")) == NULL)
  43. server_name = "default";
  44. return server_name;
  45. }
  46. /* returns the name of the per-user subdirectory of jack_tmpdir */
  47. char* JackTools::UserDir()
  48. {
  49. static char user_dir[PATH_MAX + 1] = "";
  50. /* format the path name on the first call */
  51. if (user_dir[0] == '\0') {
  52. if (getenv ("JACK_PROMISCUOUS_SERVER")) {
  53. snprintf(user_dir, sizeof(user_dir), "%s/jack", jack_tmpdir);
  54. } else {
  55. snprintf(user_dir, sizeof(user_dir), "%s/jack-%d", jack_tmpdir, GetUID());
  56. }
  57. }
  58. return user_dir;
  59. }
  60. /* returns the name of the per-server subdirectory of jack_user_dir() */
  61. char* JackTools::ServerDir(const char* server_name, char* server_dir)
  62. {
  63. /* format the path name into the suppled server_dir char array,
  64. * assuming that server_dir is at least as large as PATH_MAX+1 */
  65. snprintf(server_dir, PATH_MAX + 1, "%s/%s", UserDir(), server_name);
  66. return server_dir;
  67. }
  68. void JackTools::CleanupFiles(const char* server_name)
  69. {
  70. DIR* dir;
  71. struct dirent *dirent;
  72. char dir_name[PATH_MAX + 1] = "";
  73. ServerDir(server_name, dir_name);
  74. /* On termination, we remove all files that jackd creates so
  75. * subsequent attempts to start jackd will not believe that an
  76. * instance is already running. If the server crashes or is
  77. * terminated with SIGKILL, this is not possible. So, cleanup
  78. * is also attempted when jackd starts.
  79. *
  80. * There are several tricky issues. First, the previous JACK
  81. * server may have run for a different user ID, so its files
  82. * may be inaccessible. This is handled by using a separate
  83. * JACK_TMP_DIR subdirectory for each user. Second, there may
  84. * be other servers running with different names. Each gets
  85. * its own subdirectory within the per-user directory. The
  86. * current process has already registered as `server_name', so
  87. * we know there is no other server actively using that name.
  88. */
  89. /* nothing to do if the server directory does not exist */
  90. if ((dir = opendir(dir_name)) == NULL) {
  91. return;
  92. }
  93. /* unlink all the files in this directory, they are mine */
  94. while ((dirent = readdir(dir)) != NULL) {
  95. char fullpath[PATH_MAX + 1];
  96. if ((strcmp(dirent->d_name, ".") == 0) || (strcmp (dirent->d_name, "..") == 0)) {
  97. continue;
  98. }
  99. snprintf(fullpath, sizeof(fullpath), "%s/%s", dir_name, dirent->d_name);
  100. if (unlink(fullpath)) {
  101. jack_error("cannot unlink `%s' (%s)", fullpath, strerror(errno));
  102. }
  103. }
  104. closedir(dir);
  105. /* now, delete the per-server subdirectory, itself */
  106. if (rmdir(dir_name)) {
  107. jack_error("cannot remove `%s' (%s)", dir_name, strerror(errno));
  108. }
  109. /* finally, delete the per-user subdirectory, if empty */
  110. if (rmdir(UserDir())) {
  111. if (errno != ENOTEMPTY) {
  112. jack_error("cannot remove `%s' (%s)", UserDir(), strerror(errno));
  113. }
  114. }
  115. }
  116. }