1 | //roard.c: |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008 |
---|
5 | * |
---|
6 | * This file is part of roard a part of RoarAudio, |
---|
7 | * a cross-platform sound system for both, home and professional use. |
---|
8 | * See README for details. |
---|
9 | * |
---|
10 | * This file is free software; you can redistribute it and/or modify |
---|
11 | * it under the terms of the GNU General Public License version 3 |
---|
12 | * as published by the Free Software Foundation. |
---|
13 | * |
---|
14 | * RoarAudio is distributed in the hope that it will be useful, |
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 | * GNU General Public License for more details. |
---|
18 | * |
---|
19 | * You should have received a copy of the GNU General Public License |
---|
20 | * along with this software; see the file COPYING. If not, write to |
---|
21 | * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
22 | * |
---|
23 | */ |
---|
24 | |
---|
25 | #include "roard.h" |
---|
26 | |
---|
27 | char * server = ROAR_DEFAULT_SOCK_GLOBAL; // global server address |
---|
28 | |
---|
29 | void usage (void) { |
---|
30 | printf("Usage: roard [OPTIONS]...\n\n"); |
---|
31 | |
---|
32 | printf("Misc Options:\n\n"); |
---|
33 | printf( |
---|
34 | " --daemon - Bring the server into background after init\n" |
---|
35 | " --terminate - Terminate after last client quited\n" |
---|
36 | " --restart - Trys to stop an old instance and start a new with new settings\n" |
---|
37 | " --realtime - Trys to get realtime priority,\n" |
---|
38 | " give multible times for being more realtime\n" |
---|
39 | " --chroot DIR - chroots to the given dir\n" |
---|
40 | " --setgid - GroupID to the audio group as specified via -G\n" |
---|
41 | " --setuid - UserID to the audio user as specified via -U\n" |
---|
42 | " --sysclocksync - calculate exact sample rate using the system clock\n" |
---|
43 | ); |
---|
44 | |
---|
45 | printf("\nAudio Options:\n\n"); |
---|
46 | printf( |
---|
47 | " -R --rate RATE - Set server rate\n" |
---|
48 | " -B --bits BITS - Set server bits\n" |
---|
49 | " -C --chans CHANNELS - Set server channels\n" |
---|
50 | ); |
---|
51 | |
---|
52 | printf("\nDriver Options:\n\n"); |
---|
53 | printf(" -d --driver DRV - Set the driver (default: %s)\n", ROAR_DRIVER_DEFAULT); |
---|
54 | printf(" -D --device DEV - Set the device\n"); |
---|
55 | printf(" -dO OPTS - Set output options\n"); |
---|
56 | printf(" --list-driver - List all drivers\n"); |
---|
57 | |
---|
58 | printf("\nOutput Options:\n\n"); |
---|
59 | printf(" -o --odriver DRV - Set the driver, use '--list-driver' to get a list\n"); |
---|
60 | printf(" -O --odevice DEV - Set the device\n"); |
---|
61 | printf(" -oO OPTS - Set output options\n"); |
---|
62 | printf(" -oN - Adds another output\n"); |
---|
63 | printf(" -oP - Mark output as primary\n"); |
---|
64 | |
---|
65 | printf("\nSource Options:\n\n"); |
---|
66 | printf(" -s --source DRV - Use DRV as input driver\n" |
---|
67 | " -S DEV - Use DEV as input device\n" |
---|
68 | " -sO OPTS - Use OPTS as input options\n" |
---|
69 | " -sP - Make souce as primary\n" |
---|
70 | ); |
---|
71 | |
---|
72 | printf("\nCodec Filter Options:\n\n"); |
---|
73 | printf(" --list-cf - List all codec filter\n" |
---|
74 | ); |
---|
75 | |
---|
76 | printf("\nServer Options:\n\n"); |
---|
77 | printf(" -t --tcp - Use TCP listen socket\n" |
---|
78 | " -u --unix - Use UNIX Domain listen socket (default)\n" |
---|
79 | #ifdef ROAR_HAVE_LIBDNET |
---|
80 | " -n --decnet - use DECnet listen socket\n" |
---|
81 | #endif |
---|
82 | " -4 - Use IPv4 connections (implies -t)\n" |
---|
83 | #ifdef PF_INET6 |
---|
84 | " -6 - Use IPv6 connections (implies -t)\n" |
---|
85 | #endif |
---|
86 | #ifdef IPV6_ADDRFORM |
---|
87 | " -64 - Try to downgrade sockets from IPv6 into IPv4,\n" |
---|
88 | " this is normaly not usefull.\n" |
---|
89 | #endif |
---|
90 | " -p --port - TCP Port to bind to\n" |
---|
91 | " -b --bind - IP/Hostname to bind to\n" |
---|
92 | " --sock - Filename for UNIX Domain Socket\n" |
---|
93 | " -G GROUP - Sets the group for the UNIX Domain Socket, (default: %s)\n" |
---|
94 | " You need the permittions to change the GID\n" |
---|
95 | " -U USER - Sets the user for the UNIX Domain Socket, (default: do not set)\n" |
---|
96 | " You need the permittions to change the UID (normaly only root has)\n" |
---|
97 | " --no-listen - Do not listen for new clients\n" |
---|
98 | " (only usefull for relaing, impleys --terminate)\n" |
---|
99 | " --client-fh - Comunicate with a client over this handle\n" |
---|
100 | " (only usefull for relaing)\n" |
---|
101 | " --close-fh - Closes the given fh\n" |
---|
102 | " --standby - Start in standby state\n" |
---|
103 | " --auto-standby - Automatical goes into standby if there are no streams\n", |
---|
104 | ROAR_DEFAULT_SOCKGRP |
---|
105 | ); |
---|
106 | // printf("\n Options:\n\n"); |
---|
107 | printf("\n"); |
---|
108 | } |
---|
109 | |
---|
110 | int restart_server (char * server) { |
---|
111 | struct roar_connection con; |
---|
112 | if ( roar_connect(&con, server) == -1 ) { |
---|
113 | return -1; |
---|
114 | } |
---|
115 | |
---|
116 | if ( roar_terminate(&con, 1) == -1 ) { |
---|
117 | return -1; |
---|
118 | } |
---|
119 | |
---|
120 | return roar_disconnect(&con); |
---|
121 | } |
---|
122 | |
---|
123 | #define R_SETUID 1 |
---|
124 | #define R_SETGID 2 |
---|
125 | |
---|
126 | int add_output (char * drv, char * dev, char * opts, int prim, int count) { |
---|
127 | int stream; |
---|
128 | struct roar_stream * s; |
---|
129 | struct roar_stream_server * ss; |
---|
130 | char * k, * v; |
---|
131 | #ifdef ROAR_DRIVER_CODEC |
---|
132 | char * to_free = NULL; |
---|
133 | #endif |
---|
134 | int codec; |
---|
135 | int sync = 0; |
---|
136 | |
---|
137 | ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = ?", drv, dev, opts); |
---|
138 | |
---|
139 | if ( drv == NULL && count == 0 ) { |
---|
140 | drv = ROAR_DRIVER_DEFAULT; |
---|
141 | prim = 1; |
---|
142 | sync = 1; |
---|
143 | |
---|
144 | #ifdef ROAR_DRIVER_CODEC |
---|
145 | if ( opts == NULL ) { |
---|
146 | opts = to_free = strdup("codec=" ROAR_DRIVER_CODEC); |
---|
147 | } |
---|
148 | #endif |
---|
149 | } |
---|
150 | |
---|
151 | if ( (stream = streams_new()) == -1 ) { |
---|
152 | ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = -1", drv, dev, opts); |
---|
153 | if ( prim ) alive = 0; |
---|
154 | return -1; |
---|
155 | } |
---|
156 | |
---|
157 | streams_get(stream, &ss); |
---|
158 | s = ROAR_STREAM(ss); |
---|
159 | |
---|
160 | memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info)); |
---|
161 | |
---|
162 | s->dir = ROAR_DIR_OUTPUT; |
---|
163 | s->pos_rel_id = -1; |
---|
164 | // s->info.codec = codec; |
---|
165 | |
---|
166 | codec = s->info.codec; |
---|
167 | |
---|
168 | k = strtok(opts, ","); |
---|
169 | while (k != NULL) { |
---|
170 | // ROAR_WARN("add_output(*): opts: %s", k); |
---|
171 | |
---|
172 | if ( (v = strstr(k, "=")) != NULL ) { |
---|
173 | *v++ = 0; |
---|
174 | } |
---|
175 | |
---|
176 | ROAR_DBG("add_output(*): opts: k='%s', v='%s'", k, v); |
---|
177 | if ( strcmp(k, "rate") == 0 ) { |
---|
178 | s->info.rate = atoi(v); |
---|
179 | } else if ( strcmp(k, "channels") == 0 ) { |
---|
180 | s->info.channels = atoi(v); |
---|
181 | } else if ( strcmp(k, "bits") == 0 ) { |
---|
182 | s->info.bits = atoi(v); |
---|
183 | } else if ( strcmp(k, "codec") == 0 ) { |
---|
184 | if ( (codec = roar_str2codec(v)) == -1 ) { |
---|
185 | ROAR_ERR("add_output(*): unknown codec '%s'", v); |
---|
186 | streams_delete(stream); |
---|
187 | if ( prim ) alive = 0; |
---|
188 | #ifdef ROAR_DRIVER_CODEC |
---|
189 | if ( to_free != NULL ) |
---|
190 | free(to_free); |
---|
191 | #endif |
---|
192 | return -1; |
---|
193 | } |
---|
194 | } else if ( strcmp(k, "meta") == 0 ) { |
---|
195 | streams_set_flag(stream, ROAR_FLAG_META); |
---|
196 | } else if ( strcmp(k, "sync") == 0 ) { |
---|
197 | sync = 1; |
---|
198 | } else { |
---|
199 | ROAR_ERR("add_output(*): unknown option '%s'", k); |
---|
200 | streams_delete(stream); |
---|
201 | if ( prim ) alive = 0; |
---|
202 | #ifdef ROAR_DRIVER_CODEC |
---|
203 | if ( to_free != NULL ) |
---|
204 | free(to_free); |
---|
205 | #endif |
---|
206 | return -1; |
---|
207 | } |
---|
208 | |
---|
209 | k = strtok(NULL, ","); |
---|
210 | } |
---|
211 | |
---|
212 | #ifdef ROAR_DRIVER_CODEC |
---|
213 | if ( to_free != NULL ) |
---|
214 | free(to_free); |
---|
215 | #endif |
---|
216 | |
---|
217 | if ( codec == ROAR_CODEC_ALAW || codec == ROAR_CODEC_MULAW ) |
---|
218 | s->info.bits = 8; // needed to open OSS driver, will be overriden by codecfilter |
---|
219 | |
---|
220 | s->info.codec = codec; |
---|
221 | ROAR_STREAM_SERVER(s)->codec_orgi = codec; |
---|
222 | |
---|
223 | if ( driver_openvio(&(ss->vio), &(ss->driver_id), drv, dev, &(s->info), -1) == -1 ) { |
---|
224 | streams_delete(stream); |
---|
225 | ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = -1", drv, dev, opts); |
---|
226 | if ( prim ) alive = 0; |
---|
227 | return -1; |
---|
228 | } |
---|
229 | |
---|
230 | ROAR_DBG("add_output(*): ss->driver_id=%i", ss->driver_id); |
---|
231 | |
---|
232 | streams_set_fh(stream, -1); // update some internal structures |
---|
233 | |
---|
234 | client_stream_add(g_source_client, stream); |
---|
235 | |
---|
236 | if ( prim ) { |
---|
237 | streams_mark_primary(stream); |
---|
238 | s->pos_rel_id = stream; |
---|
239 | } |
---|
240 | |
---|
241 | if ( sync ) { |
---|
242 | streams_set_flag(stream, ROAR_FLAG_SYNC); |
---|
243 | } else { |
---|
244 | streams_reset_flag(stream, ROAR_FLAG_SYNC); |
---|
245 | } |
---|
246 | |
---|
247 | return 0; |
---|
248 | } |
---|
249 | |
---|
250 | int main (int argc, char * argv[]) { |
---|
251 | int i; |
---|
252 | char * k; |
---|
253 | char user_sock[80] = {0}; |
---|
254 | struct roar_audio_info sa; |
---|
255 | int daemon = 0; |
---|
256 | int realtime = 0; |
---|
257 | int sysclocksync = 0; |
---|
258 | char * driver = NULL; |
---|
259 | char * device = NULL; |
---|
260 | char * opts = NULL; |
---|
261 | // char * server = ROAR_DEFAULT_SOCK_GLOBAL; |
---|
262 | int port = ROAR_DEFAULT_PORT; |
---|
263 | int drvid; |
---|
264 | char * s_drv = "cf"; |
---|
265 | char * s_dev = NULL; |
---|
266 | char * s_con = NULL; |
---|
267 | char * s_opt = NULL; |
---|
268 | int s_prim = 0; |
---|
269 | char * o_drv = getenv("ROAR_DRIVER"); |
---|
270 | char * o_dev = getenv("ROAR_DEVICE"); |
---|
271 | char * o_opts = NULL; |
---|
272 | int o_prim = 0; |
---|
273 | int o_count = 0; |
---|
274 | char * sock_grp = ROAR_DEFAULT_SOCKGRP; |
---|
275 | char * sock_user = NULL; |
---|
276 | int sock_type = ROAR_SOCKET_TYPE_UNKNOWN; |
---|
277 | char * chrootdir = NULL; |
---|
278 | int setids = 0; |
---|
279 | char * env_roar_proxy_backup; |
---|
280 | struct group * grp = NULL; |
---|
281 | struct passwd * pwd = NULL; |
---|
282 | struct servent * serv = NULL; |
---|
283 | DRIVER_USERDATA_T drvinst; |
---|
284 | struct roar_client * self = NULL; |
---|
285 | #ifdef ROAR_HAVE_LIBDNET |
---|
286 | char decnethost[80]; |
---|
287 | #endif |
---|
288 | |
---|
289 | g_listen_socket = -1; |
---|
290 | g_standby = 0; |
---|
291 | g_autostandby = 0; |
---|
292 | alive = 1; |
---|
293 | g_no_listen = 0; |
---|
294 | |
---|
295 | sa.bits = ROAR_BITS_DEFAULT; |
---|
296 | sa.channels = ROAR_CHANNELS_DEFAULT; |
---|
297 | sa.rate = ROAR_RATE_DEFAULT; |
---|
298 | sa.codec = ROAR_CODEC_DEFAULT; |
---|
299 | |
---|
300 | g_sa = &sa; |
---|
301 | |
---|
302 | |
---|
303 | if ( getuid() != 0 && getenv("HOME") ) { |
---|
304 | snprintf(user_sock, 79, "%s/%s", getenv("HOME"), ROAR_DEFAULT_SOCK_USER); |
---|
305 | server = user_sock; |
---|
306 | } |
---|
307 | |
---|
308 | if ( getenv("ROAR_SERVER") != NULL ) |
---|
309 | server = getenv("ROAR_SERVER"); |
---|
310 | |
---|
311 | if ( clients_init() == -1 ) { |
---|
312 | ROAR_ERR("Can not init clients!"); |
---|
313 | return 1; |
---|
314 | } |
---|
315 | |
---|
316 | if ( streams_init() == -1 ) { |
---|
317 | ROAR_ERR("Can not init streams!"); |
---|
318 | return 1; |
---|
319 | } |
---|
320 | |
---|
321 | if ( (g_self_client = clients_new()) == -1 ) { |
---|
322 | ROAR_ERR("Can not create self client!"); |
---|
323 | return 1; |
---|
324 | } |
---|
325 | |
---|
326 | if ( sources_init() == -1 ) { |
---|
327 | ROAR_ERR("Can not init sources!"); |
---|
328 | return 1; |
---|
329 | } |
---|
330 | |
---|
331 | if ( (sources_set_client(g_self_client)) == -1 ) { |
---|
332 | ROAR_ERR("Can not init set source client!"); |
---|
333 | return 1; |
---|
334 | } |
---|
335 | |
---|
336 | for (i = 1; i < argc; i++) { |
---|
337 | k = argv[i]; |
---|
338 | |
---|
339 | if ( strcmp(k, "-h") == 0 || strcmp(k, "--help") == 0 ) { |
---|
340 | usage(); |
---|
341 | return 0; |
---|
342 | |
---|
343 | } else if ( strcmp(k, "--restart") == 0 ) { |
---|
344 | if ( restart_server(server) == -1 ) { |
---|
345 | ROAR_WARN("Can not terminate old server (not running at %s?), tring to continue anyway", server); |
---|
346 | } |
---|
347 | |
---|
348 | } else if ( strcmp(k, "--demon") == 0 || strcmp(k, "--daemon") == 0 ) { |
---|
349 | daemon = 1; |
---|
350 | } else if ( strcmp(k, "--terminate") == 0 ) { |
---|
351 | g_terminate = 1; |
---|
352 | } else if ( strcmp(k, "--sysclocksync") == 0 ) { |
---|
353 | sysclocksync = 1000; |
---|
354 | } else if ( strcmp(k, "--realtime") == 0 ) { |
---|
355 | realtime++; |
---|
356 | } else if ( strcmp(k, "--chroot") == 0 ) { |
---|
357 | chrootdir = argv[++i]; |
---|
358 | } else if ( strcmp(k, "--setgid") == 0 ) { |
---|
359 | setids |= R_SETGID; |
---|
360 | } else if ( strcmp(k, "--setuid") == 0 ) { |
---|
361 | setids |= R_SETUID; |
---|
362 | |
---|
363 | } else if ( strcmp(k, "--list-cf") == 0 ) { |
---|
364 | print_codecfilterlist(); |
---|
365 | return 0; |
---|
366 | |
---|
367 | } else if ( strcmp(k, "-R") == 0 || strcmp(k, "--rate") == 0 ) { |
---|
368 | sa.rate = atoi(argv[++i]); |
---|
369 | } else if ( strcmp(k, "-B") == 0 || strcmp(k, "--bits") == 0 ) { |
---|
370 | sa.bits = atoi(argv[++i]); |
---|
371 | } else if ( strcmp(k, "-C") == 0 || strcmp(k, "--chans") == 0 ) { |
---|
372 | sa.channels = atoi(argv[++i]); |
---|
373 | |
---|
374 | } else if ( strcmp(k, "-d") == 0 || strcmp(k, "--driver") == 0 ) { |
---|
375 | driver = argv[++i]; |
---|
376 | if ( strcmp(driver, "list") == 0 ) { |
---|
377 | ROAR_WARN("The option is obsolete, use --list-driver!"); |
---|
378 | print_driverlist(); |
---|
379 | return 0; |
---|
380 | } |
---|
381 | } else if ( strcmp(k, "-D") == 0 || strcmp(k, "--device") == 0 ) { |
---|
382 | device = argv[++i]; |
---|
383 | } else if ( strcmp(k, "-dO") == 0 ) { |
---|
384 | opts = argv[++i]; |
---|
385 | } else if ( strcmp(k, "--list-driver") == 0 ) { |
---|
386 | print_driverlist(); |
---|
387 | return 0; |
---|
388 | |
---|
389 | } else if ( strcmp(k, "-o") == 0 || strcmp(k, "--odriver") == 0 ) { |
---|
390 | o_drv = argv[++i]; |
---|
391 | } else if ( strcmp(k, "-O") == 0 || strcmp(k, "--odevice") == 0 ) { |
---|
392 | o_dev = argv[++i]; |
---|
393 | } else if ( strcmp(k, "-oO") == 0 ) { |
---|
394 | o_opts = argv[++i]; |
---|
395 | } else if ( strcmp(k, "-oP") == 0 ) { |
---|
396 | o_prim = 1; |
---|
397 | } else if ( strcmp(k, "-oN") == 0 ) { |
---|
398 | if ( add_output(o_drv, o_dev, o_opts, o_prim, o_count) != -1 ) |
---|
399 | o_count++; |
---|
400 | |
---|
401 | o_drv = o_dev = o_opts = NULL; |
---|
402 | o_prim = 0; |
---|
403 | |
---|
404 | } else if ( strcmp(k, "-s") == 0 || strcmp(k, "--source") == 0 ) { |
---|
405 | s_drv = argv[++i]; |
---|
406 | } else if ( strcmp(k, "-S") == 0 ) { |
---|
407 | s_dev = argv[++i]; |
---|
408 | } else if ( strcmp(k, "-sO") == 0 ) { |
---|
409 | s_opt = argv[++i]; |
---|
410 | } else if ( strcmp(k, "-sC") == 0 ) { |
---|
411 | s_con = argv[++i]; |
---|
412 | } else if ( strcmp(k, "-sP") == 0 ) { |
---|
413 | s_prim = 1; |
---|
414 | } else if ( strcmp(k, "-sN") == 0 ) { |
---|
415 | if ( sources_add(s_drv, s_dev, s_con, s_opt, s_prim) == -1 ) { |
---|
416 | ROAR_ERR("main(*): adding source '%s' via '%s' failed!", s_dev, s_drv); |
---|
417 | } |
---|
418 | s_opt = s_dev = s_con = NULL; |
---|
419 | s_drv = "cf"; |
---|
420 | s_prim = 0; |
---|
421 | |
---|
422 | } else if ( strcmp(k, "-p") == 0 || strcmp(k, "--port") == 0 ) { |
---|
423 | // This is only usefull in INET not UNIX mode. |
---|
424 | if ( *server == '/' ) |
---|
425 | server = ROAR_DEFAULT_HOST; |
---|
426 | |
---|
427 | errno = 0; |
---|
428 | if ( (port = atoi(argv[++i])) < 1 ) { |
---|
429 | if ( (serv = getservbyname(argv[i], "tcp")) == NULL ) { |
---|
430 | ROAR_ERR("Unknown service: %s: %s", argv[i], strerror(errno)); |
---|
431 | return 1; |
---|
432 | } |
---|
433 | // NOTE: we need to use ROAR_NET2HOST16() here even if s_port is of type int! |
---|
434 | ROAR_DBG("main(*): serv = {s_name='%s', s_aliases={...}, s_port=%i, s_proto='%s'}", |
---|
435 | serv->s_name, ROAR_NET2HOST16(serv->s_port), serv->s_proto); |
---|
436 | port = ROAR_NET2HOST16(serv->s_port); |
---|
437 | } |
---|
438 | } else if ( strcmp(k, "-b") == 0 || strcmp(k, "--bind") == 0 || strcmp(k, "--sock") == 0 ) { |
---|
439 | server = argv[++i]; |
---|
440 | |
---|
441 | } else if ( strcmp(k, "-t") == 0 || strcmp(k, "--tcp") == 0 ) { |
---|
442 | if ( sock_type != ROAR_SOCKET_TYPE_TCP && sock_type != ROAR_SOCKET_TYPE_TCP6 ) |
---|
443 | sock_type = ROAR_SOCKET_TYPE_TCP; |
---|
444 | |
---|
445 | if ( *server == '/' ) |
---|
446 | server = ROAR_DEFAULT_HOST; |
---|
447 | |
---|
448 | } else if ( strcmp(k, "-4") == 0 ) { |
---|
449 | sock_type = ROAR_SOCKET_TYPE_TCP; |
---|
450 | if ( *server == '/' ) |
---|
451 | server = ROAR_DEFAULT_HOST; |
---|
452 | } else if ( strcmp(k, "-6") == 0 ) { |
---|
453 | #ifdef PF_INET6 |
---|
454 | sock_type = ROAR_SOCKET_TYPE_TCP6; |
---|
455 | if ( *server == '/' ) |
---|
456 | server = ROAR_DEFAULT_HOST; |
---|
457 | #else |
---|
458 | ROAR_ERR("No IPv6 support compiled in!"); |
---|
459 | return 1; |
---|
460 | #endif |
---|
461 | |
---|
462 | } else if ( strcmp(k, "-u") == 0 || strcmp(k, "--unix") == 0 ) { |
---|
463 | // ignore this case as it is the default behavor. |
---|
464 | sock_type = ROAR_SOCKET_TYPE_UNIX; |
---|
465 | |
---|
466 | } else if ( strcmp(k, "-n") == 0 || strcmp(k, "--decnet") == 0 ) { |
---|
467 | #ifdef ROAR_HAVE_LIBDNET |
---|
468 | port = ROAR_DEFAULT_NUM; |
---|
469 | strcpy(decnethost, ROAR_DEFAULT_LISTEN_OBJECT); |
---|
470 | server = decnethost; |
---|
471 | sock_type = ROAR_SOCKET_TYPE_DECNET; |
---|
472 | #else |
---|
473 | ROAR_ERR("No DECnet support compiled in!"); |
---|
474 | return 1; |
---|
475 | #endif |
---|
476 | |
---|
477 | } else if ( strcmp(k, "-G") == 0 ) { |
---|
478 | sock_grp = argv[++i]; |
---|
479 | } else if ( strcmp(k, "-U") == 0 ) { |
---|
480 | sock_user = argv[++i]; |
---|
481 | |
---|
482 | } else if ( strcmp(k, "--no-listen") == 0 ) { |
---|
483 | *server = 0; |
---|
484 | g_terminate = 1; |
---|
485 | g_no_listen = 1; |
---|
486 | } else if ( strcmp(k, "--client-fh") == 0 ) { |
---|
487 | if ( clients_set_fh(clients_new(), atoi(argv[++i])) == -1 ) { |
---|
488 | ROAR_ERR("main(*): Can not set client's fh"); |
---|
489 | return 1; |
---|
490 | } |
---|
491 | } else if ( strcmp(k, "--close-fh") == 0 ) { |
---|
492 | close(atoi(argv[++i])); |
---|
493 | |
---|
494 | } else if ( strcmp(k, "--standby") == 0 ) { |
---|
495 | g_standby = 1; |
---|
496 | } else if ( strcmp(k, "--auto-standby") == 0 ) { |
---|
497 | g_autostandby = 1; |
---|
498 | } else { |
---|
499 | usage(); |
---|
500 | return 1; |
---|
501 | } |
---|
502 | |
---|
503 | } |
---|
504 | |
---|
505 | if ( s_dev != NULL ) { |
---|
506 | if ( sources_add(s_drv, s_dev, s_con, s_opt, s_prim) == -1 ) { |
---|
507 | ROAR_ERR("main(*): adding source '%s' via '%s' failed!", s_dev, s_drv); |
---|
508 | } |
---|
509 | } |
---|
510 | |
---|
511 | add_output(o_drv, o_dev, o_opts, o_prim, o_count); |
---|
512 | |
---|
513 | ROAR_DBG("Server config: rate=%i, bits=%i, chans=%i", sa.rate, sa.bits, sa.channels); |
---|
514 | |
---|
515 | if ( midi_init() == -1 ) |
---|
516 | ROAR_ERR("Can not initialize MIDI subsystem"); |
---|
517 | |
---|
518 | if ( *server != 0 ) { |
---|
519 | if ( (g_listen_socket = roar_socket_listen(sock_type, server, port)) == -1 ) { |
---|
520 | if ( *server == '/' ) { |
---|
521 | if ( (env_roar_proxy_backup = getenv("ROAR_PROXY")) != NULL ) { |
---|
522 | env_roar_proxy_backup = strdup(env_roar_proxy_backup); |
---|
523 | unsetenv("ROAR_PROXY"); |
---|
524 | } |
---|
525 | if ( (i = roar_socket_connect(server, port)) != -1 ) { |
---|
526 | close(i); |
---|
527 | ROAR_ERR("Can not open listen socket!"); |
---|
528 | return 1; |
---|
529 | } else { |
---|
530 | unlink(server); |
---|
531 | if ( (g_listen_socket = roar_socket_listen(sock_type, server, port)) == -1 ) { |
---|
532 | ROAR_ERR("Can not open listen socket!"); |
---|
533 | return 1; |
---|
534 | } |
---|
535 | } |
---|
536 | if ( env_roar_proxy_backup != NULL ) { |
---|
537 | setenv("ROAR_PROXY", env_roar_proxy_backup, 0); |
---|
538 | free(env_roar_proxy_backup); |
---|
539 | } |
---|
540 | } else { |
---|
541 | ROAR_ERR("Can not open listen socket!"); |
---|
542 | return 1; |
---|
543 | } |
---|
544 | } |
---|
545 | |
---|
546 | if ( (grp = getgrnam(sock_grp)) == NULL ) { |
---|
547 | ROAR_ERR("Can not get GID for group %s: %s", sock_grp, strerror(errno)); |
---|
548 | } |
---|
549 | if ( sock_user || (setids & R_SETUID) ) { |
---|
550 | if ( (pwd = getpwnam(sock_user)) == NULL ) { |
---|
551 | ROAR_ERR("Can not get UID for user %s: %s", sock_user, strerror(errno)); |
---|
552 | } |
---|
553 | } |
---|
554 | |
---|
555 | if ( *server == '/' ) { |
---|
556 | if ( grp ) { |
---|
557 | if ( pwd ) { |
---|
558 | chown(server, pwd->pw_uid, grp->gr_gid); |
---|
559 | } else { |
---|
560 | chown(server, -1, grp->gr_gid); |
---|
561 | } |
---|
562 | if ( getuid() == 0 ) |
---|
563 | chmod(server, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP); |
---|
564 | } |
---|
565 | } |
---|
566 | } |
---|
567 | |
---|
568 | if ( output_buffer_init(&sa) == -1 ) { |
---|
569 | ROAR_ERR("Can not init output buffer!"); |
---|
570 | return 1; |
---|
571 | } |
---|
572 | |
---|
573 | if ( driver == NULL ) { |
---|
574 | driver = "null"; |
---|
575 | } else { |
---|
576 | ROAR_WARN("Usage of old driver interface. use -o not -d!"); |
---|
577 | } |
---|
578 | |
---|
579 | if ( driver_open(&drvinst, &drvid, driver, device, &sa) == -1 ) { |
---|
580 | ROAR_ERR("Can not open output driver!"); |
---|
581 | return 1; |
---|
582 | } |
---|
583 | |
---|
584 | if ( samples_init() == -1 ) { |
---|
585 | ROAR_ERR("Can not init samples!"); |
---|
586 | return 1; |
---|
587 | } |
---|
588 | |
---|
589 | |
---|
590 | signal(SIGINT, on_sig_int); |
---|
591 | signal(SIGCHLD, on_sig_chld); |
---|
592 | signal(SIGPIPE, SIG_IGN); // ignore broken pipes |
---|
593 | |
---|
594 | if ( realtime ) { |
---|
595 | #ifdef DEBUG |
---|
596 | ROAR_WARN("compiled with -DDEBUG but realtime is enabled: for real realtime support compiel without -DDEBUG"); |
---|
597 | #endif |
---|
598 | |
---|
599 | errno = 0; |
---|
600 | nice(-5*realtime); // -5 for each --realtime |
---|
601 | if ( errno ) |
---|
602 | ROAR_WARN("Can not decrease nice value by 5: %s", strerror(errno)); |
---|
603 | /* |
---|
604 | #ifdef __linux__ |
---|
605 | if ( ioprio_set(IOPRIO_WHO_PROCESS, getpid(), IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 0)) == -1 ) |
---|
606 | ROAR_WARN("Can not set io priority: %s", strerror(errno)); |
---|
607 | #endif |
---|
608 | */ |
---|
609 | } |
---|
610 | |
---|
611 | if ( setids & R_SETGID ) { |
---|
612 | if ( setgroups(0, (const gid_t *) NULL) == -1 ) { |
---|
613 | ROAR_ERR("Can not clear supplementary group IDs: %s", strerror(errno)); |
---|
614 | } |
---|
615 | if ( !grp || setgid(grp->gr_gid) == -1 ) { |
---|
616 | ROAR_ERR("Can not set GroupID: %s", strerror(errno)); |
---|
617 | } |
---|
618 | } |
---|
619 | |
---|
620 | |
---|
621 | clients_set_pid(g_self_client, getpid()); |
---|
622 | clients_set_uid(g_self_client, getuid()); |
---|
623 | clients_set_gid(g_self_client, getgid()); |
---|
624 | clients_get(g_self_client, &self); |
---|
625 | |
---|
626 | if ( self == NULL ) { |
---|
627 | ROAR_ERR("Can not get self client!"); |
---|
628 | return 1; |
---|
629 | } |
---|
630 | |
---|
631 | strcpy(self->name, "RoarAudio daemon internal"); |
---|
632 | |
---|
633 | if ( daemon ) { |
---|
634 | close(ROAR_STDIN ); |
---|
635 | close(ROAR_STDOUT); |
---|
636 | close(ROAR_STDERR); |
---|
637 | setsid(); |
---|
638 | if ( fork() ) |
---|
639 | _exit(0); |
---|
640 | clients_set_pid(g_self_client, getpid()); // reset pid as it changed |
---|
641 | } |
---|
642 | |
---|
643 | if (chrootdir) { |
---|
644 | if ( chroot(chrootdir) == -1 ) { |
---|
645 | ROAR_ERR("Can not chroot to %s: %s", chrootdir, strerror(errno)); |
---|
646 | return 2; |
---|
647 | } |
---|
648 | if ( chdir("/") == -1 ) { |
---|
649 | ROAR_ERR("Can not chdir to /: %s", strerror(errno)); |
---|
650 | return 2; |
---|
651 | } |
---|
652 | } |
---|
653 | |
---|
654 | if ( setids & R_SETUID ) { |
---|
655 | if ( !pwd || setuid(pwd->pw_uid) == -1 ) { |
---|
656 | ROAR_ERR("Can not set UserID: %s", strerror(errno)); |
---|
657 | return 3; |
---|
658 | } |
---|
659 | clients_set_uid(g_self_client, getuid()); |
---|
660 | } |
---|
661 | |
---|
662 | // start main loop... |
---|
663 | main_loop(drvid, drvinst, &sa, sysclocksync); |
---|
664 | |
---|
665 | // clean up. |
---|
666 | clean_quit_prep(); |
---|
667 | driver_close(drvinst, drvid); |
---|
668 | output_buffer_free(); |
---|
669 | |
---|
670 | return 0; |
---|
671 | } |
---|
672 | |
---|
673 | void cleanup_listen_socket (int terminate) { |
---|
674 | |
---|
675 | if ( g_listen_socket != -1 ) { |
---|
676 | close(g_listen_socket); |
---|
677 | |
---|
678 | g_listen_socket = -1; |
---|
679 | |
---|
680 | if ( *server == '/' ) |
---|
681 | unlink(server); |
---|
682 | } |
---|
683 | |
---|
684 | if ( terminate ) |
---|
685 | g_terminate = 1; |
---|
686 | } |
---|
687 | |
---|
688 | void clean_quit_prep (void) { |
---|
689 | cleanup_listen_socket(0); |
---|
690 | |
---|
691 | sources_free(); |
---|
692 | streams_free(); |
---|
693 | clients_free(); |
---|
694 | midi_cb_stop(); // stop console beep |
---|
695 | midi_free(); |
---|
696 | } |
---|
697 | |
---|
698 | void clean_quit (void) { |
---|
699 | clean_quit_prep(); |
---|
700 | // driver_close(drvinst, drvid); |
---|
701 | // output_buffer_free(); |
---|
702 | exit(0); |
---|
703 | } |
---|
704 | |
---|
705 | //ll |
---|