source: roaraudio/roard/roard.c @ 938:639ed35cb23f

Last change on this file since 938:639ed35cb23f was 938:639ed35cb23f, checked in by phi, 15 years ago

added support for setting options via -oO

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