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.

407 lines
13KB

  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. // class JackArgParser ***************************************************
  172. JackArgParser::JackArgParser ( const char* arg )
  173. {
  174. jack_log ( "JackArgParser::JackArgParser, arg_string : '%s'", arg );
  175. fArgc = 0;
  176. //if empty string
  177. if ( strlen(arg) == 0 )
  178. return;
  179. fArgString = string(arg);
  180. //else parse the arg string
  181. const size_t arg_len = fArgString.length();
  182. unsigned int i = 0;
  183. size_t pos = 0;
  184. size_t start = 0;
  185. size_t copy_start = 0;
  186. size_t copy_length = 0;
  187. //we need a 'space terminated' string
  188. fArgString += " ";
  189. //first fill a vector with args
  190. do {
  191. //find the first non-space character from the actual position
  192. start = fArgString.find_first_not_of ( ' ', start );
  193. //get the next quote or space position
  194. pos = fArgString.find_first_of ( " \"" , start );
  195. //no more quotes or spaces, consider the end of the string
  196. if ( pos == string::npos )
  197. pos = arg_len;
  198. //if double quote
  199. if ( fArgString[pos] == '\"' ) {
  200. //first character : copy the substring
  201. if ( pos == start ) {
  202. copy_start = start + 1;
  203. pos = fArgString.find ( '\"', ++pos );
  204. copy_length = pos - copy_start;
  205. start = pos + 1;
  206. }
  207. //else there is someting before the quote, first copy that
  208. else {
  209. copy_start = start;
  210. copy_length = pos - copy_start;
  211. start = pos;
  212. }
  213. }
  214. //if space
  215. if ( fArgString[pos] == ' ' ) {
  216. //short option descriptor
  217. if ( ( fArgString[start] == '-' ) && ( fArgString[start + 1] != '-' ) ) {
  218. copy_start = start;
  219. copy_length = 2;
  220. start += copy_length;
  221. }
  222. //else copy all the space delimitated string
  223. else {
  224. copy_start = start;
  225. copy_length = pos - copy_start;
  226. start = pos + 1;
  227. }
  228. }
  229. //then push the substring to the args vector
  230. fArgv.push_back ( fArgString.substr ( copy_start, copy_length ) );
  231. jack_log ( "JackArgParser::JackArgParser, add : '%s'", (*fArgv.rbegin()).c_str() );
  232. } while ( start < arg_len );
  233. //finally count the options
  234. for ( i = 0; i < fArgv.size(); i++ )
  235. if ( fArgv[i].at(0) == '-' )
  236. fArgc++;
  237. }
  238. JackArgParser::~JackArgParser()
  239. {}
  240. string JackArgParser::GetArgString()
  241. {
  242. return fArgString;
  243. }
  244. int JackArgParser::GetNumArgv()
  245. {
  246. return fArgv.size();
  247. }
  248. int JackArgParser::GetArgc()
  249. {
  250. return fArgc;
  251. }
  252. int JackArgParser::GetArgv ( vector<string>& argv )
  253. {
  254. argv = fArgv;
  255. return 0;
  256. }
  257. int JackArgParser::GetArgv ( char** argv )
  258. {
  259. //argv must be NULL
  260. if ( argv )
  261. return -1;
  262. //else allocate and fill it
  263. argv = (char**)calloc (fArgv.size(), sizeof(char*));
  264. for ( unsigned int i = 0; i < fArgv.size(); i++ )
  265. {
  266. argv[i] = (char*)calloc(fArgv[i].length(), sizeof(char));
  267. fill_n ( argv[i], fArgv[i].length() + 1, 0 );
  268. fArgv[i].copy ( argv[i], fArgv[i].length() );
  269. }
  270. return 0;
  271. }
  272. void JackArgParser::DeleteArgv ( const char** argv )
  273. {
  274. unsigned int i;
  275. for ( i = 0; i < fArgv.size(); i++ )
  276. free((void*)argv[i]);
  277. free((void*)argv);
  278. }
  279. void JackArgParser::ParseParams ( jack_driver_desc_t* desc, JSList** param_list )
  280. {
  281. string options_list;
  282. unsigned long i = 0;
  283. unsigned int param = 0;
  284. size_t param_id = 0;
  285. JSList* params = NULL;
  286. jack_driver_param_t* intclient_param;
  287. for ( i = 0; i < desc->nparams; i++ )
  288. options_list += desc->params[i].character;
  289. for ( param = 0; param < fArgv.size(); param++ )
  290. {
  291. if ( fArgv[param][0] == '-' )
  292. {
  293. //valid option
  294. if ( ( param_id = options_list.find_first_of ( fArgv[param].at(1) ) ) != string::npos )
  295. {
  296. intclient_param = static_cast<jack_driver_param_t*> ( calloc ( 1, sizeof ( jack_driver_param_t) ) );
  297. intclient_param->character = desc->params[param_id].character;
  298. switch ( desc->params[param_id].type )
  299. {
  300. case JackDriverParamInt:
  301. if (param + 1 < fArgv.size()) // something to parse
  302. intclient_param->value.i = atoi ( fArgv[param + 1].c_str() );
  303. break;
  304. case JackDriverParamUInt:
  305. if (param + 1 < fArgv.size()) // something to parse
  306. intclient_param->value.ui = strtoul ( fArgv[param + 1].c_str(), NULL, 10 );
  307. break;
  308. case JackDriverParamChar:
  309. if (param + 1 < fArgv.size()) // something to parse
  310. intclient_param->value.c = fArgv[param + 1][0];
  311. break;
  312. case JackDriverParamString:
  313. if (param + 1 < fArgv.size()) // something to parse
  314. fArgv[param + 1].copy ( intclient_param->value.str, min(static_cast<int>(fArgv[param + 1].length()), JACK_DRIVER_PARAM_STRING_MAX) );
  315. break;
  316. case JackDriverParamBool:
  317. intclient_param->value.i = true;
  318. break;
  319. }
  320. //add to the list
  321. params = jack_slist_append ( params, intclient_param );
  322. }
  323. //invalid option
  324. else
  325. jack_error ( "Invalid option '%c'", fArgv[param][1] );
  326. }
  327. }
  328. assert(param_list);
  329. *param_list = params;
  330. }
  331. void JackArgParser::FreeParams ( JSList* param_list )
  332. {
  333. JSList *node_ptr = param_list;
  334. JSList *next_node_ptr;
  335. while (node_ptr) {
  336. next_node_ptr = node_ptr->next;
  337. free(node_ptr->data);
  338. free(node_ptr);
  339. node_ptr = next_node_ptr;
  340. }
  341. }
  342. }