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 #include "JackConstants.h"
22 #include "JackChannel.h"
23 #include "JackLibGlobals.h"
24 #include "JackServerLaunch.h"
25 #include "JackPlatformPlug.h"
29 #if defined(USE_LIBDBUS_AUTOLAUNCH)
31 #include <dbus/dbus.h>
33 static int start_server_dbus(const char* server_name
)
39 // initialise the errors
40 dbus_error_init(&err
);
43 conn
= dbus_bus_get(DBUS_BUS_SESSION
, &err
);
44 if (dbus_error_is_set(&err
)) {
45 fprintf(stderr
, "Connection Error (%s)\n", err
.message
);
46 dbus_error_free(&err
);
52 msg
= dbus_message_new_method_call(
53 "org.jackaudio.service", // target for the method call
54 "/org/jackaudio/Controller", // object to call on
55 "org.jackaudio.JackControl", // interface to call on
56 "StartServer"); // method name
58 fprintf(stderr
, "Message Null\n");
62 // send message and get a handle for a reply
63 if (!dbus_connection_send(conn
, msg
, NULL
))
65 fprintf(stderr
, "Out Of Memory!\n");
69 dbus_message_unref(msg
);
70 dbus_connection_flush(conn
);
71 dbus_error_free(&err
);
78 /* Exec the JACK server in this process. Does not return. */
79 static void start_server_classic_aux(const char* server_name
)
93 snprintf(filename
, 255, "%s/.jackdrc", getenv("HOME"));
94 fp
= fopen(filename
, "r");
97 fp
= fopen("/etc/jackdrc", "r");
99 /* if still not found, check old config name for backwards compatability */
101 fp
= fopen("/etc/jackd.conf", "r");
106 ret
= fscanf(fp
, "%s", buffer
);
107 while (ret
!= 0 && ret
!= EOF
) {
108 strcat(arguments
, buffer
);
109 strcat(arguments
, " ");
110 ret
= fscanf(fp
, "%s", buffer
);
112 if (strlen(arguments
) > 0) {
119 command
= (char*)(JACK_LOCATION
"/jackd");
120 strncpy(arguments
, JACK_LOCATION
"/jackd -T -d "JACK_DEFAULT_DRIVER
, 255);
122 result
= strcspn(arguments
, " ");
123 command
= (char*)malloc(result
+ 1);
124 strncpy(command
, arguments
, result
);
125 command
[result
] = '\0';
128 argv
= (char**)malloc(255);
131 /* insert -T and -nserver_name in front of arguments */
133 argv
[i
] = (char*)malloc(strlen ("-T") + 1);
134 strcpy (argv
[i
++], "-T");
136 size_t optlen
= strlen("-n");
137 char* buf
= (char*)malloc(optlen
+ strlen(server_name
) + 1);
139 strcpy(buf
+ optlen
, server_name
);
144 result
= strcspn(arguments
+ pos
, " ");
148 argv
[i
] = (char*)malloc(result
+ 1);
149 strncpy(argv
[i
], arguments
+ pos
, result
);
150 argv
[i
][result
] = '\0';
155 execv(command
, argv
);
157 /* If execv() succeeds, it does not return. There's no point
158 * in calling jack_error() here in the child process. */
159 fprintf(stderr
, "exec of JACK server (command = \"%s\") failed: %s\n", command
, strerror(errno
));
162 static int start_server_classic(const char* server_name
)
164 /* The double fork() forces the server to become a child of
165 * init, which will always clean up zombie process state on
166 * termination. This even works in cases where the server
167 * terminates but this client does not.
169 * Since fork() is usually implemented using copy-on-write
170 * virtual memory tricks, the overhead of the second fork() is
171 * probably relatively small.
174 case 0: /* child process */
176 case 0: /* grandchild process */
177 start_server_classic_aux(server_name
);
178 _exit(99); /* exec failed */
184 case - 1: /* fork() error */
185 return 1; /* failed to start server */
188 /* only the original parent process goes here */
189 return 0; /* (probably) successful */
194 static int start_server(const char* server_name
, jack_options_t options
)
196 if ((options
& JackNoStartServer
) || getenv("JACK_NO_START_SERVER")) {
200 #if defined(USE_LIBDBUS_AUTOLAUNCH)
201 return start_server_dbus(server_name
);
203 return start_server_classic(server_name
);
207 static int server_connect(char* server_name
)
209 JackClientChannel channel
;
210 int res
= channel
.ServerCheck(server_name
);
212 JackSleep(2000); // Added by JE - 02-01-2009 (gives
213 // the channel some time to close)
217 int try_start_server(jack_varargs_t
* va
, jack_options_t options
, jack_status_t
* status
)
219 if (server_connect(va
->server_name
) < 0) {
221 if (start_server(va
->server_name
, options
)) {
222 int my_status1
= *status
| JackFailure
| JackServerFailed
;
223 *status
= (jack_status_t
)my_status1
;
230 int my_status1
= *status
| JackFailure
| JackServerFailed
;
231 *status
= (jack_status_t
)my_status1
;
234 } while (server_connect(va
->server_name
) < 0);
235 int my_status1
= *status
| JackServerStarted
;
236 *status
= (jack_status_t
)my_status1
;