source: roaraudio/roard/roard.c @ 4851:d46b432ae11a

Last change on this file since 4851:d46b432ae11a was 4851:d46b432ae11a, checked in by phi, 13 years ago

added support to run some script after complete shutdown

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