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