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