Better isolation of server and clients system resources to allow starting the server...
[jack2.git] / posix / JackPosixServerLaunch.cpp
blob645f0a1a1a4a12e5249eea5b54819801a9917b0a
1 /*
2 Copyright (C) 2001-2003 Paul Davis
3 Copyright (C) 2004-2008 Grame
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 #ifndef JACK_LOCATION
22 #include "config.h"
23 #endif
25 #include "JackChannel.h"
26 #include "JackLibGlobals.h"
27 #include "JackServerLaunch.h"
28 #include "JackPlatformPlug.h"
30 using namespace Jack;
32 #ifndef WIN32
34 #if defined(JACK_DBUS)
36 #include <dbus/dbus.h>
38 static int start_server_dbus(const char* server_name)
40 DBusError err;
41 DBusConnection *conn;
42 DBusMessage *msg;
44 // initialise the errors
45 dbus_error_init(&err);
47 // connect to the bus
48 conn = dbus_bus_get(DBUS_BUS_SESSION, &err);
49 if (dbus_error_is_set(&err)) {
50 fprintf(stderr, "Connection Error (%s)\n", err.message);
51 dbus_error_free(&err);
53 if (NULL == conn) {
54 return 1;
57 msg = dbus_message_new_method_call(
58 "org.jackaudio.service", // target for the method call
59 "/org/jackaudio/Controller", // object to call on
60 "org.jackaudio.JackControl", // interface to call on
61 "StartServer"); // method name
62 if (NULL == msg) {
63 fprintf(stderr, "Message Null\n");
64 return 1;
67 // send message and get a handle for a reply
68 if (!dbus_connection_send(conn, msg, NULL))
70 fprintf(stderr, "Out Of Memory!\n");
71 return 1;
74 dbus_message_unref(msg);
75 dbus_connection_flush(conn);
76 dbus_error_free(&err);
78 return 0;
81 #else
83 /* Exec the JACK server in this process. Does not return. */
84 static void start_server_aux(const char* server_name)
86 FILE* fp = 0;
87 char filename[255];
88 char arguments[255];
89 char buffer[255];
90 char* command = 0;
91 size_t pos = 0;
92 size_t result = 0;
93 char** argv = 0;
94 int i = 0;
95 int good = 0;
96 int ret;
98 snprintf(filename, 255, "%s/.jackdrc", getenv("HOME"));
99 fp = fopen(filename, "r");
101 if (!fp) {
102 fp = fopen("/etc/jackdrc", "r");
104 /* if still not found, check old config name for backwards compatability */
105 if (!fp) {
106 fp = fopen("/etc/jackd.conf", "r");
109 if (fp) {
110 arguments[0] = '\0';
111 ret = fscanf(fp, "%s", buffer);
112 while (ret != 0 && ret != EOF) {
113 strcat(arguments, buffer);
114 strcat(arguments, " ");
115 ret = fscanf(fp, "%s", buffer);
117 if (strlen(arguments) > 0) {
118 good = 1;
120 fclose(fp);
123 if (!good) {
124 command = (char*)(JACK_LOCATION "/jackd");
125 strncpy(arguments, JACK_LOCATION "/jackd -T -d "JACK_DEFAULT_DRIVER, 255);
126 } else {
127 result = strcspn(arguments, " ");
128 command = (char*)malloc(result + 1);
129 strncpy(command, arguments, result);
130 command[result] = '\0';
133 argv = (char**)malloc(255);
135 while (1) {
136 /* insert -T and -nserver_name in front of arguments */
137 if (i == 1) {
138 argv[i] = (char*)malloc(strlen ("-T") + 1);
139 strcpy (argv[i++], "-T");
140 if (server_name) {
141 size_t optlen = strlen("-n");
142 char* buf = (char*)malloc(optlen + strlen(server_name) + 1);
143 strcpy(buf, "-n");
144 strcpy(buf + optlen, server_name);
145 argv[i++] = buf;
149 result = strcspn(arguments + pos, " ");
150 if (result == 0) {
151 break;
153 argv[i] = (char*)malloc(result + 1);
154 strncpy(argv[i], arguments + pos, result);
155 argv[i][result] = '\0';
156 pos += result + 1;
157 ++i;
159 argv[i] = 0;
160 execv(command, argv);
162 /* If execv() succeeds, it does not return. There's no point
163 * in calling jack_error() here in the child process. */
164 fprintf(stderr, "exec of JACK server (command = \"%s\") failed: %s\n", command, strerror(errno));
167 #endif
169 static int start_server(const char* server_name, jack_options_t options)
171 if ((options & JackNoStartServer) || getenv("JACK_NO_START_SERVER")) {
172 return 1;
175 #if defined(JACK_DBUS)
176 return start_server_dbus(server_name);
177 #else
179 /* The double fork() forces the server to become a child of
180 * init, which will always clean up zombie process state on
181 * termination. This even works in cases where the server
182 * terminates but this client does not.
184 * Since fork() is usually implemented using copy-on-write
185 * virtual memory tricks, the overhead of the second fork() is
186 * probably relatively small.
188 switch (fork()) {
189 case 0: /* child process */
190 switch (fork()) {
191 case 0: /* grandchild process */
192 start_server_aux(server_name);
193 _exit(99); /* exec failed */
194 case - 1:
195 _exit(98);
196 default:
197 _exit(0);
199 case - 1: /* fork() error */
200 return 1; /* failed to start server */
203 /* only the original parent process goes here */
204 return 0; /* (probably) successful */
205 #endif
208 static int server_connect(char* server_name)
210 JackClientChannel channel;
211 int res = channel.ServerCheck(server_name);
212 channel.Close();
213 return res;
216 int try_start_server(jack_varargs_t* va, jack_options_t options, jack_status_t* status)
218 if (server_connect(va->server_name) < 0) {
219 int trys;
220 if (start_server(va->server_name, options)) {
221 int my_status1 = *status | JackFailure | JackServerFailed;
222 *status = (jack_status_t)my_status1;
223 return -1;
225 trys = 5;
226 do {
227 sleep(1);
228 if (--trys < 0) {
229 int my_status1 = *status | JackFailure | JackServerFailed;
230 *status = (jack_status_t)my_status1;
231 return -1;
233 } while (server_connect(va->server_name) < 0);
234 int my_status1 = *status | JackServerStarted;
235 *status = (jack_status_t)my_status1;
238 return 0;
241 #endif