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.

411 lines
10.0KB

  1. /*
  2. Copyright (C) 2005 Grame
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 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 General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  14. */
  15. #ifdef WIN32
  16. #pragma warning (disable : 4786)
  17. #endif
  18. #include "JackServerGlobals.h"
  19. #include "JackError.h"
  20. #include "shm.h"
  21. #include <getopt.h>
  22. #ifndef WIN32
  23. #include <dirent.h>
  24. #endif
  25. #define DEFAULT_TMP_DIR "/tmp"
  26. static char* jack_tmpdir = DEFAULT_TMP_DIR;
  27. static char* server_name = "jackdmp_default";
  28. namespace Jack
  29. {
  30. long JackServerGlobals::fClientCount = 0;
  31. JackServer* JackServerGlobals::fServer = NULL;
  32. #ifndef WIN32
  33. static char* jack_default_server_name(void)
  34. {
  35. char *server_name;
  36. if ((server_name = getenv("JACK_DEFAULT_SERVER")) == NULL)
  37. server_name = "default";
  38. return server_name;
  39. }
  40. /* returns the name of the per-user subdirectory of jack_tmpdir */
  41. static char* jack_user_dir(void)
  42. {
  43. static char user_dir[PATH_MAX] = "";
  44. /* format the path name on the first call */
  45. if (user_dir[0] == '\0') {
  46. snprintf(user_dir, sizeof (user_dir), "%s/jack-%d",
  47. jack_tmpdir, getuid ());
  48. }
  49. return user_dir;
  50. }
  51. /* returns the name of the per-server subdirectory of jack_user_dir() */
  52. static char* get_jack_server_dir(const char* toto)
  53. {
  54. static char server_dir[PATH_MAX] = "";
  55. // format the path name on the first call
  56. if (server_dir[0] == '\0') {
  57. snprintf(server_dir, sizeof (server_dir), "%s/%s", jack_user_dir(), server_name);
  58. }
  59. return server_dir;
  60. }
  61. static void
  62. jack_cleanup_files(const char *server_name)
  63. {
  64. DIR *dir;
  65. struct dirent *dirent;
  66. char *dir_name = get_jack_server_dir(server_name);
  67. /* On termination, we remove all files that jackd creates so
  68. * subsequent attempts to start jackd will not believe that an
  69. * instance is already running. If the server crashes or is
  70. * terminated with SIGKILL, this is not possible. So, cleanup
  71. * is also attempted when jackd starts.
  72. *
  73. * There are several tricky issues. First, the previous JACK
  74. * server may have run for a different user ID, so its files
  75. * may be inaccessible. This is handled by using a separate
  76. * JACK_TMP_DIR subdirectory for each user. Second, there may
  77. * be other servers running with different names. Each gets
  78. * its own subdirectory within the per-user directory. The
  79. * current process has already registered as `server_name', so
  80. * we know there is no other server actively using that name.
  81. */
  82. /* nothing to do if the server directory does not exist */
  83. if ((dir = opendir(dir_name)) == NULL) {
  84. return ;
  85. }
  86. /* unlink all the files in this directory, they are mine */
  87. while ((dirent = readdir(dir)) != NULL) {
  88. char fullpath[PATH_MAX];
  89. if ((strcmp(dirent->d_name, ".") == 0)
  90. || (strcmp(dirent->d_name, "..") == 0)) {
  91. continue;
  92. }
  93. snprintf(fullpath, sizeof (fullpath), "%s/%s", dir_name, dirent->d_name);
  94. if (unlink(fullpath)) {
  95. jack_error("cannot unlink `%s' (%s)", fullpath, strerror(errno));
  96. }
  97. }
  98. closedir(dir);
  99. /* now, delete the per-server subdirectory, itself */
  100. if (rmdir(dir_name)) {
  101. jack_error("cannot remove `%s' (%s)", dir_name,
  102. strerror(errno));
  103. }
  104. /* finally, delete the per-user subdirectory, if empty */
  105. if (rmdir (jack_user_dir())) {
  106. if (errno != ENOTEMPTY) {
  107. jack_error("cannot remove `%s' (%s)",
  108. jack_user_dir(), strerror(errno));
  109. }
  110. }
  111. }
  112. #endif
  113. int JackServerGlobals::JackStart(jack_driver_desc_t* driver_desc, JSList* driver_params, int sync, int temporary, int time_out_ms, int rt, int priority, int loopback, int verbose)
  114. {
  115. JackLog("Jackdmp: sync = %ld timeout = %ld rt = %ld priority = %ld verbose = %ld \n", sync, time_out_ms, rt, priority, verbose);
  116. fServer = new JackServer(sync, temporary, time_out_ms, rt, priority, loopback, verbose);
  117. int res = fServer->Open(driver_desc, driver_params);
  118. return (res < 0) ? res : fServer->Start();
  119. }
  120. int JackServerGlobals::JackStop()
  121. {
  122. fServer->Stop();
  123. fServer->Close();
  124. JackLog("Jackdmp: server close\n");
  125. delete fServer;
  126. JackLog("Jackdmp: delete server\n");
  127. return 0;
  128. }
  129. int JackServerGlobals::JackDelete()
  130. {
  131. delete fServer;
  132. JackLog("Jackdmp: delete server\n");
  133. return 0;
  134. }
  135. bool JackServerGlobals::Init()
  136. {
  137. if (fClientCount++ == 0) {
  138. JackLog("JackServerGlobals Init\n");
  139. int realtime = 0;
  140. int client_timeout = 0; /* msecs; if zero, use period size. */
  141. int realtime_priority = 10;
  142. int verbose_aux = 0;
  143. int do_mlock = 1;
  144. unsigned int port_max = 128;
  145. int loopback = 0;
  146. int do_unlock = 0;
  147. int temporary = 0;
  148. jack_driver_desc_t* driver_desc;
  149. const char *options = "-ad:P:uvshVRL:STFl:t:mn:p:";
  150. static struct option long_options[] = {
  151. { "driver", 1, 0, 'd'},
  152. { "verbose", 0, 0, 'v' },
  153. { "help", 0, 0, 'h' },
  154. { "port-max", 1, 0, 'p' },
  155. { "no-mlock", 0, 0, 'm' },
  156. { "name", 0, 0, 'n' },
  157. { "unlock", 0, 0, 'u' },
  158. { "realtime", 0, 0, 'R' },
  159. { "loopback", 0, 0, 'L' },
  160. { "realtime-priority", 1, 0, 'P' },
  161. { "timeout", 1, 0, 't' },
  162. { "temporary", 0, 0, 'T' },
  163. { "version", 0, 0, 'V' },
  164. { "silent", 0, 0, 's' },
  165. { "sync", 0, 0, 'S' },
  166. { 0, 0, 0, 0 }
  167. };
  168. int opt = 0;
  169. int option_index = 0;
  170. int seen_driver = 0;
  171. char *driver_name = NULL;
  172. char **driver_args = NULL;
  173. JSList* driver_params;
  174. int driver_nargs = 1;
  175. JSList* drivers = NULL;
  176. char* server_name = NULL;
  177. int show_version = 0;
  178. int sync = 0;
  179. int rc, i;
  180. int ret;
  181. FILE* fp = 0;
  182. char filename[255];
  183. char buffer[255];
  184. int argc = 0;
  185. char* argv[32];
  186. snprintf(filename, 255, "%s/.jackdmprc", getenv("HOME"));
  187. fp = fopen(filename, "r");
  188. if (!fp) {
  189. fp = fopen("/etc/jackdmprc", "r");
  190. }
  191. // if still not found, check old config name for backwards compatability
  192. if (!fp) {
  193. fp = fopen("/etc/jackdmp.conf", "r");
  194. }
  195. argc = 0;
  196. if (fp) {
  197. ret = fscanf(fp, "%s", buffer);
  198. while (ret != 0 && ret != EOF) {
  199. argv[argc] = (char*)malloc(64);
  200. strcpy(argv[argc], buffer);
  201. ret = fscanf(fp, "%s", buffer);
  202. argc++;
  203. }
  204. fclose(fp);
  205. }
  206. /*
  207. int argc = 15;
  208. char* argv[] = {"jackdmp", "-R", "-v", "-d", "coreaudio", "-p", "512", "-d", "~:Aggregate:0", "-r", "48000", "-i", "2", "-o", "2" };
  209. */
  210. opterr = 0;
  211. optind = 1; // Important : to reset argv parsing
  212. while (!seen_driver &&
  213. (opt = getopt_long(argc, argv, options, long_options, &option_index)) != EOF) {
  214. switch (opt) {
  215. case 'd':
  216. seen_driver = 1;
  217. driver_name = optarg;
  218. break;
  219. case 'v':
  220. verbose_aux = 1;
  221. break;
  222. case 'S':
  223. sync = 1;
  224. break;
  225. case 'n':
  226. server_name = optarg;
  227. break;
  228. case 'm':
  229. do_mlock = 0;
  230. break;
  231. case 'p':
  232. port_max = (unsigned int)atol(optarg);
  233. break;
  234. case 'P':
  235. realtime_priority = atoi(optarg);
  236. break;
  237. case 'R':
  238. realtime = 1;
  239. break;
  240. case 'L':
  241. loopback = atoi(optarg);
  242. break;
  243. case 'T':
  244. temporary = 1;
  245. break;
  246. case 't':
  247. client_timeout = atoi(optarg);
  248. break;
  249. case 'u':
  250. do_unlock = 1;
  251. break;
  252. case 'V':
  253. show_version = 1;
  254. break;
  255. default:
  256. fprintf(stderr, "unknown option character %c\n", optopt);
  257. break;
  258. }
  259. }
  260. drivers = jack_drivers_load(drivers);
  261. if (!drivers) {
  262. fprintf(stderr, "jackdmp: no drivers found; exiting\n");
  263. goto error;
  264. }
  265. driver_desc = jack_find_driver_descriptor(drivers, driver_name);
  266. if (!driver_desc) {
  267. fprintf(stderr, "jackdmp: unknown driver '%s'\n", driver_name);
  268. goto error;
  269. }
  270. if (optind < argc) {
  271. driver_nargs = 1 + argc - optind;
  272. } else {
  273. driver_nargs = 1;
  274. }
  275. if (driver_nargs == 0) {
  276. fprintf(stderr, "No driver specified ... hmm. JACK won't do"
  277. " anything when run like this.\n");
  278. goto error;
  279. }
  280. driver_args = (char**)malloc(sizeof(char *) * driver_nargs);
  281. driver_args[0] = driver_name;
  282. for (i = 1; i < driver_nargs; i++) {
  283. driver_args[i] = argv[optind++];
  284. }
  285. if (jack_parse_driver_params(driver_desc, driver_nargs, driver_args, &driver_params)) {
  286. goto error;
  287. }
  288. #ifndef WIN32
  289. if (server_name == NULL)
  290. server_name = jack_default_server_name();
  291. #endif
  292. rc = jack_register_server(server_name);
  293. /* clean up shared memory and files from any previous instance of this server name */
  294. jack_cleanup_shm();
  295. #ifndef WIN32
  296. jack_cleanup_files(server_name);
  297. #endif
  298. if (!realtime && client_timeout == 0)
  299. client_timeout = 500; /* 0.5 sec; usable when non realtime. */
  300. for (i = 0; i < argc; i++) {
  301. free(argv[i]);
  302. }
  303. int res = JackStart(driver_desc, driver_params, sync, temporary, client_timeout, realtime, realtime_priority, loopback, verbose_aux);
  304. if (res < 0) {
  305. jack_error("Cannot start server... exit");
  306. JackDelete();
  307. jack_cleanup_shm();
  308. #ifndef WIN32
  309. jack_cleanup_files(server_name);
  310. #endif
  311. jack_unregister_server(server_name);
  312. goto error;
  313. }
  314. }
  315. return true;
  316. error:
  317. fClientCount--;
  318. return false;
  319. }
  320. void JackServerGlobals::Destroy()
  321. {
  322. if (--fClientCount == 0) {
  323. JackLog("JackServerGlobals Destroy\n");
  324. JackStop();
  325. jack_cleanup_shm();
  326. #ifndef WIN32
  327. jack_cleanup_files(server_name);
  328. #endif
  329. jack_unregister_server(server_name);
  330. }
  331. }
  332. } // end of namespace