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.

317 lines
9.5KB

  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. #if defined(HAVE_CONFIG_H)
  16. #include "config.h"
  17. #endif
  18. #include "JackConstants.h"
  19. #include "JackTools.h"
  20. #include "JackError.h"
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #ifdef WIN32
  24. #include <process.h>
  25. #endif
  26. using namespace std;
  27. namespace Jack {
  28. #define DEFAULT_TMP_DIR "/tmp"
  29. char* jack_tmpdir = (char*)DEFAULT_TMP_DIR;
  30. int JackTools::GetPID() {
  31. #ifdef WIN32
  32. return _getpid();
  33. #else
  34. return getpid();
  35. #endif
  36. }
  37. int JackTools::GetUID() {
  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. const char* server_name;
  47. if ((server_name = getenv("JACK_DEFAULT_SERVER")) == NULL)
  48. server_name = JACK_DEFAULT_SERVER_NAME;
  49. return server_name;
  50. }
  51. /* returns the name of the per-user subdirectory of jack_tmpdir */
  52. #ifdef WIN32
  53. char* JackTools::UserDir() {
  54. return "";
  55. }
  56. char* JackTools::ServerDir(const char* server_name, char* server_dir) {
  57. return "";
  58. }
  59. void JackTools::CleanupFiles(const char* server_name) {}
  60. int JackTools::GetTmpdir() {
  61. return 0;
  62. }
  63. #else
  64. char* JackTools::UserDir() {
  65. static char user_dir[PATH_MAX + 1] = "";
  66. /* format the path name on the first call */
  67. if (user_dir[0] == '\0') {
  68. if (getenv ("JACK_PROMISCUOUS_SERVER")) {
  69. snprintf(user_dir, sizeof(user_dir), "%s/jack", jack_tmpdir);
  70. } else {
  71. snprintf(user_dir, sizeof(user_dir), "%s/jack-%d", jack_tmpdir, GetUID());
  72. }
  73. }
  74. return user_dir;
  75. }
  76. /* returns the name of the per-server subdirectory of jack_user_dir() */
  77. char* JackTools::ServerDir(const char* server_name, char* server_dir) {
  78. /* format the path name into the suppled server_dir char array,
  79. * assuming that server_dir is at least as large as PATH_MAX+1 */
  80. snprintf(server_dir, PATH_MAX + 1, "%s/%s", UserDir(), server_name);
  81. return server_dir;
  82. }
  83. void JackTools::CleanupFiles(const char* server_name) {
  84. DIR* dir;
  85. struct dirent *dirent;
  86. char dir_name[PATH_MAX + 1] = "";
  87. ServerDir(server_name, dir_name);
  88. /* On termination, we remove all files that jackd creates so
  89. * subsequent attempts to start jackd will not believe that an
  90. * instance is already running. If the server crashes or is
  91. * terminated with SIGKILL, this is not possible. So, cleanup
  92. * is also attempted when jackd starts.
  93. *
  94. * There are several tricky issues. First, the previous JACK
  95. * server may have run for a different user ID, so its files
  96. * may be inaccessible. This is handled by using a separate
  97. * JACK_TMP_DIR subdirectory for each user. Second, there may
  98. * be other servers running with different names. Each gets
  99. * its own subdirectory within the per-user directory. The
  100. * current process has already registered as `server_name', so
  101. * we know there is no other server actively using that name.
  102. */
  103. /* nothing to do if the server directory does not exist */
  104. if ((dir = opendir(dir_name)) == NULL) {
  105. return;
  106. }
  107. /* unlink all the files in this directory, they are mine */
  108. while ((dirent = readdir(dir)) != NULL) {
  109. char fullpath[PATH_MAX + 1];
  110. if ((strcmp(dirent->d_name, ".") == 0) || (strcmp (dirent->d_name, "..") == 0)) {
  111. continue;
  112. }
  113. snprintf(fullpath, sizeof(fullpath), "%s/%s", dir_name, dirent->d_name);
  114. if (unlink(fullpath)) {
  115. jack_error("cannot unlink `%s' (%s)", fullpath, strerror(errno));
  116. }
  117. }
  118. closedir(dir);
  119. /* now, delete the per-server subdirectory, itself */
  120. if (rmdir(dir_name)) {
  121. jack_error("cannot remove `%s' (%s)", dir_name, strerror(errno));
  122. }
  123. /* finally, delete the per-user subdirectory, if empty */
  124. if (rmdir(UserDir())) {
  125. if (errno != ENOTEMPTY) {
  126. jack_error("cannot remove `%s' (%s)", UserDir(), strerror(errno));
  127. }
  128. }
  129. }
  130. int JackTools::GetTmpdir() {
  131. FILE* in;
  132. size_t len;
  133. char buf[PATH_MAX + 2]; /* allow tmpdir to live anywhere, plus newline, plus null */
  134. if ((in = popen("jackd -l", "r")) == NULL) {
  135. return -1;
  136. }
  137. if (fgets(buf, sizeof(buf), in) == NULL) {
  138. fclose(in);
  139. return -1;
  140. }
  141. len = strlen(buf);
  142. if (buf[len - 1] != '\n') {
  143. /* didn't get a whole line */
  144. fclose(in);
  145. return -1;
  146. }
  147. jack_tmpdir = (char *)malloc(len);
  148. memcpy(jack_tmpdir, buf, len - 1);
  149. jack_tmpdir[len - 1] = '\0';
  150. fclose(in);
  151. return 0;
  152. }
  153. #endif
  154. void JackTools::RewriteName(const char* name, char* new_name) {
  155. size_t i;
  156. for (i = 0; i < strlen(name); i++) {
  157. if ((name[i] == '/') || (name[i] == '\\'))
  158. new_name[i] = '_';
  159. else
  160. new_name[i] = name[i];
  161. }
  162. new_name[i] = '\0';
  163. }
  164. JackArgParser::JackArgParser ( const char* arg ) {
  165. jack_log ( "JackArgParser::JackArgParser, arg_string : '%s'", arg );
  166. fArgc = 0;
  167. fNumArgv = 0;
  168. fArgString = string(arg);
  169. //if empty string
  170. if ( strlen(arg) == 0 ) {
  171. fArgv = NULL;
  172. return;
  173. }
  174. //else parse the arg string
  175. const size_t arg_len = fArgString.length();
  176. int i = 0;
  177. size_t pos = 0;
  178. size_t start = 0;
  179. size_t copy_start = 0;
  180. size_t copy_length = 0;
  181. vector<string> args;
  182. //we need a 'space terminated' string
  183. fArgString += " ";
  184. //first fill a vector with args
  185. do {
  186. //find the first non-space character from the actual position
  187. start = fArgString.find_first_not_of ( ' ', start );
  188. //get the next quote or space position
  189. pos = fArgString.find_first_of ( " \"" , start );
  190. //no more quotes or spaces, consider the end of the string
  191. if ( pos == string::npos )
  192. pos = arg_len;
  193. //if double quote
  194. if ( fArgString.at(pos) == '\"' ) {
  195. //first character : copy the substring
  196. if ( pos == start ) {
  197. copy_start = start + 1;
  198. pos = fArgString.find ( '\"', ++pos );
  199. copy_length = pos - copy_start;
  200. start = pos + 1;
  201. }
  202. //else there is someting before the quote, first copy that
  203. else {
  204. copy_start = start;
  205. copy_length = pos - copy_start;
  206. start = pos;
  207. }
  208. }
  209. //if space
  210. if ( fArgString.at(pos) == ' ' ) {
  211. //short option descriptor
  212. if ( ( fArgString.at(start) == '-' ) && ( fArgString.at(start + 1) != '-' ) ) {
  213. copy_start = start;
  214. copy_length = 2;
  215. start += copy_length;
  216. }
  217. else {
  218. copy_start = start;
  219. copy_length = pos - copy_start;
  220. start = pos + 1;
  221. }
  222. }
  223. //then push the substring to the args vector
  224. args.push_back ( fArgString.substr ( copy_start,copy_length ) );
  225. } while ( start < arg_len );
  226. //and then duplicate args into the argv array
  227. fNumArgv = args.size();
  228. fArgv = new char* [fNumArgv];
  229. for ( i = 0; i < fNumArgv; i++ ) {
  230. fArgv[i] = new char[args[i].length()];
  231. fill_n ( fArgv[i],args[i].length() + 1, 0 );
  232. args[i].copy ( fArgv[i], args[i].length() );
  233. jack_log ( "JackArgParser::JackArgParser, adding : '%s'", fArgv[i] );
  234. }
  235. //finally count the 'real' options (the ones starting with a '-')
  236. for ( i = 0; i < fNumArgv; i++ )
  237. if ( fArgv[i][0] == '-' )
  238. fArgc++;
  239. }
  240. JackArgParser::~JackArgParser() {
  241. for ( int i = 0; i < fNumArgv; i++ )
  242. delete[] fArgv[i];
  243. delete[] fArgv;
  244. }
  245. string JackArgParser::GetArgString() {
  246. return fArgString;
  247. }
  248. int JackArgParser::GetNumArgv() {
  249. return fNumArgv;
  250. }
  251. int JackArgParser::GetArgc() {
  252. return fArgc;
  253. }
  254. const char** JackArgParser::GetArgv() {
  255. return const_cast<const char**>(fArgv);
  256. }
  257. int JackArgParser::ParseParams ( jack_driver_desc_t* desc, JSList** param_list )
  258. {
  259. //TODO : fill the param_list
  260. return 0;
  261. }
  262. }