source: roaraudio/roard/roard.c @ 1227:203ed239f616

Last change on this file since 1227:203ed239f616 was 1227:203ed239f616, checked in by phi, 15 years ago

set sync and prim flag on output if only non-default driver is given but no -oO

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