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