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.7KB

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