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.

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