source: roaraudio/roard/roard.c @ 1926:4522f8bfab00

Last change on this file since 1926:4522f8bfab00 was 1926:4522f8bfab00, checked in by phi, 15 years ago

added sill dummy stream flag MMAP :)

File size: 25.6 KB
RevLine 
[0]1//roard.c:
2
[668]3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008
5 *
6 *  This file is part of roard a part of RoarAudio,
7 *  a cross-platform sound system for both, home and professional use.
8 *  See README for details.
9 *
10 *  This file is free software; you can redistribute it and/or modify
11 *  it under the terms of the GNU General Public License version 3
12 *  as published by the Free Software Foundation.
13 *
14 *  RoarAudio is distributed in the hope that it will be useful,
15 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 *  GNU General Public License for more details.
18 *
19 *  You should have received a copy of the GNU General Public License
20 *  along with this software; see the file COPYING.  If not, write to
21 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
23 */
24
[0]25#include "roard.h"
26
[1494]27#ifdef ROAR_SUPPORT_LISTEN
[60]28char * server = ROAR_DEFAULT_SOCK_GLOBAL; // global server address
[1494]29#endif
[60]30
[1503]31#ifdef ROAR_HAVE_MAIN_ARGS
[0]32void usage (void) {
33 printf("Usage: roard [OPTIONS]...\n\n");
34
[68]35 printf("Misc Options:\n\n");
36 printf(
[775]37        " --daemon              - Bring the server into background after init\n"
[71]38        " --terminate           - Terminate after last client quited\n"
[274]39        " --restart             - Trys to stop an old instance and start a new with new settings\n"
[276]40        " --realtime            - Trys to get realtime priority,\n"
41        "                         give multible times for being more realtime\n"
[444]42        " --chroot DIR          - chroots to the given dir\n"
43        " --setgid              - GroupID to the audio group as specified via -G\n"
44        " --setuid              - UserID to the audio user as specified via -U\n"
[905]45        " --sysclocksync        - calculate exact sample rate using the system clock\n"
[68]46       );
47
48 printf("\nAudio Options:\n\n");
[0]49 printf(
50        " -R  --rate   RATE     - Set server rate\n"
51        " -B  --bits   BITS     - Set server bits\n"
52        " -C  --chans  CHANNELS - Set server channels\n"
53       );
54
[1923]55 printf("\nDriver Options: (obsolete, do not use, Use Ouput Options)\n\n");
[974]56 printf(" -d  --driver DRV      - Set the driver (default: %s)\n", ROAR_DRIVER_DEFAULT);
[0]57 printf(" -D  --device DEV      - Set the device\n");
58 printf(" -dO OPTS              - Set output options\n");
[973]59 printf(" --list-driver         - List all drivers\n");
[0]60
[932]61 printf("\nOutput Options:\n\n");
[974]62 printf(" -o  --odriver DRV     - Set the driver, use '--list-driver' to get a list\n");
[932]63 printf(" -O  --odevice DEV     - Set the device\n");
64 printf(" -oO OPTS              - Set output options\n");
65 printf(" -oN                   - Adds another output\n");
[961]66 printf(" -oP                   - Mark output as primary\n");
[932]67
[0]68 printf("\nSource Options:\n\n");
69 printf(" -s  --source DRV      - Use DRV as input driver\n"
70        " -S           DEV      - Use DEV as input device\n"
71        " -sO          OPTS     - Use OPTS as input options\n"
[1512]72        " -sN                   - Adds another source\n"
[0]73        " -sP                   - Make souce as primary\n"
74       );
75
[280]76 printf("\nCodec Filter Options:\n\n");
77 printf(" --list-cf             - List all codec filter\n"
78       );
[0]79
[1923]80 printf("\nMIDI Options:\n\n");
[1924]81 printf(" --midi-no-console     - Disable console based MIDI synth\n"
82        " --midi-console DEV    - Set device for MIDI console\n"
83       );
[1923]84
85 printf("\nLight Control Options:\n\n");
86 printf(" --light-channels NUM  - Sets the number of channels for Light control (default: %i)\n",
87                                  LIGHT_CHANNELS_DEFAULT
88       );
89
[0]90 printf("\nServer Options:\n\n");
91 printf(" -t  --tcp             - Use TCP listen socket\n"
92        " -u  --unix            - Use UNIX Domain listen socket (default)\n"
[508]93#ifdef ROAR_HAVE_LIBDNET
94        " -n  --decnet          - use DECnet listen socket\n"
95#endif
[518]96        " -4                    - Use IPv4 connections (implies -t)\n"
97#ifdef PF_INET6
98        " -6                    - Use IPv6 connections (implies -t)\n"
99#endif
100#ifdef IPV6_ADDRFORM
101        " -64                   - Try to downgrade sockets from IPv6 into IPv4,\n"
102        "                         this is normaly not usefull.\n"
103#endif
[0]104        " -p  --port            - TCP Port to bind to\n"
105        " -b  --bind            - IP/Hostname to bind to\n"
[1115]106        "     --sock            - Filename for UNIX Domain Socket\n"
[450]107        " -G  GROUP             - Sets the group for the UNIX Domain Socket, (default: %s)\n"
[60]108        "                         You need the permittions to change the GID\n"
[444]109        " -U  USER              - Sets the user for the UNIX Domain Socket, (default: do not set)\n"
110        "                         You need the permittions to change the UID (normaly only root has)\n"
[548]111        " --no-listen           - Do not listen for new clients\n"
112        "                         (only usefull for relaing, impleys --terminate)\n"
[274]113        " --client-fh           - Comunicate with a client over this handle\n"
[501]114        "                         (only usefull for relaing)\n"
[920]115        " --close-fh            - Closes the given fh\n"
116        " --standby             - Start in standby state\n"
117        " --auto-standby        - Automatical goes into standby if there are no streams\n",
[450]118        ROAR_DEFAULT_SOCKGRP
[0]119       );
120// printf("\n Options:\n\n");
121 printf("\n");
122}
[1503]123#endif
[0]124
[579]125int restart_server (char * server) {
126 struct roar_connection con;
127 if ( roar_connect(&con, server) == -1 ) {
128  return -1;
129 }
130
131 if ( roar_terminate(&con, 1) == -1 ) {
132  return -1;
133 }
134
135 return roar_disconnect(&con);
136}
137
[444]138#define R_SETUID 1
139#define R_SETGID 2
140
[1609]141int init_config (void) {
142 int i;
143
144 memset(g_config, 0, sizeof(struct roard_config));
145
146 for (i = 0; i < ROAR_DIR_DIRIDS; i++) {
147  g_config->streams[i].mixer_channels = 1;
148  g_config->streams[i].mixer.rpg_mul  = 1;
149  g_config->streams[i].mixer.rpg_div  = 1;
150  g_config->streams[i].mixer.scale    = 65535;
151  g_config->streams[i].mixer.mixer[0] = g_config->streams[i].mixer.scale;
152 }
153
[1899]154 g_config->streams[ROAR_DIR_MIDI_OUT].flags = ROAR_FLAG_SYNC;
155
[1609]156 return 0;
157}
158
[1145]159int add_output (char * drv, char * dev, char * opts, int prim, int count) {
[933]160 int stream;
161 struct roar_stream * s;
162 struct roar_stream_server * ss;
[938]163 char * k, * v;
[1208]164#ifdef ROAR_DRIVER_CODEC
165 char * to_free = NULL;
166#endif
[938]167 int codec;
[1926]168 int sync = 0, f_mmap = 0;
[1522]169 int32_t blocks = -1, blocksize = -1;
[1920]170 int dir = ROAR_DIR_OUTPUT;
[933]171
[938]172 ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = ?", drv, dev, opts);
[933]173
[1145]174 if ( drv == NULL && count == 0 ) {
175  drv  = ROAR_DRIVER_DEFAULT;
176  prim = 1;
177  sync = 1;
[1208]178
179#ifdef ROAR_DRIVER_CODEC
180  if ( opts == NULL ) {
181   opts = to_free = strdup("codec=" ROAR_DRIVER_CODEC);
182  }
183#endif
[1145]184 }
185
[1227]186 if ( opts == NULL && count == 0 ) {
187  sync = 1;
188  prim = 1; // if ( prim == 0 ) prim = 1; -> prim allways = 1
189 }
190
[933]191 if ( (stream = streams_new()) == -1 ) {
[938]192  ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = -1", drv, dev, opts);
[982]193  if ( prim ) alive = 0;
[933]194  return -1;
195 }
196
197 streams_get(stream, &ss);
198 s = ROAR_STREAM(ss);
199
200 memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info));
201
202 s->pos_rel_id = -1;
203// s->info.codec = codec;
204
[938]205 codec = s->info.codec;
206
207 k = strtok(opts, ",");
208 while (k != NULL) {
209//  ROAR_WARN("add_output(*): opts: %s", k);
210
211  if ( (v = strstr(k, "=")) != NULL ) {
212   *v++ = 0;
213  }
214
215  ROAR_DBG("add_output(*): opts: k='%s', v='%s'", k, v);
216  if ( strcmp(k, "rate") == 0 ) {
217   s->info.rate = atoi(v);
218  } else if ( strcmp(k, "channels") == 0 ) {
219   s->info.channels = atoi(v);
220  } else if ( strcmp(k, "bits") == 0 ) {
221   s->info.bits = atoi(v);
222  } else if ( strcmp(k, "codec") == 0 ) {
223   if ( (codec = roar_str2codec(v)) == -1 ) {
224    ROAR_ERR("add_output(*): unknown codec '%s'", v);
225    streams_delete(stream);
[982]226    if ( prim ) alive = 0;
[1208]227#ifdef ROAR_DRIVER_CODEC
228    if ( to_free != NULL )
229     free(to_free);
230#endif
[938]231    return -1;
232   }
[1221]233  } else if ( strcmp(k, "blocks") == 0 ) {
234   blocks = atoi(v);
[1522]235  } else if ( strcmp(k, "blocksize") == 0 ) {
236   blocksize = atoi(v);
[1926]237  } else if ( strcmp(k, "mmap") == 0 ) {
238   f_mmap = 1;
[1919]239  } else if ( strcmp(k, "subsystem") == 0 ) {
240   if ( !strcasecmp(v, "wave") || !strcasecmp(v, "waveform") ) {
[1920]241    dir = ROAR_DIR_OUTPUT;
[1919]242   } else if ( !strcasecmp(v, "midi") ) {
243    dir = ROAR_DIR_MIDI_OUT;
244   } else if ( !strcasecmp(v, "light") ) {
245    dir = ROAR_DIR_LIGHT_OUT;
246   } else {
247    ROAR_ERR("add_output(*): unknown subsystem '%s'", k);
248    streams_delete(stream);
249    if ( prim ) alive = 0;
250#ifdef ROAR_DRIVER_CODEC
251    if ( to_free != NULL )
252     free(to_free);
253#endif
254    return -1;
255   }
[1032]256  } else if ( strcmp(k, "meta") == 0 ) {
257   streams_set_flag(stream, ROAR_FLAG_META);
[1117]258  } else if ( strcmp(k, "sync") == 0 ) {
259   sync = 1;
[1221]260  } else if ( strcmp(k, "primary") == 0 ) {
261   prim = 1;
262
263  } else if ( strcmp(k, "cleanmeta") == 0 ) {
264   streams_set_flag(stream, ROAR_FLAG_CLEANMETA);
265  } else if ( strcmp(k, "autoconf") == 0 ) {
[1531]266   streams_set_flag(stream, ROAR_FLAG_AUTOCONF);
[938]267  } else {
268   ROAR_ERR("add_output(*): unknown option '%s'", k);
269   streams_delete(stream);
[982]270   if ( prim ) alive = 0;
[1208]271#ifdef ROAR_DRIVER_CODEC
272   if ( to_free != NULL )
273    free(to_free);
274#endif
[938]275   return -1;
276  }
277
278  k = strtok(NULL, ",");
279 }
280
[1919]281 if ( streams_set_dir(stream, dir, 1) == -1 ) {
282  streams_delete(stream);
283  return -1;
284 }
285
[1208]286#ifdef ROAR_DRIVER_CODEC
287 if ( to_free != NULL )
288  free(to_free);
289#endif
290
[941]291 if ( codec == ROAR_CODEC_ALAW || codec == ROAR_CODEC_MULAW )
292  s->info.bits = 8; // needed to open OSS driver, will be overriden by codecfilter
293
[938]294 s->info.codec = codec;
295 ROAR_STREAM_SERVER(s)->codec_orgi = codec;
[933]296
[1145]297 if ( driver_openvio(&(ss->vio), &(ss->driver_id), drv, dev, &(s->info), -1) == -1 ) {
[1581]298  ss->driver_id = -1; // don't close a driver not opened...
299  memset(&(ss->vio), 0, sizeof(struct roar_vio_calls));
[933]300  streams_delete(stream);
[938]301  ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = -1", drv, dev, opts);
[982]302  if ( prim ) alive = 0;
[933]303  return -1;
304 }
305
[1528]306 roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_SSTREAMID, &stream); // ignore errors here
307 roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_SSTREAM,   s); // ignore errors here
308
[1221]309 if ( blocks != -1 )
310  roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DBLOCKS, &blocks);
311
[1522]312 if ( blocksize != -1 )
313  roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DBLKSIZE, &blocksize);
314
[1156]315 ROAR_DBG("add_output(*): ss->driver_id=%i", ss->driver_id);
316
[938]317 streams_set_fh(stream, -1); // update some internal structures
318
[933]319 client_stream_add(g_source_client, stream);
320
[1200]321 if ( prim ) {
[961]322  streams_mark_primary(stream);
[1200]323  s->pos_rel_id = stream;
324 }
[961]325
[1120]326 if ( sync ) {
[1117]327  streams_set_flag(stream, ROAR_FLAG_SYNC);
[1120]328 } else {
329  streams_reset_flag(stream, ROAR_FLAG_SYNC);
330 }
[1117]331
[1926]332 if ( f_mmap )
333  streams_set_flag(stream, ROAR_FLAG_MMAP);
334
[933]335 return 0;
[932]336}
337
[1503]338#ifdef ROAR_HAVE_MAIN_ARGS
[0]339int main (int argc, char * argv[]) {
[1503]340#else
341int main (void) {
342#endif
343#ifdef ROAR_HAVE_MAIN_ARGS
[0]344 int i;
345 char * k;
[1503]346#endif
[1494]347#ifdef ROAR_SUPPORT_LISTEN
[905]348 char user_sock[80]  = {0};
[1494]349#endif
[1609]350 struct roar_audio_info sa, max_sa;
351 struct roard_config config;
[1486]352#ifdef ROAR_HAVE_FORK
[905]353 int    daemon       = 0;
[1486]354#endif
[905]355 int    realtime     = 0;
356 int    sysclocksync = 0;
[1207]357 char * driver    = NULL;
358 char * device    = NULL;
[1503]359#ifdef ROAR_HAVE_MAIN_ARGS
[1207]360 char * opts      = NULL;
[1503]361#endif
[60]362// char * server = ROAR_DEFAULT_SOCK_GLOBAL;
[1494]363#ifdef ROAR_SUPPORT_LISTEN
[1207]364 int      port    = ROAR_DEFAULT_PORT;
[1494]365#endif
[0]366 int               drvid;
[550]367 char * s_drv     = "cf";
[1110]368 char * s_dev     = NULL;
[444]369 char * s_con     = NULL;
370 char * s_opt     = NULL;
371 int    s_prim    = 0;
[1207]372 char * o_drv     = getenv("ROAR_DRIVER");
373 char * o_dev     = getenv("ROAR_DEVICE");
[932]374 char * o_opts    = NULL;
[961]375 int    o_prim    = 0;
[1145]376 int    o_count   = 0;
[1923]377 int    light_channels = LIGHT_CHANNELS_DEFAULT;
[450]378 char * sock_grp  = ROAR_DEFAULT_SOCKGRP;
[444]379 char * sock_user = NULL;
[1494]380#ifdef ROAR_SUPPORT_LISTEN
[517]381 int    sock_type = ROAR_SOCKET_TYPE_UNKNOWN;
[1494]382#endif
[1486]383#ifdef ROAR_HAVE_CHROOT
[444]384 char * chrootdir = NULL;
[1486]385#endif
386#if defined(ROAR_HAVE_SETGID) || defined(ROAR_HAVE_SETUID)
[444]387 int    setids    = 0;
[1486]388#endif
389#ifdef ROAR_HAVE_UNIX
[1011]390 char * env_roar_proxy_backup;
[1486]391#endif
392#if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX)
[446]393 struct group   * grp  = NULL;
[1486]394#endif
395#if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
[446]396 struct passwd  * pwd  = NULL;
[1486]397#endif
398#ifdef ROAR_HAVE_GETSERVBYNAME
[446]399 struct servent * serv = NULL;
[1486]400#endif
[0]401 DRIVER_USERDATA_T drvinst;
[39]402 struct roar_client * self = NULL;
[508]403#ifdef ROAR_HAVE_LIBDNET
404 char decnethost[80];
405#endif
[0]406
407 g_standby       =  0;
[920]408 g_autostandby   =  0;
[982]409 alive           =  1;
[1494]410#ifdef ROAR_SUPPORT_LISTEN
[1155]411 g_no_listen     =  0;
[1494]412 g_listen_socket = -1;
413#else
414 g_terminate     =  1;
415#endif
[0]416
417 sa.bits     = ROAR_BITS_DEFAULT;
418 sa.channels = ROAR_CHANNELS_DEFAULT;
419 sa.rate     = ROAR_RATE_DEFAULT;
420 sa.codec    = ROAR_CODEC_DEFAULT;
421
[1609]422 g_sa        = &sa;
423 g_max_sa    = &max_sa;
424
425 memcpy(g_max_sa, g_sa, sizeof(max_sa));
426
427 g_config = &config;
428
429 if ( init_config() == -1 ) {
430  ROAR_ERR("Can not init default config!");
431  return 1;
432 }
[0]433
[1924]434 if ( midi_init_config() == -1 ) {
435  ROAR_ERR("Can not init MIDI config!");
436  return 1;
437 }
[60]438
[1494]439#ifdef ROAR_SUPPORT_LISTEN
[1753]440#ifdef ROAR_HAVE_GETUID
[1486]441 if ( getuid() != 0 && getenv("HOME") != NULL ) {
442  snprintf(user_sock, 79, "%s/%s", (char*)getenv("HOME"), ROAR_DEFAULT_SOCK_USER);
[60]443  server = user_sock;
444 }
[1753]445#endif
[60]446
[279]447 if ( getenv("ROAR_SERVER") != NULL )
448  server = getenv("ROAR_SERVER");
[1494]449#endif
[279]450
[63]451 if ( clients_init() == -1 ) {
452  ROAR_ERR("Can not init clients!");
453  return 1;
454 }
455
456 if ( streams_init() == -1 ) {
457  ROAR_ERR("Can not init streams!");
458  return 1;
459 }
460
[64]461 if ( (g_self_client = clients_new()) == -1 ) {
462  ROAR_ERR("Can not create self client!");
463  return 1;
464 }
465
[0]466 if ( sources_init() == -1 ) {
467  ROAR_ERR("Can not init sources!");
468  return 1;
469 }
470
[64]471 if ( (sources_set_client(g_self_client)) == -1 ) {
472  ROAR_ERR("Can not init set source client!");
473  return 1;
474 }
475
[1503]476#ifdef ROAR_HAVE_MAIN_ARGS
[0]477 for (i = 1; i < argc; i++) {
478  k = argv[i];
479
480  if ( strcmp(k, "-h") == 0 || strcmp(k, "--help") == 0 ) {
481   usage();
482   return 0;
483
[579]484  } else if ( strcmp(k, "--restart") == 0 ) {
[1494]485#ifdef ROAR_SUPPORT_LISTEN
[579]486   if ( restart_server(server) == -1 ) {
487    ROAR_WARN("Can not terminate old server (not running at %s?), tring to continue anyway", server);
488   }
[1494]489#else
490   ROAR_ERR("--restart not supported");
491#endif
[579]492
[775]493  } else if ( strcmp(k, "--demon") == 0 || strcmp(k, "--daemon") == 0 ) {
[1486]494#ifdef ROAR_HAVE_FORK
[775]495   daemon = 1;
[1486]496#else
497   ROAR_ERR("--daemon not supported");
498#endif
[71]499  } else if ( strcmp(k, "--terminate") == 0 ) {
500   g_terminate = 1;
[905]501  } else if ( strcmp(k, "--sysclocksync") == 0 ) {
502   sysclocksync = 1000;
[275]503  } else if ( strcmp(k, "--realtime") == 0 ) {
[276]504   realtime++;
[444]505  } else if ( strcmp(k, "--chroot") == 0 ) {
[1486]506#ifdef ROAR_HAVE_CHROOT
[444]507   chrootdir = argv[++i];
[1486]508#else
509   ROAR_ERR("--chroot not supported");
510   i++;
511#endif
[444]512  } else if ( strcmp(k, "--setgid") == 0 ) {
[1486]513#ifdef ROAR_HAVE_SETGID
[444]514   setids |= R_SETGID;
[1486]515#else
516   ROAR_ERR("--setgid not supported");
517#endif
[444]518  } else if ( strcmp(k, "--setuid") == 0 ) {
[1486]519#ifdef ROAR_HAVE_SETUID
[444]520   setids |= R_SETUID;
[1486]521#else
522   ROAR_ERR("--setuid not supported");
523#endif
[68]524
[280]525  } else if ( strcmp(k, "--list-cf") == 0 ) {
526   print_codecfilterlist();
527   return 0;
528
[0]529  } else if ( strcmp(k, "-R") == 0 || strcmp(k, "--rate") == 0 ) {
530   sa.rate = atoi(argv[++i]);
531  } else if ( strcmp(k, "-B") == 0 || strcmp(k, "--bits") == 0 ) {
532   sa.bits = atoi(argv[++i]);
533  } else if ( strcmp(k, "-C") == 0 || strcmp(k, "--chans") == 0 ) {
534   sa.channels = atoi(argv[++i]);
535
536  } else if ( strcmp(k, "-d") == 0 || strcmp(k, "--driver") == 0 ) {
537   driver = argv[++i];
538   if ( strcmp(driver, "list") == 0 ) {
[974]539    ROAR_WARN("The option is obsolete, use --list-driver!");
[0]540    print_driverlist();
[973]541    return 0;
[0]542   }
543  } else if ( strcmp(k, "-D") == 0 || strcmp(k, "--device") == 0 ) {
544   device = argv[++i];
545  } else if ( strcmp(k, "-dO") == 0 ) {
546   opts = argv[++i];
[973]547  } else if ( strcmp(k, "--list-driver") == 0 ) {
548   print_driverlist();
549   return 0;
[0]550
[932]551  } else if ( strcmp(k, "-o") == 0 || strcmp(k, "--odriver") == 0 ) {
552   o_drv  = argv[++i];
553  } else if ( strcmp(k, "-O") == 0 || strcmp(k, "--odevice") == 0 ) {
554   o_dev  = argv[++i];
555  } else if ( strcmp(k, "-oO") == 0 ) {
556   o_opts = argv[++i];
[961]557  } else if ( strcmp(k, "-oP") == 0 ) {
558   o_prim = 1;
[932]559  } else if ( strcmp(k, "-oN") == 0 ) {
[1145]560   if ( add_output(o_drv, o_dev, o_opts, o_prim, o_count) != -1 )
561    o_count++;
562
[961]563   o_drv  = o_dev = o_opts = NULL;
564   o_prim = 0;
[932]565
[0]566  } else if ( strcmp(k, "-s") == 0 || strcmp(k, "--source") == 0 ) {
[550]567   s_drv = argv[++i];
568  } else if ( strcmp(k, "-S") == 0 ) {
[1110]569   s_dev = argv[++i];
[0]570  } else if ( strcmp(k, "-sO") == 0 ) {
571   s_opt = argv[++i];
572  } else if ( strcmp(k, "-sC") == 0 ) {
573   s_con = argv[++i];
574  } else if ( strcmp(k, "-sP") == 0 ) {
575   s_prim = 1;
[1110]576  } else if ( strcmp(k, "-sN") == 0 ) {
577   if ( sources_add(s_drv, s_dev, s_con, s_opt, s_prim) == -1 ) {
578    ROAR_ERR("main(*): adding source '%s' via '%s' failed!", s_dev, s_drv);
579   }
580   s_opt = s_dev = s_con = NULL;
581   s_drv = "cf";
582   s_prim = 0;
[0]583
[1923]584  } else if ( strcmp(k, "--light-channels") == 0 ) {
585   light_channels = atoi(argv[++i]);
586
[1924]587  } else if ( strcmp(k, "--midi-no-console") == 0 ) {
588   midi_config.init_cb = 0;
589  } else if ( strcmp(k, "--midi-console") == 0 ) {
590   midi_config.console_dev = argv[++i];
591
[0]592  } else if ( strcmp(k, "-p") == 0 || strcmp(k, "--port") == 0 ) {
[447]593   // This is only usefull in INET not UNIX mode.
[1494]594#ifdef ROAR_SUPPORT_LISTEN
[447]595   if ( *server == '/' )
596    server = ROAR_DEFAULT_HOST;
597
[446]598   errno = 0;
599   if ( (port = atoi(argv[++i])) < 1 ) {
[1486]600#ifdef ROAR_HAVE_GETSERVBYNAME
[446]601    if ( (serv = getservbyname(argv[i], "tcp")) == NULL ) {
602     ROAR_ERR("Unknown service: %s: %s", argv[i], strerror(errno));
603     return 1;
604    }
605    // NOTE: we need to use ROAR_NET2HOST16() here even if s_port is of type int!
606    ROAR_DBG("main(*): serv = {s_name='%s', s_aliases={...}, s_port=%i, s_proto='%s'}",
607            serv->s_name, ROAR_NET2HOST16(serv->s_port), serv->s_proto);
608    port = ROAR_NET2HOST16(serv->s_port);
[1486]609#else
610    ROAR_ERR("invalite port number: %s", argv[i]);
611    return 1;
612#endif
[446]613   }
[1494]614#endif
[1115]615  } else if ( strcmp(k, "-b") == 0 || strcmp(k, "--bind") == 0 || strcmp(k, "--sock") == 0 ) {
[1494]616#ifdef ROAR_SUPPORT_LISTEN
[0]617   server = argv[++i];
[1494]618#endif
[518]619
[573]620  } else if ( strcmp(k, "-t") == 0 || strcmp(k, "--tcp") == 0 ) {
[1494]621#ifdef ROAR_SUPPORT_LISTEN
[518]622   if ( sock_type != ROAR_SOCKET_TYPE_TCP && sock_type != ROAR_SOCKET_TYPE_TCP6 )
623    sock_type = ROAR_SOCKET_TYPE_TCP;
624
625   if ( *server == '/' )
626    server = ROAR_DEFAULT_HOST;
[1494]627#endif
[518]628
629  } else if ( strcmp(k, "-4") == 0 ) {
[1494]630#ifdef ROAR_SUPPORT_LISTEN
[517]631   sock_type = ROAR_SOCKET_TYPE_TCP;
[447]632   if ( *server == '/' )
633    server = ROAR_DEFAULT_HOST;
[1494]634#endif
[518]635  } else if ( strcmp(k, "-6") == 0 ) {
[1494]636#ifdef ROAR_SUPPORT_LISTEN
[519]637#ifdef PF_INET6
[518]638   sock_type = ROAR_SOCKET_TYPE_TCP6;
639   if ( *server == '/' )
640    server = ROAR_DEFAULT_HOST;
[519]641#else
642    ROAR_ERR("No IPv6 support compiled in!");
643    return 1;
644#endif
[1494]645#endif
[518]646
[573]647  } else if ( strcmp(k, "-u") == 0 || strcmp(k, "--unix") == 0 ) {
[1494]648#ifdef ROAR_SUPPORT_LISTEN
[62]649   // ignore this case as it is the default behavor.
[517]650   sock_type = ROAR_SOCKET_TYPE_UNIX;
[1494]651#endif
[518]652
[573]653  } else if ( strcmp(k, "-n") == 0 || strcmp(k, "--decnet") == 0 ) {
[1494]654#ifdef ROAR_SUPPORT_LISTEN
[508]655#ifdef ROAR_HAVE_LIBDNET
656    port   = ROAR_DEFAULT_NUM;
657    strcpy(decnethost, ROAR_DEFAULT_LISTEN_OBJECT);
658    server = decnethost;
[517]659    sock_type = ROAR_SOCKET_TYPE_DECNET;
[508]660#else
661    ROAR_ERR("No DECnet support compiled in!");
662    return 1;
663#endif
[1494]664#endif
[518]665
[60]666  } else if ( strcmp(k, "-G") == 0 ) {
[444]667   sock_grp  = argv[++i];
668  } else if ( strcmp(k, "-U") == 0 ) {
669   sock_user = argv[++i];
[0]670
[68]671  } else if ( strcmp(k, "--no-listen") == 0 ) {
[1494]672#ifdef ROAR_SUPPORT_LISTEN
[1787]673   server      = "";
[548]674   g_terminate = 1;
[1155]675   g_no_listen = 1;
[1494]676#endif
[68]677  } else if ( strcmp(k, "--client-fh") == 0 ) {
678   if ( clients_set_fh(clients_new(), atoi(argv[++i])) == -1 ) {
679    ROAR_ERR("main(*): Can not set client's fh");
680    return 1;
681   }
[501]682  } else if ( strcmp(k, "--close-fh") == 0 ) {
[1486]683#ifdef ROAR_HAVE_IO_POSIX
[501]684   close(atoi(argv[++i]));
[1486]685#else
686   i++;
687   ROAR_WARN("can not close file handle %s (closing not supported)", argv[i]);
688#endif
[68]689
[920]690  } else if ( strcmp(k, "--standby") == 0 ) {
691   g_standby = 1;
692  } else if ( strcmp(k, "--auto-standby") == 0 ) {
693   g_autostandby = 1;
[0]694  } else {
695   usage();
696   return 1;
697  }
698
699 }
[1503]700#endif
[0]701
[1110]702 if ( s_dev != NULL ) {
703  if ( sources_add(s_drv, s_dev, s_con, s_opt, s_prim) == -1 ) {
704   ROAR_ERR("main(*): adding source '%s' via '%s' failed!", s_dev, s_drv);
705  }
706 }
707
[1145]708 add_output(o_drv, o_dev, o_opts, o_prim, o_count);
[932]709
[0]710 ROAR_DBG("Server config: rate=%i, bits=%i, chans=%i", sa.rate, sa.bits, sa.channels);
711
[1819]712 if ( midi_init() == -1 ) {
[281]713  ROAR_ERR("Can not initialize MIDI subsystem");
[1819]714 }
715
716 if ( light_init(light_channels) == -1 ) {
717  ROAR_ERR("Can not initialize light control subsystem");
718 }
[281]719
[1494]720#ifdef ROAR_SUPPORT_LISTEN
[68]721 if ( *server != 0 ) {
[517]722  if ( (g_listen_socket = roar_socket_listen(sock_type, server, port)) == -1 ) {
[1486]723#ifdef ROAR_HAVE_UNIX
[286]724   if ( *server == '/' ) {
[1011]725    if ( (env_roar_proxy_backup = getenv("ROAR_PROXY")) != NULL ) {
726     env_roar_proxy_backup = strdup(env_roar_proxy_backup);
727     unsetenv("ROAR_PROXY");
728    }
[286]729    if ( (i = roar_socket_connect(server, port)) != -1 ) {
730     close(i);
731     ROAR_ERR("Can not open listen socket!");
732     return 1;
733    } else {
734     unlink(server);
[517]735     if ( (g_listen_socket = roar_socket_listen(sock_type, server, port)) == -1 ) {
[286]736      ROAR_ERR("Can not open listen socket!");
737      return 1;
738     }
739    }
[1011]740    if ( env_roar_proxy_backup != NULL ) {
741     setenv("ROAR_PROXY", env_roar_proxy_backup, 0);
742     free(env_roar_proxy_backup);
743    }
[1486]744#else
745   if (0) { // noop
746#endif
[286]747   } else {
748    ROAR_ERR("Can not open listen socket!");
749    return 1;
750   }
[68]751  }
[0]752
[1486]753#if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX)
[523]754  if ( (grp = getgrnam(sock_grp)) == NULL ) {
755   ROAR_ERR("Can not get GID for group %s: %s", sock_grp, strerror(errno));
756  }
[1486]757#endif
758#if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX)
[523]759  if ( sock_user || (setids & R_SETUID) ) {
760   if ( (pwd = getpwnam(sock_user)) == NULL ) {
761    ROAR_ERR("Can not get UID for user %s: %s", sock_user, strerror(errno));
762   }
763  }
[1486]764#endif
[523]765
[1516]766#if defined(ROAR_HAVE_IO_POSIX) && defined(ROAR_HAVE_UNIX)
[68]767  if ( *server == '/' ) {
[523]768   if ( grp ) {
[444]769    if ( pwd ) {
[1691]770     if ( chown(server, pwd->pw_uid, grp->gr_gid) == -1 )
771      return 1;
[444]772    } else {
[1691]773     if ( chown(server, -1, grp->gr_gid) == -1 )
774      return 1;
[444]775    }
[1753]776#ifdef ROAR_HAVE_GETUID
[68]777    if ( getuid() == 0 )
[1691]778     if ( chmod(server, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) == -1 )
779      return 1;
[1753]780#endif
[68]781   }
[60]782  }
[1486]783#endif
[60]784 }
[1494]785#endif
[60]786
[0]787 if ( output_buffer_init(&sa) == -1 ) {
788  ROAR_ERR("Can not init output buffer!");
789  return 1;
790 }
791
[1145]792 if ( driver == NULL ) {
793  driver = "null";
794 } else {
795  ROAR_WARN("Usage of old driver interface. use -o not -d!");
796 }
797
[0]798 if ( driver_open(&drvinst, &drvid, driver, device, &sa) == -1 ) {
799  ROAR_ERR("Can not open output driver!");
800  return 1;
801 }
802
[44]803 if ( samples_init() == -1 ) {
804  ROAR_ERR("Can not init samples!");
805  return 1;
806 }
807
808
[1486]809 // we should handle this on microcontrollers, too.
[1753]810#if !defined(ROAR_TARGET_MICROCONTROLLER) && !defined(ROAR_TARGET_WIN32)
[0]811 signal(SIGINT,  on_sig_int);
[285]812 signal(SIGCHLD, on_sig_chld);
[0]813 signal(SIGPIPE, SIG_IGN);  // ignore broken pipes
[1486]814#endif
[0]815
[275]816 if ( realtime ) {
[278]817#ifdef DEBUG
818  ROAR_WARN("compiled with -DDEBUG but realtime is enabled: for real realtime support compiel without -DDEBUG");
819#endif
820
[1486]821#ifdef ROAR_HAVE_NICE
[275]822  errno = 0;
[276]823  nice(-5*realtime); // -5 for each --realtime
[1486]824  if ( errno ) {
825   ROAR_WARN("Can not decrease nice value by %i: %s", 5*realtime, strerror(errno));
826  }
827#else
828  ROAR_WARN("Can not decrease nice value by %i: %s", 5*realtime, strerror(errno));
829#endif
[277]830/*
[276]831#ifdef __linux__
[277]832  if ( ioprio_set(IOPRIO_WHO_PROCESS, getpid(), IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 0)) == -1 )
833   ROAR_WARN("Can not set io priority: %s", strerror(errno));
[276]834#endif
[277]835*/
[275]836 }
837
[1486]838#ifdef ROAR_HAVE_SETGID
[444]839 if ( setids & R_SETGID ) {
840  if ( setgroups(0, (const gid_t *) NULL) == -1 ) {
841   ROAR_ERR("Can not clear supplementary group IDs: %s", strerror(errno));
842  }
[523]843  if ( !grp || setgid(grp->gr_gid) == -1 ) {
[444]844   ROAR_ERR("Can not set GroupID: %s", strerror(errno));
845  }
846 }
[1486]847#endif
[444]848
[0]849
[39]850 clients_set_pid(g_self_client, getpid());
[1753]851#ifdef ROAR_HAVE_GETUID
[440]852 clients_set_uid(g_self_client, getuid());
[1753]853#endif
854#ifdef ROAR_HAVE_GETGID
[440]855 clients_set_gid(g_self_client, getgid());
[1753]856#endif
[39]857 clients_get(g_self_client, &self);
[37]858
[39]859 if ( self == NULL ) {
860  ROAR_ERR("Can not get self client!");
861  return 1;
862 }
863
[775]864 strcpy(self->name, "RoarAudio daemon internal");
[68]865
[1486]866#ifdef ROAR_HAVE_FORK
[775]867 if ( daemon ) {
[68]868  close(ROAR_STDIN );
869  close(ROAR_STDOUT);
870  close(ROAR_STDERR);
[1753]871
[68]872  if ( fork() )
[1486]873   ROAR_U_EXIT(0);
[1753]874
875#ifdef ROAR_HAVE_SETSID
876  setsid();
877#endif
[1046]878  clients_set_pid(g_self_client, getpid()); // reset pid as it changed
[68]879 }
[1486]880#endif
[68]881
[1486]882#ifdef ROAR_HAVE_CHROOT
[444]883 if (chrootdir) {
884  if ( chroot(chrootdir) == -1 ) {
885   ROAR_ERR("Can not chroot to %s: %s", chrootdir, strerror(errno));
886   return 2;
887  }
888  if ( chdir("/") == -1 ) {
889   ROAR_ERR("Can not chdir to /: %s", strerror(errno));
890   return 2;
891  }
892 }
[1486]893#endif
[444]894
[1486]895#ifdef ROAR_HAVE_SETUID
[444]896 if ( setids & R_SETUID ) {
897  if ( !pwd || setuid(pwd->pw_uid) == -1 ) {
898   ROAR_ERR("Can not set UserID: %s", strerror(errno));
899   return 3;
900  }
[1753]901#ifdef ROAR_HAVE_GETUID
[444]902  clients_set_uid(g_self_client, getuid());
[1753]903#endif
[444]904 }
[1486]905#endif
[444]906
[0]907 // start main loop...
[905]908 main_loop(drvid, drvinst, &sa, sysclocksync);
[0]909
910 // clean up.
911 clean_quit_prep();
912 driver_close(drvinst, drvid);
913 output_buffer_free();
914
915 return 0;
916}
917
[574]918void cleanup_listen_socket (int terminate) {
[580]919
[1494]920#ifdef ROAR_SUPPORT_LISTEN
[580]921 if ( g_listen_socket != -1 ) {
[1486]922#ifdef ROAR_HAVE_IO_POSIX
[580]923  close(g_listen_socket);
[1486]924#endif // #else is useless because we are in void context.
[60]925
[580]926  g_listen_socket = -1;
[576]927
[1486]928#ifdef ROAR_HAVE_UNIX
[580]929  if ( *server == '/' )
930   unlink(server);
[1486]931#endif
[580]932 }
[60]933
[1494]934#endif
935
[574]936 if ( terminate )
937  g_terminate = 1;
938}
939
940void clean_quit_prep (void) {
941 cleanup_listen_socket(0);
[60]942
[0]943 sources_free();
944 streams_free();
945 clients_free();
[282]946 midi_cb_stop(); // stop console beep
[281]947 midi_free();
[1819]948 light_free();
[0]949}
950
951void clean_quit (void) {
952 clean_quit_prep();
953// driver_close(drvinst, drvid);
954// output_buffer_free();
955 exit(0);
956}
957
958//ll
Note: See TracBrowser for help on using the repository browser.