1 | //roard.c: |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2011 |
---|
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, 51 Franklin Street, Fifth Floor, |
---|
22 | * Boston, MA 02110-1301, USA. |
---|
23 | * |
---|
24 | */ |
---|
25 | |
---|
26 | /* ckport options: |
---|
27 | * Stuff checked by configure: |
---|
28 | * ckport: ignore-symbol: getuid of target win32 |
---|
29 | * ckport: ignore-symbol: setuid of target win32 |
---|
30 | * ckport: ignore-symbol: getgid of target win32 |
---|
31 | * ckport: ignore-symbol: setgid of target win32 |
---|
32 | * ckport: ignore-symbol: setsid of target win32 |
---|
33 | * ckport: ignore-symbol: kill of target win32 |
---|
34 | * ckport: ignore-symbol: chroot of target win32 |
---|
35 | * ckport: ignore-symbol: fork of target win32 |
---|
36 | * ckport: ignore-symbol: getservbyname of target win32 |
---|
37 | * ckport: ignore-symbol: nice of target win32 |
---|
38 | * |
---|
39 | */ |
---|
40 | |
---|
41 | #include "roard.h" |
---|
42 | |
---|
43 | enum action { |
---|
44 | START, |
---|
45 | STOP, |
---|
46 | RESTART, |
---|
47 | RESTART_RETRY, |
---|
48 | SHUTDOWN |
---|
49 | }; |
---|
50 | |
---|
51 | enum af_mode { |
---|
52 | AF_MODE_NONE, |
---|
53 | AF_MODE_LOAD, |
---|
54 | AF_MODE_GEN |
---|
55 | }; |
---|
56 | |
---|
57 | #ifdef ROAR_SUPPORT_LISTEN |
---|
58 | static char * server[ROAR_MAX_LISTEN_SOCKETS]; |
---|
59 | #endif |
---|
60 | |
---|
61 | #if defined(ROAR_HAVE_IO_POSIX) && defined(ROAR_HAVE_FS_POSIX) |
---|
62 | #define SUPPORT_PIDFILE |
---|
63 | static char * pidfile = NULL; |
---|
64 | #endif |
---|
65 | |
---|
66 | #if defined(ROAR_HAVE_SETGID) || defined(ROAR_HAVE_SETUID) |
---|
67 | static int setids = 0; |
---|
68 | #endif |
---|
69 | |
---|
70 | #ifdef ROAR_HAVE_LIBX11 |
---|
71 | static char * x11display = NULL; |
---|
72 | #endif |
---|
73 | |
---|
74 | void dbg_notify_cb(struct roar_notify_core * core, struct roar_event * event, void * userdata) { |
---|
75 | char buf[1024] = ""; |
---|
76 | char estr[1024] = "/* ROAR_??? */"; |
---|
77 | char ttstr[1024] = "/* ROAR_OT_??? */"; |
---|
78 | const char * ttname; |
---|
79 | uint32_t ev = ROAR_EVENT_GET_TYPE(event); |
---|
80 | int i; |
---|
81 | |
---|
82 | ttname = roar_ot2str(event->target_type); |
---|
83 | if ( ttname != NULL ) { |
---|
84 | snprintf(ttstr, sizeof(ttstr)-1, "/* ROAR_OT_%s */", ttname); |
---|
85 | buf[sizeof(ttstr)-1] = 0; |
---|
86 | for (i = 0; ttstr[i] != 0; i++) |
---|
87 | if ( islower(ttstr[i]) ) |
---|
88 | ttstr[i] = toupper(ttstr[i]); |
---|
89 | } |
---|
90 | |
---|
91 | if ( ev == ROAR_NOTIFY_SPECIAL ) { |
---|
92 | snprintf(estr, sizeof(estr)-1, "/* ROAR_NOTIFY_SPECIAL */"); |
---|
93 | } else if ( ROAR_NOTIFY_IS_CMD(ev) ) { |
---|
94 | if ( command_get_name(ROAR_NOTIFY_EVENT2CMD(ev), &ttname) == -1 ) { |
---|
95 | snprintf(estr, sizeof(estr)-1, "/* ROAR_NOTIFY_CMD2EVENT(%i) */", ROAR_NOTIFY_EVENT2CMD(ev)); |
---|
96 | } else { |
---|
97 | snprintf(estr, sizeof(estr)-1, "/* ROAR_NOTIFY_CMD2EVENT(ROAR_CMD_%s) */", ttname); |
---|
98 | for (i = 0; estr[i] != 0; i++) |
---|
99 | if ( islower(estr[i]) ) |
---|
100 | estr[i] = toupper(estr[i]); |
---|
101 | } |
---|
102 | } else if ( ROAR_NOTIFY_IS_EGRP(ev) ) { |
---|
103 | snprintf(estr, sizeof(estr)-1, "/* ROAR_NOTIFY_EGRP2EVENT(%i) */", ROAR_NOTIFY_EVENT2EGRP(ev)); |
---|
104 | } else if ( ROAR_NOTIFY_IS_OE(ev) ) { |
---|
105 | switch (ev) { |
---|
106 | // OE basics: |
---|
107 | case ROAR_OE_BASICS_CHANGE_STATE: |
---|
108 | snprintf(estr, sizeof(estr)-1, "/* ROAR_OE_BASICS_CHANGE_STATE */"); |
---|
109 | break; |
---|
110 | case ROAR_OE_BASICS_CHANGE_FLAGS: |
---|
111 | snprintf(estr, sizeof(estr)-1, "/* ROAR_OE_BASICS_CHANGE_FLAGS */"); |
---|
112 | break; |
---|
113 | case ROAR_OE_BASICS_NEW: |
---|
114 | snprintf(estr, sizeof(estr)-1, "/* ROAR_OE_BASICS_NEW */"); |
---|
115 | break; |
---|
116 | case ROAR_OE_BASICS_DELETE: |
---|
117 | snprintf(estr, sizeof(estr)-1, "/* ROAR_OE_BASICS_DELETE */"); |
---|
118 | break; |
---|
119 | // OE Streams: |
---|
120 | case ROAR_OE_STREAM_CHANGE_VOLUME: |
---|
121 | snprintf(estr, sizeof(estr)-1, "/* ROAR_OE_STREAM_CHANGE_VOLUME */"); |
---|
122 | break; |
---|
123 | case ROAR_OE_STREAM_XRUN: |
---|
124 | snprintf(estr, sizeof(estr)-1, "/* ROAR_OE_STREAM_XRUN */"); |
---|
125 | break; |
---|
126 | case ROAR_OE_STREAM_META_UPDATE: |
---|
127 | snprintf(estr, sizeof(estr)-1, "/* ROAR_OE_STREAM_META_UPDATE */"); |
---|
128 | break; |
---|
129 | // OE Default: |
---|
130 | default: |
---|
131 | snprintf(estr, sizeof(estr)-1, "/* ROAR_NOTIFY_OE2EVENT(%i) */", ROAR_NOTIFY_EVENT2OE(ev)); |
---|
132 | break; |
---|
133 | } |
---|
134 | } else if ( ROAR_NOTIFY_IS_USER(ev) ) { |
---|
135 | snprintf(estr, sizeof(estr)-1, "/* ROAR_NOTIFY_USER2EVENT(%i) */", ROAR_NOTIFY_EVENT2USER(ev)); |
---|
136 | } |
---|
137 | |
---|
138 | buf[sizeof(estr)-1] = 0; |
---|
139 | |
---|
140 | if ( event->flags & ROAR_EVENT_FLAG_PROXYEVENT ) { |
---|
141 | snprintf(buf, sizeof(buf)-1, ".event_proxy=0x%.8x%s, ", (int)event->event_proxy, estr); |
---|
142 | buf[sizeof(buf)-1] = 0; |
---|
143 | } |
---|
144 | |
---|
145 | ROAR_INFO("dbg_notify_cb(core=%p, event=%p{.flags=0x%.8x, event=0x%.8x%s, %s.emitter=%i, .target=%i, .target_type=%i%s, .arg0=%i, .arg1=%i, .arg2=%p}, userdata=%p) = (void)", |
---|
146 | 0 /* always print: minlevel = 0 */, |
---|
147 | core, event, |
---|
148 | (int)event->flags, |
---|
149 | (int)event->event, |
---|
150 | (event->flags & ROAR_EVENT_FLAG_PROXYEVENT ? "" : estr), |
---|
151 | buf, |
---|
152 | event->emitter, |
---|
153 | event->target, |
---|
154 | event->target_type, |
---|
155 | ttstr, |
---|
156 | event->arg0, |
---|
157 | event->arg1, |
---|
158 | event->arg2, |
---|
159 | userdata); |
---|
160 | } |
---|
161 | |
---|
162 | void dbg_notify_cb_register (void) { |
---|
163 | struct roar_event event; |
---|
164 | |
---|
165 | memset(&event, 0, sizeof(event)); |
---|
166 | |
---|
167 | event.event = ROAR_EGRP_ANY_EVENT; |
---|
168 | |
---|
169 | event.emitter = -1; |
---|
170 | event.target = -1; |
---|
171 | event.target_type = -1; |
---|
172 | |
---|
173 | roar_notify_core_subscribe(NULL, &event, dbg_notify_cb, NULL); |
---|
174 | } |
---|
175 | |
---|
176 | #ifdef ROAR_HAVE_MAIN_ARGS |
---|
177 | void usage (void) { |
---|
178 | printf("Usage: roard [OPTIONS]...\n\n"); |
---|
179 | |
---|
180 | printf("Misc Options:\n\n"); |
---|
181 | printf( |
---|
182 | #if defined(ROAR_HAVE_FORK) || defined(ROAR_TARGET_WIN32) |
---|
183 | " --daemon - Bring the server into background after init\n" |
---|
184 | #endif |
---|
185 | " --verbose - Be more verbose, can be used multiple times\n" |
---|
186 | " --terminate - Terminate after last client quited\n" |
---|
187 | " --start - No op parameter (starting roard is default operation)\n" |
---|
188 | " --restart - Trys to stop an old instance and start a new with new settings\n" |
---|
189 | " --stop - Stops a running roard (provide --pidfile!)\n" |
---|
190 | " --shutdown - Terminates a running roard (provide --pidfile!)\n" |
---|
191 | " --realtime - Trys to get realtime priority,\n" |
---|
192 | " give multible times for being more realtime\n" |
---|
193 | " --memlock LEVEL - Set default memory locking level to LEVEL\n" |
---|
194 | #ifdef ROAR_HAVE_CHROOT |
---|
195 | " --chroot DIR - chroots to the given dir\n" |
---|
196 | #endif |
---|
197 | #ifdef ROAR_HAVE_SETGID |
---|
198 | " --setgid - GroupID to the audio group as specified via -G\n" |
---|
199 | #endif |
---|
200 | #ifdef ROAR_HAVE_SETUID |
---|
201 | " --setuid - UserID to the audio user as specified via -U\n" |
---|
202 | #endif |
---|
203 | " --sysclocksync - calculate exact sample rate using the system clock\n" |
---|
204 | " --location LOC - Set lion readable location of server\n" |
---|
205 | " --description DESC - Set lion readable description of server\n" |
---|
206 | #ifdef SUPPORT_PIDFILE |
---|
207 | " --pidfile PIDFILE - Write a pidfile at PIDFILE\n" |
---|
208 | #endif |
---|
209 | #ifdef ROAR_HAVE_SYSLOG |
---|
210 | " --log-syslog - Log Warnings, Errors, ... to syslog\n" |
---|
211 | #endif |
---|
212 | #ifdef ROAR_HAVE_SYSTEM |
---|
213 | " --script-postdown S - Run command lion S after complet shutdown.\n" |
---|
214 | #endif |
---|
215 | ); |
---|
216 | |
---|
217 | printf("\nAuth Options:\n\n"); |
---|
218 | printf( |
---|
219 | " --guest-acclev ACCLEV - Sets the access level for guest access to ACCLEV\n" |
---|
220 | " --trust-acclev ACCLEV - Sets the access level for trust-authed\n" |
---|
221 | " connections to ACCLEV\n" |
---|
222 | " --trust-root - Trust root user\n" |
---|
223 | " --no-trust-root - Don't trust root user\n" |
---|
224 | " --authfile-gen FILE - Generate an new authfile\n" |
---|
225 | " --authfile-load FILE - Load an authfile\n" |
---|
226 | " --authfile-type TYPE - Type of authfile\n" |
---|
227 | " --authfile-acclev ACCLEV\n" |
---|
228 | " - Sets the access level for authfile\n" |
---|
229 | " --new-authfile - Parameters for new authfile follow\n" |
---|
230 | ); |
---|
231 | |
---|
232 | printf("\nPlugin Options:\n\n"); |
---|
233 | printf( |
---|
234 | " --plugin-load FILE - Load plugin FILE\n" |
---|
235 | ); |
---|
236 | |
---|
237 | printf("\nAudio Options:\n\n"); |
---|
238 | printf( |
---|
239 | " -R --rate RATE - Set server rate\n" |
---|
240 | " -B --bits BITS - Set server bits\n" |
---|
241 | " -C --chans CHANNELS - Set server channels\n" |
---|
242 | " --aiprofile PROFILE - Use the given audio profile\n" |
---|
243 | ); |
---|
244 | |
---|
245 | printf("\nStream Options:\n\n"); |
---|
246 | printf( |
---|
247 | " --stream-flags D=F - Set default flags for stream directions\n" |
---|
248 | " D is the stream direction and F is a comma separated\n" |
---|
249 | " list of flags in form +flag or -flag to set or unset\n" |
---|
250 | " a flag as default or remove it from the default\n" |
---|
251 | ); |
---|
252 | printf("\nStream Options:\n\n"); |
---|
253 | printf( |
---|
254 | " --list-rolestack - Show the current role stack\n" |
---|
255 | " --rolestack-push R:A - Add a rule for role R with action A on top\n" |
---|
256 | " of the role stack.\n" |
---|
257 | ); |
---|
258 | |
---|
259 | printf("\nOutput Options:\n\n"); |
---|
260 | printf(" -o --odriver DRV - Set the driver, use '--list-driver' to get a list\n"); |
---|
261 | printf(" -O --odevice DEV - Set the device\n"); |
---|
262 | printf(" -oO OPTS - Set output options\n"); |
---|
263 | printf(" -oN - Adds another output\n"); |
---|
264 | printf(" -oP - Mark output as primary\n"); |
---|
265 | #ifdef ROAR_DRIVER_DEFAULT |
---|
266 | printf(" --list-driver - List all drivers (default driver: %s)\n", ROAR_DRIVER_DEFAULT); |
---|
267 | #else |
---|
268 | printf(" --list-driver - List all drivers (default driver: (autodetect))\n"); |
---|
269 | #endif |
---|
270 | |
---|
271 | #ifndef ROAR_WITHOUT_DCOMP_SOURCES |
---|
272 | printf("\nSource Options:\n\n"); |
---|
273 | printf(" -s --source DRV - Use DRV as input driver\n" |
---|
274 | " -S DEV - Use DEV as input device\n" |
---|
275 | " -sO OPTS - Use OPTS as input options\n" |
---|
276 | " -sN - Adds another source\n" |
---|
277 | " -sP - Make souce as primary\n" |
---|
278 | ); |
---|
279 | printf(" --list-sources - List all sources\n"); |
---|
280 | #endif |
---|
281 | |
---|
282 | #ifndef ROAR_WITHOUT_DCOMP_MIXER |
---|
283 | printf("\nHardware Mixer Options:\n\n"); |
---|
284 | printf(" -m --mixer DRV - Use DRV as mixer driver\n" |
---|
285 | " -M DEV - Use DEV as mixer device\n" |
---|
286 | " -mO OPTS - Use OPTS as mixer options\n" |
---|
287 | " -mN - Adds another mixer\n" |
---|
288 | " -mP - Make mixer as primary\n" |
---|
289 | ); |
---|
290 | printf(" --list-mixers - List all mixers\n"); |
---|
291 | #endif |
---|
292 | |
---|
293 | printf("\nCodec Filter Options:\n\n"); |
---|
294 | printf(" --list-cf - List all codec filter\n" |
---|
295 | ); |
---|
296 | |
---|
297 | #ifndef ROAR_WITHOUT_DCOMP_MIDI |
---|
298 | printf("\nMIDI Options:\n\n"); |
---|
299 | printf(" --midi-no-console - Disable console based MIDI synth\n" |
---|
300 | " --midi-console-enable - Enables the console based MIDI synth\n" |
---|
301 | " --midi-console DEV - Set device for MIDI console\n" |
---|
302 | #ifndef ROAR_WITHOUT_DCOMP_SSYNTH |
---|
303 | " --ssynth-enable - Enable simple software synth\n" |
---|
304 | " --ssynth-disable - Disable simple software synth\n" |
---|
305 | #endif |
---|
306 | ); |
---|
307 | #endif |
---|
308 | |
---|
309 | #ifndef ROAR_WITHOUT_DCOMP_LIGHT |
---|
310 | printf("\nLight Control Options:\n\n"); |
---|
311 | printf(" --light-channels NUM - Sets the number of channels for Light control (default: %i)\n", |
---|
312 | LIGHT_CHANNELS_DEFAULT |
---|
313 | ); |
---|
314 | #endif |
---|
315 | |
---|
316 | #ifndef ROAR_WITHOUT_DCOMP_RDTCS |
---|
317 | printf("\nRadio Data and Transmitter Control System Options:\n\n"); |
---|
318 | printf(" --rds-pi PI - Sets the RDS Programme Identification (PI)\n" |
---|
319 | " --rds-ps PS - Sets the RDS Programme Service Name (PS)\n" |
---|
320 | " --rds-pty PTY - Sets the RDS Programme Type (PTY)\n" |
---|
321 | " --rds-tp - Sets the RDS Traffic Programme (TP) flag\n" |
---|
322 | " --rds-ct - Enables sending of RDS Clock Time (CT)\n" |
---|
323 | ); |
---|
324 | #endif |
---|
325 | |
---|
326 | #ifdef ROAR_HAVE_LIBX11 |
---|
327 | printf("\nX11 Options:\n\n"); |
---|
328 | printf( |
---|
329 | " --x11-display DISPLAY - Set display for X11\n" |
---|
330 | " --display DISPLAY - Set display for X11\n" |
---|
331 | ); |
---|
332 | #endif |
---|
333 | |
---|
334 | printf("\nServer Options:\n\n"); |
---|
335 | printf(" -t --tcp - Use TCP listen socket\n" |
---|
336 | #ifdef ROAR_HAVE_UNIX |
---|
337 | " -u --unix - Use UNIX Domain listen socket (default)\n" |
---|
338 | #endif |
---|
339 | #ifdef ROAR_HAVE_LIBDNET |
---|
340 | " -n --decnet - use DECnet listen socket\n" |
---|
341 | #endif |
---|
342 | " -4 - Use IPv4 connections (implies -t)\n" |
---|
343 | #ifdef AF_INET6 |
---|
344 | " -6 - Use IPv6 connections (implies -t)\n" |
---|
345 | #endif |
---|
346 | #ifdef IPV6_ADDRFORM |
---|
347 | " -64 - Try to downgrade sockets from IPv6 into IPv4,\n" |
---|
348 | " this is normaly not useful.\n" |
---|
349 | #endif |
---|
350 | " -p --port - TCP Port to bind to\n" |
---|
351 | " -b --bind - IP/Hostname to bind to\n" |
---|
352 | " --sock - Filename for UNIX Domain Socket\n" |
---|
353 | " --proto PROTO - Use PROTO as protocol on Socket\n" |
---|
354 | " --proto-dir DIR - Set direction parameter for protocol\n" |
---|
355 | " --proto-rate RATE - Set sample rate parameter for protocol\n" |
---|
356 | " --proto-bits BITS - Set bits per sample parameter for protocol\n" |
---|
357 | " --proto-codec E - Set codec parameter for protocol\n" |
---|
358 | " --proto-chans C - Set number of channels parameter for protocol\n" |
---|
359 | " --proto-aiprofile PROFILE\n" |
---|
360 | " - Sets the audio profile for socket\n" |
---|
361 | " --proto-profile P - Set profile for listen socket\n" |
---|
362 | " --list-proto - List supported protocols\n" |
---|
363 | " --list-profiles - List supported profiles for --proto-profile\n" |
---|
364 | " --list-aiprofiles - List supported profiles for --proto-aiprofile\n" |
---|
365 | " --new-sock - Parameters for new socket follow\n" |
---|
366 | #ifdef ROAR_HAVE_LIBSLP |
---|
367 | " --slp - Enable OpenSLP support\n" |
---|
368 | #endif |
---|
369 | #ifdef ROAR_HAVE_LIBX11 |
---|
370 | " --x11 - Enable X11 support\n" |
---|
371 | #endif |
---|
372 | " --jumbo-mtu MTU - Sets the MTU for Jumbo Packets\n" |
---|
373 | " -G GROUP - Sets the group for the UNIX Domain Socket, (default: %s)\n" |
---|
374 | " You need the permissions to change the GID\n" |
---|
375 | " -U USER - Sets the user for the UNIX Domain Socket, (default: do not set)\n" |
---|
376 | " You need the permissions to change the UID (normaly only root has)\n" |
---|
377 | " --no-listen - Do not listen for new clients\n" |
---|
378 | " (only useful for relaing, impleys --terminate)\n" |
---|
379 | " --client-fh - Comunicate with a client over this handle\n" |
---|
380 | " (only useful for relaing)\n" |
---|
381 | " --close-fh - Closes the given fh\n" |
---|
382 | " --standby - Start in standby state\n" |
---|
383 | " --auto-standby - Automatical goes into standby if there are no streams\n", |
---|
384 | #ifdef ROAR_DEFAULT_SOCKGRP |
---|
385 | ROAR_DEFAULT_SOCKGRP |
---|
386 | #else |
---|
387 | "(none)" |
---|
388 | #endif |
---|
389 | ); |
---|
390 | // printf("\n Options:\n\n"); |
---|
391 | printf("\n"); |
---|
392 | } |
---|
393 | |
---|
394 | static void list_proto (void) { |
---|
395 | printf(" Protocol Flag Subsys - Description\n"); |
---|
396 | printf("------------------------------------------------------\n"); |
---|
397 | printf(" roar WM LRX - RoarAudio native protocol\n"); |
---|
398 | #if !defined(ROAR_WITHOUT_DCOMP_EMUL_ESD) && defined(ROAR_HAVE_H_ESD) |
---|
399 | printf(" esd W - EsounD emulation\n"); |
---|
400 | #endif |
---|
401 | #ifndef ROAR_WITHOUT_DCOMP_EMUL_SIMPLE |
---|
402 | printf(" simple WM LRX - PulseAudio simple protocol\n"); |
---|
403 | #endif |
---|
404 | #ifndef ROAR_WITHOUT_DCOMP_EMUL_RSOUND |
---|
405 | printf(" rsound W - RSound emulation\n"); |
---|
406 | #endif |
---|
407 | #ifndef ROAR_WITHOUT_DCOMP_EMUL_RPLAY |
---|
408 | printf(" rplay W - RPlay emulation\n"); |
---|
409 | #endif |
---|
410 | #ifndef ROAR_WITHOUT_DCOMP_EMUL_GOPHER |
---|
411 | printf(" gopher - The Internet Gopher Protocol\n"); |
---|
412 | #endif |
---|
413 | } |
---|
414 | |
---|
415 | #endif |
---|
416 | |
---|
417 | #define _pmsg(format, args...) roar_debug_msg(type, __LINE__, __FILE__, ROAR_DBG_PREFIX, format, ## args) |
---|
418 | #define _pmsgc(group, counter, name) _pmsg(" Counter %-10s: %llu", (name), (long long unsigned int)counters_get(group, counter)) |
---|
419 | void counters_print(int type, int force) { |
---|
420 | if ( type != ROAR_DEBUG_TYPE_INFO || force || (ROAR_DBG_INFOVAR) >= ROAR_DBG_INFO_INFO ) { |
---|
421 | _pmsg("--- Counter Listing ---"); |
---|
422 | _pmsg(" Current:"); |
---|
423 | _pmsgc(cur, clients, "Clients"); |
---|
424 | _pmsgc(cur, streams, "Streams"); |
---|
425 | _pmsg(" Total:"); |
---|
426 | _pmsgc(sum, clients, "Clients"); |
---|
427 | _pmsgc(sum, streams, "Streams"); |
---|
428 | _pmsg("--- End of Counter Listing ---"); |
---|
429 | } |
---|
430 | } |
---|
431 | #undef _pmsgc |
---|
432 | #undef _pmsg |
---|
433 | |
---|
434 | int restart_server (char * server, int terminate) { |
---|
435 | struct roar_connection con; |
---|
436 | #ifdef ROAR_HAVE_KILL |
---|
437 | char buf[80]; |
---|
438 | ssize_t l; |
---|
439 | struct roar_vio_calls fh; |
---|
440 | pid_t pid; |
---|
441 | int ok; |
---|
442 | |
---|
443 | ROAR_INFO("restart_server(server='%s', terminate=%i): trying to restart server", ROAR_DBG_INFO_INFO, server, terminate); |
---|
444 | |
---|
445 | if ( pidfile != NULL ) { |
---|
446 | if ( roar_vio_open_file(&fh, pidfile, O_RDONLY, 0644) == -1 ) { |
---|
447 | ROAR_WARN("restart_server(*): Can not read pidfile: %s", pidfile); |
---|
448 | } else { |
---|
449 | l = roar_vio_read(&fh, buf, 80); |
---|
450 | roar_vio_close(&fh); |
---|
451 | if ( l > 0 ) { |
---|
452 | buf[l-1] = 0; |
---|
453 | buf[79] = 0; |
---|
454 | pid = atoi(buf); |
---|
455 | if ( terminate ) { |
---|
456 | ok = kill(pid, SIGUSR1); |
---|
457 | } else { |
---|
458 | ok = kill(pid, SIGINT); |
---|
459 | } |
---|
460 | if ( ok == 0 ) { |
---|
461 | return 0; |
---|
462 | } else { |
---|
463 | ROAR_WARN("restart_server(*): Can not kill roard by pidfile"); |
---|
464 | } |
---|
465 | } else { |
---|
466 | ROAR_WARN("restart_server(*): Can not find a PID in the pidfile"); |
---|
467 | } |
---|
468 | } |
---|
469 | } |
---|
470 | #endif |
---|
471 | |
---|
472 | if ( roar_simple_connect(&con, server, "roard") == -1 ) { |
---|
473 | return -1; |
---|
474 | } |
---|
475 | |
---|
476 | if ( roar_terminate(&con, terminate) == -1 ) { |
---|
477 | return -1; |
---|
478 | } |
---|
479 | |
---|
480 | return roar_disconnect(&con); |
---|
481 | } |
---|
482 | |
---|
483 | #define R_SETUID 1 |
---|
484 | #define R_SETGID 2 |
---|
485 | |
---|
486 | int init_config (void) { |
---|
487 | int i; |
---|
488 | |
---|
489 | memset(g_config, 0, sizeof(struct roard_config)); |
---|
490 | |
---|
491 | for (i = 0; i < ROAR_DIR_DIRIDS; i++) { |
---|
492 | g_config->streams[i].mixer_channels = 1; |
---|
493 | g_config->streams[i].mixer.rpg_mul = 1; |
---|
494 | g_config->streams[i].mixer.rpg_div = 1; |
---|
495 | g_config->streams[i].mixer.scale = 65535; |
---|
496 | g_config->streams[i].mixer.mixer[0] = g_config->streams[i].mixer.scale; |
---|
497 | } |
---|
498 | |
---|
499 | g_config->streams[ROAR_DIR_PLAY ].flags = ROAR_FLAG_META; |
---|
500 | g_config->streams[ROAR_DIR_OUTPUT ].flags = ROAR_FLAG_PASSMIXER; |
---|
501 | g_config->streams[ROAR_DIR_FILTER ].flags = ROAR_FLAG_SYNC; |
---|
502 | g_config->streams[ROAR_DIR_MIDI_OUT].flags = ROAR_FLAG_SYNC; |
---|
503 | g_config->streams[ROAR_DIR_BIDIR ].flags = ROAR_FLAG_ANTIECHO; |
---|
504 | |
---|
505 | g_config->location = CONF_DEF_STRING; |
---|
506 | g_config->description = CONF_DEF_STRING; |
---|
507 | |
---|
508 | g_config->memlock_level = -1; |
---|
509 | |
---|
510 | #ifdef ROAR_HAVE_SYSTEM |
---|
511 | g_config->scripts.post_shutdown = NULL; |
---|
512 | #endif |
---|
513 | |
---|
514 | return 0; |
---|
515 | } |
---|
516 | |
---|
517 | #ifdef ROAR_SUPPORT_LISTEN |
---|
518 | int init_listening (void) { |
---|
519 | int i; |
---|
520 | |
---|
521 | memset(g_listen, 0, sizeof(g_listen)); |
---|
522 | |
---|
523 | for (i = 0; i < ROAR_MAX_LISTEN_SOCKETS; i++) { |
---|
524 | g_listen[i].proto = ROAR_PROTO_ROARAUDIO; |
---|
525 | server[i] = NULL; |
---|
526 | } |
---|
527 | |
---|
528 | return 0; |
---|
529 | } |
---|
530 | |
---|
531 | int get_listen(struct roard_listen ** sock, char *** sockname) { |
---|
532 | int i; |
---|
533 | |
---|
534 | if ( sock == NULL ) |
---|
535 | return -1; |
---|
536 | |
---|
537 | for (i = 0; i < ROAR_MAX_LISTEN_SOCKETS; i++) { |
---|
538 | if ( ! g_listen[i].used ) { |
---|
539 | server[i] = NULL; |
---|
540 | *sock = &(g_listen[i]); |
---|
541 | |
---|
542 | if ( sockname != NULL ) |
---|
543 | *sockname = &(server[i]); |
---|
544 | |
---|
545 | return 0; |
---|
546 | } |
---|
547 | } |
---|
548 | |
---|
549 | return -1; |
---|
550 | } |
---|
551 | |
---|
552 | #ifdef ROAR_SUPPORT_LISTEN |
---|
553 | static struct _listen_profile { |
---|
554 | const char * name; |
---|
555 | int type; |
---|
556 | int port; |
---|
557 | const char * sockaddr; |
---|
558 | int proto; |
---|
559 | int dir; |
---|
560 | const char * aiprofile; |
---|
561 | const char * desc; |
---|
562 | } _g_listen_profiles[] = { |
---|
563 | // RoarAudio: |
---|
564 | #ifdef ROAR_HAVE_UNIX |
---|
565 | {"roar-usock", ROAR_SOCKET_TYPE_UNIX, 0, "~/" ROAR_DEFAULT_SOCK_USER, |
---|
566 | ROAR_PROTO_ROARAUDIO, -1, NULL, |
---|
567 | "RoarAudio default user profile"}, |
---|
568 | {"roar-gsock", ROAR_SOCKET_TYPE_UNIX, 0, ROAR_DEFAULT_SOCK_GLOBAL, |
---|
569 | ROAR_PROTO_ROARAUDIO, -1, NULL, |
---|
570 | "RoarAudio default global profile"}, |
---|
571 | #endif |
---|
572 | #ifdef ROAR_HAVE_IPV4 |
---|
573 | {"roar-tcp", ROAR_SOCKET_TYPE_TCP, ROAR_DEFAULT_PORT, ROAR_DEFAULT_INET4_HOST, |
---|
574 | ROAR_PROTO_ROARAUDIO, -1, NULL, |
---|
575 | "RoarAudio local TCP profile"}, |
---|
576 | {"roar-tcp-pub", ROAR_SOCKET_TYPE_TCP, ROAR_DEFAULT_PORT, ROAR_NET_INET4_ANYHOST, |
---|
577 | ROAR_PROTO_ROARAUDIO, -1, NULL, |
---|
578 | "RoarAudio network TCP profile"}, |
---|
579 | #endif |
---|
580 | #ifdef ROAR_HAVE_IPV6 |
---|
581 | {"roar-tcp6", ROAR_SOCKET_TYPE_TCP6, ROAR_DEFAULT_PORT, ROAR_DEFAULT_INET6_HOST, |
---|
582 | ROAR_PROTO_ROARAUDIO, -1, NULL, |
---|
583 | "RoarAudio local TCP profile"}, |
---|
584 | {"roar-tcp-pub6", ROAR_SOCKET_TYPE_TCP6, ROAR_DEFAULT_PORT, ROAR_NET_INET6_ANYHOST, |
---|
585 | ROAR_PROTO_ROARAUDIO, -1, NULL, |
---|
586 | "RoarAudio network TCP profile"}, |
---|
587 | #endif |
---|
588 | #ifdef ROAR_HAVE_LIBDNET |
---|
589 | {"roar-dnet", ROAR_SOCKET_TYPE_DECNET, 0, ROAR_DEFAULT_LISTEN_OBJECT, |
---|
590 | ROAR_PROTO_ROARAUDIO, -1, NULL, |
---|
591 | "RoarAudio default DECnet"}, |
---|
592 | #endif |
---|
593 | #ifdef ROAR_HAVE_UNIX |
---|
594 | {"roar-abstract", ROAR_SOCKET_TYPE_UNKNOWN, 0, "+abstract", ROAR_PROTO_ROARAUDIO, -1, NULL, |
---|
595 | "RoarAudio abstract namespace profile"}, |
---|
596 | #endif |
---|
597 | |
---|
598 | // EsounD: |
---|
599 | #if !defined(ROAR_WITHOUT_DCOMP_EMUL_ESD) && defined(ROAR_HAVE_H_ESD) |
---|
600 | #ifdef ROAR_HAVE_UNIX |
---|
601 | {"esd-unix", ROAR_SOCKET_TYPE_UNIX, 0, ROAR_DEFAULT_ESD_GSOCK, |
---|
602 | ROAR_PROTO_ESOUND, -1, NULL, |
---|
603 | "EsounD default local profile"}, |
---|
604 | #endif |
---|
605 | #ifdef ROAR_HAVE_IPV4 |
---|
606 | {"esd-tcp", ROAR_SOCKET_TYPE_TCP, ROAR_DEFAULT_ESD_PORT, ROAR_NET_INET4_LOCALHOST, |
---|
607 | ROAR_PROTO_ESOUND, -1, NULL, |
---|
608 | "EsounD local TCP profile"}, |
---|
609 | {"esd-tcp-pub", ROAR_SOCKET_TYPE_TCP, ROAR_DEFAULT_ESD_PORT, ROAR_NET_INET4_ANYHOST, |
---|
610 | ROAR_PROTO_ESOUND, -1, NULL, |
---|
611 | "EsounD network TCP profile"}, |
---|
612 | #endif |
---|
613 | #ifdef ROAR_HAVE_IPV6 |
---|
614 | {"esd-tcp6", ROAR_SOCKET_TYPE_TCP6, ROAR_DEFAULT_ESD_PORT, ROAR_NET_INET6_LOCALHOST, |
---|
615 | ROAR_PROTO_ESOUND, -1, NULL, |
---|
616 | "EsounD local TCP profile"}, |
---|
617 | {"esd-tcp-pub6", ROAR_SOCKET_TYPE_TCP6, ROAR_DEFAULT_ESD_PORT, ROAR_NET_INET6_ANYHOST, |
---|
618 | ROAR_PROTO_ESOUND, -1, NULL, |
---|
619 | "EsounD network TCP profile"}, |
---|
620 | #endif |
---|
621 | #endif |
---|
622 | |
---|
623 | // RSound: |
---|
624 | #ifndef ROAR_WITHOUT_DCOMP_EMUL_RSOUND |
---|
625 | #ifdef ROAR_HAVE_UNIX |
---|
626 | {"rsound-unix", ROAR_SOCKET_TYPE_UNIX, 0, ROAR_DEFAULT_RSOUND_GSOCK, |
---|
627 | ROAR_PROTO_RSOUND, -1, NULL, |
---|
628 | "RSound default local profile"}, |
---|
629 | #endif |
---|
630 | #ifdef ROAR_HAVE_IPV4 |
---|
631 | {"rsound-tcp", ROAR_SOCKET_TYPE_TCP, ROAR_DEFAULT_RSOUND_PORT, ROAR_NET_INET4_LOCALHOST, |
---|
632 | ROAR_PROTO_RSOUND, -1, NULL, |
---|
633 | "RSound local TCP profile"}, |
---|
634 | {"rsound-tcp-pub", ROAR_SOCKET_TYPE_TCP, ROAR_DEFAULT_RSOUND_PORT, ROAR_NET_INET4_ANYHOST, |
---|
635 | ROAR_PROTO_RSOUND, -1, NULL, |
---|
636 | "RSound network TCP profile"}, |
---|
637 | #endif |
---|
638 | #ifdef ROAR_HAVE_IPV6 |
---|
639 | {"rsound-tcp6", ROAR_SOCKET_TYPE_TCP6, ROAR_DEFAULT_RSOUND_PORT, ROAR_NET_INET6_LOCALHOST, |
---|
640 | ROAR_PROTO_RSOUND, -1, NULL, |
---|
641 | "RSound local TCP profile"}, |
---|
642 | {"rsound-tcp-pub6", ROAR_SOCKET_TYPE_TCP6, ROAR_DEFAULT_RSOUND_PORT, ROAR_NET_INET6_ANYHOST, |
---|
643 | ROAR_PROTO_RSOUND, -1, NULL, |
---|
644 | "RSound network TCP profile"}, |
---|
645 | #endif |
---|
646 | #ifdef ROAR_HAVE_LIBDNET |
---|
647 | {"rsound-dnet", ROAR_SOCKET_TYPE_DECNET, 0, ROAR_DEFAULT_RSOUND_OBJECT, |
---|
648 | ROAR_PROTO_RSOUND, -1, NULL, |
---|
649 | "RSound DECnet profile"}, |
---|
650 | #endif |
---|
651 | #endif |
---|
652 | |
---|
653 | // PulseAudio Simple: |
---|
654 | #ifndef ROAR_WITHOUT_DCOMP_EMUL_SIMPLE |
---|
655 | #ifdef ROAR_HAVE_IPV4 |
---|
656 | {"pas-play-tcp", ROAR_SOCKET_TYPE_TCP, ROAR_DEFAULT_PA_PORT, ROAR_NET_INET4_ANYHOST, |
---|
657 | ROAR_PROTO_SIMPLE, ROAR_DIR_PLAY, "default", |
---|
658 | "PulseAudio Simple TCP play profile"}, |
---|
659 | {"pas-mon-tcp", ROAR_SOCKET_TYPE_TCP, ROAR_DEFAULT_PA_PORT, ROAR_NET_INET4_ANYHOST, |
---|
660 | ROAR_PROTO_SIMPLE, ROAR_DIR_MONITOR, "default", |
---|
661 | "PulseAudio Simple TCP monitor profile"}, |
---|
662 | #endif |
---|
663 | #ifdef ROAR_HAVE_IPV6 |
---|
664 | {"pas-play-tcp6", ROAR_SOCKET_TYPE_TCP6, ROAR_DEFAULT_PA_PORT, ROAR_NET_INET6_ANYHOST, |
---|
665 | ROAR_PROTO_SIMPLE, ROAR_DIR_PLAY, "default", |
---|
666 | "PulseAudio Simple TCP play profile"}, |
---|
667 | {"pas-mon-tcp6", ROAR_SOCKET_TYPE_TCP6, ROAR_DEFAULT_PA_PORT, ROAR_NET_INET6_ANYHOST, |
---|
668 | ROAR_PROTO_SIMPLE, ROAR_DIR_MONITOR, "default", |
---|
669 | "PulseAudio Simple TCP monitor profile"}, |
---|
670 | #endif |
---|
671 | #endif |
---|
672 | |
---|
673 | // RPlay: |
---|
674 | #ifndef ROAR_WITHOUT_DCOMP_EMUL_RPLAY |
---|
675 | #ifdef ROAR_HAVE_IPV4 |
---|
676 | {"rplay-tcp", ROAR_SOCKET_TYPE_TCP, ROAR_DEFAULT_RPLAY_PORT, ROAR_NET_INET4_LOCALHOST, |
---|
677 | ROAR_PROTO_RPLAY, -1, NULL, |
---|
678 | "RPlay local TCP profile"}, |
---|
679 | {"rplay-tcp-pub", ROAR_SOCKET_TYPE_TCP, ROAR_DEFAULT_RPLAY_PORT, ROAR_NET_INET4_ANYHOST, |
---|
680 | ROAR_PROTO_RPLAY, -1, NULL, |
---|
681 | "RPlay network TCP profile"}, |
---|
682 | #endif |
---|
683 | #ifdef ROAR_HAVE_IPV6 |
---|
684 | {"rplay-tcp6", ROAR_SOCKET_TYPE_TCP6, ROAR_DEFAULT_RPLAY_PORT, ROAR_NET_INET6_LOCALHOST, |
---|
685 | ROAR_PROTO_RPLAY, -1, NULL, |
---|
686 | "RPlay local TCP profile"}, |
---|
687 | {"rplay-tcp-pub6", ROAR_SOCKET_TYPE_TCP6, ROAR_DEFAULT_RPLAY_PORT, ROAR_NET_INET6_ANYHOST, |
---|
688 | ROAR_PROTO_RPLAY, -1, NULL, |
---|
689 | "RPlay network TCP profile"}, |
---|
690 | #endif |
---|
691 | #endif |
---|
692 | |
---|
693 | // Gopher: |
---|
694 | #ifndef ROAR_WITHOUT_DCOMP_EMUL_GOPHER |
---|
695 | #ifdef ROAR_HAVE_IPV4 |
---|
696 | {"gopher-tcp", ROAR_SOCKET_TYPE_TCP, ROAR_DEFAULT_GOPHER_PORT, ROAR_NET_INET4_LOCALHOST, |
---|
697 | ROAR_PROTO_GOPHER, -1, NULL, |
---|
698 | "Gopher local TCP profile"}, |
---|
699 | {"gopher-tcp-pub", ROAR_SOCKET_TYPE_TCP, ROAR_DEFAULT_GOPHER_PORT, ROAR_NET_INET4_ANYHOST, |
---|
700 | ROAR_PROTO_GOPHER, -1, NULL, |
---|
701 | "Gopher network TCP profile"}, |
---|
702 | #endif |
---|
703 | #ifdef ROAR_HAVE_IPV6 |
---|
704 | {"gopher-tcp6", ROAR_SOCKET_TYPE_TCP6, ROAR_DEFAULT_GOPHER_PORT, ROAR_NET_INET6_LOCALHOST, |
---|
705 | ROAR_PROTO_GOPHER, -1, NULL, |
---|
706 | "Gopher local TCP profile"}, |
---|
707 | {"gopher-tcp-pub6", ROAR_SOCKET_TYPE_TCP6, ROAR_DEFAULT_GOPHER_PORT, ROAR_NET_INET6_ANYHOST, |
---|
708 | ROAR_PROTO_GOPHER, -1, NULL, |
---|
709 | "Gopher network TCP profile"}, |
---|
710 | #endif |
---|
711 | #endif |
---|
712 | |
---|
713 | // End of List: |
---|
714 | {NULL, -1, -1, NULL, -1, -1, NULL, NULL} |
---|
715 | }; |
---|
716 | |
---|
717 | void listen_listen_profiles (void) { |
---|
718 | struct _listen_profile * p; |
---|
719 | char * type; |
---|
720 | int i; |
---|
721 | char port[8]; |
---|
722 | |
---|
723 | printf("Name Type Address Port Protocol Dir Audio Profile - Description\n"); |
---|
724 | printf("-------------------------------------------------------------------------------------------------\n"); |
---|
725 | //roar-tcp-pub 0.0.0.0 16002 RoarAudio unknown (null) - (null) |
---|
726 | |
---|
727 | for (i = 0; (p = &(_g_listen_profiles[i]))->name != NULL; i++) { |
---|
728 | switch (p->type) { |
---|
729 | case ROAR_SOCKET_TYPE_UNIX: type = "UNIX"; break; |
---|
730 | case ROAR_SOCKET_TYPE_TCP: type = "TCP"; break; |
---|
731 | case ROAR_SOCKET_TYPE_DECNET: type = "DECnet"; break; |
---|
732 | case ROAR_SOCKET_TYPE_TCP6: type = "TCP6"; break; |
---|
733 | default: |
---|
734 | type = "unknown"; |
---|
735 | break; |
---|
736 | } |
---|
737 | |
---|
738 | if ( p->port ) { |
---|
739 | snprintf(port, sizeof(port)-1, "%i", p->port); |
---|
740 | port[sizeof(port)-1] = 0; |
---|
741 | } else { |
---|
742 | strcpy(port, "(none)"); |
---|
743 | } |
---|
744 | |
---|
745 | printf("%-15s %-7s %-16s %-7s %-9s %-10s %-13s - %s\n", |
---|
746 | p->name, |
---|
747 | type, |
---|
748 | p->sockaddr, port, |
---|
749 | roar_proto2str(p->proto), |
---|
750 | p->dir == -1 ? "(none)" : roar_dir2str(p->dir), p->aiprofile == NULL ? "(none)" : p->aiprofile, |
---|
751 | p->desc == NULL ? "" : p->desc); |
---|
752 | } |
---|
753 | } |
---|
754 | |
---|
755 | int get_listen_profile (const char * name, |
---|
756 | int * port, char ** sockaddr, int * type, |
---|
757 | int * proto, |
---|
758 | int * dir, struct roar_audio_info * info) { |
---|
759 | static char buf[1024]; |
---|
760 | struct _listen_profile * p; |
---|
761 | int i; |
---|
762 | |
---|
763 | for (i = 0; (p = &(_g_listen_profiles[i]))->name != NULL; i++) { |
---|
764 | if ( !strcasecmp(p->name, name) ) { |
---|
765 | *port = p->port; |
---|
766 | |
---|
767 | if ( p->type == ROAR_SOCKET_TYPE_UNIX && p->sockaddr[0] != '+' ) { |
---|
768 | roar_env_render_path_r(buf, sizeof(buf), p->sockaddr); |
---|
769 | } else { |
---|
770 | strncpy(buf, p->sockaddr, sizeof(buf)); |
---|
771 | } |
---|
772 | *sockaddr = buf; |
---|
773 | *type = p->type; |
---|
774 | |
---|
775 | *proto = p->proto; |
---|
776 | *dir = p->dir; |
---|
777 | |
---|
778 | if ( p->aiprofile != NULL ) { |
---|
779 | if ( roar_profile2info(info, p->aiprofile) == -1 ) { |
---|
780 | ROAR_ERR("Unknown audio profile: %s", p->aiprofile); |
---|
781 | return -1; |
---|
782 | } |
---|
783 | } |
---|
784 | return 0; |
---|
785 | } |
---|
786 | } |
---|
787 | |
---|
788 | return -1; |
---|
789 | } |
---|
790 | #endif |
---|
791 | |
---|
792 | int add_listen (char * addr, int port, int sock_type, char * user, char * group, int proto, int dir, struct roar_audio_info * info) { |
---|
793 | #if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX) |
---|
794 | struct group * grp = NULL; |
---|
795 | #endif |
---|
796 | #if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX) |
---|
797 | struct passwd * pwd = NULL; |
---|
798 | #endif |
---|
799 | #ifdef ROAR_HAVE_UNIX |
---|
800 | char * env_roar_proxy_backup; |
---|
801 | #endif |
---|
802 | int sockid = -1; |
---|
803 | #ifdef ROAR_HAVE_UNIX |
---|
804 | int sock; |
---|
805 | #endif |
---|
806 | int i; |
---|
807 | |
---|
808 | ROAR_INFO("add_listen(addr='%s', port=%i, sock_type=%i, user='%s', group='%s', proto=%s(%i), dir=%s(%i), info={.rate=%u, .bits=%u, .channels=%u, .codec=%s(%u)}): trying to add listen socket", |
---|
809 | ROAR_DBG_INFO_INFO, |
---|
810 | addr, port, sock_type, user, group, roar_proto2str(proto), proto, roar_dir2str(dir), dir, |
---|
811 | info->rate, info->bits, info->channels, roar_codec2str(info->codec), info->codec); |
---|
812 | |
---|
813 | if ( *addr != 0 ) { |
---|
814 | for (i = 0; i < ROAR_MAX_LISTEN_SOCKETS; i++) { |
---|
815 | if ( ! g_listen[i].used ) { |
---|
816 | sockid = i; |
---|
817 | break; |
---|
818 | } |
---|
819 | } |
---|
820 | |
---|
821 | if ( sockid == -1 ) |
---|
822 | return -1; |
---|
823 | |
---|
824 | g_listen[sockid].proto = proto; |
---|
825 | |
---|
826 | ROAR_DBG("add_listen(*): proto=0x%.4x", proto); |
---|
827 | |
---|
828 | if ( roar_vio_open_socket_listen(&(g_listen[sockid].sock), sock_type, addr, port) == -1 ) { |
---|
829 | #ifdef ROAR_HAVE_UNIX |
---|
830 | if ( *addr == '/' ) { |
---|
831 | if ( (env_roar_proxy_backup = getenv("ROAR_PROXY")) != NULL ) { |
---|
832 | env_roar_proxy_backup = roar_mm_strdup(env_roar_proxy_backup); |
---|
833 | unsetenv("ROAR_PROXY"); |
---|
834 | } |
---|
835 | if ( (sock = roar_socket_connect(addr, port)) != -1 ) { |
---|
836 | close(sock); |
---|
837 | ROAR_ERR("Can not open listen socket: Socket allready in use"); |
---|
838 | return -1; |
---|
839 | } else { |
---|
840 | unlink(addr); |
---|
841 | if ( roar_vio_open_socket_listen(&(g_listen[sockid].sock), sock_type, addr, port) == -1 ) { |
---|
842 | ROAR_ERR("Can not open listen socket: %s", strerror(errno)); |
---|
843 | return -1; |
---|
844 | } |
---|
845 | } |
---|
846 | if ( env_roar_proxy_backup != NULL ) { |
---|
847 | setenv("ROAR_PROXY", env_roar_proxy_backup, 0); |
---|
848 | roar_mm_free(env_roar_proxy_backup); |
---|
849 | } |
---|
850 | #else |
---|
851 | if (0) { // noop |
---|
852 | #endif |
---|
853 | } else { |
---|
854 | ROAR_ERR("Can not open listen socket: %s", strerror(errno)); |
---|
855 | return -1; |
---|
856 | } |
---|
857 | } |
---|
858 | |
---|
859 | ROAR_DBG("add_listen(*) = ?"); |
---|
860 | |
---|
861 | #if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX) |
---|
862 | if ( group != NULL ) { |
---|
863 | if ( (grp = getgrnam(group)) == NULL ) { |
---|
864 | ROAR_ERR("Can not get GID for group %s: %s", group, strerror(errno)); |
---|
865 | } |
---|
866 | } |
---|
867 | #endif |
---|
868 | |
---|
869 | ROAR_DBG("add_listen(*) = ?"); |
---|
870 | |
---|
871 | #if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX) |
---|
872 | if ( user != NULL ) { |
---|
873 | if ( (pwd = getpwnam(user)) == NULL ) { |
---|
874 | ROAR_ERR("Can not get UID for user %s: %s", user, strerror(errno)); |
---|
875 | } |
---|
876 | } |
---|
877 | #endif |
---|
878 | |
---|
879 | ROAR_DBG("add_listen(*) = ?"); |
---|
880 | |
---|
881 | #if defined(ROAR_HAVE_IO_POSIX) && defined(ROAR_HAVE_UNIX) |
---|
882 | if ( *addr == '/' ) { |
---|
883 | if ( grp != NULL || pwd != NULL ) { |
---|
884 | if ( chown(addr, pwd != NULL ? pwd->pw_uid : -1, grp != NULL ? grp->gr_gid : -1) == -1 ) |
---|
885 | return -1; |
---|
886 | } |
---|
887 | #ifdef ROAR_HAVE_GETUID |
---|
888 | if ( grp != NULL ) { |
---|
889 | if ( getuid() == 0 ) |
---|
890 | if ( chmod(addr, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) == -1 ) |
---|
891 | return -1; |
---|
892 | } |
---|
893 | #endif |
---|
894 | } |
---|
895 | #endif |
---|
896 | } |
---|
897 | |
---|
898 | ROAR_DBG("add_listen(*) = ?"); |
---|
899 | |
---|
900 | // in case we opened the listening socket correctly. |
---|
901 | if ( dir == -1 ) |
---|
902 | dir = ROAR_DIR_PLAY; |
---|
903 | |
---|
904 | g_listen[sockid].inst.stpl.dir = dir; |
---|
905 | memcpy(&(g_listen[sockid].inst.stpl.info), info, sizeof(struct roar_audio_info)); |
---|
906 | |
---|
907 | switch (dir) { |
---|
908 | case ROAR_DIR_PLAY: |
---|
909 | case ROAR_DIR_RECORD: |
---|
910 | case ROAR_DIR_MONITOR: |
---|
911 | case ROAR_DIR_FILTER: |
---|
912 | case ROAR_DIR_BIDIR: |
---|
913 | if ( !g_listen[sockid].inst.stpl.info.rate ) |
---|
914 | g_listen[sockid].inst.stpl.info.rate = g_sa->rate; |
---|
915 | |
---|
916 | if ( !g_listen[sockid].inst.stpl.info.bits ) |
---|
917 | g_listen[sockid].inst.stpl.info.bits = g_sa->bits; |
---|
918 | |
---|
919 | if ( !g_listen[sockid].inst.stpl.info.channels ) |
---|
920 | g_listen[sockid].inst.stpl.info.channels = g_sa->channels; |
---|
921 | |
---|
922 | if ( !g_listen[sockid].inst.stpl.info.codec ) |
---|
923 | g_listen[sockid].inst.stpl.info.codec = g_sa->codec; |
---|
924 | break; |
---|
925 | } |
---|
926 | |
---|
927 | g_listen[sockid].used = 1; |
---|
928 | server[sockid] = addr; |
---|
929 | |
---|
930 | return 0; |
---|
931 | } |
---|
932 | #endif |
---|
933 | |
---|
934 | static void list_aiprofiles (void) { |
---|
935 | struct roar_audio_info info; |
---|
936 | const char * list[1024]; |
---|
937 | const char * mime; |
---|
938 | ssize_t ret; |
---|
939 | ssize_t i; |
---|
940 | |
---|
941 | ret = roar_profiles_list(list, 1024, 0); |
---|
942 | |
---|
943 | if ( ret == -1 ) |
---|
944 | return; |
---|
945 | |
---|
946 | printf(" Name Rate Bits Channels Codec\n"); |
---|
947 | printf("---------------------------------------------\n"); |
---|
948 | for (i = 0; i < ret; i++) { |
---|
949 | roar_profile2info(&info, list[i]); |
---|
950 | |
---|
951 | mime = roar_codec2mime(info.codec); |
---|
952 | |
---|
953 | printf(" %-10s %5u %4u %8u %3u (%s%s%s%s)\n", list[i], |
---|
954 | info.rate, info.bits, info.channels, |
---|
955 | info.codec, roar_codec2str(info.codec), |
---|
956 | info.codec == ROAR_CODEC_DEFAULT ? " native" : "", |
---|
957 | mime == NULL ? "" : " mimetype:", |
---|
958 | mime == NULL ? "" : mime |
---|
959 | ); |
---|
960 | } |
---|
961 | } |
---|
962 | |
---|
963 | int update_stream_flags (char * str) { |
---|
964 | int dir; |
---|
965 | char * flags; |
---|
966 | char * k; |
---|
967 | int op; |
---|
968 | int flag; |
---|
969 | |
---|
970 | if ( (flags = strstr(str, "=")) == NULL ) |
---|
971 | return -1; |
---|
972 | |
---|
973 | *flags = 0; |
---|
974 | flags++; |
---|
975 | |
---|
976 | if ( (dir = roar_str2dir(str)) == -1 ) |
---|
977 | return -1; |
---|
978 | |
---|
979 | while (flags != NULL) { |
---|
980 | k = flags; |
---|
981 | flags = strstr(flags, ","); |
---|
982 | |
---|
983 | if ( flags != NULL ) |
---|
984 | *(flags++) = 0; |
---|
985 | |
---|
986 | switch (*k) { |
---|
987 | case '+': k++; op = ROAR_SET_FLAG; break; |
---|
988 | case '-': k++; op = ROAR_RESET_FLAG; break; |
---|
989 | default: |
---|
990 | op = ROAR_SET_FLAG; |
---|
991 | } |
---|
992 | |
---|
993 | flag = 0; |
---|
994 | |
---|
995 | if ( !strcmp(k, "sync") ) { |
---|
996 | flag = ROAR_FLAG_SYNC; |
---|
997 | } else if ( !strcmp(k, "meta") ) { |
---|
998 | flag = ROAR_FLAG_META; |
---|
999 | } else if ( !strcmp(k, "cleanmeta") ) { |
---|
1000 | flag = ROAR_FLAG_CLEANMETA; |
---|
1001 | } else if ( !strcmp(k, "pause") ) { |
---|
1002 | flag = ROAR_FLAG_PAUSE; |
---|
1003 | } else if ( !strcmp(k, "mute") ) { |
---|
1004 | flag = ROAR_FLAG_MUTE; |
---|
1005 | } else if ( !strcmp(k, "antiecho") ) { |
---|
1006 | flag = ROAR_FLAG_ANTIECHO; |
---|
1007 | } else if ( !strcmp(k, "passmixer") ) { |
---|
1008 | flag = ROAR_FLAG_PASSMIXER; |
---|
1009 | } else if ( !strcmp(k, "recsource") ) { |
---|
1010 | flag = ROAR_FLAG_RECSOURCE; |
---|
1011 | } else { |
---|
1012 | return -1; |
---|
1013 | } |
---|
1014 | |
---|
1015 | g_config->streams[dir].flags |= flag; |
---|
1016 | |
---|
1017 | if ( op == ROAR_RESET_FLAG ) |
---|
1018 | g_config->streams[dir].flags -= flag; |
---|
1019 | } |
---|
1020 | |
---|
1021 | return 0; |
---|
1022 | } |
---|
1023 | |
---|
1024 | int add_authfile (const char * file, const char * type, enum af_mode mode, enum roard_client_acclev acclev) { |
---|
1025 | struct roar_authfile * authfile = NULL; |
---|
1026 | struct roar_authfile_key * key = NULL; |
---|
1027 | int af_type = ROAR_AUTHFILE_TYPE_AUTO; |
---|
1028 | void * keydata; |
---|
1029 | int i; |
---|
1030 | |
---|
1031 | if ( type == NULL ) { |
---|
1032 | // noop. |
---|
1033 | } else if ( !strcasecmp(type, "roar") ) { |
---|
1034 | af_type = ROAR_AUTHFILE_TYPE_ROAR; |
---|
1035 | } else if ( !strcasecmp(type, "roar") ) { |
---|
1036 | af_type = ROAR_AUTHFILE_TYPE_ROAR; |
---|
1037 | } else if ( !strcasecmp(type, "esd") ) { |
---|
1038 | af_type = ROAR_AUTHFILE_TYPE_ESD; |
---|
1039 | } else if ( !strcasecmp(type, "pulse") ) { |
---|
1040 | af_type = ROAR_AUTHFILE_TYPE_PULSE; |
---|
1041 | } else if ( !strcasecmp(type, "htpasswd") ) { |
---|
1042 | af_type = ROAR_AUTHFILE_TYPE_HTPASSWD; |
---|
1043 | } else if ( !strcasecmp(type, "xauth") ) { |
---|
1044 | af_type = ROAR_AUTHFILE_TYPE_XAUTH; |
---|
1045 | } else { |
---|
1046 | ROAR_ERR("add_authfile(*): unknown authfile type '%s'.", type); |
---|
1047 | return -1; |
---|
1048 | } |
---|
1049 | |
---|
1050 | if ( mode == AF_MODE_GEN && af_type == ROAR_AUTHFILE_TYPE_AUTO ) |
---|
1051 | af_type = ROAR_AUTHFILE_TYPE_ESD; |
---|
1052 | |
---|
1053 | switch (mode) { |
---|
1054 | case AF_MODE_NONE: |
---|
1055 | return 0; |
---|
1056 | break; |
---|
1057 | case AF_MODE_GEN: |
---|
1058 | switch (af_type) { |
---|
1059 | case ROAR_AUTHFILE_TYPE_ESD: |
---|
1060 | key = roar_authfile_key_new_random(ROAR_AUTH_T_COOKIE, 16, NULL); |
---|
1061 | break; |
---|
1062 | case ROAR_AUTHFILE_TYPE_PULSE: |
---|
1063 | key = roar_authfile_key_new_random(ROAR_AUTH_T_COOKIE, 256, NULL); |
---|
1064 | break; |
---|
1065 | default: |
---|
1066 | return -1; |
---|
1067 | break; |
---|
1068 | } |
---|
1069 | |
---|
1070 | if ( key == NULL ) { |
---|
1071 | ROAR_ERR("add_authfile(*): Can not generate key."); |
---|
1072 | return -1; |
---|
1073 | } |
---|
1074 | |
---|
1075 | if ( (authfile = roar_authfile_open(af_type, file, 1, ROAR_AUTHFILE_VERSION_AUTO)) == NULL ) { |
---|
1076 | roar_authfile_key_unref(key); |
---|
1077 | return -1; |
---|
1078 | } |
---|
1079 | |
---|
1080 | if ( roar_authfile_add_key(authfile, key) == -1 ) { |
---|
1081 | ROAR_WARN("add_authfile(*): Can not add key to authfile."); |
---|
1082 | } |
---|
1083 | |
---|
1084 | keydata = roar_mm_memdup(key->data, key->len); |
---|
1085 | |
---|
1086 | if ( keydata == NULL ) { |
---|
1087 | ROAR_WARN("add_authfile(*): Can not allocate memory for key."); |
---|
1088 | } else if ( auth_addkey_cookie(acclev, keydata, key->len) == -1 ) { |
---|
1089 | ROAR_WARN("add_authfile(*): Can not add key to internal key storage."); |
---|
1090 | } |
---|
1091 | |
---|
1092 | roar_authfile_key_unref(key); |
---|
1093 | if ( roar_authfile_close(authfile) != 0 ) |
---|
1094 | return -1; |
---|
1095 | return 0; |
---|
1096 | break; |
---|
1097 | case AF_MODE_LOAD: |
---|
1098 | if ( (authfile = roar_authfile_open(af_type, file, 0, ROAR_AUTHFILE_VERSION_AUTO)) == NULL ) { |
---|
1099 | return -1; |
---|
1100 | } |
---|
1101 | |
---|
1102 | for (i = 0; ; i++) { |
---|
1103 | key = roar_authfile_lookup_key(authfile, ROAR_AUTH_T_AUTO, i, NULL); |
---|
1104 | if ( key == NULL ) |
---|
1105 | break; |
---|
1106 | |
---|
1107 | if ( key->type == ROAR_AUTH_T_COOKIE ) { |
---|
1108 | keydata = roar_mm_memdup(key->data, key->len); |
---|
1109 | |
---|
1110 | if ( keydata == NULL ) { |
---|
1111 | ROAR_WARN("add_authfile(*): Can not allocate memory for key."); |
---|
1112 | } else if ( auth_addkey_cookie(acclev, keydata, key->len) == -1 ) { |
---|
1113 | ROAR_WARN("add_authfile(*): Can not add key to internal key storage."); |
---|
1114 | } |
---|
1115 | } else { |
---|
1116 | ROAR_WARN("add_authfile(*): Unknown key type: %i", key->type); |
---|
1117 | } |
---|
1118 | |
---|
1119 | roar_authfile_key_unref(key); |
---|
1120 | } |
---|
1121 | |
---|
1122 | if ( roar_authfile_close(authfile) != 0 ) |
---|
1123 | return -1; |
---|
1124 | return 0; |
---|
1125 | break; |
---|
1126 | } |
---|
1127 | |
---|
1128 | return -1; |
---|
1129 | } |
---|
1130 | |
---|
1131 | // X11: |
---|
1132 | #ifdef ROAR_HAVE_LIBX11 |
---|
1133 | int register_x11 (int unreg, char * sockname) { |
---|
1134 | struct roar_x11_connection * x11con = NULL; |
---|
1135 | int ret = 0; |
---|
1136 | |
---|
1137 | if ( (x11con = roar_x11_connect(x11display)) == NULL ) { |
---|
1138 | ROAR_ERR("Can not connect to X11 server for %sregistering", unreg ? "un" : ""); |
---|
1139 | return -1; |
---|
1140 | } |
---|
1141 | |
---|
1142 | if ( unreg ) { |
---|
1143 | if ( roar_x11_delete_prop(x11con, "ROAR_SERVER") == -1 ) { |
---|
1144 | ret = -1; |
---|
1145 | ROAR_ERR("Error while unregistereing from X11", ROAR_DBG_INFO_INFO); |
---|
1146 | } else { |
---|
1147 | ROAR_INFO("Successfully unregistered from X11", ROAR_DBG_INFO_INFO); |
---|
1148 | } |
---|
1149 | } else { |
---|
1150 | if ( roar_x11_set_prop(x11con, "ROAR_SERVER", sockname) == -1 ) { |
---|
1151 | ret = -1; |
---|
1152 | ROAR_ERR("Error while registereing to X11", ROAR_DBG_INFO_INFO); |
---|
1153 | } else { |
---|
1154 | ROAR_INFO("Successfully registered to X11", ROAR_DBG_INFO_INFO); |
---|
1155 | } |
---|
1156 | } |
---|
1157 | |
---|
1158 | roar_x11_disconnect(x11con); |
---|
1159 | |
---|
1160 | return ret; |
---|
1161 | } |
---|
1162 | #endif |
---|
1163 | |
---|
1164 | // SLP: |
---|
1165 | void register_slp_callback(SLPHandle hslp, SLPError errcode, void * cookie) { |
---|
1166 | /* return the error code in the cookie */ |
---|
1167 | *(SLPError*)cookie = errcode; |
---|
1168 | } |
---|
1169 | |
---|
1170 | int register_slp (int unreg, char * sockname) { |
---|
1171 | #ifdef ROAR_HAVE_LIBSLP |
---|
1172 | static int regged = 0; |
---|
1173 | static char * sn = NULL; |
---|
1174 | SLPError err; |
---|
1175 | SLPError callbackerr; |
---|
1176 | SLPHandle hslp; |
---|
1177 | char addr[1024]; |
---|
1178 | char attr[1024] = ""; |
---|
1179 | char * location; |
---|
1180 | char * description; |
---|
1181 | char * standards; |
---|
1182 | |
---|
1183 | if ( sockname != NULL ) |
---|
1184 | sn = sockname; |
---|
1185 | |
---|
1186 | snprintf(addr, sizeof(addr), ROAR_SLP_URL_TYPE_ROAR "://%s", sn); |
---|
1187 | |
---|
1188 | err = SLPOpen("en", SLP_FALSE, &hslp); |
---|
1189 | |
---|
1190 | if (err != SLP_OK) { |
---|
1191 | ROAR_ERR("Error opening slp handle: Error #%i", err); |
---|
1192 | return -1; |
---|
1193 | } |
---|
1194 | |
---|
1195 | if (!unreg) { |
---|
1196 | |
---|
1197 | if ( SLPEscape(g_config->location, &location, SLP_FALSE) != SLP_OK ) { |
---|
1198 | ROAR_ERR("Error using SLPEscape() on server location, really bad!"); |
---|
1199 | SLPClose(hslp); |
---|
1200 | return -1; |
---|
1201 | } |
---|
1202 | |
---|
1203 | if ( SLPEscape(g_config->description, &description, SLP_FALSE) != SLP_OK ) { |
---|
1204 | ROAR_ERR("Error using SLPEscape() on server location, really bad!"); |
---|
1205 | SLPClose(hslp); |
---|
1206 | return -1; |
---|
1207 | } |
---|
1208 | |
---|
1209 | standards = stds_string(); |
---|
1210 | |
---|
1211 | if ( standards == NULL ) { |
---|
1212 | if ( (standards = roar_mm_malloc(1)) == NULL ) { |
---|
1213 | SLPClose(hslp); |
---|
1214 | return -1; |
---|
1215 | } |
---|
1216 | standards[0] = 0; |
---|
1217 | } |
---|
1218 | |
---|
1219 | snprintf(attr, sizeof(attr), "(wave-rate=%i),(wave-channels=%i),(wave-bits=%i)," |
---|
1220 | #ifndef ROAR_WITHOUT_DCOMP_LIGHT |
---|
1221 | "(light-channels=%i)," |
---|
1222 | #endif |
---|
1223 | "(standards=%s)," |
---|
1224 | "(location=%s),(description=%s)", |
---|
1225 | g_sa->rate, g_sa->channels, g_sa->bits, |
---|
1226 | #ifndef ROAR_WITHOUT_DCOMP_LIGHT |
---|
1227 | g_light_state.channels, |
---|
1228 | #endif |
---|
1229 | standards, |
---|
1230 | location, description |
---|
1231 | ); |
---|
1232 | |
---|
1233 | roar_mm_free(standards); |
---|
1234 | |
---|
1235 | /* Register a service with SLP */ |
---|
1236 | err = SLPReg(hslp, |
---|
1237 | addr, |
---|
1238 | SLP_LIFETIME_MAXIMUM, |
---|
1239 | 0, |
---|
1240 | attr, |
---|
1241 | SLP_TRUE, |
---|
1242 | register_slp_callback, |
---|
1243 | &callbackerr); |
---|
1244 | regged = 1; |
---|
1245 | } else if ( unreg && regged ) { |
---|
1246 | err = SLPDereg(hslp, addr, register_slp_callback, &callbackerr); |
---|
1247 | regged = 0; |
---|
1248 | } else { |
---|
1249 | SLPClose(hslp); |
---|
1250 | return -1; |
---|
1251 | } |
---|
1252 | |
---|
1253 | /* err may contain an error code that occurred as the slp library */ |
---|
1254 | /* _prepared_ to make the call. */ |
---|
1255 | if ( err != SLP_OK ) { |
---|
1256 | ROAR_ERR("Error (de)registering service with slp: Error #%i", err); |
---|
1257 | return -1; |
---|
1258 | } |
---|
1259 | |
---|
1260 | /* callbackerr may contain an error code (that was assigned through */ |
---|
1261 | /* the callback cookie) that occurred as slp packets were sent on */ |
---|
1262 | /* the wire */ |
---|
1263 | if (callbackerr != SLP_OK) { |
---|
1264 | ROAR_ERR("Error (de)registering service with slp: Error #%i", callbackerr); |
---|
1265 | return -1; |
---|
1266 | } |
---|
1267 | |
---|
1268 | SLPClose(hslp); |
---|
1269 | return 0; |
---|
1270 | #else |
---|
1271 | return -1; |
---|
1272 | #endif |
---|
1273 | } |
---|
1274 | |
---|
1275 | static int auth_setup (enum roard_client_acclev none_acclev, |
---|
1276 | enum roard_client_acclev trust_acclev, int trust_root, uid_t trust_uid, gid_t trust_gid) { |
---|
1277 | union auth_typeunion * key; |
---|
1278 | size_t i; |
---|
1279 | |
---|
1280 | // Info: |
---|
1281 | // if a authlevel is <= ACCLEV_IDENTED we do not need to add this to the keyring |
---|
1282 | // as ACCLEV_IDENTED is already gained if IDENTIFY call was done correctly. |
---|
1283 | |
---|
1284 | if ( !(none_acclev <= ACCLEV_IDENTED) ) { |
---|
1285 | if ( auth_addkey_anonymous(none_acclev) == -1 ) |
---|
1286 | return -1; |
---|
1287 | } |
---|
1288 | |
---|
1289 | if ( !(trust_acclev <= ACCLEV_IDENTED) ) { |
---|
1290 | if ( (key = auth_regkey_simple(ROAR_AUTH_T_TRUST, trust_acclev)) == NULL ) |
---|
1291 | return -1; |
---|
1292 | |
---|
1293 | // zerosize all counters. |
---|
1294 | memset(key, 0, sizeof(union auth_typeunion)); |
---|
1295 | |
---|
1296 | i = 0; |
---|
1297 | |
---|
1298 | if ( trust_uid != -1 ) |
---|
1299 | key->trust.uids[i++] = trust_uid; |
---|
1300 | |
---|
1301 | #ifdef ROAR_ROOT_UID |
---|
1302 | if ( trust_root ) |
---|
1303 | key->trust.uids[i++] = ROAR_ROOT_UID; |
---|
1304 | #endif |
---|
1305 | |
---|
1306 | key->trust.uids_len = i; |
---|
1307 | |
---|
1308 | i = 0; |
---|
1309 | |
---|
1310 | if ( trust_gid != -1 ) |
---|
1311 | key->trust.gids[i++] = trust_gid; |
---|
1312 | |
---|
1313 | key->trust.gids_len = i; |
---|
1314 | } |
---|
1315 | |
---|
1316 | return 0; |
---|
1317 | } |
---|
1318 | |
---|
1319 | |
---|
1320 | // MAIN: |
---|
1321 | |
---|
1322 | #define _CKHAVEARGS(x) if ( (i + (x)) >= argc ) { \ |
---|
1323 | ROAR_ERR("Option %s requires more arguments. See --help for more details.", k); \ |
---|
1324 | return 70; \ |
---|
1325 | } |
---|
1326 | |
---|
1327 | #ifdef ROAR_HAVE_MAIN_ARGS |
---|
1328 | int main (int argc, char * argv[]) { |
---|
1329 | #else |
---|
1330 | int main (void) { |
---|
1331 | #endif |
---|
1332 | #ifdef ROAR_HAVE_MAIN_ARGS |
---|
1333 | enum action action = START; |
---|
1334 | int i; |
---|
1335 | char * k; |
---|
1336 | enum output_format print_format = FORMAT_NATIVE; |
---|
1337 | #endif |
---|
1338 | #if defined(ROAR_SUPPORT_LISTEN) && defined(ROAR_HAVE_GETUID) |
---|
1339 | char user_sock[80] = {0}; |
---|
1340 | #endif |
---|
1341 | struct roar_audio_info sa, max_sa; |
---|
1342 | struct roard_config config; |
---|
1343 | const struct rolestack * rolestack; |
---|
1344 | #if defined(ROAR_HAVE_FORK) || defined(ROAR_TARGET_WIN32) |
---|
1345 | int daemon = 0; |
---|
1346 | #endif |
---|
1347 | int realtime = 0; |
---|
1348 | int sysclocksync = 0; |
---|
1349 | // char * server = ROAR_DEFAULT_SOCK_GLOBAL; |
---|
1350 | #ifdef ROAR_SUPPORT_LISTEN |
---|
1351 | int port = ROAR_DEFAULT_PORT; |
---|
1352 | char * sock_addr = NULL; |
---|
1353 | int sock_proto = ROAR_PROTO_ROARAUDIO; |
---|
1354 | int sock_dir = -1; |
---|
1355 | struct roar_audio_info sock_info = {0, 0, 0, 0}; |
---|
1356 | #endif |
---|
1357 | #ifndef ROAR_WITHOUT_DCOMP_SOURCES |
---|
1358 | char * s_drv = "cf"; |
---|
1359 | char * s_dev = NULL; |
---|
1360 | char * s_con = NULL; |
---|
1361 | char * s_opt = NULL; |
---|
1362 | int s_prim = 0; |
---|
1363 | #endif |
---|
1364 | char * o_drv = getenv("ROAR_DRIVER"); |
---|
1365 | char * o_dev = getenv("ROAR_DEVICE"); |
---|
1366 | char * o_opts = NULL; |
---|
1367 | int o_prim = 0; |
---|
1368 | int o_count = 0; |
---|
1369 | #ifndef ROAR_WITHOUT_DCOMP_MIXER |
---|
1370 | char * m_drv = NULL; |
---|
1371 | char * m_dev = NULL; |
---|
1372 | char * m_opts = NULL; |
---|
1373 | int m_prim = 0; |
---|
1374 | int m_count = 0; |
---|
1375 | #endif |
---|
1376 | enum roard_client_acclev none_acclev = ACCLEV_ALL; |
---|
1377 | enum roard_client_acclev trust_acclev = ACCLEV_ALL; |
---|
1378 | int trust_root = 1; |
---|
1379 | char * af_file = NULL; |
---|
1380 | char * af_type = NULL; |
---|
1381 | enum af_mode af_mode = AF_MODE_NONE; |
---|
1382 | enum roard_client_acclev af_acclev = ACCLEV_ALL; |
---|
1383 | #ifndef ROAR_WITHOUT_DCOMP_LIGHT |
---|
1384 | int light_channels = LIGHT_CHANNELS_DEFAULT; |
---|
1385 | #endif |
---|
1386 | #ifdef ROAR_DEFAULT_SOCKGRP |
---|
1387 | char * sock_grp = ROAR_DEFAULT_SOCKGRP; |
---|
1388 | #else |
---|
1389 | char * sock_grp = NULL; |
---|
1390 | #endif |
---|
1391 | char * sock_user = NULL; |
---|
1392 | #ifdef ROAR_SUPPORT_LISTEN |
---|
1393 | int sock_type = ROAR_SOCKET_TYPE_UNKNOWN; |
---|
1394 | #endif |
---|
1395 | #ifdef ROAR_HAVE_LIBSLP |
---|
1396 | int reg_slp = 0; |
---|
1397 | #endif |
---|
1398 | #ifdef ROAR_HAVE_LIBX11 |
---|
1399 | int reg_x11 = 0; |
---|
1400 | #endif |
---|
1401 | #ifdef ROAR_HAVE_CHROOT |
---|
1402 | char * chrootdir = NULL; |
---|
1403 | #endif |
---|
1404 | #if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX) |
---|
1405 | struct group * grp = NULL; |
---|
1406 | #endif |
---|
1407 | #if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX) |
---|
1408 | struct passwd * pwd = NULL; |
---|
1409 | #endif |
---|
1410 | #ifdef ROAR_HAVE_GETSERVBYNAME |
---|
1411 | struct servent * serv = NULL; |
---|
1412 | #endif |
---|
1413 | struct roar_client * self = NULL; |
---|
1414 | #ifdef ROAR_HAVE_LIBDNET |
---|
1415 | char decnethost[80]; |
---|
1416 | #endif |
---|
1417 | #ifdef SUPPORT_PIDFILE |
---|
1418 | struct roar_vio_calls pidfile_vio; |
---|
1419 | #endif |
---|
1420 | |
---|
1421 | ROAR_DBG("main(*): starting roard..."); |
---|
1422 | |
---|
1423 | g_standby = 0; |
---|
1424 | g_autostandby = 0; |
---|
1425 | alive = 1; |
---|
1426 | #ifdef ROAR_SUPPORT_LISTEN |
---|
1427 | g_no_listen = 0; |
---|
1428 | #else |
---|
1429 | g_terminate = 1; |
---|
1430 | #endif |
---|
1431 | |
---|
1432 | g_verbose = ROAR_DBG_INFO_NONE; |
---|
1433 | |
---|
1434 | sa.bits = ROAR_BITS_DEFAULT; |
---|
1435 | sa.channels = ROAR_CHANNELS_DEFAULT; |
---|
1436 | sa.rate = ROAR_RATE_DEFAULT; |
---|
1437 | sa.codec = ROAR_CODEC_DEFAULT; |
---|
1438 | |
---|
1439 | g_sa = &sa; |
---|
1440 | g_max_sa = &max_sa; |
---|
1441 | |
---|
1442 | memcpy(g_max_sa, g_sa, sizeof(max_sa)); |
---|
1443 | |
---|
1444 | counters_init(); |
---|
1445 | |
---|
1446 | g_config = &config; |
---|
1447 | |
---|
1448 | if ( init_config() == -1 ) { |
---|
1449 | ROAR_ERR("Can not init default config!"); |
---|
1450 | return 1; |
---|
1451 | } |
---|
1452 | |
---|
1453 | // load config |
---|
1454 | roar_libroar_get_config(); |
---|
1455 | |
---|
1456 | // init notify core: |
---|
1457 | // TODO: reconsider number of lists. |
---|
1458 | if ( roar_notify_core_new_global(-1) == -1 ) { |
---|
1459 | ROAR_ERR("Can not init notify core!"); |
---|
1460 | return 1; |
---|
1461 | } |
---|
1462 | |
---|
1463 | if ( roar_notify_core_register_proxy(NULL, roar_notify_proxy_std, NULL) == -1 ) { |
---|
1464 | ROAR_ERR("Can not init notify core!"); |
---|
1465 | return 1; |
---|
1466 | } |
---|
1467 | |
---|
1468 | #ifdef DEBUG |
---|
1469 | // enable early in case we have DEBUG set. |
---|
1470 | dbg_notify_cb_register(); |
---|
1471 | #endif |
---|
1472 | |
---|
1473 | rolestack_init(); |
---|
1474 | |
---|
1475 | if ( auth_init() == -1 ) { |
---|
1476 | ROAR_ERR("Can not init auth subsystem!"); |
---|
1477 | return 1; |
---|
1478 | } |
---|
1479 | |
---|
1480 | #ifdef ROAR_SUPPORT_LISTEN |
---|
1481 | if ( init_listening() == -1 ) { |
---|
1482 | ROAR_ERR("Can not init listening sockets!"); |
---|
1483 | return 1; |
---|
1484 | } |
---|
1485 | #endif |
---|
1486 | |
---|
1487 | #ifndef ROAR_WITHOUT_DCOMP_MIDI |
---|
1488 | if ( midi_init_config() == -1 ) { |
---|
1489 | ROAR_ERR("Can not init MIDI config!"); |
---|
1490 | return 1; |
---|
1491 | } |
---|
1492 | #endif |
---|
1493 | |
---|
1494 | #ifndef ROAR_WITHOUT_DCOMP_SSYNTH |
---|
1495 | if ( ssynth_init_config() == -1 ) { |
---|
1496 | ROAR_ERR("Can not init ssynth config!"); |
---|
1497 | return 1; |
---|
1498 | } |
---|
1499 | #endif |
---|
1500 | |
---|
1501 | #ifndef ROAR_WITHOUT_DCOMP_RDTCS |
---|
1502 | if ( rdtcs_init_config() == -1 ) { |
---|
1503 | ROAR_ERR("Can not init RDTCS config!"); |
---|
1504 | return 1; |
---|
1505 | } |
---|
1506 | #endif |
---|
1507 | |
---|
1508 | if ( plugins_preinit() == -1 ) { |
---|
1509 | ROAR_ERR("Can not pre-init plugins!"); |
---|
1510 | return 1; |
---|
1511 | } |
---|
1512 | |
---|
1513 | #ifdef ROAR_SUPPORT_LISTEN |
---|
1514 | #ifndef ROAR_TARGET_WIN32 |
---|
1515 | sock_addr = ROAR_DEFAULT_SOCK_GLOBAL; |
---|
1516 | #else |
---|
1517 | sock_addr = ROAR_DEFAULT_HOST; |
---|
1518 | #endif |
---|
1519 | |
---|
1520 | #ifdef ROAR_HAVE_GETUID |
---|
1521 | if ( getuid() != 0 && getenv("HOME") != NULL ) { |
---|
1522 | /* |
---|
1523 | snprintf(user_sock, 79, "%s/%s", (char*)getenv("HOME"), ROAR_DEFAULT_SOCK_USER); |
---|
1524 | */ |
---|
1525 | roar_env_render_path_r(user_sock, sizeof(user_sock), "~/" ROAR_DEFAULT_SOCK_USER); |
---|
1526 | sock_addr = user_sock; |
---|
1527 | ROAR_DBG("main(*): setting sock_addr='%s'", sock_addr); |
---|
1528 | } |
---|
1529 | #endif |
---|
1530 | |
---|
1531 | if ( getenv("ROAR_SERVER") != NULL ) |
---|
1532 | sock_addr = getenv("ROAR_SERVER"); |
---|
1533 | #endif |
---|
1534 | |
---|
1535 | if ( clients_init() == -1 ) { |
---|
1536 | ROAR_ERR("Can not init clients!"); |
---|
1537 | return 1; |
---|
1538 | } |
---|
1539 | |
---|
1540 | if ( streams_init() == -1 ) { |
---|
1541 | ROAR_ERR("Can not init streams!"); |
---|
1542 | return 1; |
---|
1543 | } |
---|
1544 | |
---|
1545 | if ( (g_self_client = clients_new()) == -1 ) { |
---|
1546 | ROAR_ERR("Can not create self client!"); |
---|
1547 | return 1; |
---|
1548 | } |
---|
1549 | |
---|
1550 | #ifndef ROAR_WITHOUT_DCOMP_SOURCES |
---|
1551 | if ( sources_init() == -1 ) { |
---|
1552 | ROAR_ERR("Can not init sources!"); |
---|
1553 | return 1; |
---|
1554 | } |
---|
1555 | |
---|
1556 | if ( (sources_set_client(g_self_client)) == -1 ) { |
---|
1557 | ROAR_ERR("Can not init set source client!"); |
---|
1558 | return 1; |
---|
1559 | } |
---|
1560 | #endif |
---|
1561 | |
---|
1562 | #ifdef ROAR_HAVE_MAIN_ARGS |
---|
1563 | for (i = 1; i < argc; i++) { |
---|
1564 | k = argv[i]; |
---|
1565 | |
---|
1566 | if ( strcmp(k, "-h") == 0 || strcmp(k, "--help") == 0 ) { |
---|
1567 | usage(); |
---|
1568 | return 0; |
---|
1569 | |
---|
1570 | } else if ( strcmp(k, "--start") == 0 ) { |
---|
1571 | // this is a no op |
---|
1572 | action = START; |
---|
1573 | } else if ( strcmp(k, "--restart") == 0 ) { |
---|
1574 | action = RESTART; |
---|
1575 | #ifdef ROAR_SUPPORT_LISTEN |
---|
1576 | if ( restart_server(sock_addr, 1) == -1 ) { |
---|
1577 | ROAR_WARN("Can not terminate old server (not running at %s?), will retry later.", sock_addr); |
---|
1578 | action = RESTART_RETRY; |
---|
1579 | } |
---|
1580 | #else |
---|
1581 | ROAR_ERR("--restart not supported"); |
---|
1582 | #endif |
---|
1583 | } else if ( strcmp(k, "--shutdown") == 0 ) { |
---|
1584 | action = SHUTDOWN; |
---|
1585 | } else if ( strcmp(k, "--stop") == 0 ) { |
---|
1586 | action = STOP; |
---|
1587 | |
---|
1588 | |
---|
1589 | } else if ( strcmp(k, "--demon") == 0 || strcmp(k, "--daemon") == 0 ) { |
---|
1590 | #if defined(ROAR_HAVE_FORK) || defined(ROAR_TARGET_WIN32) |
---|
1591 | daemon = 1; |
---|
1592 | #else |
---|
1593 | ROAR_ERR("--daemon not supported"); |
---|
1594 | #endif |
---|
1595 | } else if ( strcmp(k, "--verbose") == 0 ) { |
---|
1596 | g_verbose++; |
---|
1597 | |
---|
1598 | } else if ( strcmp(k, "--print-format") == 0 ) { |
---|
1599 | k = argv[++i]; |
---|
1600 | if ( !strcasecmp(k, "native") ) { |
---|
1601 | print_format = FORMAT_NATIVE; |
---|
1602 | } else if ( !strcasecmp(k, "wiki") ) { |
---|
1603 | print_format = FORMAT_WIKI; |
---|
1604 | } else if ( !strcasecmp(k, "csv") ) { |
---|
1605 | print_format = FORMAT_CSV; |
---|
1606 | } else { |
---|
1607 | ROAR_WARN("Unknown print format: %s", k); |
---|
1608 | } |
---|
1609 | |
---|
1610 | } else if ( strcmp(k, "--print-format") == 0 ) { |
---|
1611 | _CKHAVEARGS(1); |
---|
1612 | |
---|
1613 | } else if ( strcmp(k, "--terminate") == 0 ) { |
---|
1614 | g_terminate = 1; |
---|
1615 | } else if ( strcmp(k, "--sysclocksync") == 0 ) { |
---|
1616 | sysclocksync = 1000; |
---|
1617 | } else if ( strcmp(k, "--realtime") == 0 ) { |
---|
1618 | realtime++; |
---|
1619 | } else if ( strcmp(k, "--memlock") == 0 ) { |
---|
1620 | _CKHAVEARGS(1); |
---|
1621 | g_config->memlock_level = memlock_str2level(argv[++i]); |
---|
1622 | } else if ( strcmp(k, "--chroot") == 0 ) { |
---|
1623 | _CKHAVEARGS(1); |
---|
1624 | #ifdef ROAR_HAVE_CHROOT |
---|
1625 | chrootdir = argv[++i]; |
---|
1626 | #else |
---|
1627 | ROAR_ERR("--chroot not supported"); |
---|
1628 | i++; |
---|
1629 | #endif |
---|
1630 | } else if ( strcmp(k, "--setgid") == 0 ) { |
---|
1631 | #ifdef ROAR_HAVE_SETGID |
---|
1632 | setids |= R_SETGID; |
---|
1633 | #else |
---|
1634 | ROAR_ERR("--setgid not supported"); |
---|
1635 | #endif |
---|
1636 | } else if ( strcmp(k, "--setuid") == 0 ) { |
---|
1637 | #ifdef ROAR_HAVE_SETUID |
---|
1638 | setids |= R_SETUID; |
---|
1639 | #else |
---|
1640 | ROAR_ERR("--setuid not supported"); |
---|
1641 | #endif |
---|
1642 | } else if ( strcmp(k, "--location") == 0 ) { |
---|
1643 | _CKHAVEARGS(1); |
---|
1644 | g_config->location = argv[++i]; |
---|
1645 | } else if ( strcmp(k, "--description") == 0 ) { |
---|
1646 | _CKHAVEARGS(1); |
---|
1647 | g_config->description = argv[++i]; |
---|
1648 | } else if ( strcmp(k, "--pidfile") == 0 ) { |
---|
1649 | _CKHAVEARGS(1); |
---|
1650 | #ifdef SUPPORT_PIDFILE |
---|
1651 | pidfile = argv[++i]; |
---|
1652 | #else |
---|
1653 | ROAR_ERR("--pidfile not supported"); |
---|
1654 | i++; |
---|
1655 | #endif |
---|
1656 | } else if ( strcmp(k, "--log-syslog") == 0 ) { |
---|
1657 | #ifdef ROAR_HAVE_SYSLOG |
---|
1658 | roar_debug_set_stderr_mode(ROAR_DEBUG_MODE_SYSLOG); |
---|
1659 | #else |
---|
1660 | ROAR_ERR("--log-syslog not supported"); |
---|
1661 | #endif |
---|
1662 | } else if ( strcmp(k, "--script-postdown") == 0 ) { |
---|
1663 | _CKHAVEARGS(1); |
---|
1664 | #ifdef ROAR_HAVE_SYSTEM |
---|
1665 | g_config->scripts.post_shutdown = argv[++i]; |
---|
1666 | #else |
---|
1667 | ROAR_ERR("--script-postdown not supported"); |
---|
1668 | i++; |
---|
1669 | #endif |
---|
1670 | |
---|
1671 | |
---|
1672 | } else if ( strcmp(k, "--plugin-load") == 0 ) { |
---|
1673 | _CKHAVEARGS(1); |
---|
1674 | if ( plugins_load(argv[++i]) == -1 ) { |
---|
1675 | ROAR_ERR("Can not load plugin"); |
---|
1676 | } |
---|
1677 | |
---|
1678 | } else if ( strcmp(k, "--guest-acclev") == 0 ) { |
---|
1679 | _CKHAVEARGS(1); |
---|
1680 | none_acclev = clients_str2acclev(argv[++i]); |
---|
1681 | } else if ( strcmp(k, "--trust-acclev") == 0 ) { |
---|
1682 | _CKHAVEARGS(1); |
---|
1683 | trust_acclev = clients_str2acclev(argv[++i]); |
---|
1684 | } else if ( strcmp(k, "--trust-root") == 0 ) { |
---|
1685 | trust_root = 1; |
---|
1686 | } else if ( strcmp(k, "--no-trust-root") == 0 ) { |
---|
1687 | trust_root = 0; |
---|
1688 | |
---|
1689 | } else if ( strcmp(k, "--authfile-gen") == 0 ) { |
---|
1690 | _CKHAVEARGS(1); |
---|
1691 | af_file = argv[++i]; |
---|
1692 | af_mode = AF_MODE_GEN; |
---|
1693 | } else if ( strcmp(k, "--authfile-load") == 0 ) { |
---|
1694 | _CKHAVEARGS(1); |
---|
1695 | af_file = argv[++i]; |
---|
1696 | af_mode = AF_MODE_LOAD; |
---|
1697 | } else if ( strcmp(k, "--authfile-type") == 0 ) { |
---|
1698 | _CKHAVEARGS(1); |
---|
1699 | af_type = argv[++i]; |
---|
1700 | } else if ( strcmp(k, "--authfile-acclev") == 0 ) { |
---|
1701 | _CKHAVEARGS(1); |
---|
1702 | af_acclev = clients_str2acclev(argv[++i]); |
---|
1703 | } else if ( strcmp(k, "--new-authfile") == 0 ) { |
---|
1704 | if ( af_mode != AF_MODE_NONE ) { |
---|
1705 | if ( add_authfile(af_file, af_type, af_mode, af_acclev) == -1 ) { |
---|
1706 | ROAR_ERR("main(*): adding authfile '%s' failed!", af_file); |
---|
1707 | } |
---|
1708 | } |
---|
1709 | af_file = NULL; |
---|
1710 | af_type = NULL; |
---|
1711 | af_mode = AF_MODE_NONE; |
---|
1712 | af_acclev = ACCLEV_ALL; |
---|
1713 | |
---|
1714 | } else if ( strcmp(k, "--list-cf") == 0 ) { |
---|
1715 | print_codecfilterlist(); |
---|
1716 | return 0; |
---|
1717 | |
---|
1718 | } else if ( strcmp(k, "-R") == 0 || strcmp(k, "--rate") == 0 ) { |
---|
1719 | _CKHAVEARGS(1); |
---|
1720 | sa.rate = atoi(argv[++i]); |
---|
1721 | } else if ( strcmp(k, "-B") == 0 || strcmp(k, "--bits") == 0 ) { |
---|
1722 | _CKHAVEARGS(1); |
---|
1723 | sa.bits = atoi(argv[++i]); |
---|
1724 | } else if ( strcmp(k, "-C") == 0 || strcmp(k, "--chans") == 0 ) { |
---|
1725 | _CKHAVEARGS(1); |
---|
1726 | sa.channels = atoi(argv[++i]); |
---|
1727 | |
---|
1728 | } else if ( strcmp(k, "--aiprofile") == 0 ) { |
---|
1729 | _CKHAVEARGS(1); |
---|
1730 | if ( roar_profile2info(&sa, argv[++i]) == -1 ) { |
---|
1731 | ROAR_ERR("Unknown audio profile: %s", argv[i]); |
---|
1732 | return 1; |
---|
1733 | } |
---|
1734 | sa.codec = ROAR_CODEC_DEFAULT; |
---|
1735 | |
---|
1736 | } else if ( strcmp(k, "--stream-flags") == 0 ) { |
---|
1737 | _CKHAVEARGS(1); |
---|
1738 | if ( update_stream_flags(argv[++i]) == -1 ) { |
---|
1739 | ROAR_ERR("Can not set stream flags"); |
---|
1740 | return 1; |
---|
1741 | } |
---|
1742 | |
---|
1743 | } else if ( strcmp(k, "--list-rolestack") == 0 ) { |
---|
1744 | print_rolestack(); |
---|
1745 | return 0; |
---|
1746 | } else if ( strcmp(k, "--rolestack-push") == 0 ) { |
---|
1747 | _CKHAVEARGS(1); |
---|
1748 | if ( (rolestack = rolestack_parse(argv[++i])) == NULL ) { |
---|
1749 | ROAR_ERR("Can not parse rolestack request: %s", roar_error2str(roar_error)); |
---|
1750 | } else { |
---|
1751 | if ( rolestack_push(rolestack) == -1 ) { |
---|
1752 | ROAR_ERR("Can not push request to rolestack: %s", roar_error2str(roar_error)); |
---|
1753 | } |
---|
1754 | } |
---|
1755 | |
---|
1756 | } else if ( strcmp(k, "--list-aiprofiles") == 0 ) { |
---|
1757 | list_aiprofiles(); |
---|
1758 | return 0; |
---|
1759 | |
---|
1760 | } else if ( strcmp(k, "--list-driver") == 0 ) { |
---|
1761 | print_driverlist(print_format); |
---|
1762 | return 0; |
---|
1763 | |
---|
1764 | } else if ( strcmp(k, "-o") == 0 || strcmp(k, "--odriver") == 0 ) { |
---|
1765 | _CKHAVEARGS(1); |
---|
1766 | o_drv = argv[++i]; |
---|
1767 | } else if ( strcmp(k, "-O") == 0 || strcmp(k, "--odevice") == 0 ) { |
---|
1768 | _CKHAVEARGS(1); |
---|
1769 | o_dev = argv[++i]; |
---|
1770 | } else if ( strcmp(k, "-oO") == 0 ) { |
---|
1771 | _CKHAVEARGS(1); |
---|
1772 | o_opts = argv[++i]; |
---|
1773 | } else if ( strcmp(k, "-oP") == 0 ) { |
---|
1774 | o_prim = 1; |
---|
1775 | } else if ( strcmp(k, "-oN") == 0 ) { |
---|
1776 | if ( action == START || action == RESTART ) { |
---|
1777 | if ( output_add(o_drv, o_dev, o_opts, o_prim, o_count) != -1 ) |
---|
1778 | o_count++; |
---|
1779 | |
---|
1780 | o_drv = o_dev = o_opts = NULL; |
---|
1781 | o_prim = 0; |
---|
1782 | } |
---|
1783 | |
---|
1784 | } else if ( strcmp(k, "-s") == 0 || strcmp(k, "--source") == 0 ) { |
---|
1785 | _CKHAVEARGS(1); |
---|
1786 | #ifndef ROAR_WITHOUT_DCOMP_SOURCES |
---|
1787 | s_drv = argv[++i]; |
---|
1788 | #else |
---|
1789 | ROAR_ERR("main(*): No support for sources compiled in"); |
---|
1790 | i++; |
---|
1791 | #endif |
---|
1792 | } else if ( strcmp(k, "-S") == 0 ) { |
---|
1793 | _CKHAVEARGS(1); |
---|
1794 | #ifndef ROAR_WITHOUT_DCOMP_SOURCES |
---|
1795 | s_dev = argv[++i]; |
---|
1796 | #else |
---|
1797 | ROAR_ERR("main(*): No support for sources compiled in"); |
---|
1798 | i++; |
---|
1799 | #endif |
---|
1800 | } else if ( strcmp(k, "-sO") == 0 ) { |
---|
1801 | _CKHAVEARGS(1); |
---|
1802 | #ifndef ROAR_WITHOUT_DCOMP_SOURCES |
---|
1803 | s_opt = argv[++i]; |
---|
1804 | #else |
---|
1805 | ROAR_ERR("main(*): No support for sources compiled in"); |
---|
1806 | i++; |
---|
1807 | #endif |
---|
1808 | } else if ( strcmp(k, "-sC") == 0 ) { |
---|
1809 | _CKHAVEARGS(1); |
---|
1810 | #ifndef ROAR_WITHOUT_DCOMP_SOURCES |
---|
1811 | s_con = argv[++i]; |
---|
1812 | #else |
---|
1813 | ROAR_ERR("main(*): No support for sources compiled in"); |
---|
1814 | i++; |
---|
1815 | #endif |
---|
1816 | } else if ( strcmp(k, "-sP") == 0 ) { |
---|
1817 | #ifndef ROAR_WITHOUT_DCOMP_SOURCES |
---|
1818 | s_prim = 1; |
---|
1819 | #else |
---|
1820 | ROAR_ERR("main(*): No support for sources compiled in"); |
---|
1821 | #endif |
---|
1822 | } else if ( strcmp(k, "-sN") == 0 ) { |
---|
1823 | #ifndef ROAR_WITHOUT_DCOMP_SOURCES |
---|
1824 | if ( sources_add(s_drv, s_dev, s_con, s_opt, s_prim) == -1 ) { |
---|
1825 | ROAR_ERR("main(*): adding source '%s' via '%s' failed!", s_dev, s_drv); |
---|
1826 | } |
---|
1827 | s_opt = s_dev = s_con = NULL; |
---|
1828 | s_drv = "cf"; |
---|
1829 | s_prim = 0; |
---|
1830 | #else |
---|
1831 | ROAR_ERR("main(*): No support for sources compiled in"); |
---|
1832 | #endif |
---|
1833 | } else if ( strcmp(k, "--list-sources") == 0 ) { |
---|
1834 | #ifndef ROAR_WITHOUT_DCOMP_SOURCES |
---|
1835 | print_sourcelist(); |
---|
1836 | return 0; |
---|
1837 | #else |
---|
1838 | ROAR_ERR("main(*): No support for sources compiled in"); |
---|
1839 | return 1; |
---|
1840 | #endif |
---|
1841 | |
---|
1842 | } else if ( strcmp(k, "-m") == 0 || strcmp(k, "--mixer") == 0 ) { |
---|
1843 | _CKHAVEARGS(1); |
---|
1844 | #ifndef ROAR_WITHOUT_DCOMP_MIXER |
---|
1845 | m_drv = argv[++i]; |
---|
1846 | #else |
---|
1847 | ROAR_ERR("main(*): No support for mixer compiled in"); |
---|
1848 | return 1; |
---|
1849 | #endif |
---|
1850 | } else if ( strcmp(k, "-M") == 0 ) { |
---|
1851 | _CKHAVEARGS(1); |
---|
1852 | #ifndef ROAR_WITHOUT_DCOMP_MIXER |
---|
1853 | m_dev = argv[++i]; |
---|
1854 | #else |
---|
1855 | ROAR_ERR("main(*): No support for mixer compiled in"); |
---|
1856 | return 1; |
---|
1857 | #endif |
---|
1858 | } else if ( strcmp(k, "-mO") == 0 ) { |
---|
1859 | _CKHAVEARGS(1); |
---|
1860 | #ifndef ROAR_WITHOUT_DCOMP_MIXER |
---|
1861 | m_opts = argv[++i]; |
---|
1862 | #else |
---|
1863 | ROAR_ERR("main(*): No support for mixer compiled in"); |
---|
1864 | return 1; |
---|
1865 | #endif |
---|
1866 | } else if ( strcmp(k, "-mP") == 0 ) { |
---|
1867 | #ifndef ROAR_WITHOUT_DCOMP_MIXER |
---|
1868 | m_prim = 1; |
---|
1869 | #else |
---|
1870 | ROAR_ERR("main(*): No support for mixer compiled in"); |
---|
1871 | return 1; |
---|
1872 | #endif |
---|
1873 | } else if ( strcmp(k, "-mN") == 0 ) { |
---|
1874 | #ifndef ROAR_WITHOUT_DCOMP_MIXER |
---|
1875 | if ( hwmixer_add(m_drv, m_dev, m_opts, m_prim, m_count) != -1 ) |
---|
1876 | m_count++; |
---|
1877 | |
---|
1878 | m_drv = o_dev = o_opts = NULL; |
---|
1879 | m_prim = 0; |
---|
1880 | #else |
---|
1881 | ROAR_ERR("main(*): No support for mixer compiled in"); |
---|
1882 | return 1; |
---|
1883 | #endif |
---|
1884 | } else if ( strcmp(k, "--list-mixers") == 0 ) { |
---|
1885 | #ifndef ROAR_WITHOUT_DCOMP_MIXER |
---|
1886 | print_hwmixerlist(); |
---|
1887 | return 0; |
---|
1888 | #else |
---|
1889 | ROAR_ERR("main(*): No support for mixer compiled in"); |
---|
1890 | return 1; |
---|
1891 | #endif |
---|
1892 | |
---|
1893 | } else if ( strcmp(k, "--light-channels") == 0 ) { |
---|
1894 | _CKHAVEARGS(1); |
---|
1895 | #ifndef ROAR_WITHOUT_DCOMP_LIGHT |
---|
1896 | light_channels = atoi(argv[++i]); |
---|
1897 | #else |
---|
1898 | ROAR_WARN("main(*): no light subsystem compiled in"); |
---|
1899 | i++; |
---|
1900 | #endif |
---|
1901 | |
---|
1902 | } else if ( strcmp(k, "--rds-pi") == 0 ) { |
---|
1903 | _CKHAVEARGS(1); |
---|
1904 | #ifndef ROAR_WITHOUT_DCOMP_RDTCS |
---|
1905 | g_rdtcs.rds.pi = atoi(argv[++i]); |
---|
1906 | #else |
---|
1907 | ROAR_WARN("main(*): no RDTCS subsystem compiled in"); |
---|
1908 | i++; |
---|
1909 | #endif |
---|
1910 | } else if ( strcmp(k, "--rds-ps") == 0 ) { |
---|
1911 | _CKHAVEARGS(1); |
---|
1912 | #ifndef ROAR_WITHOUT_DCOMP_RDTCS |
---|
1913 | if ( rdtcs_rds_set_ps(argv[++i]) == -1 ) { |
---|
1914 | ROAR_ERR("Can not set RDS PS to '%s' (longer than 8 chars?)", argv[i]); |
---|
1915 | return 1; |
---|
1916 | } |
---|
1917 | #else |
---|
1918 | ROAR_WARN("main(*): no RDTCS subsystem compiled in"); |
---|
1919 | i++; |
---|
1920 | #endif |
---|
1921 | } else if ( strcmp(k, "--rds-pty") == 0 ) { |
---|
1922 | _CKHAVEARGS(1); |
---|
1923 | #ifndef ROAR_WITHOUT_DCOMP_RDTCS |
---|
1924 | if ( rdtcs_rds_set_pty(argv[++i]) == -1 ) { |
---|
1925 | ROAR_ERR("Can not set RDS PTY to '%s'", argv[i]); |
---|
1926 | return 1; |
---|
1927 | } |
---|
1928 | #else |
---|
1929 | ROAR_WARN("main(*): no RDTCS subsystem compiled in"); |
---|
1930 | i++; |
---|
1931 | #endif |
---|
1932 | } else if ( strcmp(k, "--rds-tp") == 0 ) { |
---|
1933 | #ifndef ROAR_WITHOUT_DCOMP_RDTCS |
---|
1934 | if ( rdtcs_rds_set_flag(RDTCS_RDS_FLAG_TP, 0) == -1 ) { |
---|
1935 | ROAR_ERR("Can not set RDS TP flag"); |
---|
1936 | return 1; |
---|
1937 | } |
---|
1938 | #else |
---|
1939 | ROAR_WARN("main(*): no RDTCS subsystem compiled in"); |
---|
1940 | #endif |
---|
1941 | } else if ( strcmp(k, "--rds-ct") == 0 ) { |
---|
1942 | #ifndef ROAR_WITHOUT_DCOMP_RDTCS |
---|
1943 | if ( rdtcs_rds_set_flag(RDTCS_RDS_FLAG_CT, 0) == -1 ) { |
---|
1944 | ROAR_ERR("Can not set RDS CT flag"); |
---|
1945 | return 1; |
---|
1946 | } |
---|
1947 | #else |
---|
1948 | ROAR_WARN("main(*): no RDTCS subsystem compiled in"); |
---|
1949 | #endif |
---|
1950 | |
---|
1951 | |
---|
1952 | } else if ( strcmp(k, "--midi-no-console") == 0 ) { |
---|
1953 | #ifndef ROAR_WITHOUT_DCOMP_CB |
---|
1954 | midi_config.init_cb = 0; |
---|
1955 | #else |
---|
1956 | // no warning here as this is the disable option |
---|
1957 | #endif |
---|
1958 | } else if ( strcmp(k, "--midi-console-enable") == 0 ) { |
---|
1959 | #ifndef ROAR_WITHOUT_DCOMP_CB |
---|
1960 | midi_config.init_cb = 1; |
---|
1961 | #else |
---|
1962 | ROAR_ERR("main(*): No support for MIDI subsystem part CB compiled in"); |
---|
1963 | #endif |
---|
1964 | } else if ( strcmp(k, "--midi-console") == 0 ) { |
---|
1965 | _CKHAVEARGS(1); |
---|
1966 | #ifndef ROAR_WITHOUT_DCOMP_CB |
---|
1967 | midi_config.console_dev = argv[++i]; |
---|
1968 | midi_config.init_cb = 1; |
---|
1969 | #else |
---|
1970 | ROAR_ERR("main(*): No support for MIDI subsystem part CB compiled in"); |
---|
1971 | i++; |
---|
1972 | #endif |
---|
1973 | |
---|
1974 | } else if ( strcmp(k, "--ssynth-enable") == 0 ) { |
---|
1975 | #ifndef ROAR_WITHOUT_DCOMP_SSYNTH |
---|
1976 | ssynth_conf.enable = 1; |
---|
1977 | #else |
---|
1978 | ROAR_ERR("main(*): No support for ssynth compiled in"); |
---|
1979 | #endif |
---|
1980 | } else if ( strcmp(k, "--ssynth-disable") == 0 ) { |
---|
1981 | #ifndef ROAR_WITHOUT_DCOMP_SSYNTH |
---|
1982 | ssynth_conf.enable = 0; |
---|
1983 | #else |
---|
1984 | // we can safely ignore the disable |
---|
1985 | #endif |
---|
1986 | |
---|
1987 | } else if ( strcmp(k, "--x11-display") == 0 || strcmp(k, "--display") == 0 ) { |
---|
1988 | _CKHAVEARGS(1); |
---|
1989 | #ifdef ROAR_HAVE_LIBX11 |
---|
1990 | x11display = argv[++i]; |
---|
1991 | #else |
---|
1992 | ROAR_ERR("No X11 support compiled in!"); |
---|
1993 | return 1; |
---|
1994 | #endif |
---|
1995 | |
---|
1996 | |
---|
1997 | } else if ( strcmp(k, "-p") == 0 || strcmp(k, "--port") == 0 ) { |
---|
1998 | _CKHAVEARGS(1); |
---|
1999 | // This is only useful in INET not UNIX mode. |
---|
2000 | #ifdef ROAR_SUPPORT_LISTEN |
---|
2001 | if ( *sock_addr == '/' ) |
---|
2002 | sock_addr = ROAR_DEFAULT_HOST; |
---|
2003 | |
---|
2004 | errno = 0; |
---|
2005 | if ( (port = atoi(argv[++i])) < 1 ) { |
---|
2006 | #ifdef ROAR_HAVE_GETSERVBYNAME |
---|
2007 | if ( (serv = getservbyname(argv[i], "tcp")) == NULL ) { |
---|
2008 | ROAR_ERR("Unknown service: %s: %s", argv[i], strerror(errno)); |
---|
2009 | return 1; |
---|
2010 | } |
---|
2011 | // NOTE: we need to use ROAR_NET2HOST16() here even if s_port is of type int! |
---|
2012 | ROAR_DBG("main(*): serv = {s_name='%s', s_aliases={...}, s_port=%i, s_proto='%s'}", |
---|
2013 | serv->s_name, ROAR_NET2HOST16(serv->s_port), serv->s_proto); |
---|
2014 | port = ROAR_NET2HOST16(serv->s_port); |
---|
2015 | #else |
---|
2016 | ROAR_ERR("invalite port number: %s", argv[i]); |
---|
2017 | return 1; |
---|
2018 | #endif |
---|
2019 | } |
---|
2020 | #endif |
---|
2021 | } else if ( strcmp(k, "-b") == 0 || strcmp(k, "--bind") == 0 || strcmp(k, "--sock") == 0 ) { |
---|
2022 | _CKHAVEARGS(1); |
---|
2023 | #ifdef ROAR_SUPPORT_LISTEN |
---|
2024 | sock_addr = argv[++i]; |
---|
2025 | #else |
---|
2026 | i++; |
---|
2027 | #endif |
---|
2028 | |
---|
2029 | } else if ( strcmp(k, "--proto") == 0 ) { |
---|
2030 | #ifdef ROAR_SUPPORT_LISTEN |
---|
2031 | if ( (sock_proto = roar_str2proto(argv[++i])) == -1 ) { |
---|
2032 | ROAR_ERR("Unknown protocol: %s", argv[i]); |
---|
2033 | return 1; |
---|
2034 | } |
---|
2035 | #endif |
---|
2036 | } else if ( strcmp(k, "--proto-dir") == 0 ) { |
---|
2037 | _CKHAVEARGS(1); |
---|
2038 | #ifdef ROAR_SUPPORT_LISTEN |
---|
2039 | if ( (sock_dir = roar_str2dir(argv[++i])) == -1 ) { |
---|
2040 | ROAR_ERR("Unknown stream direction: %s", argv[i]); |
---|
2041 | return 1; |
---|
2042 | } |
---|
2043 | #else |
---|
2044 | i++; |
---|
2045 | #endif |
---|
2046 | } else if ( strcmp(k, "--proto-rate") == 0 ) { |
---|
2047 | _CKHAVEARGS(1); |
---|
2048 | #ifdef ROAR_SUPPORT_LISTEN |
---|
2049 | sock_info.rate = atoi(argv[++i]); |
---|
2050 | #else |
---|
2051 | i++; |
---|
2052 | #endif |
---|
2053 | } else if ( strcmp(k, "--proto-bits") == 0 ) { |
---|
2054 | _CKHAVEARGS(1); |
---|
2055 | #ifdef ROAR_SUPPORT_LISTEN |
---|
2056 | sock_info.bits = atoi(argv[++i]); |
---|
2057 | #else |
---|
2058 | i++; |
---|
2059 | #endif |
---|
2060 | } else if ( strcmp(k, "--proto-chans") == 0 ) { |
---|
2061 | _CKHAVEARGS(1); |
---|
2062 | #ifdef ROAR_SUPPORT_LISTEN |
---|
2063 | sock_info.channels = atoi(argv[++i]); |
---|
2064 | #else |
---|
2065 | i++; |
---|
2066 | #endif |
---|
2067 | } else if ( strcmp(k, "--proto-codec") == 0 ) { |
---|
2068 | _CKHAVEARGS(1); |
---|
2069 | #ifdef ROAR_SUPPORT_LISTEN |
---|
2070 | if ( (sock_info.codec = roar_str2codec(argv[++i])) == -1 ) { |
---|
2071 | ROAR_ERR("Unknown codec: %s", argv[i]); |
---|
2072 | return 1; |
---|
2073 | } |
---|
2074 | #else |
---|
2075 | i++; |
---|
2076 | #endif |
---|
2077 | } else if ( strcmp(k, "--proto-aiprofile") == 0 ) { |
---|
2078 | _CKHAVEARGS(1); |
---|
2079 | #ifdef ROAR_SUPPORT_LISTEN |
---|
2080 | if ( roar_profile2info(&sock_info, argv[++i]) == -1 ) { |
---|
2081 | ROAR_ERR("Unknown audio profile: %s", argv[i]); |
---|
2082 | return 1; |
---|
2083 | } |
---|
2084 | #else |
---|
2085 | i++; |
---|
2086 | #endif |
---|
2087 | } else if ( strcmp(k, "--list-profiles") == 0 ) { |
---|
2088 | #ifdef ROAR_SUPPORT_LISTEN |
---|
2089 | listen_listen_profiles(); |
---|
2090 | return 0; |
---|
2091 | #endif |
---|
2092 | } else if ( strcmp(k, "--proto-profile") == 0 ) { |
---|
2093 | _CKHAVEARGS(1); |
---|
2094 | #ifdef ROAR_SUPPORT_LISTEN |
---|
2095 | if ( get_listen_profile(argv[++i], &port, &sock_addr, &sock_type, &sock_proto, &sock_dir, &sock_info) == -1 ) { |
---|
2096 | ROAR_ERR("Unknown listen profile: %s", argv[i]); |
---|
2097 | return 1; |
---|
2098 | } |
---|
2099 | #else |
---|
2100 | i++; |
---|
2101 | #endif |
---|
2102 | |
---|
2103 | |
---|
2104 | } else if ( strcmp(k, "--list-proto") == 0 ) { |
---|
2105 | list_proto(); |
---|
2106 | return 0; |
---|
2107 | |
---|
2108 | } else if ( strcmp(k, "-t") == 0 || strcmp(k, "--tcp") == 0 ) { |
---|
2109 | #ifdef ROAR_SUPPORT_LISTEN |
---|
2110 | if ( sock_type != ROAR_SOCKET_TYPE_TCP && sock_type != ROAR_SOCKET_TYPE_TCP6 ) |
---|
2111 | sock_type = ROAR_SOCKET_TYPE_TCP; |
---|
2112 | |
---|
2113 | if ( *sock_addr == '/' ) |
---|
2114 | sock_addr = ROAR_DEFAULT_HOST; |
---|
2115 | #endif |
---|
2116 | |
---|
2117 | } else if ( strcmp(k, "-4") == 0 ) { |
---|
2118 | #ifdef ROAR_SUPPORT_LISTEN |
---|
2119 | sock_type = ROAR_SOCKET_TYPE_TCP; |
---|
2120 | if ( *sock_addr == '/' ) |
---|
2121 | sock_addr = ROAR_DEFAULT_HOST; |
---|
2122 | #endif |
---|
2123 | } else if ( strcmp(k, "-6") == 0 ) { |
---|
2124 | #ifdef ROAR_SUPPORT_LISTEN |
---|
2125 | #ifdef AF_INET6 |
---|
2126 | sock_type = ROAR_SOCKET_TYPE_TCP6; |
---|
2127 | if ( *sock_addr == '/' ) |
---|
2128 | sock_addr = ROAR_DEFAULT_HOST; |
---|
2129 | #else |
---|
2130 | ROAR_ERR("No IPv6 support compiled in!"); |
---|
2131 | return 1; |
---|
2132 | #endif |
---|
2133 | #endif |
---|
2134 | |
---|
2135 | } else if ( strcmp(k, "-u") == 0 || strcmp(k, "--unix") == 0 ) { |
---|
2136 | #ifdef ROAR_SUPPORT_LISTEN |
---|
2137 | // ignore this case as it is the default behavor. |
---|
2138 | sock_type = ROAR_SOCKET_TYPE_UNIX; |
---|
2139 | #endif |
---|
2140 | |
---|
2141 | } else if ( strcmp(k, "-n") == 0 || strcmp(k, "--decnet") == 0 ) { |
---|
2142 | #ifdef ROAR_SUPPORT_LISTEN |
---|
2143 | #ifdef ROAR_HAVE_LIBDNET |
---|
2144 | port = ROAR_DEFAULT_NUM; |
---|
2145 | strcpy(decnethost, ROAR_DEFAULT_LISTEN_OBJECT); |
---|
2146 | sock_addr = decnethost; |
---|
2147 | sock_type = ROAR_SOCKET_TYPE_DECNET; |
---|
2148 | #else |
---|
2149 | ROAR_ERR("No DECnet support compiled in!"); |
---|
2150 | return 1; |
---|
2151 | #endif |
---|
2152 | #endif |
---|
2153 | } else if ( strcmp(k, "--new-sock") == 0 ) { |
---|
2154 | #ifdef ROAR_SUPPORT_LISTEN |
---|
2155 | if ( action == START || action == RESTART ) { |
---|
2156 | if ( add_listen(sock_addr, port, sock_type, sock_user, sock_grp, sock_proto, sock_dir, &sock_info) != 0 ) { |
---|
2157 | ROAR_ERR("Can not open listen socket!"); |
---|
2158 | return 1; |
---|
2159 | } |
---|
2160 | } |
---|
2161 | #endif |
---|
2162 | |
---|
2163 | } else if ( strcmp(k, "--slp") == 0 ) { |
---|
2164 | #ifdef ROAR_HAVE_LIBSLP |
---|
2165 | reg_slp = 1; |
---|
2166 | #else |
---|
2167 | ROAR_ERR("No OpenSLP support compiled in!"); |
---|
2168 | return 1; |
---|
2169 | #endif |
---|
2170 | |
---|
2171 | } else if ( strcmp(k, "--x11") == 0 ) { |
---|
2172 | #ifdef ROAR_HAVE_LIBX11 |
---|
2173 | reg_x11 = 1; |
---|
2174 | #else |
---|
2175 | ROAR_ERR("No X11 support compiled in!"); |
---|
2176 | return 1; |
---|
2177 | #endif |
---|
2178 | |
---|
2179 | |
---|
2180 | } else if ( strcmp(k, "--jumbo-mtu") == 0 ) { |
---|
2181 | _CKHAVEARGS(1); |
---|
2182 | g_config->jumbo_mtu = atoi(argv[++i]); |
---|
2183 | |
---|
2184 | } else if ( strcmp(k, "-G") == 0 ) { |
---|
2185 | _CKHAVEARGS(1); |
---|
2186 | sock_grp = argv[++i]; |
---|
2187 | } else if ( strcmp(k, "-U") == 0 ) { |
---|
2188 | _CKHAVEARGS(1); |
---|
2189 | sock_user = argv[++i]; |
---|
2190 | |
---|
2191 | } else if ( strcmp(k, "--no-listen") == 0 ) { |
---|
2192 | #ifdef ROAR_SUPPORT_LISTEN |
---|
2193 | sock_addr = ""; |
---|
2194 | g_terminate = 1; |
---|
2195 | g_no_listen = 1; |
---|
2196 | #endif |
---|
2197 | } else if ( strcmp(k, "--client-fh") == 0 ) { |
---|
2198 | _CKHAVEARGS(1); |
---|
2199 | if ( clients_new_from_fh(atoi(argv[++i]), ROAR_PROTO_ROARAUDIO, ROAR_BYTEORDER_NETWORK, 1) == -1 ) { |
---|
2200 | ROAR_ERR("main(*): Can not set client's fh"); |
---|
2201 | return 1; |
---|
2202 | } |
---|
2203 | } else if ( strcmp(k, "--close-fh") == 0 ) { |
---|
2204 | _CKHAVEARGS(1); |
---|
2205 | #ifdef ROAR_HAVE_IO_POSIX |
---|
2206 | close(atoi(argv[++i])); |
---|
2207 | #else |
---|
2208 | i++; |
---|
2209 | ROAR_WARN("can not close file handle %s (closing not supported)", argv[i]); |
---|
2210 | #endif |
---|
2211 | |
---|
2212 | } else if ( strcmp(k, "--standby") == 0 ) { |
---|
2213 | g_standby = 1; |
---|
2214 | } else if ( strcmp(k, "--auto-standby") == 0 ) { |
---|
2215 | g_autostandby = 1; |
---|
2216 | } else { |
---|
2217 | usage(); |
---|
2218 | return 1; |
---|
2219 | } |
---|
2220 | |
---|
2221 | } |
---|
2222 | #endif |
---|
2223 | |
---|
2224 | #ifdef ROAR_HAVE_MAIN_ARGS |
---|
2225 | switch (action) { |
---|
2226 | case START: |
---|
2227 | // NO-OP. |
---|
2228 | break; |
---|
2229 | case RESTART: |
---|
2230 | // NO-OP, done before. |
---|
2231 | break; |
---|
2232 | case STOP: |
---|
2233 | #ifdef ROAR_SUPPORT_LISTEN |
---|
2234 | if ( restart_server(sock_addr, 0) == -1 ) { |
---|
2235 | ROAR_WARN("Can not stop old server (not running at %s?)", sock_addr); |
---|
2236 | return 1; |
---|
2237 | } |
---|
2238 | return 0; |
---|
2239 | #else |
---|
2240 | ROAR_ERR("--stop not supported"); |
---|
2241 | return 1; |
---|
2242 | #endif |
---|
2243 | break; |
---|
2244 | case SHUTDOWN: |
---|
2245 | #ifdef ROAR_SUPPORT_LISTEN |
---|
2246 | if ( restart_server(sock_addr, 1) == -1 ) { |
---|
2247 | ROAR_WARN("Can not terminate old server (not running at %s?)", sock_addr); |
---|
2248 | return 1; |
---|
2249 | } |
---|
2250 | return 0; |
---|
2251 | #else |
---|
2252 | ROAR_ERR("--shutdown not supported"); |
---|
2253 | return 1; |
---|
2254 | #endif |
---|
2255 | break; |
---|
2256 | case RESTART_RETRY: |
---|
2257 | #ifdef ROAR_SUPPORT_LISTEN |
---|
2258 | if ( restart_server(sock_addr, 1) == -1 ) { |
---|
2259 | ROAR_WARN("Can not terminate old server (again) (not running at %s?), tring to continue anyway", sock_addr); |
---|
2260 | action = RESTART; |
---|
2261 | } |
---|
2262 | #endif |
---|
2263 | break; |
---|
2264 | } |
---|
2265 | #endif |
---|
2266 | |
---|
2267 | #ifndef DEBUG |
---|
2268 | // notify dbg if requested, if in DEBUG mode |
---|
2269 | // do not able because it got enabled early. |
---|
2270 | if ( g_verbose >= 4 ) |
---|
2271 | dbg_notify_cb_register(); |
---|
2272 | #endif |
---|
2273 | |
---|
2274 | if ( af_mode != AF_MODE_NONE ) { |
---|
2275 | if ( add_authfile(af_file, af_type, af_mode, af_acclev) == -1 ) { |
---|
2276 | ROAR_ERR("main(*): adding authfile '%s' failed!", af_file); |
---|
2277 | } |
---|
2278 | } |
---|
2279 | |
---|
2280 | #ifndef ROAR_WITHOUT_DCOMP_MIXER |
---|
2281 | if ( m_drv != NULL ) { |
---|
2282 | if ( hwmixer_add(m_drv, m_dev, m_opts, m_prim, m_count) == -1 ) { |
---|
2283 | ROAR_ERR("main(*): adding mixer '%s' via '%s' failed!", m_dev, m_drv); |
---|
2284 | } |
---|
2285 | } |
---|
2286 | #endif |
---|
2287 | |
---|
2288 | #ifndef ROAR_WITHOUT_DCOMP_SOURCES |
---|
2289 | if ( s_dev != NULL ) { |
---|
2290 | if ( sources_add(s_drv, s_dev, s_con, s_opt, s_prim) == -1 ) { |
---|
2291 | ROAR_ERR("main(*): adding source '%s' via '%s' failed!", s_dev, s_drv); |
---|
2292 | } |
---|
2293 | } |
---|
2294 | #endif |
---|
2295 | |
---|
2296 | if ( output_add_default(o_drv, o_dev, o_opts, o_prim, o_count) == -1 ) { |
---|
2297 | ROAR_ERR("Can not initialize default driver"); |
---|
2298 | return 1; |
---|
2299 | } |
---|
2300 | |
---|
2301 | ROAR_INFO("Server config: rate=%i, bits=%i, chans=%i", ROAR_DBG_INFO_NOTICE, sa.rate, sa.bits, sa.channels); |
---|
2302 | |
---|
2303 | if ( waveform_init() == -1 ) { |
---|
2304 | ROAR_ERR("Can not initialize Waveform subsystem"); |
---|
2305 | return 1; |
---|
2306 | } |
---|
2307 | |
---|
2308 | #ifndef ROAR_WITHOUT_DCOMP_MIDI |
---|
2309 | if ( midi_init() == -1 ) { |
---|
2310 | ROAR_ERR("Can not initialize MIDI subsystem"); |
---|
2311 | } |
---|
2312 | #endif |
---|
2313 | |
---|
2314 | #ifndef ROAR_WITHOUT_DCOMP_SSYNTH |
---|
2315 | if ( ssynth_init() == -1 ) { |
---|
2316 | ROAR_ERR("Can not initialize ssynth subsystem"); |
---|
2317 | } |
---|
2318 | #endif |
---|
2319 | |
---|
2320 | #ifndef ROAR_WITHOUT_DCOMP_LIGHT |
---|
2321 | if ( light_init(light_channels) == -1 ) { |
---|
2322 | ROAR_ERR("Can not initialize light control subsystem"); |
---|
2323 | } |
---|
2324 | #endif |
---|
2325 | |
---|
2326 | #ifndef ROAR_WITHOUT_DCOMP_RDTCS |
---|
2327 | if ( rdtcs_init() == -1 ) { |
---|
2328 | ROAR_ERR("Can not initialize RDTCS subsystem"); |
---|
2329 | } |
---|
2330 | #endif |
---|
2331 | |
---|
2332 | if ( plugins_init() == -1 ) { |
---|
2333 | ROAR_ERR("Can not initialize plugins"); |
---|
2334 | } |
---|
2335 | |
---|
2336 | #ifdef ROAR_SUPPORT_LISTEN |
---|
2337 | if ( !g_no_listen ) { |
---|
2338 | if ( add_listen(sock_addr, port, sock_type, sock_user, sock_grp, sock_proto, sock_dir, &sock_info) != 0 ) { |
---|
2339 | ROAR_ERR("Can not open listen socket!"); |
---|
2340 | return 1; |
---|
2341 | } |
---|
2342 | } |
---|
2343 | #endif |
---|
2344 | |
---|
2345 | if ( output_buffer_init(&sa) == -1 ) { |
---|
2346 | ROAR_ERR("Can not init output buffer!"); |
---|
2347 | return 1; |
---|
2348 | } |
---|
2349 | |
---|
2350 | if ( samples_init() == -1 ) { |
---|
2351 | ROAR_ERR("Can not init samples!"); |
---|
2352 | return 1; |
---|
2353 | } |
---|
2354 | |
---|
2355 | |
---|
2356 | // we should handle this on microcontrollers, too. |
---|
2357 | #if !defined(ROAR_TARGET_MICROCONTROLLER) && !defined(ROAR_TARGET_WIN32) |
---|
2358 | signal(SIGINT, on_sig_int); |
---|
2359 | signal(SIGTERM, on_sig_term); |
---|
2360 | signal(SIGCHLD, on_sig_chld); |
---|
2361 | signal(SIGUSR1, on_sig_usr1); |
---|
2362 | signal(SIGPIPE, SIG_IGN); // ignore broken pipes |
---|
2363 | #endif |
---|
2364 | |
---|
2365 | if ( g_config->memlock_level == -1 ) { |
---|
2366 | g_config->memlock_level = MEMLOCK_DEFAULT; |
---|
2367 | } |
---|
2368 | |
---|
2369 | if ( memlock_set_level(g_config->memlock_level) == -1 ) { |
---|
2370 | ROAR_WARN("Can not set memory locking level to target level."); |
---|
2371 | } |
---|
2372 | |
---|
2373 | if ( realtime ) { |
---|
2374 | #ifdef DEBUG |
---|
2375 | ROAR_WARN("compiled with -DDEBUG but realtime is enabled: for real realtime support compile without -DDEBUG"); |
---|
2376 | #endif |
---|
2377 | |
---|
2378 | #ifdef ROAR_HAVE_NICE |
---|
2379 | // this stupid error check is because type of returned data of nice() changed |
---|
2380 | // too often. On some systems it may return 0/-1, on some new nice value or |
---|
2381 | // mixed forms of both. |
---|
2382 | errno = 0; |
---|
2383 | (void)nice(-5*realtime); // -5 for each --realtime |
---|
2384 | if ( errno ) { |
---|
2385 | ROAR_WARN("Can not decrease nice value by %i: %s", 5*realtime, strerror(errno)); |
---|
2386 | } |
---|
2387 | #else |
---|
2388 | ROAR_WARN("Can not decrease nice value by %i: %s", 5*realtime, strerror(errno)); |
---|
2389 | #endif |
---|
2390 | /* |
---|
2391 | #ifdef __linux__ |
---|
2392 | if ( ioprio_set(IOPRIO_WHO_PROCESS, getpid(), IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 0)) == -1 ) |
---|
2393 | ROAR_WARN("Can not set io priority: %s", strerror(errno)); |
---|
2394 | #endif |
---|
2395 | */ |
---|
2396 | } |
---|
2397 | |
---|
2398 | #if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX) |
---|
2399 | if ( setids & R_SETGID ) { |
---|
2400 | if ( sock_grp == NULL ) { |
---|
2401 | ROAR_ERR("Can not set GID if no groupname is supplied"); |
---|
2402 | return 1; |
---|
2403 | } |
---|
2404 | if ( (grp = getgrnam(sock_grp)) == NULL ) { |
---|
2405 | ROAR_ERR("Can not get GID for group %s: %s", sock_grp, strerror(errno)); |
---|
2406 | return 1; |
---|
2407 | } |
---|
2408 | if ( setgroups(0, (const gid_t *) NULL) == -1 ) { |
---|
2409 | ROAR_ERR("Can not clear supplementary group IDs: %s", strerror(errno)); |
---|
2410 | } |
---|
2411 | if ( !grp || setgid(grp->gr_gid) == -1 ) { |
---|
2412 | ROAR_ERR("Can not set GroupID: %s", strerror(errno)); |
---|
2413 | } |
---|
2414 | } |
---|
2415 | #endif |
---|
2416 | |
---|
2417 | |
---|
2418 | clients_set_pid(g_self_client, getpid()); |
---|
2419 | #ifdef ROAR_HAVE_GETUID |
---|
2420 | clients_set_uid(g_self_client, getuid()); |
---|
2421 | #endif |
---|
2422 | #ifdef ROAR_HAVE_GETGID |
---|
2423 | clients_set_gid(g_self_client, getgid()); |
---|
2424 | #endif |
---|
2425 | clients_get(g_self_client, &self); |
---|
2426 | |
---|
2427 | if ( self == NULL ) { |
---|
2428 | ROAR_ERR("Can not get self client!"); |
---|
2429 | return 1; |
---|
2430 | } |
---|
2431 | |
---|
2432 | strcpy(self->name, "RoarAudio daemon internal"); |
---|
2433 | |
---|
2434 | if ( roar_nnode_free(&(self->nnode)) == -1 ) |
---|
2435 | return 1; |
---|
2436 | |
---|
2437 | // not fully correct but ok as workaorund |
---|
2438 | // so tools assume that roard runs on the same machine as |
---|
2439 | // they in case they use AF_UNIX: |
---|
2440 | if ( roar_nnode_new(&(self->nnode), ROAR_SOCKET_TYPE_UNIX) == -1 ) |
---|
2441 | return 1; |
---|
2442 | |
---|
2443 | #if defined(ROAR_HAVE_FORK) || defined(ROAR_TARGET_WIN32) |
---|
2444 | if ( daemon ) { |
---|
2445 | #ifdef ROAR_HAVE_SYSLOG |
---|
2446 | roar_debug_set_stderr_mode(ROAR_DEBUG_MODE_SYSLOG); |
---|
2447 | #else |
---|
2448 | roar_debug_set_stderr_fh(-1); |
---|
2449 | #endif |
---|
2450 | |
---|
2451 | #ifdef ROAR_TARGET_WIN32 |
---|
2452 | FreeConsole(); |
---|
2453 | #else |
---|
2454 | |
---|
2455 | close(ROAR_STDIN ); |
---|
2456 | close(ROAR_STDOUT); |
---|
2457 | close(ROAR_STDERR); |
---|
2458 | |
---|
2459 | if ( fork() ) |
---|
2460 | ROAR_U_EXIT(0); |
---|
2461 | |
---|
2462 | #ifdef ROAR_HAVE_SETSID |
---|
2463 | setsid(); |
---|
2464 | #endif |
---|
2465 | clients_set_pid(g_self_client, getpid()); // reset pid as it changed |
---|
2466 | #endif |
---|
2467 | } |
---|
2468 | #endif |
---|
2469 | |
---|
2470 | #if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX) |
---|
2471 | // early test for UID as we need this for the pidfile and the setuid() |
---|
2472 | if ( sock_user != NULL ) { |
---|
2473 | if ( (pwd = getpwnam(sock_user)) == NULL ) { |
---|
2474 | ROAR_ERR("Can not get UID for user %s: %s", sock_user, strerror(errno)); |
---|
2475 | return 1; |
---|
2476 | } |
---|
2477 | } |
---|
2478 | #endif |
---|
2479 | |
---|
2480 | ROAR_INFO("Process ID: %i", ROAR_DBG_INFO_INFO, (int)getpid()); |
---|
2481 | |
---|
2482 | #ifdef SUPPORT_PIDFILE |
---|
2483 | if ( pidfile != NULL ) { |
---|
2484 | if ( roar_vio_open_file(&pidfile_vio, pidfile, O_WRONLY|O_CREAT, 0644) == -1 ) { |
---|
2485 | ROAR_ERR("Can not write pidfile: %s", pidfile); |
---|
2486 | } else { |
---|
2487 | roar_vio_printf(&pidfile_vio, "%i\n", getpid()); |
---|
2488 | roar_vio_close(&pidfile_vio); |
---|
2489 | } |
---|
2490 | #if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX) |
---|
2491 | if ( pwd || grp ) { |
---|
2492 | if ( chown(pidfile, pwd ? pwd->pw_uid : -1, grp ? grp->gr_gid : -1) == -1 ) { |
---|
2493 | ROAR_WARN("Can not change ownership of pidfile: %s: %s", pidfile, strerror(errno)); |
---|
2494 | } |
---|
2495 | } |
---|
2496 | if ( chmod(pidfile, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) == -1 ) { |
---|
2497 | ROAR_WARN("Can not change permissions of pidfile: %s: %s", pidfile, strerror(errno)); |
---|
2498 | } |
---|
2499 | #endif |
---|
2500 | } |
---|
2501 | #endif |
---|
2502 | |
---|
2503 | #ifdef ROAR_HAVE_CHROOT |
---|
2504 | if (chrootdir) { |
---|
2505 | if ( chroot(chrootdir) == -1 ) { |
---|
2506 | ROAR_ERR("Can not chroot to %s: %s", chrootdir, strerror(errno)); |
---|
2507 | return 2; |
---|
2508 | } |
---|
2509 | if ( chdir("/") == -1 ) { |
---|
2510 | ROAR_ERR("Can not chdir to /: %s", strerror(errno)); |
---|
2511 | return 2; |
---|
2512 | } |
---|
2513 | } |
---|
2514 | #endif |
---|
2515 | |
---|
2516 | #if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX) |
---|
2517 | if ( setids & R_SETUID ) { |
---|
2518 | if ( sock_user == NULL ) { |
---|
2519 | ROAR_ERR("Can not set UID if no username is supplied"); |
---|
2520 | return 1; |
---|
2521 | } |
---|
2522 | if ( !pwd || setuid(pwd->pw_uid) == -1 ) { |
---|
2523 | ROAR_ERR("Can not set UserID: %s", strerror(errno)); |
---|
2524 | return 3; |
---|
2525 | } |
---|
2526 | #ifdef ROAR_HAVE_GETUID |
---|
2527 | clients_set_uid(g_self_client, getuid()); |
---|
2528 | #endif |
---|
2529 | } |
---|
2530 | #endif |
---|
2531 | |
---|
2532 | // setup auth: |
---|
2533 | #if defined(ROAR_HAVE_GETUID) && defined(ROAR_HAVE_GETGID) |
---|
2534 | if ( auth_setup(none_acclev, trust_acclev, trust_root, getuid(), getgid()) == -1 ) { |
---|
2535 | #else |
---|
2536 | if ( auth_setup(none_acclev, trust_acclev, trust_root, -1, -1) == -1 ) { |
---|
2537 | #endif |
---|
2538 | ROAR_ERR("Can not set up auth. Bad."); |
---|
2539 | alive = 0; |
---|
2540 | } |
---|
2541 | |
---|
2542 | // Register with OpenSLP: |
---|
2543 | #ifdef ROAR_HAVE_LIBSLP |
---|
2544 | if ( reg_slp ) { |
---|
2545 | register_slp(0, sock_addr); |
---|
2546 | } |
---|
2547 | #endif |
---|
2548 | |
---|
2549 | #ifdef ROAR_HAVE_LIBX11 |
---|
2550 | if ( reg_x11 ) { |
---|
2551 | register_x11(0, sock_addr); |
---|
2552 | } |
---|
2553 | #endif |
---|
2554 | |
---|
2555 | // update sync counter. |
---|
2556 | streams_change_sync_num(-1, 0); |
---|
2557 | |
---|
2558 | ROAR_INFO("Startup complet", ROAR_DBG_INFO_INFO); |
---|
2559 | |
---|
2560 | // start main loop... |
---|
2561 | ROAR_INFO("Entering main loop", ROAR_DBG_INFO_INFO); |
---|
2562 | main_loop(&sa, sysclocksync); |
---|
2563 | ROAR_INFO("Left main loop", ROAR_DBG_INFO_INFO); |
---|
2564 | |
---|
2565 | // clean up. |
---|
2566 | clean_quit_prep(); |
---|
2567 | output_buffer_free(); |
---|
2568 | |
---|
2569 | roar_notify_core_free(NULL); |
---|
2570 | |
---|
2571 | ROAR_INFO("Shuting down complete", ROAR_DBG_INFO_INFO); |
---|
2572 | |
---|
2573 | if ( g_config->scripts.post_shutdown != NULL ) |
---|
2574 | system(g_config->scripts.post_shutdown); |
---|
2575 | |
---|
2576 | ROAR_INFO("Exiting, no error", ROAR_DBG_INFO_INFO); |
---|
2577 | return 0; |
---|
2578 | } |
---|
2579 | |
---|
2580 | void cleanup_listen_socket (int terminate) { |
---|
2581 | int i; |
---|
2582 | |
---|
2583 | ROAR_DBG("cleanup_listen_socket(terminate=%i) = (void)?", terminate); |
---|
2584 | |
---|
2585 | ROAR_INFO("Cleaning up listen sockets", ROAR_DBG_INFO_INFO); |
---|
2586 | |
---|
2587 | // Deregister from SLP: |
---|
2588 | #ifdef ROAR_HAVE_LIBSLP |
---|
2589 | register_slp(1, NULL); |
---|
2590 | #endif |
---|
2591 | |
---|
2592 | #ifdef ROAR_HAVE_LIBX11 |
---|
2593 | register_x11(1, NULL); |
---|
2594 | #endif |
---|
2595 | |
---|
2596 | #ifdef ROAR_SUPPORT_LISTEN |
---|
2597 | for (i = 0; i < ROAR_MAX_LISTEN_SOCKETS; i++) { |
---|
2598 | if ( g_listen[i].used ) { |
---|
2599 | roar_vio_close(&(g_listen[i].sock)); |
---|
2600 | |
---|
2601 | g_listen[i].used = 0; |
---|
2602 | |
---|
2603 | #ifdef ROAR_HAVE_UNIX |
---|
2604 | if ( server[i] != NULL ) |
---|
2605 | if ( *(server[i]) == '/' ) |
---|
2606 | unlink(server[i]); |
---|
2607 | #endif |
---|
2608 | } |
---|
2609 | } |
---|
2610 | |
---|
2611 | #endif |
---|
2612 | |
---|
2613 | if ( terminate ) |
---|
2614 | g_terminate = 1; |
---|
2615 | } |
---|
2616 | |
---|
2617 | void clean_quit_prep (void) { |
---|
2618 | cleanup_listen_socket(0); |
---|
2619 | |
---|
2620 | plugins_free(); |
---|
2621 | |
---|
2622 | #ifndef ROAR_WITHOUT_DCOMP_SOURCES |
---|
2623 | sources_free(); |
---|
2624 | #endif |
---|
2625 | streams_free(); |
---|
2626 | clients_free(); |
---|
2627 | #ifndef ROAR_WITHOUT_DCOMP_SSYNTH |
---|
2628 | ssynth_free(); |
---|
2629 | #endif |
---|
2630 | #ifndef ROAR_WITHOUT_DCOMP_CB |
---|
2631 | midi_cb_stop(); // stop console beep |
---|
2632 | #endif |
---|
2633 | #ifndef ROAR_WITHOUT_DCOMP_MIDI |
---|
2634 | midi_free(); |
---|
2635 | #endif |
---|
2636 | #ifndef ROAR_WITHOUT_DCOMP_LIGHT |
---|
2637 | light_free(); |
---|
2638 | #endif |
---|
2639 | #ifndef ROAR_WITHOUT_DCOMP_RDTCS |
---|
2640 | rdtcs_free(); |
---|
2641 | #endif |
---|
2642 | |
---|
2643 | waveform_free(); |
---|
2644 | |
---|
2645 | #ifdef SUPPORT_PIDFILE |
---|
2646 | if ( pidfile != NULL ) |
---|
2647 | unlink(pidfile); |
---|
2648 | #endif |
---|
2649 | |
---|
2650 | auth_free(); |
---|
2651 | } |
---|
2652 | |
---|
2653 | void clean_quit (void) { |
---|
2654 | ROAR_INFO("Shuting down", ROAR_DBG_INFO_INFO); |
---|
2655 | |
---|
2656 | counters_print(ROAR_DEBUG_TYPE_INFO, 0); |
---|
2657 | |
---|
2658 | clean_quit_prep(); |
---|
2659 | // output_buffer_free(); |
---|
2660 | |
---|
2661 | roar_notify_core_free(NULL); |
---|
2662 | |
---|
2663 | ROAR_INFO("Shuting down complete", ROAR_DBG_INFO_INFO); |
---|
2664 | |
---|
2665 | if ( g_config->scripts.post_shutdown != NULL ) |
---|
2666 | system(g_config->scripts.post_shutdown); |
---|
2667 | |
---|
2668 | ROAR_INFO("Exiting, no error", ROAR_DBG_INFO_INFO); |
---|
2669 | exit(0); |
---|
2670 | } |
---|
2671 | |
---|
2672 | //ll |
---|