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 | #if defined(ROAR_HAVE_IO_POSIX) && defined(ROAR_HAVE_FS_POSIX) |
---|
32 | #define SUPPORT_PIDFILE |
---|
33 | char * pidfile = NULL; |
---|
34 | #endif |
---|
35 | |
---|
36 | #ifdef ROAR_HAVE_MAIN_ARGS |
---|
37 | void usage (void) { |
---|
38 | printf("Usage: roard [OPTIONS]...\n\n"); |
---|
39 | |
---|
40 | printf("Misc Options:\n\n"); |
---|
41 | printf( |
---|
42 | " --daemon - Bring the server into background after init\n" |
---|
43 | " --terminate - Terminate after last client quited\n" |
---|
44 | " --start - No op parameter (starting roard is default operation)\n" |
---|
45 | " --restart - Trys to stop an old instance and start a new with new settings\n" |
---|
46 | " --stop - Stops a running roard (provide --pidfile!)\n" |
---|
47 | " --shutdown - Terminates a running roard (provide --pidfile!)\n" |
---|
48 | " --realtime - Trys to get realtime priority,\n" |
---|
49 | " give multible times for being more realtime\n" |
---|
50 | " --chroot DIR - chroots to the given dir\n" |
---|
51 | " --setgid - GroupID to the audio group as specified via -G\n" |
---|
52 | " --setuid - UserID to the audio user as specified via -U\n" |
---|
53 | " --sysclocksync - calculate exact sample rate using the system clock\n" |
---|
54 | " --location LOC - Set lion readable location of server\n" |
---|
55 | #ifdef SUPPORT_PIDFILE |
---|
56 | " --pidfile PIDFILE - Write a pidfile at PIDFILE\n" |
---|
57 | #endif |
---|
58 | ); |
---|
59 | |
---|
60 | printf("\nAudio Options:\n\n"); |
---|
61 | printf( |
---|
62 | " -R --rate RATE - Set server rate\n" |
---|
63 | " -B --bits BITS - Set server bits\n" |
---|
64 | " -C --chans CHANNELS - Set server channels\n" |
---|
65 | ); |
---|
66 | |
---|
67 | printf("\nStream Options:\n\n"); |
---|
68 | printf( |
---|
69 | " --stream-flags D=F - Set default flags for stream directions\n" |
---|
70 | " D is the stream direction and F is a comma seperated\n" |
---|
71 | " list of flags in form +flag or -flag to set or unset\n" |
---|
72 | " a flag as default or remove it from the default\n" |
---|
73 | ); |
---|
74 | |
---|
75 | printf("\nDriver Options: (obsolete, do not use, Use Ouput Options)\n\n"); |
---|
76 | printf(" -d --driver DRV - Set the driver (default: %s)\n", ROAR_DRIVER_DEFAULT); |
---|
77 | printf(" -D --device DEV - Set the device\n"); |
---|
78 | printf(" -dO OPTS - Set output options\n"); |
---|
79 | printf(" --list-driver - List all drivers\n"); |
---|
80 | |
---|
81 | printf("\nOutput Options:\n\n"); |
---|
82 | printf(" -o --odriver DRV - Set the driver, use '--list-driver' to get a list\n"); |
---|
83 | printf(" -O --odevice DEV - Set the device\n"); |
---|
84 | printf(" -oO OPTS - Set output options\n"); |
---|
85 | printf(" -oN - Adds another output\n"); |
---|
86 | printf(" -oP - Mark output as primary\n"); |
---|
87 | |
---|
88 | printf("\nSource Options:\n\n"); |
---|
89 | printf(" -s --source DRV - Use DRV as input driver\n" |
---|
90 | " -S DEV - Use DEV as input device\n" |
---|
91 | " -sO OPTS - Use OPTS as input options\n" |
---|
92 | " -sN - Adds another source\n" |
---|
93 | " -sP - Make souce as primary\n" |
---|
94 | ); |
---|
95 | printf(" --list-sources - List all sources\n"); |
---|
96 | |
---|
97 | printf("\nCodec Filter Options:\n\n"); |
---|
98 | printf(" --list-cf - List all codec filter\n" |
---|
99 | ); |
---|
100 | |
---|
101 | printf("\nMIDI Options:\n\n"); |
---|
102 | printf(" --midi-no-console - Disable console based MIDI synth\n" |
---|
103 | " --midi-console DEV - Set device for MIDI console\n" |
---|
104 | ); |
---|
105 | |
---|
106 | printf("\nLight Control Options:\n\n"); |
---|
107 | printf(" --light-channels NUM - Sets the number of channels for Light control (default: %i)\n", |
---|
108 | LIGHT_CHANNELS_DEFAULT |
---|
109 | ); |
---|
110 | |
---|
111 | printf("\nServer Options:\n\n"); |
---|
112 | printf(" -t --tcp - Use TCP listen socket\n" |
---|
113 | " -u --unix - Use UNIX Domain listen socket (default)\n" |
---|
114 | #ifdef ROAR_HAVE_LIBDNET |
---|
115 | " -n --decnet - use DECnet listen socket\n" |
---|
116 | #endif |
---|
117 | " -4 - Use IPv4 connections (implies -t)\n" |
---|
118 | #ifdef PF_INET6 |
---|
119 | " -6 - Use IPv6 connections (implies -t)\n" |
---|
120 | #endif |
---|
121 | #ifdef IPV6_ADDRFORM |
---|
122 | " -64 - Try to downgrade sockets from IPv6 into IPv4,\n" |
---|
123 | " this is normaly not usefull.\n" |
---|
124 | #endif |
---|
125 | " -p --port - TCP Port to bind to\n" |
---|
126 | " -b --bind - IP/Hostname to bind to\n" |
---|
127 | " --sock - Filename for UNIX Domain Socket\n" |
---|
128 | #ifdef ROAR_HAVE_LIBSLP |
---|
129 | " --slp - Enable OpenSLP support\n" |
---|
130 | #endif |
---|
131 | " -G GROUP - Sets the group for the UNIX Domain Socket, (default: %s)\n" |
---|
132 | " You need the permittions to change the GID\n" |
---|
133 | " -U USER - Sets the user for the UNIX Domain Socket, (default: do not set)\n" |
---|
134 | " You need the permittions to change the UID (normaly only root has)\n" |
---|
135 | " --no-listen - Do not listen for new clients\n" |
---|
136 | " (only usefull for relaing, impleys --terminate)\n" |
---|
137 | " --client-fh - Comunicate with a client over this handle\n" |
---|
138 | " (only usefull for relaing)\n" |
---|
139 | " --close-fh - Closes the given fh\n" |
---|
140 | " --standby - Start in standby state\n" |
---|
141 | " --auto-standby - Automatical goes into standby if there are no streams\n", |
---|
142 | ROAR_DEFAULT_SOCKGRP |
---|
143 | ); |
---|
144 | // printf("\n Options:\n\n"); |
---|
145 | printf("\n"); |
---|
146 | } |
---|
147 | #endif |
---|
148 | |
---|
149 | int restart_server (char * server, int terminate) { |
---|
150 | struct roar_connection con; |
---|
151 | #ifdef ROAR_HAVE_KILL |
---|
152 | char buf[80]; |
---|
153 | ssize_t l; |
---|
154 | struct roar_vio_calls fh; |
---|
155 | pid_t pid; |
---|
156 | int ok; |
---|
157 | |
---|
158 | if ( pidfile != NULL ) { |
---|
159 | if ( roar_vio_open_file(&fh, pidfile, O_RDONLY, 0644) == -1 ) { |
---|
160 | ROAR_WARN("restart_server(*): Can not read pidfile: %s", pidfile); |
---|
161 | } else { |
---|
162 | l = roar_vio_read(&fh, buf, 80); |
---|
163 | roar_vio_close(&fh); |
---|
164 | if ( l > 0 ) { |
---|
165 | buf[l-1] = 0; |
---|
166 | buf[79] = 0; |
---|
167 | pid = atoi(buf); |
---|
168 | if ( terminate ) { |
---|
169 | ok = kill(pid, SIGUSR1); |
---|
170 | } else { |
---|
171 | ok = kill(pid, SIGINT); |
---|
172 | } |
---|
173 | if ( ok == 0 ) { |
---|
174 | return 0; |
---|
175 | } else { |
---|
176 | ROAR_WARN("restart_server(*): Can not kill roard by pidfile"); |
---|
177 | } |
---|
178 | } else { |
---|
179 | ROAR_WARN("restart_server(*): Can not find a PID in the pidfile"); |
---|
180 | } |
---|
181 | } |
---|
182 | } |
---|
183 | #endif |
---|
184 | |
---|
185 | if ( roar_connect(&con, server) == -1 ) { |
---|
186 | return -1; |
---|
187 | } |
---|
188 | |
---|
189 | if ( roar_terminate(&con, terminate) == -1 ) { |
---|
190 | return -1; |
---|
191 | } |
---|
192 | |
---|
193 | return roar_disconnect(&con); |
---|
194 | } |
---|
195 | |
---|
196 | #define R_SETUID 1 |
---|
197 | #define R_SETGID 2 |
---|
198 | |
---|
199 | int init_config (void) { |
---|
200 | int i; |
---|
201 | |
---|
202 | memset(g_config, 0, sizeof(struct roard_config)); |
---|
203 | |
---|
204 | for (i = 0; i < ROAR_DIR_DIRIDS; i++) { |
---|
205 | g_config->streams[i].mixer_channels = 1; |
---|
206 | g_config->streams[i].mixer.rpg_mul = 1; |
---|
207 | g_config->streams[i].mixer.rpg_div = 1; |
---|
208 | g_config->streams[i].mixer.scale = 65535; |
---|
209 | g_config->streams[i].mixer.mixer[0] = g_config->streams[i].mixer.scale; |
---|
210 | } |
---|
211 | |
---|
212 | g_config->streams[ROAR_DIR_PLAY ].flags = ROAR_FLAG_META; |
---|
213 | g_config->streams[ROAR_DIR_OUTPUT ].flags = ROAR_FLAG_PASSMIXER; |
---|
214 | g_config->streams[ROAR_DIR_FILTER ].flags = ROAR_FLAG_SYNC; |
---|
215 | g_config->streams[ROAR_DIR_MIDI_OUT].flags = ROAR_FLAG_SYNC; |
---|
216 | g_config->streams[ROAR_DIR_BIDIR ].flags = ROAR_FLAG_ANTIECHO; |
---|
217 | |
---|
218 | g_config->location = "***default***"; |
---|
219 | |
---|
220 | return 0; |
---|
221 | } |
---|
222 | |
---|
223 | int update_stream_flags (char * str) { |
---|
224 | int dir; |
---|
225 | char * flags; |
---|
226 | char * k; |
---|
227 | int op; |
---|
228 | int flag; |
---|
229 | |
---|
230 | if ( (flags = strstr(str, "=")) == NULL ) |
---|
231 | return -1; |
---|
232 | |
---|
233 | *flags = 0; |
---|
234 | flags++; |
---|
235 | |
---|
236 | if ( (dir = roar_str2dir(str)) == -1 ) |
---|
237 | return -1; |
---|
238 | |
---|
239 | while (flags != NULL) { |
---|
240 | k = flags; |
---|
241 | flags = strstr(flags, ","); |
---|
242 | |
---|
243 | if ( flags != NULL ) |
---|
244 | *(flags++) = 0; |
---|
245 | |
---|
246 | switch (*k) { |
---|
247 | case '+': k++; op = ROAR_SET_FLAG; break; |
---|
248 | case '-': k++; op = ROAR_RESET_FLAG; break; |
---|
249 | default: |
---|
250 | op = ROAR_SET_FLAG; |
---|
251 | } |
---|
252 | |
---|
253 | flag = 0; |
---|
254 | |
---|
255 | if ( !strcmp(k, "sync") ) { |
---|
256 | flag = ROAR_FLAG_SYNC; |
---|
257 | } else if ( !strcmp(k, "meta") ) { |
---|
258 | flag = ROAR_FLAG_META; |
---|
259 | } else if ( !strcmp(k, "cleanmeta") ) { |
---|
260 | flag = ROAR_FLAG_CLEANMETA; |
---|
261 | } else if ( !strcmp(k, "pause") ) { |
---|
262 | flag = ROAR_FLAG_PAUSE; |
---|
263 | } else if ( !strcmp(k, "mute") ) { |
---|
264 | flag = ROAR_FLAG_MUTE; |
---|
265 | } else if ( !strcmp(k, "antiecho") ) { |
---|
266 | flag = ROAR_FLAG_ANTIECHO; |
---|
267 | } else if ( !strcmp(k, "passmixer") ) { |
---|
268 | flag = ROAR_FLAG_PASSMIXER; |
---|
269 | } else { |
---|
270 | return -1; |
---|
271 | } |
---|
272 | |
---|
273 | g_config->streams[dir].flags |= flag; |
---|
274 | |
---|
275 | if ( op == ROAR_RESET_FLAG ) |
---|
276 | g_config->streams[dir].flags -= flag; |
---|
277 | } |
---|
278 | |
---|
279 | return 0; |
---|
280 | } |
---|
281 | |
---|
282 | int add_output (char * drv, char * dev, char * opts, int prim, int count) { |
---|
283 | int stream; |
---|
284 | struct roar_stream * s; |
---|
285 | struct roar_stream_server * ss; |
---|
286 | char * k, * v; |
---|
287 | #ifdef ROAR_DRIVER_CODEC |
---|
288 | char * to_free = NULL; |
---|
289 | #endif |
---|
290 | int codec; |
---|
291 | int sync = 0, f_mmap = 0; |
---|
292 | int32_t blocks = -1, blocksize = -1; |
---|
293 | int dir = ROAR_DIR_OUTPUT; |
---|
294 | int error = 0; |
---|
295 | // DMX: |
---|
296 | int32_t channel = -1; |
---|
297 | int32_t universe = -1; |
---|
298 | uint16_t tu16; |
---|
299 | float q = -32e6; |
---|
300 | |
---|
301 | ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = ?", drv, dev, opts); |
---|
302 | |
---|
303 | if ( drv == NULL && count == 0 ) { |
---|
304 | drv = ROAR_DRIVER_DEFAULT; |
---|
305 | prim = 1; |
---|
306 | sync = 1; |
---|
307 | |
---|
308 | #ifdef ROAR_DRIVER_CODEC |
---|
309 | if ( opts == NULL ) { |
---|
310 | opts = to_free = strdup("codec=" ROAR_DRIVER_CODEC); |
---|
311 | } |
---|
312 | #endif |
---|
313 | } |
---|
314 | |
---|
315 | if ( opts == NULL && count == 0 ) { |
---|
316 | sync = 1; |
---|
317 | prim = 1; // if ( prim == 0 ) prim = 1; -> prim allways = 1 |
---|
318 | } |
---|
319 | |
---|
320 | if ( (stream = streams_new()) == -1 ) { |
---|
321 | ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = -1", drv, dev, opts); |
---|
322 | if ( prim ) alive = 0; |
---|
323 | return -1; |
---|
324 | } |
---|
325 | |
---|
326 | streams_get(stream, &ss); |
---|
327 | s = ROAR_STREAM(ss); |
---|
328 | |
---|
329 | memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info)); |
---|
330 | |
---|
331 | s->pos_rel_id = -1; |
---|
332 | // s->info.codec = codec; |
---|
333 | |
---|
334 | codec = s->info.codec; |
---|
335 | |
---|
336 | k = strtok(opts, ","); |
---|
337 | while (k != NULL) { |
---|
338 | // ROAR_WARN("add_output(*): opts: %s", k); |
---|
339 | |
---|
340 | if ( (v = strstr(k, "=")) != NULL ) { |
---|
341 | *v++ = 0; |
---|
342 | } |
---|
343 | |
---|
344 | ROAR_DBG("add_output(*): opts: k='%s', v='%s'", k, v); |
---|
345 | if ( strcmp(k, "rate") == 0 ) { |
---|
346 | s->info.rate = atoi(v); |
---|
347 | } else if ( strcmp(k, "channels") == 0 ) { |
---|
348 | s->info.channels = atoi(v); |
---|
349 | } else if ( strcmp(k, "bits") == 0 ) { |
---|
350 | s->info.bits = atoi(v); |
---|
351 | } else if ( strcmp(k, "codec") == 0 ) { |
---|
352 | if ( (codec = roar_str2codec(v)) == -1 ) { |
---|
353 | ROAR_ERR("add_output(*): unknown codec '%s'", v); |
---|
354 | error++; |
---|
355 | } |
---|
356 | } else if ( strcmp(k, "q") == 0 ) { |
---|
357 | q = atof(v); |
---|
358 | } else if ( strcmp(k, "blocks") == 0 ) { |
---|
359 | blocks = atoi(v); |
---|
360 | } else if ( strcmp(k, "blocksize") == 0 ) { |
---|
361 | blocksize = atoi(v); |
---|
362 | } else if ( strcmp(k, "mmap") == 0 ) { |
---|
363 | f_mmap = 1; |
---|
364 | } else if ( strcmp(k, "subsystem") == 0 ) { |
---|
365 | if ( !strcasecmp(v, "wave") || !strcasecmp(v, "waveform") ) { |
---|
366 | dir = ROAR_DIR_OUTPUT; |
---|
367 | } else if ( !strcasecmp(v, "midi") ) { |
---|
368 | dir = ROAR_DIR_MIDI_OUT; |
---|
369 | } else if ( !strcasecmp(v, "light") ) { |
---|
370 | dir = ROAR_DIR_LIGHT_OUT; |
---|
371 | } else if ( !strcasecmp(v, "raw") ) { |
---|
372 | dir = ROAR_DIR_RAW_OUT; |
---|
373 | } else { |
---|
374 | ROAR_ERR("add_output(*): unknown subsystem '%s'", k); |
---|
375 | error++; |
---|
376 | } |
---|
377 | // DMX: |
---|
378 | } else if ( strcmp(k, "channel") == 0 ) { |
---|
379 | channel = atoi(v); |
---|
380 | if ( channel < 0 || channel > 65535 ) { |
---|
381 | ROAR_ERR("add_output(*): Invalide channel (not within 0..65535): %i", channel); |
---|
382 | channel = -1; |
---|
383 | error++; |
---|
384 | } |
---|
385 | } else if ( strcmp(k, "universe") == 0 ) { |
---|
386 | universe = atoi(v); |
---|
387 | if ( universe < 0 || universe > 255 ) { |
---|
388 | ROAR_ERR("add_output(*): Invalide universe (not within 0..255): %i", universe); |
---|
389 | universe = -1; |
---|
390 | error++; |
---|
391 | } |
---|
392 | |
---|
393 | } else if ( strcmp(k, "name") == 0 ) { |
---|
394 | if ( streams_set_name(stream, v) == -1 ) { |
---|
395 | ROAR_ERR("add_output(*): Can not set Stream name"); |
---|
396 | error++; |
---|
397 | } |
---|
398 | |
---|
399 | } else if ( strcmp(k, "meta") == 0 ) { |
---|
400 | streams_set_flag(stream, ROAR_FLAG_META); |
---|
401 | } else if ( strcmp(k, "sync") == 0 ) { |
---|
402 | sync = 1; |
---|
403 | } else if ( strcmp(k, "primary") == 0 ) { |
---|
404 | prim = 1; |
---|
405 | |
---|
406 | } else if ( strcmp(k, "cleanmeta") == 0 ) { |
---|
407 | streams_set_flag(stream, ROAR_FLAG_CLEANMETA); |
---|
408 | } else if ( strcmp(k, "autoconf") == 0 ) { |
---|
409 | streams_set_flag(stream, ROAR_FLAG_AUTOCONF); |
---|
410 | } else if ( strcmp(k, "recsource") == 0 ) { |
---|
411 | streams_set_flag(stream, ROAR_FLAG_RECSOURCE); |
---|
412 | } else if ( strcmp(k, "passmixer") == 0 ) { |
---|
413 | streams_set_flag(stream, ROAR_FLAG_PASSMIXER); |
---|
414 | } else { |
---|
415 | ROAR_ERR("add_output(*): unknown option '%s'", k); |
---|
416 | error++; |
---|
417 | } |
---|
418 | |
---|
419 | if ( error ) { |
---|
420 | streams_delete(stream); |
---|
421 | if ( prim ) alive = 0; |
---|
422 | #ifdef ROAR_DRIVER_CODEC |
---|
423 | if ( to_free != NULL ) |
---|
424 | free(to_free); |
---|
425 | #endif |
---|
426 | return -1; |
---|
427 | } |
---|
428 | |
---|
429 | k = strtok(NULL, ","); |
---|
430 | } |
---|
431 | |
---|
432 | if ( streams_set_dir(stream, dir, 1) == -1 ) { |
---|
433 | streams_delete(stream); |
---|
434 | return -1; |
---|
435 | } |
---|
436 | |
---|
437 | #ifdef ROAR_DRIVER_CODEC |
---|
438 | if ( to_free != NULL ) |
---|
439 | free(to_free); |
---|
440 | #endif |
---|
441 | |
---|
442 | if ( codec == ROAR_CODEC_ALAW || codec == ROAR_CODEC_MULAW ) |
---|
443 | s->info.bits = 8; // needed to open OSS driver, will be overriden by codecfilter |
---|
444 | |
---|
445 | s->info.codec = codec; |
---|
446 | ROAR_STREAM_SERVER(s)->codec_orgi = codec; |
---|
447 | |
---|
448 | if ( driver_openvio(&(ss->vio), &(ss->driver_id), drv, dev, &(s->info), -1, ss) == -1 ) { |
---|
449 | ss->driver_id = -1; // don't close a driver not opened... |
---|
450 | memset(&(ss->vio), 0, sizeof(struct roar_vio_calls)); |
---|
451 | streams_delete(stream); |
---|
452 | ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = -1", drv, dev, opts); |
---|
453 | if ( prim ) alive = 0; |
---|
454 | return -1; |
---|
455 | } |
---|
456 | |
---|
457 | roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_SSTREAMID, &stream); // ignore errors here |
---|
458 | roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_SSTREAM, s); // ignore errors here |
---|
459 | |
---|
460 | if ( blocks != -1 ) |
---|
461 | roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DBLOCKS, &blocks); |
---|
462 | |
---|
463 | if ( blocksize != -1 ) |
---|
464 | roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DBLKSIZE, &blocksize); |
---|
465 | |
---|
466 | // TODO: we shoudld *really* check for errors here... |
---|
467 | if ( channel != -1 ) { |
---|
468 | tu16 = channel; |
---|
469 | roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DMXSCHAN, &tu16); |
---|
470 | } |
---|
471 | if ( universe != -1 ) { |
---|
472 | tu16 = universe; |
---|
473 | roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DMXUNIV, &tu16); |
---|
474 | } |
---|
475 | |
---|
476 | ROAR_DBG("add_output(*): ss->driver_id=%i", ss->driver_id); |
---|
477 | |
---|
478 | streams_set_fh(stream, -1); // update some internal structures |
---|
479 | |
---|
480 | if ( q > -1e6 ) { |
---|
481 | ROAR_DBG("add_output(*): setting q=%f", q); |
---|
482 | streams_ctl(stream, ROAR_CODECFILTER_CTL_SET_Q|ROAR_STREAM_CTL_TYPE_FLOAT, &q); |
---|
483 | } |
---|
484 | |
---|
485 | client_stream_add(g_source_client, stream); |
---|
486 | |
---|
487 | if ( prim ) { |
---|
488 | streams_mark_primary(stream); |
---|
489 | s->pos_rel_id = stream; |
---|
490 | } |
---|
491 | |
---|
492 | if ( sync ) { |
---|
493 | streams_set_flag(stream, ROAR_FLAG_SYNC); |
---|
494 | } else { |
---|
495 | streams_reset_flag(stream, ROAR_FLAG_SYNC); |
---|
496 | } |
---|
497 | |
---|
498 | if ( f_mmap ) |
---|
499 | streams_set_flag(stream, ROAR_FLAG_MMAP); |
---|
500 | |
---|
501 | return 0; |
---|
502 | } |
---|
503 | |
---|
504 | |
---|
505 | // SLP: |
---|
506 | void register_slp_callback(SLPHandle hslp, SLPError errcode, void * cookie) { |
---|
507 | /* return the error code in the cookie */ |
---|
508 | *(SLPError*)cookie = errcode; |
---|
509 | } |
---|
510 | |
---|
511 | int register_slp (int unreg, char * sockname) { |
---|
512 | #ifdef ROAR_HAVE_LIBSLP |
---|
513 | static int regged = 0; |
---|
514 | static char * sn = NULL; |
---|
515 | SLPError err; |
---|
516 | SLPError callbackerr; |
---|
517 | SLPHandle hslp; |
---|
518 | char addr[1024]; |
---|
519 | char attr[1024] = ""; |
---|
520 | char * location; |
---|
521 | |
---|
522 | if ( sockname != NULL ) |
---|
523 | sn = sockname; |
---|
524 | |
---|
525 | snprintf(addr, sizeof(addr), ROAR_SLP_URL_TYPE "://%s", sn); |
---|
526 | |
---|
527 | err = SLPOpen("en", SLP_FALSE, &hslp); |
---|
528 | |
---|
529 | if (err != SLP_OK) { |
---|
530 | ROAR_ERR("Error opening slp handle: Error #%i", err); |
---|
531 | return -1; |
---|
532 | } |
---|
533 | |
---|
534 | if (!unreg) { |
---|
535 | |
---|
536 | if ( SLPEscape(g_config->location, &location, SLP_FALSE) != SLP_OK ) { |
---|
537 | ROAR_ERR("Error using SLPEscape() on server location, really bad!"); |
---|
538 | SLPClose(hslp); |
---|
539 | return -1; |
---|
540 | } |
---|
541 | |
---|
542 | snprintf(attr, sizeof(attr), "(wave-rate=%i),(wave-channels=%i),(wave-bits=%i)," |
---|
543 | "(light-channels=%i),(location=%s)", |
---|
544 | g_sa->rate, g_sa->channels, g_sa->bits, |
---|
545 | g_light_state.channels, |
---|
546 | location |
---|
547 | ); |
---|
548 | |
---|
549 | /* Register a service with SLP */ |
---|
550 | err = SLPReg(hslp, |
---|
551 | addr, |
---|
552 | SLP_LIFETIME_MAXIMUM, |
---|
553 | 0, |
---|
554 | attr, |
---|
555 | SLP_TRUE, |
---|
556 | register_slp_callback, |
---|
557 | &callbackerr); |
---|
558 | regged = 1; |
---|
559 | } else if ( unreg && regged ) { |
---|
560 | err = SLPDereg(hslp, addr, register_slp_callback, &callbackerr); |
---|
561 | regged = 0; |
---|
562 | } else { |
---|
563 | SLPClose(hslp); |
---|
564 | return -1; |
---|
565 | } |
---|
566 | |
---|
567 | /* err may contain an error code that occurred as the slp library */ |
---|
568 | /* _prepared_ to make the call. */ |
---|
569 | if ( err != SLP_OK ) { |
---|
570 | ROAR_ERR("Error (de)registering service with slp: Error #%i", err); |
---|
571 | return -1; |
---|
572 | } |
---|
573 | |
---|
574 | /* callbackerr may contain an error code (that was assigned through */ |
---|
575 | /* the callback cookie) that occurred as slp packets were sent on */ |
---|
576 | /* the wire */ |
---|
577 | if (callbackerr != SLP_OK) { |
---|
578 | ROAR_ERR("Error (de)registering service with slp: Error #%i", callbackerr); |
---|
579 | return -1; |
---|
580 | } |
---|
581 | |
---|
582 | SLPClose(hslp); |
---|
583 | return 0; |
---|
584 | #else |
---|
585 | return -1; |
---|
586 | #endif |
---|
587 | } |
---|
588 | |
---|
589 | |
---|
590 | // MAIN: |
---|
591 | |
---|
592 | #ifdef ROAR_HAVE_MAIN_ARGS |
---|
593 | int main (int argc, char * argv[]) { |
---|
594 | #else |
---|
595 | int main (void) { |
---|
596 | #endif |
---|
597 | #ifdef ROAR_HAVE_MAIN_ARGS |
---|
598 | int i; |
---|
599 | char * k; |
---|
600 | #endif |
---|
601 | #ifdef ROAR_SUPPORT_LISTEN |
---|
602 | char user_sock[80] = {0}; |
---|
603 | #endif |
---|
604 | struct roar_audio_info sa, max_sa; |
---|
605 | struct roard_config config; |
---|
606 | #ifdef ROAR_HAVE_FORK |
---|
607 | int daemon = 0; |
---|
608 | #endif |
---|
609 | int realtime = 0; |
---|
610 | int sysclocksync = 0; |
---|
611 | char * driver = NULL; |
---|
612 | char * device = NULL; |
---|
613 | #ifdef ROAR_HAVE_MAIN_ARGS |
---|
614 | char * opts = NULL; |
---|
615 | #endif |
---|
616 | // char * server = ROAR_DEFAULT_SOCK_GLOBAL; |
---|
617 | #ifdef ROAR_SUPPORT_LISTEN |
---|
618 | int port = ROAR_DEFAULT_PORT; |
---|
619 | #endif |
---|
620 | int drvid; |
---|
621 | char * s_drv = "cf"; |
---|
622 | char * s_dev = NULL; |
---|
623 | char * s_con = NULL; |
---|
624 | char * s_opt = NULL; |
---|
625 | int s_prim = 0; |
---|
626 | char * o_drv = getenv("ROAR_DRIVER"); |
---|
627 | char * o_dev = getenv("ROAR_DEVICE"); |
---|
628 | char * o_opts = NULL; |
---|
629 | int o_prim = 0; |
---|
630 | int o_count = 0; |
---|
631 | int light_channels = LIGHT_CHANNELS_DEFAULT; |
---|
632 | char * sock_grp = ROAR_DEFAULT_SOCKGRP; |
---|
633 | char * sock_user = NULL; |
---|
634 | #ifdef ROAR_SUPPORT_LISTEN |
---|
635 | int sock_type = ROAR_SOCKET_TYPE_UNKNOWN; |
---|
636 | #endif |
---|
637 | #ifdef ROAR_HAVE_LIBSLP |
---|
638 | int reg_slp = 0; |
---|
639 | #endif |
---|
640 | #ifdef ROAR_HAVE_CHROOT |
---|
641 | char * chrootdir = NULL; |
---|
642 | #endif |
---|
643 | #if defined(ROAR_HAVE_SETGID) || defined(ROAR_HAVE_SETUID) |
---|
644 | int setids = 0; |
---|
645 | #endif |
---|
646 | #ifdef ROAR_HAVE_UNIX |
---|
647 | char * env_roar_proxy_backup; |
---|
648 | #endif |
---|
649 | #if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX) |
---|
650 | struct group * grp = NULL; |
---|
651 | #endif |
---|
652 | #if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX) |
---|
653 | struct passwd * pwd = NULL; |
---|
654 | #endif |
---|
655 | #ifdef ROAR_HAVE_GETSERVBYNAME |
---|
656 | struct servent * serv = NULL; |
---|
657 | #endif |
---|
658 | DRIVER_USERDATA_T drvinst; |
---|
659 | struct roar_client * self = NULL; |
---|
660 | #ifdef ROAR_HAVE_LIBDNET |
---|
661 | char decnethost[80]; |
---|
662 | #endif |
---|
663 | #ifdef SUPPORT_PIDFILE |
---|
664 | struct roar_vio_calls pidfile_vio; |
---|
665 | #endif |
---|
666 | |
---|
667 | g_standby = 0; |
---|
668 | g_autostandby = 0; |
---|
669 | alive = 1; |
---|
670 | #ifdef ROAR_SUPPORT_LISTEN |
---|
671 | g_no_listen = 0; |
---|
672 | g_listen_socket = -1; |
---|
673 | #else |
---|
674 | g_terminate = 1; |
---|
675 | #endif |
---|
676 | |
---|
677 | sa.bits = ROAR_BITS_DEFAULT; |
---|
678 | sa.channels = ROAR_CHANNELS_DEFAULT; |
---|
679 | sa.rate = ROAR_RATE_DEFAULT; |
---|
680 | sa.codec = ROAR_CODEC_DEFAULT; |
---|
681 | |
---|
682 | g_sa = &sa; |
---|
683 | g_max_sa = &max_sa; |
---|
684 | |
---|
685 | memcpy(g_max_sa, g_sa, sizeof(max_sa)); |
---|
686 | |
---|
687 | g_config = &config; |
---|
688 | |
---|
689 | if ( init_config() == -1 ) { |
---|
690 | ROAR_ERR("Can not init default config!"); |
---|
691 | return 1; |
---|
692 | } |
---|
693 | |
---|
694 | if ( midi_init_config() == -1 ) { |
---|
695 | ROAR_ERR("Can not init MIDI config!"); |
---|
696 | return 1; |
---|
697 | } |
---|
698 | |
---|
699 | #ifdef ROAR_SUPPORT_LISTEN |
---|
700 | #ifdef ROAR_HAVE_GETUID |
---|
701 | if ( getuid() != 0 && getenv("HOME") != NULL ) { |
---|
702 | snprintf(user_sock, 79, "%s/%s", (char*)getenv("HOME"), ROAR_DEFAULT_SOCK_USER); |
---|
703 | server = user_sock; |
---|
704 | } |
---|
705 | #endif |
---|
706 | |
---|
707 | if ( getenv("ROAR_SERVER") != NULL ) |
---|
708 | server = getenv("ROAR_SERVER"); |
---|
709 | #endif |
---|
710 | |
---|
711 | if ( clients_init() == -1 ) { |
---|
712 | ROAR_ERR("Can not init clients!"); |
---|
713 | return 1; |
---|
714 | } |
---|
715 | |
---|
716 | if ( streams_init() == -1 ) { |
---|
717 | ROAR_ERR("Can not init streams!"); |
---|
718 | return 1; |
---|
719 | } |
---|
720 | |
---|
721 | if ( (g_self_client = clients_new()) == -1 ) { |
---|
722 | ROAR_ERR("Can not create self client!"); |
---|
723 | return 1; |
---|
724 | } |
---|
725 | |
---|
726 | if ( sources_init() == -1 ) { |
---|
727 | ROAR_ERR("Can not init sources!"); |
---|
728 | return 1; |
---|
729 | } |
---|
730 | |
---|
731 | if ( (sources_set_client(g_self_client)) == -1 ) { |
---|
732 | ROAR_ERR("Can not init set source client!"); |
---|
733 | return 1; |
---|
734 | } |
---|
735 | |
---|
736 | #ifdef ROAR_HAVE_MAIN_ARGS |
---|
737 | for (i = 1; i < argc; i++) { |
---|
738 | k = argv[i]; |
---|
739 | |
---|
740 | if ( strcmp(k, "-h") == 0 || strcmp(k, "--help") == 0 ) { |
---|
741 | usage(); |
---|
742 | return 0; |
---|
743 | |
---|
744 | } else if ( strcmp(k, "--start") == 0 ) { |
---|
745 | // this is a no op |
---|
746 | } else if ( strcmp(k, "--restart") == 0 ) { |
---|
747 | #ifdef ROAR_SUPPORT_LISTEN |
---|
748 | if ( restart_server(server, 1) == -1 ) { |
---|
749 | ROAR_WARN("Can not terminate old server (not running at %s?), tring to continue anyway", server); |
---|
750 | } |
---|
751 | #else |
---|
752 | ROAR_ERR("--restart not supported"); |
---|
753 | #endif |
---|
754 | } else if ( strcmp(k, "--shutdown") == 0 ) { |
---|
755 | #ifdef ROAR_SUPPORT_LISTEN |
---|
756 | if ( restart_server(server, 1) == -1 ) { |
---|
757 | ROAR_WARN("Can not terminate old server (not running at %s?)", server); |
---|
758 | return 1; |
---|
759 | } |
---|
760 | return 0; |
---|
761 | #else |
---|
762 | ROAR_ERR("--shutdown not supported"); |
---|
763 | return 1; |
---|
764 | #endif |
---|
765 | } else if ( strcmp(k, "--stop") == 0 ) { |
---|
766 | #ifdef ROAR_SUPPORT_LISTEN |
---|
767 | if ( restart_server(server, 0) == -1 ) { |
---|
768 | ROAR_WARN("Can not stop old server (not running at %s?)", server); |
---|
769 | return 1; |
---|
770 | } |
---|
771 | return 0; |
---|
772 | #else |
---|
773 | ROAR_ERR("--stop not supported"); |
---|
774 | return 1; |
---|
775 | #endif |
---|
776 | |
---|
777 | |
---|
778 | } else if ( strcmp(k, "--demon") == 0 || strcmp(k, "--daemon") == 0 ) { |
---|
779 | #ifdef ROAR_HAVE_FORK |
---|
780 | daemon = 1; |
---|
781 | #else |
---|
782 | ROAR_ERR("--daemon not supported"); |
---|
783 | #endif |
---|
784 | } else if ( strcmp(k, "--terminate") == 0 ) { |
---|
785 | g_terminate = 1; |
---|
786 | } else if ( strcmp(k, "--sysclocksync") == 0 ) { |
---|
787 | sysclocksync = 1000; |
---|
788 | } else if ( strcmp(k, "--realtime") == 0 ) { |
---|
789 | realtime++; |
---|
790 | } else if ( strcmp(k, "--chroot") == 0 ) { |
---|
791 | #ifdef ROAR_HAVE_CHROOT |
---|
792 | chrootdir = argv[++i]; |
---|
793 | #else |
---|
794 | ROAR_ERR("--chroot not supported"); |
---|
795 | i++; |
---|
796 | #endif |
---|
797 | } else if ( strcmp(k, "--setgid") == 0 ) { |
---|
798 | #ifdef ROAR_HAVE_SETGID |
---|
799 | setids |= R_SETGID; |
---|
800 | #else |
---|
801 | ROAR_ERR("--setgid not supported"); |
---|
802 | #endif |
---|
803 | } else if ( strcmp(k, "--setuid") == 0 ) { |
---|
804 | #ifdef ROAR_HAVE_SETUID |
---|
805 | setids |= R_SETUID; |
---|
806 | #else |
---|
807 | ROAR_ERR("--setuid not supported"); |
---|
808 | #endif |
---|
809 | } else if ( strcmp(k, "--location") == 0 ) { |
---|
810 | g_config->location = argv[++i]; |
---|
811 | } else if ( strcmp(k, "--pidfile") == 0 ) { |
---|
812 | #ifdef SUPPORT_PIDFILE |
---|
813 | pidfile = argv[++i]; |
---|
814 | #else |
---|
815 | ROAR_ERR("--pidfile not supported"); |
---|
816 | i++; |
---|
817 | #endif |
---|
818 | |
---|
819 | } else if ( strcmp(k, "--list-cf") == 0 ) { |
---|
820 | print_codecfilterlist(); |
---|
821 | return 0; |
---|
822 | |
---|
823 | } else if ( strcmp(k, "-R") == 0 || strcmp(k, "--rate") == 0 ) { |
---|
824 | sa.rate = atoi(argv[++i]); |
---|
825 | } else if ( strcmp(k, "-B") == 0 || strcmp(k, "--bits") == 0 ) { |
---|
826 | sa.bits = atoi(argv[++i]); |
---|
827 | } else if ( strcmp(k, "-C") == 0 || strcmp(k, "--chans") == 0 ) { |
---|
828 | sa.channels = atoi(argv[++i]); |
---|
829 | |
---|
830 | } else if ( strcmp(k, "--stream-flags") == 0 ) { |
---|
831 | if ( update_stream_flags(argv[++i]) == -1 ) { |
---|
832 | ROAR_ERR("Can not set stream flags"); |
---|
833 | return 1; |
---|
834 | } |
---|
835 | |
---|
836 | } else if ( strcmp(k, "-d") == 0 || strcmp(k, "--driver") == 0 ) { |
---|
837 | driver = argv[++i]; |
---|
838 | if ( strcmp(driver, "list") == 0 ) { |
---|
839 | ROAR_WARN("The option is obsolete, use --list-driver!"); |
---|
840 | print_driverlist(); |
---|
841 | return 0; |
---|
842 | } |
---|
843 | } else if ( strcmp(k, "-D") == 0 || strcmp(k, "--device") == 0 ) { |
---|
844 | device = argv[++i]; |
---|
845 | } else if ( strcmp(k, "-dO") == 0 ) { |
---|
846 | opts = argv[++i]; |
---|
847 | } else if ( strcmp(k, "--list-driver") == 0 ) { |
---|
848 | print_driverlist(); |
---|
849 | return 0; |
---|
850 | |
---|
851 | } else if ( strcmp(k, "-o") == 0 || strcmp(k, "--odriver") == 0 ) { |
---|
852 | o_drv = argv[++i]; |
---|
853 | } else if ( strcmp(k, "-O") == 0 || strcmp(k, "--odevice") == 0 ) { |
---|
854 | o_dev = argv[++i]; |
---|
855 | } else if ( strcmp(k, "-oO") == 0 ) { |
---|
856 | o_opts = argv[++i]; |
---|
857 | } else if ( strcmp(k, "-oP") == 0 ) { |
---|
858 | o_prim = 1; |
---|
859 | } else if ( strcmp(k, "-oN") == 0 ) { |
---|
860 | if ( add_output(o_drv, o_dev, o_opts, o_prim, o_count) != -1 ) |
---|
861 | o_count++; |
---|
862 | |
---|
863 | o_drv = o_dev = o_opts = NULL; |
---|
864 | o_prim = 0; |
---|
865 | |
---|
866 | } else if ( strcmp(k, "-s") == 0 || strcmp(k, "--source") == 0 ) { |
---|
867 | s_drv = argv[++i]; |
---|
868 | } else if ( strcmp(k, "-S") == 0 ) { |
---|
869 | s_dev = argv[++i]; |
---|
870 | } else if ( strcmp(k, "-sO") == 0 ) { |
---|
871 | s_opt = argv[++i]; |
---|
872 | } else if ( strcmp(k, "-sC") == 0 ) { |
---|
873 | s_con = argv[++i]; |
---|
874 | } else if ( strcmp(k, "-sP") == 0 ) { |
---|
875 | s_prim = 1; |
---|
876 | } else if ( strcmp(k, "-sN") == 0 ) { |
---|
877 | if ( sources_add(s_drv, s_dev, s_con, s_opt, s_prim) == -1 ) { |
---|
878 | ROAR_ERR("main(*): adding source '%s' via '%s' failed!", s_dev, s_drv); |
---|
879 | } |
---|
880 | s_opt = s_dev = s_con = NULL; |
---|
881 | s_drv = "cf"; |
---|
882 | s_prim = 0; |
---|
883 | } else if ( strcmp(k, "--list-sources") == 0 ) { |
---|
884 | print_sourcelist(); |
---|
885 | return 0; |
---|
886 | |
---|
887 | } else if ( strcmp(k, "--light-channels") == 0 ) { |
---|
888 | light_channels = atoi(argv[++i]); |
---|
889 | |
---|
890 | } else if ( strcmp(k, "--midi-no-console") == 0 ) { |
---|
891 | midi_config.init_cb = 0; |
---|
892 | } else if ( strcmp(k, "--midi-console") == 0 ) { |
---|
893 | midi_config.console_dev = argv[++i]; |
---|
894 | |
---|
895 | } else if ( strcmp(k, "-p") == 0 || strcmp(k, "--port") == 0 ) { |
---|
896 | // This is only usefull in INET not UNIX mode. |
---|
897 | #ifdef ROAR_SUPPORT_LISTEN |
---|
898 | if ( *server == '/' ) |
---|
899 | server = ROAR_DEFAULT_HOST; |
---|
900 | |
---|
901 | errno = 0; |
---|
902 | if ( (port = atoi(argv[++i])) < 1 ) { |
---|
903 | #ifdef ROAR_HAVE_GETSERVBYNAME |
---|
904 | if ( (serv = getservbyname(argv[i], "tcp")) == NULL ) { |
---|
905 | ROAR_ERR("Unknown service: %s: %s", argv[i], strerror(errno)); |
---|
906 | return 1; |
---|
907 | } |
---|
908 | // NOTE: we need to use ROAR_NET2HOST16() here even if s_port is of type int! |
---|
909 | ROAR_DBG("main(*): serv = {s_name='%s', s_aliases={...}, s_port=%i, s_proto='%s'}", |
---|
910 | serv->s_name, ROAR_NET2HOST16(serv->s_port), serv->s_proto); |
---|
911 | port = ROAR_NET2HOST16(serv->s_port); |
---|
912 | #else |
---|
913 | ROAR_ERR("invalite port number: %s", argv[i]); |
---|
914 | return 1; |
---|
915 | #endif |
---|
916 | } |
---|
917 | #endif |
---|
918 | } else if ( strcmp(k, "-b") == 0 || strcmp(k, "--bind") == 0 || strcmp(k, "--sock") == 0 ) { |
---|
919 | #ifdef ROAR_SUPPORT_LISTEN |
---|
920 | server = argv[++i]; |
---|
921 | #endif |
---|
922 | |
---|
923 | } else if ( strcmp(k, "-t") == 0 || strcmp(k, "--tcp") == 0 ) { |
---|
924 | #ifdef ROAR_SUPPORT_LISTEN |
---|
925 | if ( sock_type != ROAR_SOCKET_TYPE_TCP && sock_type != ROAR_SOCKET_TYPE_TCP6 ) |
---|
926 | sock_type = ROAR_SOCKET_TYPE_TCP; |
---|
927 | |
---|
928 | if ( *server == '/' ) |
---|
929 | server = ROAR_DEFAULT_HOST; |
---|
930 | #endif |
---|
931 | |
---|
932 | } else if ( strcmp(k, "-4") == 0 ) { |
---|
933 | #ifdef ROAR_SUPPORT_LISTEN |
---|
934 | sock_type = ROAR_SOCKET_TYPE_TCP; |
---|
935 | if ( *server == '/' ) |
---|
936 | server = ROAR_DEFAULT_HOST; |
---|
937 | #endif |
---|
938 | } else if ( strcmp(k, "-6") == 0 ) { |
---|
939 | #ifdef ROAR_SUPPORT_LISTEN |
---|
940 | #ifdef PF_INET6 |
---|
941 | sock_type = ROAR_SOCKET_TYPE_TCP6; |
---|
942 | if ( *server == '/' ) |
---|
943 | server = ROAR_DEFAULT_HOST; |
---|
944 | #else |
---|
945 | ROAR_ERR("No IPv6 support compiled in!"); |
---|
946 | return 1; |
---|
947 | #endif |
---|
948 | #endif |
---|
949 | |
---|
950 | } else if ( strcmp(k, "-u") == 0 || strcmp(k, "--unix") == 0 ) { |
---|
951 | #ifdef ROAR_SUPPORT_LISTEN |
---|
952 | // ignore this case as it is the default behavor. |
---|
953 | sock_type = ROAR_SOCKET_TYPE_UNIX; |
---|
954 | #endif |
---|
955 | |
---|
956 | } else if ( strcmp(k, "-n") == 0 || strcmp(k, "--decnet") == 0 ) { |
---|
957 | #ifdef ROAR_SUPPORT_LISTEN |
---|
958 | #ifdef ROAR_HAVE_LIBDNET |
---|
959 | port = ROAR_DEFAULT_NUM; |
---|
960 | strcpy(decnethost, ROAR_DEFAULT_LISTEN_OBJECT); |
---|
961 | server = decnethost; |
---|
962 | sock_type = ROAR_SOCKET_TYPE_DECNET; |
---|
963 | #else |
---|
964 | ROAR_ERR("No DECnet support compiled in!"); |
---|
965 | return 1; |
---|
966 | #endif |
---|
967 | #endif |
---|
968 | |
---|
969 | } else if ( strcmp(k, "--slp") == 0 ) { |
---|
970 | #ifdef ROAR_HAVE_LIBSLP |
---|
971 | reg_slp = 1; |
---|
972 | #else |
---|
973 | ROAR_ERR("No OpenSLP support compiled in!"); |
---|
974 | return 1; |
---|
975 | #endif |
---|
976 | |
---|
977 | } else if ( strcmp(k, "-G") == 0 ) { |
---|
978 | sock_grp = argv[++i]; |
---|
979 | } else if ( strcmp(k, "-U") == 0 ) { |
---|
980 | sock_user = argv[++i]; |
---|
981 | |
---|
982 | } else if ( strcmp(k, "--no-listen") == 0 ) { |
---|
983 | #ifdef ROAR_SUPPORT_LISTEN |
---|
984 | server = ""; |
---|
985 | g_terminate = 1; |
---|
986 | g_no_listen = 1; |
---|
987 | #endif |
---|
988 | } else if ( strcmp(k, "--client-fh") == 0 ) { |
---|
989 | if ( clients_set_fh(clients_new(), atoi(argv[++i])) == -1 ) { |
---|
990 | ROAR_ERR("main(*): Can not set client's fh"); |
---|
991 | return 1; |
---|
992 | } |
---|
993 | } else if ( strcmp(k, "--close-fh") == 0 ) { |
---|
994 | #ifdef ROAR_HAVE_IO_POSIX |
---|
995 | close(atoi(argv[++i])); |
---|
996 | #else |
---|
997 | i++; |
---|
998 | ROAR_WARN("can not close file handle %s (closing not supported)", argv[i]); |
---|
999 | #endif |
---|
1000 | |
---|
1001 | } else if ( strcmp(k, "--standby") == 0 ) { |
---|
1002 | g_standby = 1; |
---|
1003 | } else if ( strcmp(k, "--auto-standby") == 0 ) { |
---|
1004 | g_autostandby = 1; |
---|
1005 | } else { |
---|
1006 | usage(); |
---|
1007 | return 1; |
---|
1008 | } |
---|
1009 | |
---|
1010 | } |
---|
1011 | #endif |
---|
1012 | |
---|
1013 | if ( s_dev != NULL ) { |
---|
1014 | if ( sources_add(s_drv, s_dev, s_con, s_opt, s_prim) == -1 ) { |
---|
1015 | ROAR_ERR("main(*): adding source '%s' via '%s' failed!", s_dev, s_drv); |
---|
1016 | } |
---|
1017 | } |
---|
1018 | |
---|
1019 | add_output(o_drv, o_dev, o_opts, o_prim, o_count); |
---|
1020 | |
---|
1021 | ROAR_DBG("Server config: rate=%i, bits=%i, chans=%i", sa.rate, sa.bits, sa.channels); |
---|
1022 | |
---|
1023 | if ( midi_init() == -1 ) { |
---|
1024 | ROAR_ERR("Can not initialize MIDI subsystem"); |
---|
1025 | } |
---|
1026 | |
---|
1027 | if ( light_init(light_channels) == -1 ) { |
---|
1028 | ROAR_ERR("Can not initialize light control subsystem"); |
---|
1029 | } |
---|
1030 | |
---|
1031 | #ifdef ROAR_SUPPORT_LISTEN |
---|
1032 | if ( *server != 0 ) { |
---|
1033 | if ( (g_listen_socket = roar_socket_listen(sock_type, server, port)) == -1 ) { |
---|
1034 | #ifdef ROAR_HAVE_UNIX |
---|
1035 | if ( *server == '/' ) { |
---|
1036 | if ( (env_roar_proxy_backup = getenv("ROAR_PROXY")) != NULL ) { |
---|
1037 | env_roar_proxy_backup = strdup(env_roar_proxy_backup); |
---|
1038 | unsetenv("ROAR_PROXY"); |
---|
1039 | } |
---|
1040 | if ( (i = roar_socket_connect(server, port)) != -1 ) { |
---|
1041 | close(i); |
---|
1042 | ROAR_ERR("Can not open listen socket!"); |
---|
1043 | return 1; |
---|
1044 | } else { |
---|
1045 | unlink(server); |
---|
1046 | if ( (g_listen_socket = roar_socket_listen(sock_type, server, port)) == -1 ) { |
---|
1047 | ROAR_ERR("Can not open listen socket!"); |
---|
1048 | return 1; |
---|
1049 | } |
---|
1050 | } |
---|
1051 | if ( env_roar_proxy_backup != NULL ) { |
---|
1052 | setenv("ROAR_PROXY", env_roar_proxy_backup, 0); |
---|
1053 | free(env_roar_proxy_backup); |
---|
1054 | } |
---|
1055 | #else |
---|
1056 | if (0) { // noop |
---|
1057 | #endif |
---|
1058 | } else { |
---|
1059 | ROAR_ERR("Can not open listen socket!"); |
---|
1060 | return 1; |
---|
1061 | } |
---|
1062 | } |
---|
1063 | |
---|
1064 | #if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX) |
---|
1065 | if ( (grp = getgrnam(sock_grp)) == NULL ) { |
---|
1066 | ROAR_ERR("Can not get GID for group %s: %s", sock_grp, strerror(errno)); |
---|
1067 | } |
---|
1068 | #endif |
---|
1069 | #if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX) |
---|
1070 | if ( sock_user || (setids & R_SETUID) ) { |
---|
1071 | if ( (pwd = getpwnam(sock_user)) == NULL ) { |
---|
1072 | ROAR_ERR("Can not get UID for user %s: %s", sock_user, strerror(errno)); |
---|
1073 | } |
---|
1074 | } |
---|
1075 | #endif |
---|
1076 | |
---|
1077 | #if defined(ROAR_HAVE_IO_POSIX) && defined(ROAR_HAVE_UNIX) |
---|
1078 | if ( *server == '/' ) { |
---|
1079 | if ( grp ) { |
---|
1080 | if ( pwd ) { |
---|
1081 | if ( chown(server, pwd->pw_uid, grp->gr_gid) == -1 ) |
---|
1082 | return 1; |
---|
1083 | } else { |
---|
1084 | if ( chown(server, -1, grp->gr_gid) == -1 ) |
---|
1085 | return 1; |
---|
1086 | } |
---|
1087 | #ifdef ROAR_HAVE_GETUID |
---|
1088 | if ( getuid() == 0 ) |
---|
1089 | if ( chmod(server, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) == -1 ) |
---|
1090 | return 1; |
---|
1091 | #endif |
---|
1092 | } |
---|
1093 | } |
---|
1094 | #endif |
---|
1095 | } |
---|
1096 | #endif |
---|
1097 | |
---|
1098 | if ( output_buffer_init(&sa) == -1 ) { |
---|
1099 | ROAR_ERR("Can not init output buffer!"); |
---|
1100 | return 1; |
---|
1101 | } |
---|
1102 | |
---|
1103 | if ( driver == NULL ) { |
---|
1104 | driver = "null"; |
---|
1105 | } else { |
---|
1106 | ROAR_WARN("Usage of old driver interface. use -o not -d!"); |
---|
1107 | } |
---|
1108 | |
---|
1109 | if ( driver_open(&drvinst, &drvid, driver, device, &sa) == -1 ) { |
---|
1110 | ROAR_ERR("Can not open output driver!"); |
---|
1111 | return 1; |
---|
1112 | } |
---|
1113 | |
---|
1114 | if ( samples_init() == -1 ) { |
---|
1115 | ROAR_ERR("Can not init samples!"); |
---|
1116 | return 1; |
---|
1117 | } |
---|
1118 | |
---|
1119 | |
---|
1120 | // we should handle this on microcontrollers, too. |
---|
1121 | #if !defined(ROAR_TARGET_MICROCONTROLLER) && !defined(ROAR_TARGET_WIN32) |
---|
1122 | signal(SIGINT, on_sig_int); |
---|
1123 | signal(SIGCHLD, on_sig_chld); |
---|
1124 | signal(SIGUSR1, on_sig_usr1); |
---|
1125 | signal(SIGPIPE, SIG_IGN); // ignore broken pipes |
---|
1126 | #endif |
---|
1127 | |
---|
1128 | if ( realtime ) { |
---|
1129 | #ifdef DEBUG |
---|
1130 | ROAR_WARN("compiled with -DDEBUG but realtime is enabled: for real realtime support compiel without -DDEBUG"); |
---|
1131 | #endif |
---|
1132 | |
---|
1133 | #ifdef ROAR_HAVE_NICE |
---|
1134 | errno = 0; |
---|
1135 | nice(-5*realtime); // -5 for each --realtime |
---|
1136 | if ( errno ) { |
---|
1137 | ROAR_WARN("Can not decrease nice value by %i: %s", 5*realtime, strerror(errno)); |
---|
1138 | } |
---|
1139 | #else |
---|
1140 | ROAR_WARN("Can not decrease nice value by %i: %s", 5*realtime, strerror(errno)); |
---|
1141 | #endif |
---|
1142 | /* |
---|
1143 | #ifdef __linux__ |
---|
1144 | if ( ioprio_set(IOPRIO_WHO_PROCESS, getpid(), IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 0)) == -1 ) |
---|
1145 | ROAR_WARN("Can not set io priority: %s", strerror(errno)); |
---|
1146 | #endif |
---|
1147 | */ |
---|
1148 | } |
---|
1149 | |
---|
1150 | #ifdef ROAR_HAVE_SETGID |
---|
1151 | if ( setids & R_SETGID ) { |
---|
1152 | if ( setgroups(0, (const gid_t *) NULL) == -1 ) { |
---|
1153 | ROAR_ERR("Can not clear supplementary group IDs: %s", strerror(errno)); |
---|
1154 | } |
---|
1155 | if ( !grp || setgid(grp->gr_gid) == -1 ) { |
---|
1156 | ROAR_ERR("Can not set GroupID: %s", strerror(errno)); |
---|
1157 | } |
---|
1158 | } |
---|
1159 | #endif |
---|
1160 | |
---|
1161 | |
---|
1162 | clients_set_pid(g_self_client, getpid()); |
---|
1163 | #ifdef ROAR_HAVE_GETUID |
---|
1164 | clients_set_uid(g_self_client, getuid()); |
---|
1165 | #endif |
---|
1166 | #ifdef ROAR_HAVE_GETGID |
---|
1167 | clients_set_gid(g_self_client, getgid()); |
---|
1168 | #endif |
---|
1169 | clients_get(g_self_client, &self); |
---|
1170 | |
---|
1171 | if ( self == NULL ) { |
---|
1172 | ROAR_ERR("Can not get self client!"); |
---|
1173 | return 1; |
---|
1174 | } |
---|
1175 | |
---|
1176 | strcpy(self->name, "RoarAudio daemon internal"); |
---|
1177 | |
---|
1178 | #ifdef ROAR_HAVE_FORK |
---|
1179 | if ( daemon ) { |
---|
1180 | close(ROAR_STDIN ); |
---|
1181 | close(ROAR_STDOUT); |
---|
1182 | close(ROAR_STDERR); |
---|
1183 | |
---|
1184 | if ( fork() ) |
---|
1185 | ROAR_U_EXIT(0); |
---|
1186 | |
---|
1187 | #ifdef ROAR_HAVE_SETSID |
---|
1188 | setsid(); |
---|
1189 | #endif |
---|
1190 | clients_set_pid(g_self_client, getpid()); // reset pid as it changed |
---|
1191 | } |
---|
1192 | #endif |
---|
1193 | |
---|
1194 | #ifdef SUPPORT_PIDFILE |
---|
1195 | if ( pidfile != NULL ) { |
---|
1196 | if ( roar_vio_open_file(&pidfile_vio, pidfile, O_WRONLY|O_CREAT, 0644) == -1 ) { |
---|
1197 | ROAR_ERR("Can not write pidfile: %s", pidfile); |
---|
1198 | } else { |
---|
1199 | roar_vio_printf(&pidfile_vio, "%i\n", getpid()); |
---|
1200 | roar_vio_close(&pidfile_vio); |
---|
1201 | } |
---|
1202 | } |
---|
1203 | #endif |
---|
1204 | |
---|
1205 | #ifdef ROAR_HAVE_CHROOT |
---|
1206 | if (chrootdir) { |
---|
1207 | if ( chroot(chrootdir) == -1 ) { |
---|
1208 | ROAR_ERR("Can not chroot to %s: %s", chrootdir, strerror(errno)); |
---|
1209 | return 2; |
---|
1210 | } |
---|
1211 | if ( chdir("/") == -1 ) { |
---|
1212 | ROAR_ERR("Can not chdir to /: %s", strerror(errno)); |
---|
1213 | return 2; |
---|
1214 | } |
---|
1215 | } |
---|
1216 | #endif |
---|
1217 | |
---|
1218 | #ifdef ROAR_HAVE_SETUID |
---|
1219 | if ( setids & R_SETUID ) { |
---|
1220 | if ( !pwd || setuid(pwd->pw_uid) == -1 ) { |
---|
1221 | ROAR_ERR("Can not set UserID: %s", strerror(errno)); |
---|
1222 | return 3; |
---|
1223 | } |
---|
1224 | #ifdef ROAR_HAVE_GETUID |
---|
1225 | clients_set_uid(g_self_client, getuid()); |
---|
1226 | #endif |
---|
1227 | } |
---|
1228 | #endif |
---|
1229 | |
---|
1230 | // Register with OpenSLP: |
---|
1231 | #ifdef ROAR_HAVE_LIBSLP |
---|
1232 | if ( reg_slp ) { |
---|
1233 | register_slp(0, server); |
---|
1234 | } |
---|
1235 | #endif |
---|
1236 | |
---|
1237 | // start main loop... |
---|
1238 | main_loop(drvid, drvinst, &sa, sysclocksync); |
---|
1239 | |
---|
1240 | // clean up. |
---|
1241 | clean_quit_prep(); |
---|
1242 | driver_close(drvinst, drvid); |
---|
1243 | output_buffer_free(); |
---|
1244 | |
---|
1245 | return 0; |
---|
1246 | } |
---|
1247 | |
---|
1248 | void cleanup_listen_socket (int terminate) { |
---|
1249 | // Deregister from SLP: |
---|
1250 | #ifdef ROAR_HAVE_LIBSLP |
---|
1251 | register_slp(1, NULL); |
---|
1252 | #endif |
---|
1253 | |
---|
1254 | #ifdef ROAR_SUPPORT_LISTEN |
---|
1255 | if ( g_listen_socket != -1 ) { |
---|
1256 | #ifdef ROAR_HAVE_IO_POSIX |
---|
1257 | close(g_listen_socket); |
---|
1258 | #endif // #else is useless because we are in void context. |
---|
1259 | |
---|
1260 | g_listen_socket = -1; |
---|
1261 | |
---|
1262 | #ifdef ROAR_HAVE_UNIX |
---|
1263 | if ( *server == '/' ) |
---|
1264 | unlink(server); |
---|
1265 | #endif |
---|
1266 | } |
---|
1267 | |
---|
1268 | #endif |
---|
1269 | |
---|
1270 | if ( terminate ) |
---|
1271 | g_terminate = 1; |
---|
1272 | } |
---|
1273 | |
---|
1274 | void clean_quit_prep (void) { |
---|
1275 | cleanup_listen_socket(0); |
---|
1276 | |
---|
1277 | sources_free(); |
---|
1278 | streams_free(); |
---|
1279 | clients_free(); |
---|
1280 | midi_cb_stop(); // stop console beep |
---|
1281 | midi_free(); |
---|
1282 | light_free(); |
---|
1283 | |
---|
1284 | #ifdef SUPPORT_PIDFILE |
---|
1285 | if ( pidfile != NULL ) |
---|
1286 | unlink(pidfile); |
---|
1287 | #endif |
---|
1288 | } |
---|
1289 | |
---|
1290 | void clean_quit (void) { |
---|
1291 | clean_quit_prep(); |
---|
1292 | // driver_close(drvinst, drvid); |
---|
1293 | // output_buffer_free(); |
---|
1294 | exit(0); |
---|
1295 | } |
---|
1296 | |
---|
1297 | //ll |
---|