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. using namespace Jack;
  26. #ifndef WIN32
  27. #if defined(JACK_DBUS)
  28. #include <dbus/dbus.h>
  29. 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. #else
  66. /* Exec the JACK server in this process. Does not return. */
  67. static void start_server_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 ret;
  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 compatability */
  86. if (!fp) {
  87. fp = fopen("/etc/jackd.conf", "r");
  88. }
  89. if (fp) {
  90. arguments[0] = '\0';
  91. ret = fscanf(fp, "%s", buffer);
  92. while (ret != 0 && ret != EOF) {
  93. strcat(arguments, buffer);
  94. strcat(arguments, " ");
  95. ret = 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. result = strcspn(arguments + pos, " ");
  126. if (result == 0) {
  127. break;
  128. }
  129. argv[i] = (char*)malloc(result + 1);
  130. strncpy(argv[i], arguments + pos, result);
  131. argv[i][result] = '\0';
  132. pos += result + 1;
  133. ++i;
  134. }
  135. argv[i] = 0;
  136. execv(command, argv);
  137. /* If execv() succeeds, it does not return. There's no point
  138. * in calling jack_error() here in the child process. */
  139. fprintf(stderr, "exec of JACK server (command = \"%s\") failed: %s\n", command, strerror(errno));
  140. }
  141. #endif
  142. int start_server(const char* server_name, jack_options_t options)
  143. {
  144. if ((options & JackNoStartServer) || getenv("JACK_NO_START_SERVER")) {
  145. return 1;
  146. }
  147. #if defined(JACK_DBUS)
  148. return start_server_dbus(server_name);
  149. #else
  150. /* The double fork() forces the server to become a child of
  151. * init, which will always clean up zombie process state on
  152. * termination. This even works in cases where the server
  153. * terminates but this client does not.
  154. *
  155. * Since fork() is usually implemented using copy-on-write
  156. * virtual memory tricks, the overhead of the second fork() is
  157. * probably relatively small.
  158. */
  159. switch (fork()) {
  160. case 0: /* child process */
  161. switch (fork()) {
  162. case 0: /* grandchild process */
  163. start_server_aux(server_name);
  164. _exit(99); /* exec failed */
  165. case - 1:
  166. _exit(98);
  167. default:
  168. _exit(0);
  169. }
  170. case - 1: /* fork() error */
  171. return 1; /* failed to start server */
  172. }
  173. /* only the original parent process goes here */
  174. return 0; /* (probably) successful */
  175. #endif
  176. }
  177. int server_connect(char* server_name)
  178. {
  179. JackClientChannelInterface* channel = JackGlobals::MakeClientChannel();
  180. int res = channel->ServerCheck(server_name);
  181. channel->Close();
  182. delete channel;
  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