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.

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