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.

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