source: roaraudio/roard/roard.c @ 4858:ef003b336588

Last change on this file since 4858:ef003b336588 was 4852:166a9cc557c9, checked in by phi, 13 years ago

moved a lot stuff out of roard.c

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