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.

246 lines
6.6KB

  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(HAVE_CONFIG_H)
  17. #include "config.h"
  18. #endif
  19. #ifndef JACK_LOCATION
  20. #include "config.h"
  21. #endif
  22. #include "JackChannel.h"
  23. #include "JackLibGlobals.h"
  24. #include "JackServerLaunch.h"
  25. #include "JackPlatformPlug.h"
  26. using namespace Jack;
  27. #ifndef WIN32
  28. #if defined(JACK_DBUS)
  29. #include <dbus/dbus.h>
  30. static int start_server_dbus(const char* server_name)
  31. {
  32. DBusError err;
  33. DBusConnection *conn;
  34. DBusMessage *msg;
  35. // initialise the errors
  36. dbus_error_init(&err);
  37. // connect to the bus
  38. conn = dbus_bus_get(DBUS_BUS_SESSION, &err);
  39. if (dbus_error_is_set(&err)) {
  40. fprintf(stderr, "Connection Error (%s)\n", err.message);
  41. dbus_error_free(&err);
  42. }
  43. if (NULL == conn) {
  44. return 1;
  45. }
  46. msg = dbus_message_new_method_call(
  47. "org.jackaudio.service", // target for the method call
  48. "/org/jackaudio/Controller", // object to call on
  49. "org.jackaudio.JackControl", // interface to call on
  50. "StartServer"); // method name
  51. if (NULL == msg) {
  52. fprintf(stderr, "Message Null\n");
  53. return 1;
  54. }
  55. // send message and get a handle for a reply
  56. if (!dbus_connection_send(conn, msg, NULL))
  57. {
  58. fprintf(stderr, "Out Of Memory!\n");
  59. return 1;
  60. }
  61. dbus_message_unref(msg);
  62. dbus_connection_flush(conn);
  63. dbus_error_free(&err);
  64. return 0;
  65. }
  66. #else
  67. /* Exec the JACK server in this process. Does not return. */
  68. static void start_server_aux(const char* server_name)
  69. {
  70. FILE* fp = 0;
  71. char filename[255];
  72. char arguments[255];
  73. char buffer[255];
  74. char* command = 0;
  75. size_t pos = 0;
  76. size_t result = 0;
  77. char** argv = 0;
  78. int i = 0;
  79. int good = 0;
  80. int ret;
  81. snprintf(filename, 255, "%s/.jackdrc", getenv("HOME"));
  82. fp = fopen(filename, "r");
  83. if (!fp) {
  84. fp = fopen("/etc/jackdrc", "r");
  85. }
  86. /* if still not found, check old config name for backwards compatability */
  87. if (!fp) {
  88. fp = fopen("/etc/jackd.conf", "r");
  89. }
  90. if (fp) {
  91. arguments[0] = '\0';
  92. ret = fscanf(fp, "%s", buffer);
  93. while (ret != 0 && ret != EOF) {
  94. strcat(arguments, buffer);
  95. strcat(arguments, " ");
  96. ret = fscanf(fp, "%s", buffer);
  97. }
  98. if (strlen(arguments) > 0) {
  99. good = 1;
  100. }
  101. fclose(fp);
  102. }
  103. if (!good) {
  104. command = (char*)(JACK_LOCATION "/jackd");
  105. strncpy(arguments, JACK_LOCATION "/jackd -T -d "JACK_DEFAULT_DRIVER, 255);
  106. } else {
  107. result = strcspn(arguments, " ");
  108. command = (char*)malloc(result + 1);
  109. strncpy(command, arguments, result);
  110. command[result] = '\0';
  111. }
  112. argv = (char**)malloc(255);
  113. while (1) {
  114. /* insert -T and -nserver_name in front of arguments */
  115. if (i == 1) {
  116. argv[i] = (char*)malloc(strlen ("-T") + 1);
  117. strcpy (argv[i++], "-T");
  118. if (server_name) {
  119. size_t optlen = strlen("-n");
  120. char* buf = (char*)malloc(optlen + strlen(server_name) + 1);
  121. strcpy(buf, "-n");
  122. strcpy(buf + optlen, server_name);
  123. argv[i++] = buf;
  124. }
  125. }
  126. result = strcspn(arguments + pos, " ");
  127. if (result == 0) {
  128. break;
  129. }
  130. argv[i] = (char*)malloc(result + 1);
  131. strncpy(argv[i], arguments + pos, result);
  132. argv[i][result] = '\0';
  133. pos += result + 1;
  134. ++i;
  135. }
  136. argv[i] = 0;
  137. execv(command, argv);
  138. /* If execv() succeeds, it does not return. There's no point
  139. * in calling jack_error() here in the child process. */
  140. fprintf(stderr, "exec of JACK server (command = \"%s\") failed: %s\n", command, strerror(errno));
  141. }
  142. #endif
  143. static int start_server(const char* server_name, jack_options_t options)
  144. {
  145. if ((options & JackNoStartServer) || getenv("JACK_NO_START_SERVER")) {
  146. return 1;
  147. }
  148. #if defined(JACK_DBUS)
  149. return start_server_dbus(server_name);
  150. #else
  151. /* The double fork() forces the server to become a child of
  152. * init, which will always clean up zombie process state on
  153. * termination. This even works in cases where the server
  154. * terminates but this client does not.
  155. *
  156. * Since fork() is usually implemented using copy-on-write
  157. * virtual memory tricks, the overhead of the second fork() is
  158. * probably relatively small.
  159. */
  160. switch (fork()) {
  161. case 0: /* child process */
  162. switch (fork()) {
  163. case 0: /* grandchild process */
  164. start_server_aux(server_name);
  165. _exit(99); /* exec failed */
  166. case - 1:
  167. _exit(98);
  168. default:
  169. _exit(0);
  170. }
  171. case - 1: /* fork() error */
  172. return 1; /* failed to start server */
  173. }
  174. /* only the original parent process goes here */
  175. return 0; /* (probably) successful */
  176. #endif
  177. }
  178. static int server_connect(char* server_name)
  179. {
  180. JackClientChannel channel;
  181. int res = channel.ServerCheck(server_name);
  182. channel.Close();
  183. return res;
  184. }
  185. int try_start_server(jack_varargs_t* va, jack_options_t options, jack_status_t* status)
  186. {
  187. if (server_connect(va->server_name) < 0) {
  188. int trys;
  189. if (start_server(va->server_name, options)) {
  190. int my_status1 = *status | JackFailure | JackServerFailed;
  191. *status = (jack_status_t)my_status1;
  192. return -1;
  193. }
  194. trys = 5;
  195. do {
  196. sleep(1);
  197. if (--trys < 0) {
  198. int my_status1 = *status | JackFailure | JackServerFailed;
  199. *status = (jack_status_t)my_status1;
  200. return -1;
  201. }
  202. } while (server_connect(va->server_name) < 0);
  203. int my_status1 = *status | JackServerStarted;
  204. *status = (jack_status_t)my_status1;
  205. }
  206. return 0;
  207. }
  208. #endif