source: roaraudio/roard/roard.c @ 932:0de90185644f

Last change on this file since 932:0de90185644f was 932:0de90185644f, checked in by phi, 15 years ago

added output options, need to write add_output() to actually init a new driver instants

File size: 15.6 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 ROAR_WARN("add_output(drv='%s', dev='%s', opts='%s') = ?", drv, dev, opts);
126 return -1;
127}
128
129int main (int argc, char * argv[]) {
130 int i;
131 char * k;
132 char user_sock[80]  = {0};
133 struct roar_audio_info sa;
134 int    daemon       = 0;
135 int    realtime     = 0;
136 int    sysclocksync = 0;
137 char * driver = getenv("ROAR_DRIVER");
138 char * device = getenv("ROAR_DEVICE");
139 char * opts   = NULL;
140// char * server = ROAR_DEFAULT_SOCK_GLOBAL;
141 int      port = ROAR_DEFAULT_PORT;
142 int               drvid;
143 char * s_drv     = "cf";
144 char * s_con     = NULL;
145 char * s_opt     = NULL;
146 int    s_prim    = 0;
147 char * o_drv     = NULL;
148 char * o_dev     = NULL;
149 char * o_opts    = NULL;
150 char * sock_grp  = ROAR_DEFAULT_SOCKGRP;
151 char * sock_user = NULL;
152 int    sock_type = ROAR_SOCKET_TYPE_UNKNOWN;
153 char * chrootdir = NULL;
154 int    setids    = 0;
155 struct group   * grp  = NULL;
156 struct passwd  * pwd  = NULL;
157 struct servent * serv = NULL;
158 DRIVER_USERDATA_T drvinst;
159 struct roar_client * self = NULL;
160#ifdef ROAR_HAVE_LIBDNET
161 char decnethost[80];
162#endif
163
164 g_listen_socket = -1;
165 g_standby       =  0;
166 g_autostandby   =  0;
167
168 sa.bits     = ROAR_BITS_DEFAULT;
169 sa.channels = ROAR_CHANNELS_DEFAULT;
170 sa.rate     = ROAR_RATE_DEFAULT;
171 sa.codec    = ROAR_CODEC_DEFAULT;
172
173 g_sa = &sa;
174
175
176 if ( getuid() != 0 && getenv("HOME") ) {
177  snprintf(user_sock, 79, "%s/%s", getenv("HOME"), ROAR_DEFAULT_SOCK_USER);
178  server = user_sock;
179 }
180
181 if ( getenv("ROAR_SERVER") != NULL )
182  server = getenv("ROAR_SERVER");
183
184 if ( clients_init() == -1 ) {
185  ROAR_ERR("Can not init clients!");
186  return 1;
187 }
188
189 if ( streams_init() == -1 ) {
190  ROAR_ERR("Can not init streams!");
191  return 1;
192 }
193
194 if ( (g_self_client = clients_new()) == -1 ) {
195  ROAR_ERR("Can not create self client!");
196  return 1;
197 }
198
199 if ( sources_init() == -1 ) {
200  ROAR_ERR("Can not init sources!");
201  return 1;
202 }
203
204 if ( (sources_set_client(g_self_client)) == -1 ) {
205  ROAR_ERR("Can not init set source client!");
206  return 1;
207 }
208
209 for (i = 1; i < argc; i++) {
210  k = argv[i];
211
212  if ( strcmp(k, "-h") == 0 || strcmp(k, "--help") == 0 ) {
213   usage();
214   return 0;
215
216  } else if ( strcmp(k, "--restart") == 0 ) {
217   if ( restart_server(server) == -1 ) {
218    ROAR_WARN("Can not terminate old server (not running at %s?), tring to continue anyway", server);
219   }
220
221  } else if ( strcmp(k, "--demon") == 0 || strcmp(k, "--daemon") == 0 ) {
222   daemon = 1;
223  } else if ( strcmp(k, "--terminate") == 0 ) {
224   g_terminate = 1;
225  } else if ( strcmp(k, "--sysclocksync") == 0 ) {
226   sysclocksync = 1000;
227  } else if ( strcmp(k, "--realtime") == 0 ) {
228   realtime++;
229  } else if ( strcmp(k, "--chroot") == 0 ) {
230   chrootdir = argv[++i];
231  } else if ( strcmp(k, "--setgid") == 0 ) {
232   setids |= R_SETGID;
233  } else if ( strcmp(k, "--setuid") == 0 ) {
234   setids |= R_SETUID;
235
236  } else if ( strcmp(k, "--list-cf") == 0 ) {
237   print_codecfilterlist();
238   return 0;
239
240  } else if ( strcmp(k, "-R") == 0 || strcmp(k, "--rate") == 0 ) {
241   sa.rate = atoi(argv[++i]);
242  } else if ( strcmp(k, "-B") == 0 || strcmp(k, "--bits") == 0 ) {
243   sa.bits = atoi(argv[++i]);
244  } else if ( strcmp(k, "-C") == 0 || strcmp(k, "--chans") == 0 ) {
245   sa.channels = atoi(argv[++i]);
246
247  } else if ( strcmp(k, "-d") == 0 || strcmp(k, "--driver") == 0 ) {
248   driver = argv[++i];
249   if ( strcmp(driver, "list") == 0 ) {
250    print_driverlist();
251   }
252  } else if ( strcmp(k, "-D") == 0 || strcmp(k, "--device") == 0 ) {
253   device = argv[++i];
254  } else if ( strcmp(k, "-dO") == 0 ) {
255   opts = argv[++i];
256
257  } else if ( strcmp(k, "-o") == 0 || strcmp(k, "--odriver") == 0 ) {
258   o_drv  = argv[++i];
259  } else if ( strcmp(k, "-O") == 0 || strcmp(k, "--odevice") == 0 ) {
260   o_dev  = argv[++i];
261  } else if ( strcmp(k, "-oO") == 0 ) {
262   o_opts = argv[++i];
263  } else if ( strcmp(k, "-oN") == 0 ) {
264   add_output(o_drv, o_dev, o_opts);
265
266  } else if ( strcmp(k, "-s") == 0 || strcmp(k, "--source") == 0 ) {
267   s_drv = argv[++i];
268  } else if ( strcmp(k, "-S") == 0 ) {
269   k = argv[++i];
270   if ( sources_add(s_drv, k, s_con, s_opt, s_prim) == -1 ) {
271    ROAR_ERR("main(*): adding source '%s' via '%s' failed!", k, s_drv);
272   }
273   s_opt = s_con = NULL;
274   s_prim = 0;
275  } else if ( strcmp(k, "-sO") == 0 ) {
276   s_opt = argv[++i];
277  } else if ( strcmp(k, "-sC") == 0 ) {
278   s_con = argv[++i];
279  } else if ( strcmp(k, "-sP") == 0 ) {
280   s_prim = 1;
281
282  } else if ( strcmp(k, "-p") == 0 || strcmp(k, "--port") == 0 ) {
283   // This is only usefull in INET not UNIX mode.
284   if ( *server == '/' )
285    server = ROAR_DEFAULT_HOST;
286
287   errno = 0;
288   if ( (port = atoi(argv[++i])) < 1 ) {
289    if ( (serv = getservbyname(argv[i], "tcp")) == NULL ) {
290     ROAR_ERR("Unknown service: %s: %s", argv[i], strerror(errno));
291     return 1;
292    }
293    // NOTE: we need to use ROAR_NET2HOST16() here even if s_port is of type int!
294    ROAR_DBG("main(*): serv = {s_name='%s', s_aliases={...}, s_port=%i, s_proto='%s'}",
295            serv->s_name, ROAR_NET2HOST16(serv->s_port), serv->s_proto);
296    port = ROAR_NET2HOST16(serv->s_port);
297   }
298  } else if ( strcmp(k, "-b") == 0 || strcmp(k, "--bind") == 0 || strcmp(k, "-s") == 0 || strcmp(k, "--sock") == 0 ) {
299   server = argv[++i];
300
301  } else if ( strcmp(k, "-t") == 0 || strcmp(k, "--tcp") == 0 ) {
302   if ( sock_type != ROAR_SOCKET_TYPE_TCP && sock_type != ROAR_SOCKET_TYPE_TCP6 )
303    sock_type = ROAR_SOCKET_TYPE_TCP;
304
305   if ( *server == '/' )
306    server = ROAR_DEFAULT_HOST;
307
308  } else if ( strcmp(k, "-4") == 0 ) {
309   sock_type = ROAR_SOCKET_TYPE_TCP;
310   if ( *server == '/' )
311    server = ROAR_DEFAULT_HOST;
312  } else if ( strcmp(k, "-6") == 0 ) {
313#ifdef PF_INET6
314   sock_type = ROAR_SOCKET_TYPE_TCP6;
315   if ( *server == '/' )
316    server = ROAR_DEFAULT_HOST;
317#else
318    ROAR_ERR("No IPv6 support compiled in!");
319    return 1;
320#endif
321
322  } else if ( strcmp(k, "-u") == 0 || strcmp(k, "--unix") == 0 ) {
323   // ignore this case as it is the default behavor.
324   sock_type = ROAR_SOCKET_TYPE_UNIX;
325
326  } else if ( strcmp(k, "-n") == 0 || strcmp(k, "--decnet") == 0 ) {
327#ifdef ROAR_HAVE_LIBDNET
328    port   = ROAR_DEFAULT_NUM;
329    strcpy(decnethost, ROAR_DEFAULT_LISTEN_OBJECT);
330    server = decnethost;
331    sock_type = ROAR_SOCKET_TYPE_DECNET;
332#else
333    ROAR_ERR("No DECnet support compiled in!");
334    return 1;
335#endif
336
337  } else if ( strcmp(k, "-G") == 0 ) {
338   sock_grp  = argv[++i];
339  } else if ( strcmp(k, "-U") == 0 ) {
340   sock_user = argv[++i];
341
342  } else if ( strcmp(k, "--no-listen") == 0 ) {
343   *server     = 0;
344   g_terminate = 1;
345  } else if ( strcmp(k, "--client-fh") == 0 ) {
346   if ( clients_set_fh(clients_new(), atoi(argv[++i])) == -1 ) {
347    ROAR_ERR("main(*): Can not set client's fh");
348    return 1;
349   }
350  } else if ( strcmp(k, "--close-fh") == 0 ) {
351   close(atoi(argv[++i]));
352
353  } else if ( strcmp(k, "--standby") == 0 ) {
354   g_standby = 1;
355  } else if ( strcmp(k, "--auto-standby") == 0 ) {
356   g_autostandby = 1;
357  } else {
358   usage();
359   return 1;
360  }
361
362 }
363
364 if ( o_drv != NULL )
365  add_output(o_drv, o_dev, o_opts);
366
367 ROAR_DBG("Server config: rate=%i, bits=%i, chans=%i", sa.rate, sa.bits, sa.channels);
368
369 if ( midi_init() == -1 )
370  ROAR_ERR("Can not initialize MIDI subsystem");
371
372 if ( *server != 0 ) {
373  if ( (g_listen_socket = roar_socket_listen(sock_type, server, port)) == -1 ) {
374   if ( *server == '/' ) {
375    if ( (i = roar_socket_connect(server, port)) != -1 ) {
376     close(i);
377     ROAR_ERR("Can not open listen socket!");
378     return 1;
379    } else {
380     unlink(server);
381     if ( (g_listen_socket = roar_socket_listen(sock_type, server, port)) == -1 ) {
382      ROAR_ERR("Can not open listen socket!");
383      return 1;
384     }
385    }
386   } else {
387    ROAR_ERR("Can not open listen socket!");
388    return 1;
389   }
390  }
391
392  if ( (grp = getgrnam(sock_grp)) == NULL ) {
393   ROAR_ERR("Can not get GID for group %s: %s", sock_grp, strerror(errno));
394  }
395  if ( sock_user || (setids & R_SETUID) ) {
396   if ( (pwd = getpwnam(sock_user)) == NULL ) {
397    ROAR_ERR("Can not get UID for user %s: %s", sock_user, strerror(errno));
398   }
399  }
400
401  if ( *server == '/' ) {
402   if ( grp ) {
403    if ( pwd ) {
404     chown(server, pwd->pw_uid, grp->gr_gid);
405    } else {
406     chown(server, -1, grp->gr_gid);
407    }
408    if ( getuid() == 0 )
409     chmod(server, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
410   }
411  }
412 }
413
414 if ( output_buffer_init(&sa) == -1 ) {
415  ROAR_ERR("Can not init output buffer!");
416  return 1;
417 }
418
419 if ( driver_open(&drvinst, &drvid, driver, device, &sa) == -1 ) {
420  ROAR_ERR("Can not open output driver!");
421  return 1;
422 }
423
424 if ( samples_init() == -1 ) {
425  ROAR_ERR("Can not init samples!");
426  return 1;
427 }
428
429
430 signal(SIGINT,  on_sig_int);
431 signal(SIGCHLD, on_sig_chld);
432 signal(SIGPIPE, SIG_IGN);  // ignore broken pipes
433
434 if ( realtime ) {
435#ifdef DEBUG
436  ROAR_WARN("compiled with -DDEBUG but realtime is enabled: for real realtime support compiel without -DDEBUG");
437#endif
438
439  errno = 0;
440  nice(-5*realtime); // -5 for each --realtime
441  if ( errno )
442   ROAR_WARN("Can not decrease nice value by 5: %s", strerror(errno));
443/*
444#ifdef __linux__
445  if ( ioprio_set(IOPRIO_WHO_PROCESS, getpid(), IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 0)) == -1 )
446   ROAR_WARN("Can not set io priority: %s", strerror(errno));
447#endif
448*/
449 }
450
451 if ( setids & R_SETGID ) {
452  if ( setgroups(0, (const gid_t *) NULL) == -1 ) {
453   ROAR_ERR("Can not clear supplementary group IDs: %s", strerror(errno));
454  }
455  if ( !grp || setgid(grp->gr_gid) == -1 ) {
456   ROAR_ERR("Can not set GroupID: %s", strerror(errno));
457  }
458 }
459
460
461 clients_set_pid(g_self_client, getpid());
462 clients_set_uid(g_self_client, getuid());
463 clients_set_gid(g_self_client, getgid());
464 clients_get(g_self_client, &self);
465
466 if ( self == NULL ) {
467  ROAR_ERR("Can not get self client!");
468  return 1;
469 }
470
471 strcpy(self->name, "RoarAudio daemon internal");
472
473 if ( daemon ) {
474  close(ROAR_STDIN );
475  close(ROAR_STDOUT);
476  close(ROAR_STDERR);
477  setsid();
478  if ( fork() )
479   _exit(0);
480 }
481
482 if (chrootdir) {
483  if ( chroot(chrootdir) == -1 ) {
484   ROAR_ERR("Can not chroot to %s: %s", chrootdir, strerror(errno));
485   return 2;
486  }
487  if ( chdir("/") == -1 ) {
488   ROAR_ERR("Can not chdir to /: %s", strerror(errno));
489   return 2;
490  }
491 }
492
493 if ( setids & R_SETUID ) {
494  if ( !pwd || setuid(pwd->pw_uid) == -1 ) {
495   ROAR_ERR("Can not set UserID: %s", strerror(errno));
496   return 3;
497  }
498  clients_set_uid(g_self_client, getuid());
499 }
500
501 // start main loop...
502 main_loop(drvid, drvinst, &sa, sysclocksync);
503
504 // clean up.
505 clean_quit_prep();
506 driver_close(drvinst, drvid);
507 output_buffer_free();
508
509 return 0;
510}
511
512void cleanup_listen_socket (int terminate) {
513
514 if ( g_listen_socket != -1 ) {
515  close(g_listen_socket);
516
517  g_listen_socket = -1;
518
519  if ( *server == '/' )
520   unlink(server);
521 }
522
523 if ( terminate )
524  g_terminate = 1;
525}
526
527void clean_quit_prep (void) {
528 cleanup_listen_socket(0);
529
530 sources_free();
531 streams_free();
532 clients_free();
533 midi_cb_stop(); // stop console beep
534 midi_free();
535}
536
537void clean_quit (void) {
538 clean_quit_prep();
539// driver_close(drvinst, drvid);
540// output_buffer_free();
541 exit(0);
542}
543
544//ll
Note: See TracBrowser for help on using the repository browser.