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