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.

423 lines
10KB

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