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 | #ifdef ROAR_SUPPORT_LISTEN |
---|
28 | char * server = ROAR_DEFAULT_SOCK_GLOBAL; // global server address |
---|
29 | #endif |
---|
30 | |
---|
31 | #ifdef ROAR_HAVE_MAIN_ARGS |
---|
32 | void usage (void) { |
---|
33 | printf("Usage: roard [OPTIONS]...\n\n"); |
---|
34 | |
---|
35 | printf("Misc Options:\n\n"); |
---|
36 | printf( |
---|
37 | " --daemon - Bring the server into background after init\n" |
---|
38 | " --terminate - Terminate after last client quited\n" |
---|
39 | " --restart - Trys to stop an old instance and start a new with new settings\n" |
---|
40 | " --realtime - Trys to get realtime priority,\n" |
---|
41 | " give multible times for being more realtime\n" |
---|
42 | " --chroot DIR - chroots to the given dir\n" |
---|
43 | " --setgid - GroupID to the audio group as specified via -G\n" |
---|
44 | " --setuid - UserID to the audio user as specified via -U\n" |
---|
45 | " --sysclocksync - calculate exact sample rate using the system clock\n" |
---|
46 | ); |
---|
47 | |
---|
48 | printf("\nAudio Options:\n\n"); |
---|
49 | printf( |
---|
50 | " -R --rate RATE - Set server rate\n" |
---|
51 | " -B --bits BITS - Set server bits\n" |
---|
52 | " -C --chans CHANNELS - Set server channels\n" |
---|
53 | ); |
---|
54 | |
---|
55 | printf("\nDriver Options:\n\n"); |
---|
56 | printf(" -d --driver DRV - Set the driver (default: %s)\n", ROAR_DRIVER_DEFAULT); |
---|
57 | printf(" -D --device DEV - Set the device\n"); |
---|
58 | printf(" -dO OPTS - Set output options\n"); |
---|
59 | printf(" --list-driver - List all drivers\n"); |
---|
60 | |
---|
61 | printf("\nOutput Options:\n\n"); |
---|
62 | printf(" -o --odriver DRV - Set the driver, use '--list-driver' to get a list\n"); |
---|
63 | printf(" -O --odevice DEV - Set the device\n"); |
---|
64 | printf(" -oO OPTS - Set output options\n"); |
---|
65 | printf(" -oN - Adds another output\n"); |
---|
66 | printf(" -oP - Mark output as primary\n"); |
---|
67 | |
---|
68 | printf("\nSource Options:\n\n"); |
---|
69 | printf(" -s --source DRV - Use DRV as input driver\n" |
---|
70 | " -S DEV - Use DEV as input device\n" |
---|
71 | " -sO OPTS - Use OPTS as input options\n" |
---|
72 | " -sN - Adds another source\n" |
---|
73 | " -sP - Make souce as primary\n" |
---|
74 | ); |
---|
75 | |
---|
76 | printf("\nCodec Filter Options:\n\n"); |
---|
77 | printf(" --list-cf - List all codec filter\n" |
---|
78 | ); |
---|
79 | |
---|
80 | printf("\nServer Options:\n\n"); |
---|
81 | printf(" -t --tcp - Use TCP listen socket\n" |
---|
82 | " -u --unix - Use UNIX Domain listen socket (default)\n" |
---|
83 | #ifdef ROAR_HAVE_LIBDNET |
---|
84 | " -n --decnet - use DECnet listen socket\n" |
---|
85 | #endif |
---|
86 | " -4 - Use IPv4 connections (implies -t)\n" |
---|
87 | #ifdef PF_INET6 |
---|
88 | " -6 - Use IPv6 connections (implies -t)\n" |
---|
89 | #endif |
---|
90 | #ifdef IPV6_ADDRFORM |
---|
91 | " -64 - Try to downgrade sockets from IPv6 into IPv4,\n" |
---|
92 | " this is normaly not usefull.\n" |
---|
93 | #endif |
---|
94 | " -p --port - TCP Port to bind to\n" |
---|
95 | " -b --bind - IP/Hostname to bind to\n" |
---|
96 | " --sock - Filename for UNIX Domain Socket\n" |
---|
97 | " -G GROUP - Sets the group for the UNIX Domain Socket, (default: %s)\n" |
---|
98 | " You need the permittions to change the GID\n" |
---|
99 | " -U USER - Sets the user for the UNIX Domain Socket, (default: do not set)\n" |
---|
100 | " You need the permittions to change the UID (normaly only root has)\n" |
---|
101 | " --no-listen - Do not listen for new clients\n" |
---|
102 | " (only usefull for relaing, impleys --terminate)\n" |
---|
103 | " --client-fh - Comunicate with a client over this handle\n" |
---|
104 | " (only usefull for relaing)\n" |
---|
105 | " --close-fh - Closes the given fh\n" |
---|
106 | " --standby - Start in standby state\n" |
---|
107 | " --auto-standby - Automatical goes into standby if there are no streams\n", |
---|
108 | ROAR_DEFAULT_SOCKGRP |
---|
109 | ); |
---|
110 | // printf("\n Options:\n\n"); |
---|
111 | printf("\n"); |
---|
112 | } |
---|
113 | #endif |
---|
114 | |
---|
115 | int restart_server (char * server) { |
---|
116 | struct roar_connection con; |
---|
117 | if ( roar_connect(&con, server) == -1 ) { |
---|
118 | return -1; |
---|
119 | } |
---|
120 | |
---|
121 | if ( roar_terminate(&con, 1) == -1 ) { |
---|
122 | return -1; |
---|
123 | } |
---|
124 | |
---|
125 | return roar_disconnect(&con); |
---|
126 | } |
---|
127 | |
---|
128 | #define R_SETUID 1 |
---|
129 | #define R_SETGID 2 |
---|
130 | |
---|
131 | int init_config (void) { |
---|
132 | int i; |
---|
133 | |
---|
134 | memset(g_config, 0, sizeof(struct roard_config)); |
---|
135 | |
---|
136 | for (i = 0; i < ROAR_DIR_DIRIDS; i++) { |
---|
137 | g_config->streams[i].mixer_channels = 1; |
---|
138 | g_config->streams[i].mixer.rpg_mul = 1; |
---|
139 | g_config->streams[i].mixer.rpg_div = 1; |
---|
140 | g_config->streams[i].mixer.scale = 65535; |
---|
141 | g_config->streams[i].mixer.mixer[0] = g_config->streams[i].mixer.scale; |
---|
142 | } |
---|
143 | |
---|
144 | g_config->streams[ROAR_DIR_MIDI_OUT].flags = ROAR_FLAG_SYNC; |
---|
145 | |
---|
146 | return 0; |
---|
147 | } |
---|
148 | |
---|
149 | int add_output (char * drv, char * dev, char * opts, int prim, int count) { |
---|
150 | int stream; |
---|
151 | struct roar_stream * s; |
---|
152 | struct roar_stream_server * ss; |
---|
153 | char * k, * v; |
---|
154 | #ifdef ROAR_DRIVER_CODEC |
---|
155 | char * to_free = NULL; |
---|
156 | #endif |
---|
157 | int codec; |
---|
158 | int sync = 0; |
---|
159 | int32_t blocks = -1, blocksize = -1; |
---|
160 | |
---|
161 | ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = ?", drv, dev, opts); |
---|
162 | |
---|
163 | if ( drv == NULL && count == 0 ) { |
---|
164 | drv = ROAR_DRIVER_DEFAULT; |
---|
165 | prim = 1; |
---|
166 | sync = 1; |
---|
167 | |
---|
168 | #ifdef ROAR_DRIVER_CODEC |
---|
169 | if ( opts == NULL ) { |
---|
170 | opts = to_free = strdup("codec=" ROAR_DRIVER_CODEC); |
---|
171 | } |
---|
172 | #endif |
---|
173 | } |
---|
174 | |
---|
175 | if ( opts == NULL && count == 0 ) { |
---|
176 | sync = 1; |
---|
177 | prim = 1; // if ( prim == 0 ) prim = 1; -> prim allways = 1 |
---|
178 | } |
---|
179 | |
---|
180 | if ( (stream = streams_new()) == -1 ) { |
---|
181 | ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = -1", drv, dev, opts); |
---|
182 | if ( prim ) alive = 0; |
---|
183 | return -1; |
---|
184 | } |
---|
185 | |
---|
186 | streams_get(stream, &ss); |
---|
187 | s = ROAR_STREAM(ss); |
---|
188 | |
---|
189 | memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info)); |
---|
190 | |
---|
191 | if ( streams_set_dir(stream, ROAR_DIR_OUTPUT, 1) == -1 ) { |
---|
192 | streams_delete(stream); |
---|
193 | return -1; |
---|
194 | } |
---|
195 | s->pos_rel_id = -1; |
---|
196 | // s->info.codec = codec; |
---|
197 | |
---|
198 | codec = s->info.codec; |
---|
199 | |
---|
200 | k = strtok(opts, ","); |
---|
201 | while (k != NULL) { |
---|
202 | // ROAR_WARN("add_output(*): opts: %s", k); |
---|
203 | |
---|
204 | if ( (v = strstr(k, "=")) != NULL ) { |
---|
205 | *v++ = 0; |
---|
206 | } |
---|
207 | |
---|
208 | ROAR_DBG("add_output(*): opts: k='%s', v='%s'", k, v); |
---|
209 | if ( strcmp(k, "rate") == 0 ) { |
---|
210 | s->info.rate = atoi(v); |
---|
211 | } else if ( strcmp(k, "channels") == 0 ) { |
---|
212 | s->info.channels = atoi(v); |
---|
213 | } else if ( strcmp(k, "bits") == 0 ) { |
---|
214 | s->info.bits = atoi(v); |
---|
215 | } else if ( strcmp(k, "codec") == 0 ) { |
---|
216 | if ( (codec = roar_str2codec(v)) == -1 ) { |
---|
217 | ROAR_ERR("add_output(*): unknown codec '%s'", v); |
---|
218 | streams_delete(stream); |
---|
219 | if ( prim ) alive = 0; |
---|
220 | #ifdef ROAR_DRIVER_CODEC |
---|
221 | if ( to_free != NULL ) |
---|
222 | free(to_free); |
---|
223 | #endif |
---|
224 | return -1; |
---|
225 | } |
---|
226 | } else if ( strcmp(k, "blocks") == 0 ) { |
---|
227 | blocks = atoi(v); |
---|
228 | } else if ( strcmp(k, "blocksize") == 0 ) { |
---|
229 | blocksize = atoi(v); |
---|
230 | } else if ( strcmp(k, "meta") == 0 ) { |
---|
231 | streams_set_flag(stream, ROAR_FLAG_META); |
---|
232 | } else if ( strcmp(k, "sync") == 0 ) { |
---|
233 | sync = 1; |
---|
234 | } else if ( strcmp(k, "primary") == 0 ) { |
---|
235 | prim = 1; |
---|
236 | |
---|
237 | } else if ( strcmp(k, "cleanmeta") == 0 ) { |
---|
238 | streams_set_flag(stream, ROAR_FLAG_CLEANMETA); |
---|
239 | } else if ( strcmp(k, "autoconf") == 0 ) { |
---|
240 | streams_set_flag(stream, ROAR_FLAG_AUTOCONF); |
---|
241 | } else { |
---|
242 | ROAR_ERR("add_output(*): unknown option '%s'", k); |
---|
243 | streams_delete(stream); |
---|
244 | if ( prim ) alive = 0; |
---|
245 | #ifdef ROAR_DRIVER_CODEC |
---|
246 | if ( to_free != NULL ) |
---|
247 | free(to_free); |
---|
248 | #endif |
---|
249 | return -1; |
---|
250 | } |
---|
251 | |
---|
252 | k = strtok(NULL, ","); |
---|
253 | } |
---|
254 | |
---|
255 | #ifdef ROAR_DRIVER_CODEC |
---|
256 | if ( to_free != NULL ) |
---|
257 | free(to_free); |
---|
258 | #endif |
---|
259 | |
---|
260 | if ( codec == ROAR_CODEC_ALAW || codec == ROAR_CODEC_MULAW ) |
---|
261 | s->info.bits = 8; // needed to open OSS driver, will be overriden by codecfilter |
---|
262 | |
---|
263 | s->info.codec = codec; |
---|
264 | ROAR_STREAM_SERVER(s)->codec_orgi = codec; |
---|
265 | |
---|
266 | if ( driver_openvio(&(ss->vio), &(ss->driver_id), drv, dev, &(s->info), -1) == -1 ) { |
---|
267 | ss->driver_id = -1; // don't close a driver not opened... |
---|
268 | memset(&(ss->vio), 0, sizeof(struct roar_vio_calls)); |
---|
269 | streams_delete(stream); |
---|
270 | ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = -1", drv, dev, opts); |
---|
271 | if ( prim ) alive = 0; |
---|
272 | return -1; |
---|
273 | } |
---|
274 | |
---|
275 | roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_SSTREAMID, &stream); // ignore errors here |
---|
276 | roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_SSTREAM, s); // ignore errors here |
---|
277 | |
---|
278 | if ( blocks != -1 ) |
---|
279 | roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DBLOCKS, &blocks); |
---|
280 | |
---|
281 | if ( blocksize != -1 ) |
---|
282 | roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DBLKSIZE, &blocksize); |
---|
283 | |
---|
284 | ROAR_DBG("add_output(*): ss->driver_id=%i", ss->driver_id); |
---|
285 | |
---|
286 | streams_set_fh(stream, -1); // update some internal structures |
---|
287 | |
---|
288 | client_stream_add(g_source_client, stream); |
---|
289 | |
---|
290 | if ( prim ) { |
---|
291 | streams_mark_primary(stream); |
---|
292 | s->pos_rel_id = stream; |
---|
293 | } |
---|
294 | |
---|
295 | if ( sync ) { |
---|
296 | streams_set_flag(stream, ROAR_FLAG_SYNC); |
---|
297 | } else { |
---|
298 | streams_reset_flag(stream, ROAR_FLAG_SYNC); |
---|
299 | } |
---|
300 | |
---|
301 | return 0; |
---|
302 | } |
---|
303 | |
---|
304 | #ifdef ROAR_HAVE_MAIN_ARGS |
---|
305 | int main (int argc, char * argv[]) { |
---|
306 | #else |
---|
307 | int main (void) { |
---|
308 | #endif |
---|
309 | #ifdef ROAR_HAVE_MAIN_ARGS |
---|
310 | int i; |
---|
311 | char * k; |
---|
312 | #endif |
---|
313 | #ifdef ROAR_SUPPORT_LISTEN |
---|
314 | char user_sock[80] = {0}; |
---|
315 | #endif |
---|
316 | struct roar_audio_info sa, max_sa; |
---|
317 | struct roard_config config; |
---|
318 | #ifdef ROAR_HAVE_FORK |
---|
319 | int daemon = 0; |
---|
320 | #endif |
---|
321 | int realtime = 0; |
---|
322 | int sysclocksync = 0; |
---|
323 | char * driver = NULL; |
---|
324 | char * device = NULL; |
---|
325 | #ifdef ROAR_HAVE_MAIN_ARGS |
---|
326 | char * opts = NULL; |
---|
327 | #endif |
---|
328 | // char * server = ROAR_DEFAULT_SOCK_GLOBAL; |
---|
329 | #ifdef ROAR_SUPPORT_LISTEN |
---|
330 | int port = ROAR_DEFAULT_PORT; |
---|
331 | #endif |
---|
332 | int drvid; |
---|
333 | char * s_drv = "cf"; |
---|
334 | char * s_dev = NULL; |
---|
335 | char * s_con = NULL; |
---|
336 | char * s_opt = NULL; |
---|
337 | int s_prim = 0; |
---|
338 | char * o_drv = getenv("ROAR_DRIVER"); |
---|
339 | char * o_dev = getenv("ROAR_DEVICE"); |
---|
340 | char * o_opts = NULL; |
---|
341 | int o_prim = 0; |
---|
342 | int o_count = 0; |
---|
343 | int light_channels = 512; |
---|
344 | char * sock_grp = ROAR_DEFAULT_SOCKGRP; |
---|
345 | char * sock_user = NULL; |
---|
346 | #ifdef ROAR_SUPPORT_LISTEN |
---|
347 | int sock_type = ROAR_SOCKET_TYPE_UNKNOWN; |
---|
348 | #endif |
---|
349 | #ifdef ROAR_HAVE_CHROOT |
---|
350 | char * chrootdir = NULL; |
---|
351 | #endif |
---|
352 | #if defined(ROAR_HAVE_SETGID) || defined(ROAR_HAVE_SETUID) |
---|
353 | int setids = 0; |
---|
354 | #endif |
---|
355 | #ifdef ROAR_HAVE_UNIX |
---|
356 | char * env_roar_proxy_backup; |
---|
357 | #endif |
---|
358 | #if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX) |
---|
359 | struct group * grp = NULL; |
---|
360 | #endif |
---|
361 | #if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX) |
---|
362 | struct passwd * pwd = NULL; |
---|
363 | #endif |
---|
364 | #ifdef ROAR_HAVE_GETSERVBYNAME |
---|
365 | struct servent * serv = NULL; |
---|
366 | #endif |
---|
367 | DRIVER_USERDATA_T drvinst; |
---|
368 | struct roar_client * self = NULL; |
---|
369 | #ifdef ROAR_HAVE_LIBDNET |
---|
370 | char decnethost[80]; |
---|
371 | #endif |
---|
372 | |
---|
373 | g_standby = 0; |
---|
374 | g_autostandby = 0; |
---|
375 | alive = 1; |
---|
376 | #ifdef ROAR_SUPPORT_LISTEN |
---|
377 | g_no_listen = 0; |
---|
378 | g_listen_socket = -1; |
---|
379 | #else |
---|
380 | g_terminate = 1; |
---|
381 | #endif |
---|
382 | |
---|
383 | sa.bits = ROAR_BITS_DEFAULT; |
---|
384 | sa.channels = ROAR_CHANNELS_DEFAULT; |
---|
385 | sa.rate = ROAR_RATE_DEFAULT; |
---|
386 | sa.codec = ROAR_CODEC_DEFAULT; |
---|
387 | |
---|
388 | g_sa = &sa; |
---|
389 | g_max_sa = &max_sa; |
---|
390 | |
---|
391 | memcpy(g_max_sa, g_sa, sizeof(max_sa)); |
---|
392 | |
---|
393 | g_config = &config; |
---|
394 | |
---|
395 | if ( init_config() == -1 ) { |
---|
396 | ROAR_ERR("Can not init default config!"); |
---|
397 | return 1; |
---|
398 | } |
---|
399 | |
---|
400 | |
---|
401 | #ifdef ROAR_SUPPORT_LISTEN |
---|
402 | #ifdef ROAR_HAVE_GETUID |
---|
403 | if ( getuid() != 0 && getenv("HOME") != NULL ) { |
---|
404 | snprintf(user_sock, 79, "%s/%s", (char*)getenv("HOME"), ROAR_DEFAULT_SOCK_USER); |
---|
405 | server = user_sock; |
---|
406 | } |
---|
407 | #endif |
---|
408 | |
---|
409 | if ( getenv("ROAR_SERVER") != NULL ) |
---|
410 | server = getenv("ROAR_SERVER"); |
---|
411 | #endif |
---|
412 | |
---|
413 | if ( clients_init() == -1 ) { |
---|
414 | ROAR_ERR("Can not init clients!"); |
---|
415 | return 1; |
---|
416 | } |
---|
417 | |
---|
418 | if ( streams_init() == -1 ) { |
---|
419 | ROAR_ERR("Can not init streams!"); |
---|
420 | return 1; |
---|
421 | } |
---|
422 | |
---|
423 | if ( (g_self_client = clients_new()) == -1 ) { |
---|
424 | ROAR_ERR("Can not create self client!"); |
---|
425 | return 1; |
---|
426 | } |
---|
427 | |
---|
428 | if ( sources_init() == -1 ) { |
---|
429 | ROAR_ERR("Can not init sources!"); |
---|
430 | return 1; |
---|
431 | } |
---|
432 | |
---|
433 | if ( (sources_set_client(g_self_client)) == -1 ) { |
---|
434 | ROAR_ERR("Can not init set source client!"); |
---|
435 | return 1; |
---|
436 | } |
---|
437 | |
---|
438 | #ifdef ROAR_HAVE_MAIN_ARGS |
---|
439 | for (i = 1; i < argc; i++) { |
---|
440 | k = argv[i]; |
---|
441 | |
---|
442 | if ( strcmp(k, "-h") == 0 || strcmp(k, "--help") == 0 ) { |
---|
443 | usage(); |
---|
444 | return 0; |
---|
445 | |
---|
446 | } else if ( strcmp(k, "--restart") == 0 ) { |
---|
447 | #ifdef ROAR_SUPPORT_LISTEN |
---|
448 | if ( restart_server(server) == -1 ) { |
---|
449 | ROAR_WARN("Can not terminate old server (not running at %s?), tring to continue anyway", server); |
---|
450 | } |
---|
451 | #else |
---|
452 | ROAR_ERR("--restart not supported"); |
---|
453 | #endif |
---|
454 | |
---|
455 | } else if ( strcmp(k, "--demon") == 0 || strcmp(k, "--daemon") == 0 ) { |
---|
456 | #ifdef ROAR_HAVE_FORK |
---|
457 | daemon = 1; |
---|
458 | #else |
---|
459 | ROAR_ERR("--daemon not supported"); |
---|
460 | #endif |
---|
461 | } else if ( strcmp(k, "--terminate") == 0 ) { |
---|
462 | g_terminate = 1; |
---|
463 | } else if ( strcmp(k, "--sysclocksync") == 0 ) { |
---|
464 | sysclocksync = 1000; |
---|
465 | } else if ( strcmp(k, "--realtime") == 0 ) { |
---|
466 | realtime++; |
---|
467 | } else if ( strcmp(k, "--chroot") == 0 ) { |
---|
468 | #ifdef ROAR_HAVE_CHROOT |
---|
469 | chrootdir = argv[++i]; |
---|
470 | #else |
---|
471 | ROAR_ERR("--chroot not supported"); |
---|
472 | i++; |
---|
473 | #endif |
---|
474 | } else if ( strcmp(k, "--setgid") == 0 ) { |
---|
475 | #ifdef ROAR_HAVE_SETGID |
---|
476 | setids |= R_SETGID; |
---|
477 | #else |
---|
478 | ROAR_ERR("--setgid not supported"); |
---|
479 | #endif |
---|
480 | } else if ( strcmp(k, "--setuid") == 0 ) { |
---|
481 | #ifdef ROAR_HAVE_SETUID |
---|
482 | setids |= R_SETUID; |
---|
483 | #else |
---|
484 | ROAR_ERR("--setuid not supported"); |
---|
485 | #endif |
---|
486 | |
---|
487 | } else if ( strcmp(k, "--list-cf") == 0 ) { |
---|
488 | print_codecfilterlist(); |
---|
489 | return 0; |
---|
490 | |
---|
491 | } else if ( strcmp(k, "-R") == 0 || strcmp(k, "--rate") == 0 ) { |
---|
492 | sa.rate = atoi(argv[++i]); |
---|
493 | } else if ( strcmp(k, "-B") == 0 || strcmp(k, "--bits") == 0 ) { |
---|
494 | sa.bits = atoi(argv[++i]); |
---|
495 | } else if ( strcmp(k, "-C") == 0 || strcmp(k, "--chans") == 0 ) { |
---|
496 | sa.channels = atoi(argv[++i]); |
---|
497 | |
---|
498 | } else if ( strcmp(k, "-d") == 0 || strcmp(k, "--driver") == 0 ) { |
---|
499 | driver = argv[++i]; |
---|
500 | if ( strcmp(driver, "list") == 0 ) { |
---|
501 | ROAR_WARN("The option is obsolete, use --list-driver!"); |
---|
502 | print_driverlist(); |
---|
503 | return 0; |
---|
504 | } |
---|
505 | } else if ( strcmp(k, "-D") == 0 || strcmp(k, "--device") == 0 ) { |
---|
506 | device = argv[++i]; |
---|
507 | } else if ( strcmp(k, "-dO") == 0 ) { |
---|
508 | opts = argv[++i]; |
---|
509 | } else if ( strcmp(k, "--list-driver") == 0 ) { |
---|
510 | print_driverlist(); |
---|
511 | return 0; |
---|
512 | |
---|
513 | } else if ( strcmp(k, "-o") == 0 || strcmp(k, "--odriver") == 0 ) { |
---|
514 | o_drv = argv[++i]; |
---|
515 | } else if ( strcmp(k, "-O") == 0 || strcmp(k, "--odevice") == 0 ) { |
---|
516 | o_dev = argv[++i]; |
---|
517 | } else if ( strcmp(k, "-oO") == 0 ) { |
---|
518 | o_opts = argv[++i]; |
---|
519 | } else if ( strcmp(k, "-oP") == 0 ) { |
---|
520 | o_prim = 1; |
---|
521 | } else if ( strcmp(k, "-oN") == 0 ) { |
---|
522 | if ( add_output(o_drv, o_dev, o_opts, o_prim, o_count) != -1 ) |
---|
523 | o_count++; |
---|
524 | |
---|
525 | o_drv = o_dev = o_opts = NULL; |
---|
526 | o_prim = 0; |
---|
527 | |
---|
528 | } else if ( strcmp(k, "-s") == 0 || strcmp(k, "--source") == 0 ) { |
---|
529 | s_drv = argv[++i]; |
---|
530 | } else if ( strcmp(k, "-S") == 0 ) { |
---|
531 | s_dev = argv[++i]; |
---|
532 | } else if ( strcmp(k, "-sO") == 0 ) { |
---|
533 | s_opt = argv[++i]; |
---|
534 | } else if ( strcmp(k, "-sC") == 0 ) { |
---|
535 | s_con = argv[++i]; |
---|
536 | } else if ( strcmp(k, "-sP") == 0 ) { |
---|
537 | s_prim = 1; |
---|
538 | } else if ( strcmp(k, "-sN") == 0 ) { |
---|
539 | if ( sources_add(s_drv, s_dev, s_con, s_opt, s_prim) == -1 ) { |
---|
540 | ROAR_ERR("main(*): adding source '%s' via '%s' failed!", s_dev, s_drv); |
---|
541 | } |
---|
542 | s_opt = s_dev = s_con = NULL; |
---|
543 | s_drv = "cf"; |
---|
544 | s_prim = 0; |
---|
545 | |
---|
546 | } else if ( strcmp(k, "-p") == 0 || strcmp(k, "--port") == 0 ) { |
---|
547 | // This is only usefull in INET not UNIX mode. |
---|
548 | #ifdef ROAR_SUPPORT_LISTEN |
---|
549 | if ( *server == '/' ) |
---|
550 | server = ROAR_DEFAULT_HOST; |
---|
551 | |
---|
552 | errno = 0; |
---|
553 | if ( (port = atoi(argv[++i])) < 1 ) { |
---|
554 | #ifdef ROAR_HAVE_GETSERVBYNAME |
---|
555 | if ( (serv = getservbyname(argv[i], "tcp")) == NULL ) { |
---|
556 | ROAR_ERR("Unknown service: %s: %s", argv[i], strerror(errno)); |
---|
557 | return 1; |
---|
558 | } |
---|
559 | // NOTE: we need to use ROAR_NET2HOST16() here even if s_port is of type int! |
---|
560 | ROAR_DBG("main(*): serv = {s_name='%s', s_aliases={...}, s_port=%i, s_proto='%s'}", |
---|
561 | serv->s_name, ROAR_NET2HOST16(serv->s_port), serv->s_proto); |
---|
562 | port = ROAR_NET2HOST16(serv->s_port); |
---|
563 | #else |
---|
564 | ROAR_ERR("invalite port number: %s", argv[i]); |
---|
565 | return 1; |
---|
566 | #endif |
---|
567 | } |
---|
568 | #endif |
---|
569 | } else if ( strcmp(k, "-b") == 0 || strcmp(k, "--bind") == 0 || strcmp(k, "--sock") == 0 ) { |
---|
570 | #ifdef ROAR_SUPPORT_LISTEN |
---|
571 | server = argv[++i]; |
---|
572 | #endif |
---|
573 | |
---|
574 | } else if ( strcmp(k, "-t") == 0 || strcmp(k, "--tcp") == 0 ) { |
---|
575 | #ifdef ROAR_SUPPORT_LISTEN |
---|
576 | if ( sock_type != ROAR_SOCKET_TYPE_TCP && sock_type != ROAR_SOCKET_TYPE_TCP6 ) |
---|
577 | sock_type = ROAR_SOCKET_TYPE_TCP; |
---|
578 | |
---|
579 | if ( *server == '/' ) |
---|
580 | server = ROAR_DEFAULT_HOST; |
---|
581 | #endif |
---|
582 | |
---|
583 | } else if ( strcmp(k, "-4") == 0 ) { |
---|
584 | #ifdef ROAR_SUPPORT_LISTEN |
---|
585 | sock_type = ROAR_SOCKET_TYPE_TCP; |
---|
586 | if ( *server == '/' ) |
---|
587 | server = ROAR_DEFAULT_HOST; |
---|
588 | #endif |
---|
589 | } else if ( strcmp(k, "-6") == 0 ) { |
---|
590 | #ifdef ROAR_SUPPORT_LISTEN |
---|
591 | #ifdef PF_INET6 |
---|
592 | sock_type = ROAR_SOCKET_TYPE_TCP6; |
---|
593 | if ( *server == '/' ) |
---|
594 | server = ROAR_DEFAULT_HOST; |
---|
595 | #else |
---|
596 | ROAR_ERR("No IPv6 support compiled in!"); |
---|
597 | return 1; |
---|
598 | #endif |
---|
599 | #endif |
---|
600 | |
---|
601 | } else if ( strcmp(k, "-u") == 0 || strcmp(k, "--unix") == 0 ) { |
---|
602 | #ifdef ROAR_SUPPORT_LISTEN |
---|
603 | // ignore this case as it is the default behavor. |
---|
604 | sock_type = ROAR_SOCKET_TYPE_UNIX; |
---|
605 | #endif |
---|
606 | |
---|
607 | } else if ( strcmp(k, "-n") == 0 || strcmp(k, "--decnet") == 0 ) { |
---|
608 | #ifdef ROAR_SUPPORT_LISTEN |
---|
609 | #ifdef ROAR_HAVE_LIBDNET |
---|
610 | port = ROAR_DEFAULT_NUM; |
---|
611 | strcpy(decnethost, ROAR_DEFAULT_LISTEN_OBJECT); |
---|
612 | server = decnethost; |
---|
613 | sock_type = ROAR_SOCKET_TYPE_DECNET; |
---|
614 | #else |
---|
615 | ROAR_ERR("No DECnet support compiled in!"); |
---|
616 | return 1; |
---|
617 | #endif |
---|
618 | #endif |
---|
619 | |
---|
620 | } else if ( strcmp(k, "-G") == 0 ) { |
---|
621 | sock_grp = argv[++i]; |
---|
622 | } else if ( strcmp(k, "-U") == 0 ) { |
---|
623 | sock_user = argv[++i]; |
---|
624 | |
---|
625 | } else if ( strcmp(k, "--no-listen") == 0 ) { |
---|
626 | #ifdef ROAR_SUPPORT_LISTEN |
---|
627 | server = ""; |
---|
628 | g_terminate = 1; |
---|
629 | g_no_listen = 1; |
---|
630 | #endif |
---|
631 | } else if ( strcmp(k, "--client-fh") == 0 ) { |
---|
632 | if ( clients_set_fh(clients_new(), atoi(argv[++i])) == -1 ) { |
---|
633 | ROAR_ERR("main(*): Can not set client's fh"); |
---|
634 | return 1; |
---|
635 | } |
---|
636 | } else if ( strcmp(k, "--close-fh") == 0 ) { |
---|
637 | #ifdef ROAR_HAVE_IO_POSIX |
---|
638 | close(atoi(argv[++i])); |
---|
639 | #else |
---|
640 | i++; |
---|
641 | ROAR_WARN("can not close file handle %s (closing not supported)", argv[i]); |
---|
642 | #endif |
---|
643 | |
---|
644 | } else if ( strcmp(k, "--standby") == 0 ) { |
---|
645 | g_standby = 1; |
---|
646 | } else if ( strcmp(k, "--auto-standby") == 0 ) { |
---|
647 | g_autostandby = 1; |
---|
648 | } else { |
---|
649 | usage(); |
---|
650 | return 1; |
---|
651 | } |
---|
652 | |
---|
653 | } |
---|
654 | #endif |
---|
655 | |
---|
656 | if ( s_dev != NULL ) { |
---|
657 | if ( sources_add(s_drv, s_dev, s_con, s_opt, s_prim) == -1 ) { |
---|
658 | ROAR_ERR("main(*): adding source '%s' via '%s' failed!", s_dev, s_drv); |
---|
659 | } |
---|
660 | } |
---|
661 | |
---|
662 | add_output(o_drv, o_dev, o_opts, o_prim, o_count); |
---|
663 | |
---|
664 | ROAR_DBG("Server config: rate=%i, bits=%i, chans=%i", sa.rate, sa.bits, sa.channels); |
---|
665 | |
---|
666 | if ( midi_init() == -1 ) { |
---|
667 | ROAR_ERR("Can not initialize MIDI subsystem"); |
---|
668 | } |
---|
669 | |
---|
670 | if ( light_init(light_channels) == -1 ) { |
---|
671 | ROAR_ERR("Can not initialize light control subsystem"); |
---|
672 | } |
---|
673 | |
---|
674 | #ifdef ROAR_SUPPORT_LISTEN |
---|
675 | if ( *server != 0 ) { |
---|
676 | if ( (g_listen_socket = roar_socket_listen(sock_type, server, port)) == -1 ) { |
---|
677 | #ifdef ROAR_HAVE_UNIX |
---|
678 | if ( *server == '/' ) { |
---|
679 | if ( (env_roar_proxy_backup = getenv("ROAR_PROXY")) != NULL ) { |
---|
680 | env_roar_proxy_backup = strdup(env_roar_proxy_backup); |
---|
681 | unsetenv("ROAR_PROXY"); |
---|
682 | } |
---|
683 | if ( (i = roar_socket_connect(server, port)) != -1 ) { |
---|
684 | close(i); |
---|
685 | ROAR_ERR("Can not open listen socket!"); |
---|
686 | return 1; |
---|
687 | } else { |
---|
688 | unlink(server); |
---|
689 | if ( (g_listen_socket = roar_socket_listen(sock_type, server, port)) == -1 ) { |
---|
690 | ROAR_ERR("Can not open listen socket!"); |
---|
691 | return 1; |
---|
692 | } |
---|
693 | } |
---|
694 | if ( env_roar_proxy_backup != NULL ) { |
---|
695 | setenv("ROAR_PROXY", env_roar_proxy_backup, 0); |
---|
696 | free(env_roar_proxy_backup); |
---|
697 | } |
---|
698 | #else |
---|
699 | if (0) { // noop |
---|
700 | #endif |
---|
701 | } else { |
---|
702 | ROAR_ERR("Can not open listen socket!"); |
---|
703 | return 1; |
---|
704 | } |
---|
705 | } |
---|
706 | |
---|
707 | #if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX) |
---|
708 | if ( (grp = getgrnam(sock_grp)) == NULL ) { |
---|
709 | ROAR_ERR("Can not get GID for group %s: %s", sock_grp, strerror(errno)); |
---|
710 | } |
---|
711 | #endif |
---|
712 | #if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX) |
---|
713 | if ( sock_user || (setids & R_SETUID) ) { |
---|
714 | if ( (pwd = getpwnam(sock_user)) == NULL ) { |
---|
715 | ROAR_ERR("Can not get UID for user %s: %s", sock_user, strerror(errno)); |
---|
716 | } |
---|
717 | } |
---|
718 | #endif |
---|
719 | |
---|
720 | #if defined(ROAR_HAVE_IO_POSIX) && defined(ROAR_HAVE_UNIX) |
---|
721 | if ( *server == '/' ) { |
---|
722 | if ( grp ) { |
---|
723 | if ( pwd ) { |
---|
724 | if ( chown(server, pwd->pw_uid, grp->gr_gid) == -1 ) |
---|
725 | return 1; |
---|
726 | } else { |
---|
727 | if ( chown(server, -1, grp->gr_gid) == -1 ) |
---|
728 | return 1; |
---|
729 | } |
---|
730 | #ifdef ROAR_HAVE_GETUID |
---|
731 | if ( getuid() == 0 ) |
---|
732 | if ( chmod(server, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) == -1 ) |
---|
733 | return 1; |
---|
734 | #endif |
---|
735 | } |
---|
736 | } |
---|
737 | #endif |
---|
738 | } |
---|
739 | #endif |
---|
740 | |
---|
741 | if ( output_buffer_init(&sa) == -1 ) { |
---|
742 | ROAR_ERR("Can not init output buffer!"); |
---|
743 | return 1; |
---|
744 | } |
---|
745 | |
---|
746 | if ( driver == NULL ) { |
---|
747 | driver = "null"; |
---|
748 | } else { |
---|
749 | ROAR_WARN("Usage of old driver interface. use -o not -d!"); |
---|
750 | } |
---|
751 | |
---|
752 | if ( driver_open(&drvinst, &drvid, driver, device, &sa) == -1 ) { |
---|
753 | ROAR_ERR("Can not open output driver!"); |
---|
754 | return 1; |
---|
755 | } |
---|
756 | |
---|
757 | if ( samples_init() == -1 ) { |
---|
758 | ROAR_ERR("Can not init samples!"); |
---|
759 | return 1; |
---|
760 | } |
---|
761 | |
---|
762 | |
---|
763 | // we should handle this on microcontrollers, too. |
---|
764 | #if !defined(ROAR_TARGET_MICROCONTROLLER) && !defined(ROAR_TARGET_WIN32) |
---|
765 | signal(SIGINT, on_sig_int); |
---|
766 | signal(SIGCHLD, on_sig_chld); |
---|
767 | signal(SIGPIPE, SIG_IGN); // ignore broken pipes |
---|
768 | #endif |
---|
769 | |
---|
770 | if ( realtime ) { |
---|
771 | #ifdef DEBUG |
---|
772 | ROAR_WARN("compiled with -DDEBUG but realtime is enabled: for real realtime support compiel without -DDEBUG"); |
---|
773 | #endif |
---|
774 | |
---|
775 | #ifdef ROAR_HAVE_NICE |
---|
776 | errno = 0; |
---|
777 | nice(-5*realtime); // -5 for each --realtime |
---|
778 | if ( errno ) { |
---|
779 | ROAR_WARN("Can not decrease nice value by %i: %s", 5*realtime, strerror(errno)); |
---|
780 | } |
---|
781 | #else |
---|
782 | ROAR_WARN("Can not decrease nice value by %i: %s", 5*realtime, strerror(errno)); |
---|
783 | #endif |
---|
784 | /* |
---|
785 | #ifdef __linux__ |
---|
786 | if ( ioprio_set(IOPRIO_WHO_PROCESS, getpid(), IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 0)) == -1 ) |
---|
787 | ROAR_WARN("Can not set io priority: %s", strerror(errno)); |
---|
788 | #endif |
---|
789 | */ |
---|
790 | } |
---|
791 | |
---|
792 | #ifdef ROAR_HAVE_SETGID |
---|
793 | if ( setids & R_SETGID ) { |
---|
794 | if ( setgroups(0, (const gid_t *) NULL) == -1 ) { |
---|
795 | ROAR_ERR("Can not clear supplementary group IDs: %s", strerror(errno)); |
---|
796 | } |
---|
797 | if ( !grp || setgid(grp->gr_gid) == -1 ) { |
---|
798 | ROAR_ERR("Can not set GroupID: %s", strerror(errno)); |
---|
799 | } |
---|
800 | } |
---|
801 | #endif |
---|
802 | |
---|
803 | |
---|
804 | clients_set_pid(g_self_client, getpid()); |
---|
805 | #ifdef ROAR_HAVE_GETUID |
---|
806 | clients_set_uid(g_self_client, getuid()); |
---|
807 | #endif |
---|
808 | #ifdef ROAR_HAVE_GETGID |
---|
809 | clients_set_gid(g_self_client, getgid()); |
---|
810 | #endif |
---|
811 | clients_get(g_self_client, &self); |
---|
812 | |
---|
813 | if ( self == NULL ) { |
---|
814 | ROAR_ERR("Can not get self client!"); |
---|
815 | return 1; |
---|
816 | } |
---|
817 | |
---|
818 | strcpy(self->name, "RoarAudio daemon internal"); |
---|
819 | |
---|
820 | #ifdef ROAR_HAVE_FORK |
---|
821 | if ( daemon ) { |
---|
822 | close(ROAR_STDIN ); |
---|
823 | close(ROAR_STDOUT); |
---|
824 | close(ROAR_STDERR); |
---|
825 | |
---|
826 | if ( fork() ) |
---|
827 | ROAR_U_EXIT(0); |
---|
828 | |
---|
829 | #ifdef ROAR_HAVE_SETSID |
---|
830 | setsid(); |
---|
831 | #endif |
---|
832 | clients_set_pid(g_self_client, getpid()); // reset pid as it changed |
---|
833 | } |
---|
834 | #endif |
---|
835 | |
---|
836 | #ifdef ROAR_HAVE_CHROOT |
---|
837 | if (chrootdir) { |
---|
838 | if ( chroot(chrootdir) == -1 ) { |
---|
839 | ROAR_ERR("Can not chroot to %s: %s", chrootdir, strerror(errno)); |
---|
840 | return 2; |
---|
841 | } |
---|
842 | if ( chdir("/") == -1 ) { |
---|
843 | ROAR_ERR("Can not chdir to /: %s", strerror(errno)); |
---|
844 | return 2; |
---|
845 | } |
---|
846 | } |
---|
847 | #endif |
---|
848 | |
---|
849 | #ifdef ROAR_HAVE_SETUID |
---|
850 | if ( setids & R_SETUID ) { |
---|
851 | if ( !pwd || setuid(pwd->pw_uid) == -1 ) { |
---|
852 | ROAR_ERR("Can not set UserID: %s", strerror(errno)); |
---|
853 | return 3; |
---|
854 | } |
---|
855 | #ifdef ROAR_HAVE_GETUID |
---|
856 | clients_set_uid(g_self_client, getuid()); |
---|
857 | #endif |
---|
858 | } |
---|
859 | #endif |
---|
860 | |
---|
861 | // start main loop... |
---|
862 | main_loop(drvid, drvinst, &sa, sysclocksync); |
---|
863 | |
---|
864 | // clean up. |
---|
865 | clean_quit_prep(); |
---|
866 | driver_close(drvinst, drvid); |
---|
867 | output_buffer_free(); |
---|
868 | |
---|
869 | return 0; |
---|
870 | } |
---|
871 | |
---|
872 | void cleanup_listen_socket (int terminate) { |
---|
873 | |
---|
874 | #ifdef ROAR_SUPPORT_LISTEN |
---|
875 | if ( g_listen_socket != -1 ) { |
---|
876 | #ifdef ROAR_HAVE_IO_POSIX |
---|
877 | close(g_listen_socket); |
---|
878 | #endif // #else is useless because we are in void context. |
---|
879 | |
---|
880 | g_listen_socket = -1; |
---|
881 | |
---|
882 | #ifdef ROAR_HAVE_UNIX |
---|
883 | if ( *server == '/' ) |
---|
884 | unlink(server); |
---|
885 | #endif |
---|
886 | } |
---|
887 | |
---|
888 | #endif |
---|
889 | |
---|
890 | if ( terminate ) |
---|
891 | g_terminate = 1; |
---|
892 | } |
---|
893 | |
---|
894 | void clean_quit_prep (void) { |
---|
895 | cleanup_listen_socket(0); |
---|
896 | |
---|
897 | sources_free(); |
---|
898 | streams_free(); |
---|
899 | clients_free(); |
---|
900 | midi_cb_stop(); // stop console beep |
---|
901 | midi_free(); |
---|
902 | light_free(); |
---|
903 | } |
---|
904 | |
---|
905 | void clean_quit (void) { |
---|
906 | clean_quit_prep(); |
---|
907 | // driver_close(drvinst, drvid); |
---|
908 | // output_buffer_free(); |
---|
909 | exit(0); |
---|
910 | } |
---|
911 | |
---|
912 | //ll |
---|