source: roaraudio/roard/roard.c @ 934:0277459c7e79

Last change on this file since 934:0277459c7e79 was 934:0277459c7e79, checked in by phi, 15 years ago

got output streams basicly working, we should clean up some code anyway

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