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.
25 #include "JackChannel.h"
26 #include "JackLibGlobals.h"
27 #include "JackServerLaunch.h"
28 #include "JackPlatformPlug.h"
34 #if defined(JACK_DBUS)
36 #include <dbus/dbus.h>
38 static int start_server_dbus(const char* server_name
)
44 // initialise the errors
45 dbus_error_init(&err
);
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
);
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
63 fprintf(stderr
, "Message Null\n");
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");
74 dbus_message_unref(msg
);
75 dbus_connection_flush(conn
);
76 dbus_error_free(&err
);
83 /* Exec the JACK server in this process. Does not return. */
84 static void start_server_aux(const char* server_name
)
98 snprintf(filename
, 255, "%s/.jackdrc", getenv("HOME"));
99 fp
= fopen(filename
, "r");
102 fp
= fopen("/etc/jackdrc", "r");
104 /* if still not found, check old config name for backwards compatability */
106 fp
= fopen("/etc/jackd.conf", "r");
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) {
124 command
= (char*)(JACK_LOCATION
"/jackd");
125 strncpy(arguments
, JACK_LOCATION
"/jackd -T -d "JACK_DEFAULT_DRIVER
, 255);
127 result
= strcspn(arguments
, " ");
128 command
= (char*)malloc(result
+ 1);
129 strncpy(command
, arguments
, result
);
130 command
[result
] = '\0';
133 argv
= (char**)malloc(255);
136 /* insert -T and -nserver_name in front of arguments */
138 argv
[i
] = (char*)malloc(strlen ("-T") + 1);
139 strcpy (argv
[i
++], "-T");
141 size_t optlen
= strlen("-n");
142 char* buf
= (char*)malloc(optlen
+ strlen(server_name
) + 1);
144 strcpy(buf
+ optlen
, server_name
);
149 result
= strcspn(arguments
+ pos
, " ");
153 argv
[i
] = (char*)malloc(result
+ 1);
154 strncpy(argv
[i
], arguments
+ pos
, result
);
155 argv
[i
][result
] = '\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
));
169 static int start_server(const char* server_name
, jack_options_t options
)
171 if ((options
& JackNoStartServer
) || getenv("JACK_NO_START_SERVER")) {
175 #if defined(JACK_DBUS)
176 return start_server_dbus(server_name
);
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.
189 case 0: /* child process */
191 case 0: /* grandchild process */
192 start_server_aux(server_name
);
193 _exit(99); /* exec failed */
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 */
208 static int server_connect(char* server_name
)
210 JackClientChannel channel
;
211 int res
= channel
.ServerCheck(server_name
);
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) {
220 if (start_server(va
->server_name
, options
)) {
221 int my_status1
= *status
| JackFailure
| JackServerFailed
;
222 *status
= (jack_status_t
)my_status1
;
229 int my_status1
= *status
| JackFailure
| JackServerFailed
;
230 *status
= (jack_status_t
)my_status1
;
233 } while (server_connect(va
->server_name
) < 0);
234 int my_status1
= *status
| JackServerStarted
;
235 *status
= (jack_status_t
)my_status1
;