source: roaraudio/roard/roard.c @ 961:ac5a0e9430cf

Last change on this file since 961:ac5a0e9430cf was 961:ac5a0e9430cf, checked in by phi, 15 years ago

added support vor primary output streams

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