source: roaraudio/roard/roard.c @ 1221:8ab0332cd92b

Last change on this file since 1221:8ab0332cd92b was 1221:8ab0332cd92b, checked in by phi, 15 years ago

added support for flag primary as -oO flag, for flag cleanmeta, dummy for flag autoconf and way to set number of blocks (bot not blocksize...)

File size: 20.0 KB
Line 
1//roard.c:
2
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
25#include "roard.h"
26
27char * server = ROAR_DEFAULT_SOCK_GLOBAL; // global server address
28
29void usage (void) {
30 printf("Usage: roard [OPTIONS]...\n\n");
31
32 printf("Misc Options:\n\n");
33 printf(
34        " --daemon              - Bring the server into background after init\n"
35        " --terminate           - Terminate after last client quited\n"
36        " --restart             - Trys to stop an old instance and start a new with new settings\n"
37        " --realtime            - Trys to get realtime priority,\n"
38        "                         give multible times for being more realtime\n"
39        " --chroot DIR          - chroots to the given dir\n"
40        " --setgid              - GroupID to the audio group as specified via -G\n"
41        " --setuid              - UserID to the audio user as specified via -U\n"
42        " --sysclocksync        - calculate exact sample rate using the system clock\n"
43       );
44
45 printf("\nAudio Options:\n\n");
46 printf(
47        " -R  --rate   RATE     - Set server rate\n"
48        " -B  --bits   BITS     - Set server bits\n"
49        " -C  --chans  CHANNELS - Set server channels\n"
50       );
51
52 printf("\nDriver Options:\n\n");
53 printf(" -d  --driver DRV      - Set the driver (default: %s)\n", ROAR_DRIVER_DEFAULT);
54 printf(" -D  --device DEV      - Set the device\n");
55 printf(" -dO OPTS              - Set output options\n");
56 printf(" --list-driver         - List all drivers\n");
57
58 printf("\nOutput Options:\n\n");
59 printf(" -o  --odriver DRV     - Set the driver, use '--list-driver' to get a list\n");
60 printf(" -O  --odevice DEV     - Set the device\n");
61 printf(" -oO OPTS              - Set output options\n");
62 printf(" -oN                   - Adds another output\n");
63 printf(" -oP                   - Mark output as primary\n");
64
65 printf("\nSource Options:\n\n");
66 printf(" -s  --source DRV      - Use DRV as input driver\n"
67        " -S           DEV      - Use DEV as input device\n"
68        " -sO          OPTS     - Use OPTS as input options\n"
69        " -sP                   - Make souce as primary\n"
70       );
71
72 printf("\nCodec Filter Options:\n\n");
73 printf(" --list-cf             - List all codec filter\n"
74       );
75
76 printf("\nServer Options:\n\n");
77 printf(" -t  --tcp             - Use TCP listen socket\n"
78        " -u  --unix            - Use UNIX Domain listen socket (default)\n"
79#ifdef ROAR_HAVE_LIBDNET
80        " -n  --decnet          - use DECnet listen socket\n"
81#endif
82        " -4                    - Use IPv4 connections (implies -t)\n"
83#ifdef PF_INET6
84        " -6                    - Use IPv6 connections (implies -t)\n"
85#endif
86#ifdef IPV6_ADDRFORM
87        " -64                   - Try to downgrade sockets from IPv6 into IPv4,\n"
88        "                         this is normaly not usefull.\n"
89#endif
90        " -p  --port            - TCP Port to bind to\n"
91        " -b  --bind            - IP/Hostname to bind to\n"
92        "     --sock            - Filename for UNIX Domain Socket\n"
93        " -G  GROUP             - Sets the group for the UNIX Domain Socket, (default: %s)\n"
94        "                         You need the permittions to change the GID\n"
95        " -U  USER              - Sets the user for the UNIX Domain Socket, (default: do not set)\n"
96        "                         You need the permittions to change the UID (normaly only root has)\n"
97        " --no-listen           - Do not listen for new clients\n"
98        "                         (only usefull for relaing, impleys --terminate)\n"
99        " --client-fh           - Comunicate with a client over this handle\n"
100        "                         (only usefull for relaing)\n"
101        " --close-fh            - Closes the given fh\n"
102        " --standby             - Start in standby state\n"
103        " --auto-standby        - Automatical goes into standby if there are no streams\n",
104        ROAR_DEFAULT_SOCKGRP
105       );
106// printf("\n Options:\n\n");
107 printf("\n");
108}
109
110int restart_server (char * server) {
111 struct roar_connection con;
112 if ( roar_connect(&con, server) == -1 ) {
113  return -1;
114 }
115
116 if ( roar_terminate(&con, 1) == -1 ) {
117  return -1;
118 }
119
120 return roar_disconnect(&con);
121}
122
123#define R_SETUID 1
124#define R_SETGID 2
125
126int add_output (char * drv, char * dev, char * opts, int prim, int count) {
127 int stream;
128 struct roar_stream * s;
129 struct roar_stream_server * ss;
130 char * k, * v;
131#ifdef ROAR_DRIVER_CODEC
132 char * to_free = NULL;
133#endif
134 int codec;
135 int sync = 0;
136 int32_t blocks = -1;
137
138 ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = ?", drv, dev, opts);
139
140 if ( drv == NULL && count == 0 ) {
141  drv  = ROAR_DRIVER_DEFAULT;
142  prim = 1;
143  sync = 1;
144
145#ifdef ROAR_DRIVER_CODEC
146  if ( opts == NULL ) {
147   opts = to_free = strdup("codec=" ROAR_DRIVER_CODEC);
148  }
149#endif
150 }
151
152 if ( (stream = streams_new()) == -1 ) {
153  ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = -1", drv, dev, opts);
154  if ( prim ) alive = 0;
155  return -1;
156 }
157
158 streams_get(stream, &ss);
159 s = ROAR_STREAM(ss);
160
161 memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info));
162
163 s->dir        = ROAR_DIR_OUTPUT;
164 s->pos_rel_id = -1;
165// s->info.codec = codec;
166
167 codec = s->info.codec;
168
169 k = strtok(opts, ",");
170 while (k != NULL) {
171//  ROAR_WARN("add_output(*): opts: %s", k);
172
173  if ( (v = strstr(k, "=")) != NULL ) {
174   *v++ = 0;
175  }
176
177  ROAR_DBG("add_output(*): opts: k='%s', v='%s'", k, v);
178  if ( strcmp(k, "rate") == 0 ) {
179   s->info.rate = atoi(v);
180  } else if ( strcmp(k, "channels") == 0 ) {
181   s->info.channels = atoi(v);
182  } else if ( strcmp(k, "bits") == 0 ) {
183   s->info.bits = atoi(v);
184  } else if ( strcmp(k, "codec") == 0 ) {
185   if ( (codec = roar_str2codec(v)) == -1 ) {
186    ROAR_ERR("add_output(*): unknown codec '%s'", v);
187    streams_delete(stream);
188    if ( prim ) alive = 0;
189#ifdef ROAR_DRIVER_CODEC
190    if ( to_free != NULL )
191     free(to_free);
192#endif
193    return -1;
194   }
195  } else if ( strcmp(k, "blocks") == 0 ) {
196   blocks = atoi(v);
197  } else if ( strcmp(k, "meta") == 0 ) {
198   streams_set_flag(stream, ROAR_FLAG_META);
199  } else if ( strcmp(k, "sync") == 0 ) {
200   sync = 1;
201  } else if ( strcmp(k, "primary") == 0 ) {
202   prim = 1;
203
204  } else if ( strcmp(k, "cleanmeta") == 0 ) {
205   streams_set_flag(stream, ROAR_FLAG_CLEANMETA);
206
207  /* ignore this for the moment ... */
208  } else if ( strcmp(k, "autoconf") == 0 ) {
209  } else {
210   ROAR_ERR("add_output(*): unknown option '%s'", k);
211   streams_delete(stream);
212   if ( prim ) alive = 0;
213#ifdef ROAR_DRIVER_CODEC
214   if ( to_free != NULL )
215    free(to_free);
216#endif
217   return -1;
218  }
219
220  k = strtok(NULL, ",");
221 }
222
223#ifdef ROAR_DRIVER_CODEC
224 if ( to_free != NULL )
225  free(to_free);
226#endif
227
228 if ( codec == ROAR_CODEC_ALAW || codec == ROAR_CODEC_MULAW )
229  s->info.bits = 8; // needed to open OSS driver, will be overriden by codecfilter
230
231 s->info.codec = codec;
232 ROAR_STREAM_SERVER(s)->codec_orgi = codec;
233
234 if ( driver_openvio(&(ss->vio), &(ss->driver_id), drv, dev, &(s->info), -1) == -1 ) {
235  streams_delete(stream);
236  ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = -1", drv, dev, opts);
237  if ( prim ) alive = 0;
238  return -1;
239 }
240
241 if ( blocks != -1 )
242  roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DBLOCKS, &blocks);
243
244 ROAR_DBG("add_output(*): ss->driver_id=%i", ss->driver_id);
245
246 streams_set_fh(stream, -1); // update some internal structures
247
248 client_stream_add(g_source_client, stream);
249
250 if ( prim ) {
251  streams_mark_primary(stream);
252  s->pos_rel_id = stream;
253 }
254
255 if ( sync ) {
256  streams_set_flag(stream, ROAR_FLAG_SYNC);
257 } else {
258  streams_reset_flag(stream, ROAR_FLAG_SYNC);
259 }
260
261 return 0;
262}
263
264int main (int argc, char * argv[]) {
265 int i;
266 char * k;
267 char user_sock[80]  = {0};
268 struct roar_audio_info sa;
269 int    daemon       = 0;
270 int    realtime     = 0;
271 int    sysclocksync = 0;
272 char * driver    = NULL;
273 char * device    = NULL;
274 char * opts      = NULL;
275// char * server = ROAR_DEFAULT_SOCK_GLOBAL;
276 int      port    = ROAR_DEFAULT_PORT;
277 int               drvid;
278 char * s_drv     = "cf";
279 char * s_dev     = NULL;
280 char * s_con     = NULL;
281 char * s_opt     = NULL;
282 int    s_prim    = 0;
283 char * o_drv     = getenv("ROAR_DRIVER");
284 char * o_dev     = getenv("ROAR_DEVICE");
285 char * o_opts    = NULL;
286 int    o_prim    = 0;
287 int    o_count   = 0;
288 char * sock_grp  = ROAR_DEFAULT_SOCKGRP;
289 char * sock_user = NULL;
290 int    sock_type = ROAR_SOCKET_TYPE_UNKNOWN;
291 char * chrootdir = NULL;
292 int    setids    = 0;
293 char * env_roar_proxy_backup;
294 struct group   * grp  = NULL;
295 struct passwd  * pwd  = NULL;
296 struct servent * serv = NULL;
297 DRIVER_USERDATA_T drvinst;
298 struct roar_client * self = NULL;
299#ifdef ROAR_HAVE_LIBDNET
300 char decnethost[80];
301#endif
302
303 g_listen_socket = -1;
304 g_standby       =  0;
305 g_autostandby   =  0;
306 alive           =  1;
307 g_no_listen     =  0;
308
309 sa.bits     = ROAR_BITS_DEFAULT;
310 sa.channels = ROAR_CHANNELS_DEFAULT;
311 sa.rate     = ROAR_RATE_DEFAULT;
312 sa.codec    = ROAR_CODEC_DEFAULT;
313
314 g_sa = &sa;
315
316
317 if ( getuid() != 0 && getenv("HOME") ) {
318  snprintf(user_sock, 79, "%s/%s", getenv("HOME"), ROAR_DEFAULT_SOCK_USER);
319  server = user_sock;
320 }
321
322 if ( getenv("ROAR_SERVER") != NULL )
323  server = getenv("ROAR_SERVER");
324
325 if ( clients_init() == -1 ) {
326  ROAR_ERR("Can not init clients!");
327  return 1;
328 }
329
330 if ( streams_init() == -1 ) {
331  ROAR_ERR("Can not init streams!");
332  return 1;
333 }
334
335 if ( (g_self_client = clients_new()) == -1 ) {
336  ROAR_ERR("Can not create self client!");
337  return 1;
338 }
339
340 if ( sources_init() == -1 ) {
341  ROAR_ERR("Can not init sources!");
342  return 1;
343 }
344
345 if ( (sources_set_client(g_self_client)) == -1 ) {
346  ROAR_ERR("Can not init set source client!");
347  return 1;
348 }
349
350 for (i = 1; i < argc; i++) {
351  k = argv[i];
352
353  if ( strcmp(k, "-h") == 0 || strcmp(k, "--help") == 0 ) {
354   usage();
355   return 0;
356
357  } else if ( strcmp(k, "--restart") == 0 ) {
358   if ( restart_server(server) == -1 ) {
359    ROAR_WARN("Can not terminate old server (not running at %s?), tring to continue anyway", server);
360   }
361
362  } else if ( strcmp(k, "--demon") == 0 || strcmp(k, "--daemon") == 0 ) {
363   daemon = 1;
364  } else if ( strcmp(k, "--terminate") == 0 ) {
365   g_terminate = 1;
366  } else if ( strcmp(k, "--sysclocksync") == 0 ) {
367   sysclocksync = 1000;
368  } else if ( strcmp(k, "--realtime") == 0 ) {
369   realtime++;
370  } else if ( strcmp(k, "--chroot") == 0 ) {
371   chrootdir = argv[++i];
372  } else if ( strcmp(k, "--setgid") == 0 ) {
373   setids |= R_SETGID;
374  } else if ( strcmp(k, "--setuid") == 0 ) {
375   setids |= R_SETUID;
376
377  } else if ( strcmp(k, "--list-cf") == 0 ) {
378   print_codecfilterlist();
379   return 0;
380
381  } else if ( strcmp(k, "-R") == 0 || strcmp(k, "--rate") == 0 ) {
382   sa.rate = atoi(argv[++i]);
383  } else if ( strcmp(k, "-B") == 0 || strcmp(k, "--bits") == 0 ) {
384   sa.bits = atoi(argv[++i]);
385  } else if ( strcmp(k, "-C") == 0 || strcmp(k, "--chans") == 0 ) {
386   sa.channels = atoi(argv[++i]);
387
388  } else if ( strcmp(k, "-d") == 0 || strcmp(k, "--driver") == 0 ) {
389   driver = argv[++i];
390   if ( strcmp(driver, "list") == 0 ) {
391    ROAR_WARN("The option is obsolete, use --list-driver!");
392    print_driverlist();
393    return 0;
394   }
395  } else if ( strcmp(k, "-D") == 0 || strcmp(k, "--device") == 0 ) {
396   device = argv[++i];
397  } else if ( strcmp(k, "-dO") == 0 ) {
398   opts = argv[++i];
399  } else if ( strcmp(k, "--list-driver") == 0 ) {
400   print_driverlist();
401   return 0;
402
403  } else if ( strcmp(k, "-o") == 0 || strcmp(k, "--odriver") == 0 ) {
404   o_drv  = argv[++i];
405  } else if ( strcmp(k, "-O") == 0 || strcmp(k, "--odevice") == 0 ) {
406   o_dev  = argv[++i];
407  } else if ( strcmp(k, "-oO") == 0 ) {
408   o_opts = argv[++i];
409  } else if ( strcmp(k, "-oP") == 0 ) {
410   o_prim = 1;
411  } else if ( strcmp(k, "-oN") == 0 ) {
412   if ( add_output(o_drv, o_dev, o_opts, o_prim, o_count) != -1 )
413    o_count++;
414
415   o_drv  = o_dev = o_opts = NULL;
416   o_prim = 0;
417
418  } else if ( strcmp(k, "-s") == 0 || strcmp(k, "--source") == 0 ) {
419   s_drv = argv[++i];
420  } else if ( strcmp(k, "-S") == 0 ) {
421   s_dev = argv[++i];
422  } else if ( strcmp(k, "-sO") == 0 ) {
423   s_opt = argv[++i];
424  } else if ( strcmp(k, "-sC") == 0 ) {
425   s_con = argv[++i];
426  } else if ( strcmp(k, "-sP") == 0 ) {
427   s_prim = 1;
428  } else if ( strcmp(k, "-sN") == 0 ) {
429   if ( sources_add(s_drv, s_dev, s_con, s_opt, s_prim) == -1 ) {
430    ROAR_ERR("main(*): adding source '%s' via '%s' failed!", s_dev, s_drv);
431   }
432   s_opt = s_dev = s_con = NULL;
433   s_drv = "cf";
434   s_prim = 0;
435
436  } else if ( strcmp(k, "-p") == 0 || strcmp(k, "--port") == 0 ) {
437   // This is only usefull in INET not UNIX mode.
438   if ( *server == '/' )
439    server = ROAR_DEFAULT_HOST;
440
441   errno = 0;
442   if ( (port = atoi(argv[++i])) < 1 ) {
443    if ( (serv = getservbyname(argv[i], "tcp")) == NULL ) {
444     ROAR_ERR("Unknown service: %s: %s", argv[i], strerror(errno));
445     return 1;
446    }
447    // NOTE: we need to use ROAR_NET2HOST16() here even if s_port is of type int!
448    ROAR_DBG("main(*): serv = {s_name='%s', s_aliases={...}, s_port=%i, s_proto='%s'}",
449            serv->s_name, ROAR_NET2HOST16(serv->s_port), serv->s_proto);
450    port = ROAR_NET2HOST16(serv->s_port);
451   }
452  } else if ( strcmp(k, "-b") == 0 || strcmp(k, "--bind") == 0 || strcmp(k, "--sock") == 0 ) {
453   server = argv[++i];
454
455  } else if ( strcmp(k, "-t") == 0 || strcmp(k, "--tcp") == 0 ) {
456   if ( sock_type != ROAR_SOCKET_TYPE_TCP && sock_type != ROAR_SOCKET_TYPE_TCP6 )
457    sock_type = ROAR_SOCKET_TYPE_TCP;
458
459   if ( *server == '/' )
460    server = ROAR_DEFAULT_HOST;
461
462  } else if ( strcmp(k, "-4") == 0 ) {
463   sock_type = ROAR_SOCKET_TYPE_TCP;
464   if ( *server == '/' )
465    server = ROAR_DEFAULT_HOST;
466  } else if ( strcmp(k, "-6") == 0 ) {
467#ifdef PF_INET6
468   sock_type = ROAR_SOCKET_TYPE_TCP6;
469   if ( *server == '/' )
470    server = ROAR_DEFAULT_HOST;
471#else
472    ROAR_ERR("No IPv6 support compiled in!");
473    return 1;
474#endif
475
476  } else if ( strcmp(k, "-u") == 0 || strcmp(k, "--unix") == 0 ) {
477   // ignore this case as it is the default behavor.
478   sock_type = ROAR_SOCKET_TYPE_UNIX;
479
480  } else if ( strcmp(k, "-n") == 0 || strcmp(k, "--decnet") == 0 ) {
481#ifdef ROAR_HAVE_LIBDNET
482    port   = ROAR_DEFAULT_NUM;
483    strcpy(decnethost, ROAR_DEFAULT_LISTEN_OBJECT);
484    server = decnethost;
485    sock_type = ROAR_SOCKET_TYPE_DECNET;
486#else
487    ROAR_ERR("No DECnet support compiled in!");
488    return 1;
489#endif
490
491  } else if ( strcmp(k, "-G") == 0 ) {
492   sock_grp  = argv[++i];
493  } else if ( strcmp(k, "-U") == 0 ) {
494   sock_user = argv[++i];
495
496  } else if ( strcmp(k, "--no-listen") == 0 ) {
497   *server     = 0;
498   g_terminate = 1;
499   g_no_listen = 1;
500  } else if ( strcmp(k, "--client-fh") == 0 ) {
501   if ( clients_set_fh(clients_new(), atoi(argv[++i])) == -1 ) {
502    ROAR_ERR("main(*): Can not set client's fh");
503    return 1;
504   }
505  } else if ( strcmp(k, "--close-fh") == 0 ) {
506   close(atoi(argv[++i]));
507
508  } else if ( strcmp(k, "--standby") == 0 ) {
509   g_standby = 1;
510  } else if ( strcmp(k, "--auto-standby") == 0 ) {
511   g_autostandby = 1;
512  } else {
513   usage();
514   return 1;
515  }
516
517 }
518
519 if ( s_dev != NULL ) {
520  if ( sources_add(s_drv, s_dev, s_con, s_opt, s_prim) == -1 ) {
521   ROAR_ERR("main(*): adding source '%s' via '%s' failed!", s_dev, s_drv);
522  }
523 }
524
525 add_output(o_drv, o_dev, o_opts, o_prim, o_count);
526
527 ROAR_DBG("Server config: rate=%i, bits=%i, chans=%i", sa.rate, sa.bits, sa.channels);
528
529 if ( midi_init() == -1 )
530  ROAR_ERR("Can not initialize MIDI subsystem");
531
532 if ( *server != 0 ) {
533  if ( (g_listen_socket = roar_socket_listen(sock_type, server, port)) == -1 ) {
534   if ( *server == '/' ) {
535    if ( (env_roar_proxy_backup = getenv("ROAR_PROXY")) != NULL ) {
536     env_roar_proxy_backup = strdup(env_roar_proxy_backup);
537     unsetenv("ROAR_PROXY");
538    }
539    if ( (i = roar_socket_connect(server, port)) != -1 ) {
540     close(i);
541     ROAR_ERR("Can not open listen socket!");
542     return 1;
543    } else {
544     unlink(server);
545     if ( (g_listen_socket = roar_socket_listen(sock_type, server, port)) == -1 ) {
546      ROAR_ERR("Can not open listen socket!");
547      return 1;
548     }
549    }
550    if ( env_roar_proxy_backup != NULL ) {
551     setenv("ROAR_PROXY", env_roar_proxy_backup, 0);
552     free(env_roar_proxy_backup);
553    }
554   } else {
555    ROAR_ERR("Can not open listen socket!");
556    return 1;
557   }
558  }
559
560  if ( (grp = getgrnam(sock_grp)) == NULL ) {
561   ROAR_ERR("Can not get GID for group %s: %s", sock_grp, strerror(errno));
562  }
563  if ( sock_user || (setids & R_SETUID) ) {
564   if ( (pwd = getpwnam(sock_user)) == NULL ) {
565    ROAR_ERR("Can not get UID for user %s: %s", sock_user, strerror(errno));
566   }
567  }
568
569  if ( *server == '/' ) {
570   if ( grp ) {
571    if ( pwd ) {
572     chown(server, pwd->pw_uid, grp->gr_gid);
573    } else {
574     chown(server, -1, grp->gr_gid);
575    }
576    if ( getuid() == 0 )
577     chmod(server, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
578   }
579  }
580 }
581
582 if ( output_buffer_init(&sa) == -1 ) {
583  ROAR_ERR("Can not init output buffer!");
584  return 1;
585 }
586
587 if ( driver == NULL ) {
588  driver = "null";
589 } else {
590  ROAR_WARN("Usage of old driver interface. use -o not -d!");
591 }
592
593 if ( driver_open(&drvinst, &drvid, driver, device, &sa) == -1 ) {
594  ROAR_ERR("Can not open output driver!");
595  return 1;
596 }
597
598 if ( samples_init() == -1 ) {
599  ROAR_ERR("Can not init samples!");
600  return 1;
601 }
602
603
604 signal(SIGINT,  on_sig_int);
605 signal(SIGCHLD, on_sig_chld);
606 signal(SIGPIPE, SIG_IGN);  // ignore broken pipes
607
608 if ( realtime ) {
609#ifdef DEBUG
610  ROAR_WARN("compiled with -DDEBUG but realtime is enabled: for real realtime support compiel without -DDEBUG");
611#endif
612
613  errno = 0;
614  nice(-5*realtime); // -5 for each --realtime
615  if ( errno )
616   ROAR_WARN("Can not decrease nice value by 5: %s", strerror(errno));
617/*
618#ifdef __linux__
619  if ( ioprio_set(IOPRIO_WHO_PROCESS, getpid(), IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 0)) == -1 )
620   ROAR_WARN("Can not set io priority: %s", strerror(errno));
621#endif
622*/
623 }
624
625 if ( setids & R_SETGID ) {
626  if ( setgroups(0, (const gid_t *) NULL) == -1 ) {
627   ROAR_ERR("Can not clear supplementary group IDs: %s", strerror(errno));
628  }
629  if ( !grp || setgid(grp->gr_gid) == -1 ) {
630   ROAR_ERR("Can not set GroupID: %s", strerror(errno));
631  }
632 }
633
634
635 clients_set_pid(g_self_client, getpid());
636 clients_set_uid(g_self_client, getuid());
637 clients_set_gid(g_self_client, getgid());
638 clients_get(g_self_client, &self);
639
640 if ( self == NULL ) {
641  ROAR_ERR("Can not get self client!");
642  return 1;
643 }
644
645 strcpy(self->name, "RoarAudio daemon internal");
646
647 if ( daemon ) {
648  close(ROAR_STDIN );
649  close(ROAR_STDOUT);
650  close(ROAR_STDERR);
651  setsid();
652  if ( fork() )
653   _exit(0);
654  clients_set_pid(g_self_client, getpid()); // reset pid as it changed
655 }
656
657 if (chrootdir) {
658  if ( chroot(chrootdir) == -1 ) {
659   ROAR_ERR("Can not chroot to %s: %s", chrootdir, strerror(errno));
660   return 2;
661  }
662  if ( chdir("/") == -1 ) {
663   ROAR_ERR("Can not chdir to /: %s", strerror(errno));
664   return 2;
665  }
666 }
667
668 if ( setids & R_SETUID ) {
669  if ( !pwd || setuid(pwd->pw_uid) == -1 ) {
670   ROAR_ERR("Can not set UserID: %s", strerror(errno));
671   return 3;
672  }
673  clients_set_uid(g_self_client, getuid());
674 }
675
676 // start main loop...
677 main_loop(drvid, drvinst, &sa, sysclocksync);
678
679 // clean up.
680 clean_quit_prep();
681 driver_close(drvinst, drvid);
682 output_buffer_free();
683
684 return 0;
685}
686
687void cleanup_listen_socket (int terminate) {
688
689 if ( g_listen_socket != -1 ) {
690  close(g_listen_socket);
691
692  g_listen_socket = -1;
693
694  if ( *server == '/' )
695   unlink(server);
696 }
697
698 if ( terminate )
699  g_terminate = 1;
700}
701
702void clean_quit_prep (void) {
703 cleanup_listen_socket(0);
704
705 sources_free();
706 streams_free();
707 clients_free();
708 midi_cb_stop(); // stop console beep
709 midi_free();
710}
711
712void clean_quit (void) {
713 clean_quit_prep();
714// driver_close(drvinst, drvid);
715// output_buffer_free();
716 exit(0);
717}
718
719//ll
Note: See TracBrowser for help on using the repository browser.