source: roaraudio/roard/roard.c @ 4848:0b3d312d4253

Last change on this file since 4848:0b3d312d4253 was 4848:0b3d312d4253, checked in by phi, 13 years ago

updated add_driver() to check the args and fix segfauls if user gave incorrect parameters

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