source: roaraudio/roard/roard.c @ 933:2c06d248d2e6

Last change on this file since 933:2c06d248d2e6 was 933:2c06d248d2e6, checked in by phi, 15 years ago

wrote add_output() but does not work yet

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