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