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.

279 lines
7.8KB

  1. /*
  2. Copyright (C) 2001-2003 Paul Davis
  3. Copyright (C) 2004-2008 Grame
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  15. */
  16. #if !defined(WIN32) || defined(__CYGWIN__)
  17. #ifdef PTHREAD_WIN32 // Added by JE - 13-02-2010
  18. #include <ptw32/pthread.h> // Makes sure we #include the ptw32 version for
  19. #endif // consistency - even though we won't need it !
  20. #include "JackConstants.h"
  21. #include "JackChannel.h"
  22. #include "JackLibGlobals.h"
  23. #include "JackServerLaunch.h"
  24. #include "JackPlatformPlug.h"
  25. #include <sys/wait.h>
  26. using namespace Jack;
  27. #if defined(USE_LIBDBUS_AUTOLAUNCH)
  28. #include <dbus/dbus.h>
  29. static int start_server_dbus(const char* server_name)
  30. {
  31. DBusError err;
  32. DBusConnection *conn;
  33. DBusMessage *msg;
  34. // initialise the errors
  35. dbus_error_init(&err);
  36. // connect to the bus
  37. conn = dbus_bus_get(DBUS_BUS_SESSION, &err);
  38. if (dbus_error_is_set(&err)) {
  39. fprintf(stderr, "Connection Error (%s)\n", err.message);
  40. dbus_error_free(&err);
  41. }
  42. if (NULL == conn) {
  43. return 1;
  44. }
  45. msg = dbus_message_new_method_call(
  46. "org.jackaudio.service", // target for the method call
  47. "/org/jackaudio/Controller", // object to call on
  48. "org.jackaudio.JackControl", // interface to call on
  49. "StartServer"); // method name
  50. if (NULL == msg) {
  51. fprintf(stderr, "Message Null\n");
  52. return 1;
  53. }
  54. // send message and get a handle for a reply
  55. if (!dbus_connection_send(conn, msg, NULL))
  56. {
  57. fprintf(stderr, "Out Of Memory!\n");
  58. return 1;
  59. }
  60. dbus_message_unref(msg);
  61. dbus_connection_flush(conn);
  62. dbus_error_free(&err);
  63. return 0;
  64. }
  65. #elif defined(USE_CLASSIC_AUTOLAUNCH)
  66. /* Exec the JACK server in this process. Does not return. */
  67. static void start_server_classic_aux(const char* server_name)
  68. {
  69. FILE* fp = 0;
  70. char filename[255];
  71. char arguments[255];
  72. char buffer[255];
  73. char* command = 0;
  74. size_t pos = 0;
  75. size_t result = 0;
  76. char** argv = 0;
  77. int i = 0;
  78. int good = 0;
  79. int res;
  80. snprintf(filename, 255, "%s/.jackdrc", getenv("HOME"));
  81. fp = fopen(filename, "r");
  82. if (!fp) {
  83. fp = fopen("/etc/jackdrc", "r");
  84. }
  85. /* if still not found, check old config name for backwards compatibility */
  86. if (!fp) {
  87. fp = fopen("/etc/jackd.conf", "r");
  88. }
  89. if (fp) {
  90. arguments[0] = '\0';
  91. res = fscanf(fp, "%s", buffer);
  92. while (res != 0 && res != EOF) {
  93. strcat(arguments, buffer);
  94. strcat(arguments, " ");
  95. res = fscanf(fp, "%s", buffer);
  96. }
  97. if (strlen(arguments) > 0) {
  98. good = 1;
  99. }
  100. fclose(fp);
  101. }
  102. if (!good) {
  103. command = (char*)(JACK_LOCATION "/jackd");
  104. strncpy(arguments, JACK_LOCATION "/jackd -T -d " JACK_DEFAULT_DRIVER, 255);
  105. } else {
  106. result = strcspn(arguments, " ");
  107. command = (char*)malloc(result + 1);
  108. strncpy(command, arguments, result);
  109. command[result] = '\0';
  110. }
  111. argv = (char**)malloc(255);
  112. while (1) {
  113. /* insert -T and -nserver_name in front of arguments */
  114. if (i == 1) {
  115. argv[i] = (char*)malloc(strlen ("-T") + 1);
  116. strcpy (argv[i++], "-T");
  117. if (server_name) {
  118. size_t optlen = strlen("-n");
  119. char* buf = (char*)malloc(optlen + strlen(server_name) + 1);
  120. strcpy(buf, "-n");
  121. strcpy(buf + optlen, server_name);
  122. argv[i++] = buf;
  123. }
  124. }
  125. /* skip whitespace */
  126. while (pos < strlen(arguments) && arguments[pos] && arguments[pos] == ' ') {
  127. ++pos;
  128. }
  129. if (pos >= strlen(arguments)) {
  130. break;
  131. }
  132. if (arguments[pos] == '\"') {
  133. ++pos;
  134. result = strcspn(arguments + pos, "\"");
  135. } else {
  136. result = strcspn(arguments + pos, " ");
  137. }
  138. if (0 == result) {
  139. break;
  140. }
  141. argv[i] = (char*)malloc(result + 1);
  142. strncpy(argv[i], arguments + pos, result);
  143. argv[i][result] = '\0';
  144. pos += result + 1;
  145. if (++i > 253) {
  146. break;
  147. }
  148. }
  149. argv[i] = 0;
  150. execv(command, argv);
  151. /* If execv() succeeds, it does not return. There's no point
  152. * in calling jack_error() here in the child process. */
  153. fprintf(stderr, "exec of JACK server (command = \"%s\") failed: %s\n", command, strerror(errno));
  154. }
  155. static int start_server_classic(const char* server_name)
  156. {
  157. /* The double fork() forces the server to become a child of
  158. * init, which will always clean up zombie process state on
  159. * termination. This even works in cases where the server
  160. * terminates but this client does not.
  161. *
  162. * Since fork() is usually implemented using copy-on-write
  163. * virtual memory tricks, the overhead of the second fork() is
  164. * probably relatively small.
  165. */
  166. int status;
  167. pid_t first_child_pid;
  168. first_child_pid = fork();
  169. switch (first_child_pid) {
  170. case 0: /* child process */
  171. switch (fork()) {
  172. case 0: /* grandchild process */
  173. start_server_classic_aux(server_name);
  174. _exit(99); /* exec failed */
  175. case - 1:
  176. _exit(98);
  177. default:
  178. _exit(0);
  179. }
  180. case - 1: /* fork() error */
  181. return 1; /* failed to start server */
  182. }
  183. waitpid(first_child_pid, &status, 0);
  184. /* only the original parent process goes here */
  185. return 0; /* (probably) successful */
  186. }
  187. #endif
  188. static int start_server(const char* server_name, jack_options_t options)
  189. {
  190. if ((options & JackNoStartServer) || getenv("JACK_NO_START_SERVER")) {
  191. return 1;
  192. }
  193. #if defined(USE_LIBDBUS_AUTOLAUNCH)
  194. return start_server_dbus(server_name);
  195. #elif defined(USE_CLASSIC_AUTOLAUNCH)
  196. return start_server_classic(server_name);
  197. #else
  198. fprintf(stderr, "Automatic start of JACK server is disabled at configure time\n");
  199. return 1;
  200. #endif
  201. }
  202. static int server_connect(char* server_name)
  203. {
  204. JackClientChannel channel;
  205. int res = channel.ServerCheck(server_name);
  206. channel.Close();
  207. JackSleep(2000); // Added by JE - 02-01-2009 (gives
  208. // the channel some time to close)
  209. return res;
  210. }
  211. int try_start_server(jack_varargs_t* va, jack_options_t options, jack_status_t* status)
  212. {
  213. if (server_connect(va->server_name) < 0) {
  214. int trys;
  215. if (start_server(va->server_name, options)) {
  216. int my_status1 = *status | JackFailure | JackServerFailed;
  217. *status = (jack_status_t)my_status1;
  218. return -1;
  219. }
  220. trys = 5;
  221. do {
  222. sleep(1);
  223. if (--trys < 0) {
  224. int my_status1 = *status | JackFailure | JackServerFailed;
  225. *status = (jack_status_t)my_status1;
  226. return -1;
  227. }
  228. } while (server_connect(va->server_name) < 0);
  229. int my_status1 = *status | JackServerStarted;
  230. *status = (jack_status_t)my_status1;
  231. }
  232. return 0;
  233. }
  234. #endif // !defined(WIN32) || defined(__CYGWIN__)