source: roaraudio/roard/roard.c @ 920:ce0dc2fbe8e2

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

added --standby and --auto-standby

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