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.
20 #if !defined(WIN32) || defined(__CYGWIN__)
22 #ifdef PTHREAD_WIN32 // Added by JE - 13-02-2010
23 #include <ptw32/pthread.h> // Makes sure we #include the ptw32 version for
24 #endif // consistency - even though we won't need it !
26 #include "JackConstants.h"
27 #include "JackChannel.h"
28 #include "JackLibGlobals.h"
29 #include "JackServerLaunch.h"
30 #include "JackPlatformPlug.h"
36 #if defined(USE_LIBDBUS_AUTOLAUNCH)
38 #include <dbus/dbus.h>
40 static int start_server_dbus(const char* server_name
)
46 // initialise the errors
47 dbus_error_init(&err
);
50 conn
= dbus_bus_get(DBUS_BUS_SESSION
, &err
);
51 if (dbus_error_is_set(&err
)) {
52 fprintf(stderr
, "Connection Error (%s)\n", err
.message
);
53 dbus_error_free(&err
);
59 msg
= dbus_message_new_method_call(
60 "org.jackaudio.service", // target for the method call
61 "/org/jackaudio/Controller", // object to call on
62 "org.jackaudio.JackControl", // interface to call on
63 "StartServer"); // method name
65 fprintf(stderr
, "Message Null\n");
69 // send message and get a handle for a reply
70 if (!dbus_connection_send(conn
, msg
, NULL
))
72 fprintf(stderr
, "Out Of Memory!\n");
76 dbus_message_unref(msg
);
77 dbus_connection_flush(conn
);
78 dbus_error_free(&err
);
83 #elif defined(USE_CLASSIC_AUTOLAUNCH)
85 /* Exec the JACK server in this process. Does not return. */
86 static void start_server_classic_aux(const char* server_name
)
100 snprintf(filename
, 255, "%s/.jackdrc", getenv("HOME"));
101 fp
= fopen(filename
, "r");
104 fp
= fopen("/etc/jackdrc", "r");
106 /* if still not found, check old config name for backwards compatibility */
108 fp
= fopen("/etc/jackd.conf", "r");
113 res
= fscanf(fp
, "%s", buffer
);
114 while (res
!= 0 && res
!= EOF
) {
115 strcat(arguments
, buffer
);
116 strcat(arguments
, " ");
117 res
= fscanf(fp
, "%s", buffer
);
119 if (strlen(arguments
) > 0) {
126 command
= (char*)(JACK_LOCATION
"/jackd");
127 strncpy(arguments
, JACK_LOCATION
"/jackd -T -d " JACK_DEFAULT_DRIVER
, 255);
129 result
= strcspn(arguments
, " ");
130 command
= (char*)malloc(result
+ 1);
131 strncpy(command
, arguments
, result
);
132 command
[result
] = '\0';
135 argv
= (char**)malloc(255);
138 /* insert -T and -nserver_name in front of arguments */
140 argv
[i
] = (char*)malloc(strlen ("-T") + 1);
141 strcpy (argv
[i
++], "-T");
143 size_t optlen
= strlen("-n");
144 char* buf
= (char*)malloc(optlen
+ strlen(server_name
) + 1);
146 strcpy(buf
+ optlen
, server_name
);
151 /* skip whitespace */
152 while (pos
< strlen(arguments
) && arguments
[pos
] && arguments
[pos
] == ' ') {
156 if (pos
>= strlen(arguments
)) {
160 if (arguments
[pos
] == '\"') {
162 result
= strcspn(arguments
+ pos
, "\"");
164 result
= strcspn(arguments
+ pos
, " ");
171 argv
[i
] = (char*)malloc(result
+ 1);
172 strncpy(argv
[i
], arguments
+ pos
, result
);
173 argv
[i
][result
] = '\0';
182 execv(command
, argv
);
184 /* If execv() succeeds, it does not return. There's no point
185 * in calling jack_error() here in the child process. */
186 fprintf(stderr
, "exec of JACK server (command = \"%s\") failed: %s\n", command
, strerror(errno
));
189 static int start_server_classic(const char* server_name
)
191 /* The double fork() forces the server to become a child of
192 * init, which will always clean up zombie process state on
193 * termination. This even works in cases where the server
194 * terminates but this client does not.
196 * Since fork() is usually implemented using copy-on-write
197 * virtual memory tricks, the overhead of the second fork() is
198 * probably relatively small.
202 pid_t first_child_pid
;
204 first_child_pid
= fork();
205 switch (first_child_pid
) {
206 case 0: /* child process */
208 case 0: /* grandchild process */
209 start_server_classic_aux(server_name
);
210 _exit(99); /* exec failed */
216 case - 1: /* fork() error */
217 return 1; /* failed to start server */
219 waitpid(first_child_pid
, &status
, 0);
221 /* only the original parent process goes here */
222 return 0; /* (probably) successful */
227 static int start_server(const char* server_name
, jack_options_t options
)
229 if ((options
& JackNoStartServer
) || getenv("JACK_NO_START_SERVER")) {
233 #if defined(USE_LIBDBUS_AUTOLAUNCH)
234 return start_server_dbus(server_name
);
235 #elif defined(USE_CLASSIC_AUTOLAUNCH)
236 return start_server_classic(server_name
);
238 fprintf(stderr
, "Automatic start of JACK server is disabled at configure time\n");
243 static int server_connect(char* server_name
)
245 JackClientChannel channel
;
246 int res
= channel
.ServerCheck(server_name
);
248 JackSleep(2000); // Added by JE - 02-01-2009 (gives
249 // the channel some time to close)
253 int try_start_server(jack_varargs_t
* va
, jack_options_t options
, jack_status_t
* status
)
255 if (server_connect(va
->server_name
) < 0) {
257 if (start_server(va
->server_name
, options
)) {
258 int my_status1
= *status
| JackFailure
| JackServerFailed
;
259 *status
= (jack_status_t
)my_status1
;
266 int my_status1
= *status
| JackFailure
| JackServerFailed
;
267 *status
= (jack_status_t
)my_status1
;
270 } while (server_connect(va
->server_name
) < 0);
271 int my_status1
= *status
| JackServerStarted
;
272 *status
= (jack_status_t
)my_status1
;
278 #endif // !defined(WIN32) || defined(__CYGWIN__)