source: roaraudio/roard/roard.c @ 668:71ac426690da

Last change on this file since 668:71ac426690da was 668:71ac426690da, checked in by phi, 16 years ago

added license statements

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