[0] | 1 | //roard.c: |
---|
| 2 | |
---|
[668] | 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 | |
---|
[0] | 25 | #include "roard.h" |
---|
| 26 | |
---|
[60] | 27 | char * server = ROAR_DEFAULT_SOCK_GLOBAL; // global server address |
---|
| 28 | |
---|
[0] | 29 | void usage (void) { |
---|
| 30 | printf("Usage: roard [OPTIONS]...\n\n"); |
---|
| 31 | |
---|
[68] | 32 | printf("Misc Options:\n\n"); |
---|
| 33 | printf( |
---|
[775] | 34 | " --daemon - Bring the server into background after init\n" |
---|
[71] | 35 | " --terminate - Terminate after last client quited\n" |
---|
[274] | 36 | " --restart - Trys to stop an old instance and start a new with new settings\n" |
---|
[276] | 37 | " --realtime - Trys to get realtime priority,\n" |
---|
| 38 | " give multible times for being more realtime\n" |
---|
[444] | 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" |
---|
[905] | 42 | " --sysclocksync - calculate exact sample rate using the system clock\n" |
---|
[68] | 43 | ); |
---|
| 44 | |
---|
| 45 | printf("\nAudio Options:\n\n"); |
---|
[0] | 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"); |
---|
[974] | 53 | printf(" -d --driver DRV - Set the driver (default: %s)\n", ROAR_DRIVER_DEFAULT); |
---|
[0] | 54 | printf(" -D --device DEV - Set the device\n"); |
---|
| 55 | printf(" -dO OPTS - Set output options\n"); |
---|
[973] | 56 | printf(" --list-driver - List all drivers\n"); |
---|
[0] | 57 | |
---|
[932] | 58 | printf("\nOutput Options:\n\n"); |
---|
[974] | 59 | printf(" -o --odriver DRV - Set the driver, use '--list-driver' to get a list\n"); |
---|
[932] | 60 | printf(" -O --odevice DEV - Set the device\n"); |
---|
| 61 | printf(" -oO OPTS - Set output options\n"); |
---|
| 62 | printf(" -oN - Adds another output\n"); |
---|
[961] | 63 | printf(" -oP - Mark output as primary\n"); |
---|
[932] | 64 | |
---|
[0] | 65 | printf("\nSource Options:\n\n"); |
---|
| 66 | printf(" -s --source DRV - Use DRV as input driver\n" |
---|
| 67 | " -S DEV - Use DEV as input device\n" |
---|
| 68 | " -sO OPTS - Use OPTS as input options\n" |
---|
| 69 | " -sP - Make souce as primary\n" |
---|
| 70 | ); |
---|
| 71 | |
---|
[280] | 72 | printf("\nCodec Filter Options:\n\n"); |
---|
| 73 | printf(" --list-cf - List all codec filter\n" |
---|
| 74 | ); |
---|
[0] | 75 | |
---|
| 76 | printf("\nServer Options:\n\n"); |
---|
| 77 | printf(" -t --tcp - Use TCP listen socket\n" |
---|
| 78 | " -u --unix - Use UNIX Domain listen socket (default)\n" |
---|
[508] | 79 | #ifdef ROAR_HAVE_LIBDNET |
---|
| 80 | " -n --decnet - use DECnet listen socket\n" |
---|
| 81 | #endif |
---|
[518] | 82 | " -4 - Use IPv4 connections (implies -t)\n" |
---|
| 83 | #ifdef PF_INET6 |
---|
| 84 | " -6 - Use IPv6 connections (implies -t)\n" |
---|
| 85 | #endif |
---|
| 86 | #ifdef IPV6_ADDRFORM |
---|
| 87 | " -64 - Try to downgrade sockets from IPv6 into IPv4,\n" |
---|
| 88 | " this is normaly not usefull.\n" |
---|
| 89 | #endif |
---|
[0] | 90 | " -p --port - TCP Port to bind to\n" |
---|
| 91 | " -b --bind - IP/Hostname to bind to\n" |
---|
| 92 | " -s --sock - Filename for UNIX Domain Socket\n" |
---|
[450] | 93 | " -G GROUP - Sets the group for the UNIX Domain Socket, (default: %s)\n" |
---|
[60] | 94 | " You need the permittions to change the GID\n" |
---|
[444] | 95 | " -U USER - Sets the user for the UNIX Domain Socket, (default: do not set)\n" |
---|
| 96 | " You need the permittions to change the UID (normaly only root has)\n" |
---|
[548] | 97 | " --no-listen - Do not listen for new clients\n" |
---|
| 98 | " (only usefull for relaing, impleys --terminate)\n" |
---|
[274] | 99 | " --client-fh - Comunicate with a client over this handle\n" |
---|
[501] | 100 | " (only usefull for relaing)\n" |
---|
[920] | 101 | " --close-fh - Closes the given fh\n" |
---|
| 102 | " --standby - Start in standby state\n" |
---|
| 103 | " --auto-standby - Automatical goes into standby if there are no streams\n", |
---|
[450] | 104 | ROAR_DEFAULT_SOCKGRP |
---|
[0] | 105 | ); |
---|
| 106 | // printf("\n Options:\n\n"); |
---|
| 107 | printf("\n"); |
---|
| 108 | } |
---|
| 109 | |
---|
[579] | 110 | int restart_server (char * server) { |
---|
| 111 | struct roar_connection con; |
---|
| 112 | if ( roar_connect(&con, server) == -1 ) { |
---|
| 113 | return -1; |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | if ( roar_terminate(&con, 1) == -1 ) { |
---|
| 117 | return -1; |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | return roar_disconnect(&con); |
---|
| 121 | } |
---|
| 122 | |
---|
[444] | 123 | #define R_SETUID 1 |
---|
| 124 | #define R_SETGID 2 |
---|
| 125 | |
---|
[961] | 126 | int add_output (char * drv, char * dev, char * opts, int prim) { |
---|
[933] | 127 | int stream; |
---|
| 128 | struct roar_stream * s; |
---|
| 129 | struct roar_stream_server * ss; |
---|
[938] | 130 | char * k, * v; |
---|
| 131 | int codec; |
---|
[933] | 132 | |
---|
[938] | 133 | ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = ?", drv, dev, opts); |
---|
[933] | 134 | |
---|
| 135 | if ( (stream = streams_new()) == -1 ) { |
---|
[938] | 136 | ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = -1", drv, dev, opts); |
---|
[982] | 137 | if ( prim ) alive = 0; |
---|
[933] | 138 | return -1; |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | streams_get(stream, &ss); |
---|
| 142 | s = ROAR_STREAM(ss); |
---|
| 143 | |
---|
| 144 | memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info)); |
---|
| 145 | |
---|
| 146 | s->dir = ROAR_DIR_OUTPUT; |
---|
| 147 | s->pos_rel_id = -1; |
---|
| 148 | // s->info.codec = codec; |
---|
| 149 | |
---|
[938] | 150 | codec = s->info.codec; |
---|
| 151 | |
---|
| 152 | k = strtok(opts, ","); |
---|
| 153 | while (k != NULL) { |
---|
| 154 | // ROAR_WARN("add_output(*): opts: %s", k); |
---|
| 155 | |
---|
| 156 | if ( (v = strstr(k, "=")) != NULL ) { |
---|
| 157 | *v++ = 0; |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | ROAR_DBG("add_output(*): opts: k='%s', v='%s'", k, v); |
---|
| 161 | if ( strcmp(k, "rate") == 0 ) { |
---|
| 162 | s->info.rate = atoi(v); |
---|
| 163 | } else if ( strcmp(k, "channels") == 0 ) { |
---|
| 164 | s->info.channels = atoi(v); |
---|
| 165 | } else if ( strcmp(k, "bits") == 0 ) { |
---|
| 166 | s->info.bits = atoi(v); |
---|
| 167 | } else if ( strcmp(k, "codec") == 0 ) { |
---|
| 168 | if ( (codec = roar_str2codec(v)) == -1 ) { |
---|
| 169 | ROAR_ERR("add_output(*): unknown codec '%s'", v); |
---|
| 170 | streams_delete(stream); |
---|
[982] | 171 | if ( prim ) alive = 0; |
---|
[938] | 172 | return -1; |
---|
| 173 | } |
---|
| 174 | } else { |
---|
| 175 | ROAR_ERR("add_output(*): unknown option '%s'", k); |
---|
| 176 | streams_delete(stream); |
---|
[982] | 177 | if ( prim ) alive = 0; |
---|
[938] | 178 | return -1; |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | k = strtok(NULL, ","); |
---|
| 182 | } |
---|
| 183 | |
---|
[941] | 184 | if ( codec == ROAR_CODEC_ALAW || codec == ROAR_CODEC_MULAW ) |
---|
| 185 | s->info.bits = 8; // needed to open OSS driver, will be overriden by codecfilter |
---|
| 186 | |
---|
[938] | 187 | s->info.codec = codec; |
---|
| 188 | ROAR_STREAM_SERVER(s)->codec_orgi = codec; |
---|
[933] | 189 | |
---|
| 190 | if ( driver_openvio(&(ss->vio), &(ss->driver_id), drv, dev, &(s->info), -1) ) { |
---|
| 191 | streams_delete(stream); |
---|
[938] | 192 | ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = -1", drv, dev, opts); |
---|
[982] | 193 | if ( prim ) alive = 0; |
---|
[933] | 194 | return -1; |
---|
| 195 | } |
---|
| 196 | |
---|
[938] | 197 | streams_set_fh(stream, -1); // update some internal structures |
---|
| 198 | |
---|
[933] | 199 | client_stream_add(g_source_client, stream); |
---|
| 200 | |
---|
[961] | 201 | if ( prim ) |
---|
| 202 | streams_mark_primary(stream); |
---|
| 203 | |
---|
[933] | 204 | return 0; |
---|
[932] | 205 | } |
---|
| 206 | |
---|
[0] | 207 | int main (int argc, char * argv[]) { |
---|
| 208 | int i; |
---|
| 209 | char * k; |
---|
[905] | 210 | char user_sock[80] = {0}; |
---|
[0] | 211 | struct roar_audio_info sa; |
---|
[905] | 212 | int daemon = 0; |
---|
| 213 | int realtime = 0; |
---|
| 214 | int sysclocksync = 0; |
---|
[74] | 215 | char * driver = getenv("ROAR_DRIVER"); |
---|
| 216 | char * device = getenv("ROAR_DEVICE"); |
---|
[0] | 217 | char * opts = NULL; |
---|
[60] | 218 | // char * server = ROAR_DEFAULT_SOCK_GLOBAL; |
---|
[0] | 219 | int port = ROAR_DEFAULT_PORT; |
---|
| 220 | int drvid; |
---|
[550] | 221 | char * s_drv = "cf"; |
---|
[444] | 222 | char * s_con = NULL; |
---|
| 223 | char * s_opt = NULL; |
---|
| 224 | int s_prim = 0; |
---|
[932] | 225 | char * o_drv = NULL; |
---|
| 226 | char * o_dev = NULL; |
---|
| 227 | char * o_opts = NULL; |
---|
[961] | 228 | int o_prim = 0; |
---|
[450] | 229 | char * sock_grp = ROAR_DEFAULT_SOCKGRP; |
---|
[444] | 230 | char * sock_user = NULL; |
---|
[517] | 231 | int sock_type = ROAR_SOCKET_TYPE_UNKNOWN; |
---|
[444] | 232 | char * chrootdir = NULL; |
---|
| 233 | int setids = 0; |
---|
[446] | 234 | struct group * grp = NULL; |
---|
| 235 | struct passwd * pwd = NULL; |
---|
| 236 | struct servent * serv = NULL; |
---|
[0] | 237 | DRIVER_USERDATA_T drvinst; |
---|
[39] | 238 | struct roar_client * self = NULL; |
---|
[508] | 239 | #ifdef ROAR_HAVE_LIBDNET |
---|
| 240 | char decnethost[80]; |
---|
| 241 | #endif |
---|
[0] | 242 | |
---|
| 243 | g_listen_socket = -1; |
---|
| 244 | g_standby = 0; |
---|
[920] | 245 | g_autostandby = 0; |
---|
[982] | 246 | alive = 1; |
---|
[0] | 247 | |
---|
| 248 | sa.bits = ROAR_BITS_DEFAULT; |
---|
| 249 | sa.channels = ROAR_CHANNELS_DEFAULT; |
---|
| 250 | sa.rate = ROAR_RATE_DEFAULT; |
---|
| 251 | sa.codec = ROAR_CODEC_DEFAULT; |
---|
| 252 | |
---|
| 253 | g_sa = &sa; |
---|
| 254 | |
---|
[60] | 255 | |
---|
| 256 | if ( getuid() != 0 && getenv("HOME") ) { |
---|
| 257 | snprintf(user_sock, 79, "%s/%s", getenv("HOME"), ROAR_DEFAULT_SOCK_USER); |
---|
| 258 | server = user_sock; |
---|
| 259 | } |
---|
| 260 | |
---|
[279] | 261 | if ( getenv("ROAR_SERVER") != NULL ) |
---|
| 262 | server = getenv("ROAR_SERVER"); |
---|
| 263 | |
---|
[63] | 264 | if ( clients_init() == -1 ) { |
---|
| 265 | ROAR_ERR("Can not init clients!"); |
---|
| 266 | return 1; |
---|
| 267 | } |
---|
| 268 | |
---|
| 269 | if ( streams_init() == -1 ) { |
---|
| 270 | ROAR_ERR("Can not init streams!"); |
---|
| 271 | return 1; |
---|
| 272 | } |
---|
| 273 | |
---|
[64] | 274 | if ( (g_self_client = clients_new()) == -1 ) { |
---|
| 275 | ROAR_ERR("Can not create self client!"); |
---|
| 276 | return 1; |
---|
| 277 | } |
---|
| 278 | |
---|
[0] | 279 | if ( sources_init() == -1 ) { |
---|
| 280 | ROAR_ERR("Can not init sources!"); |
---|
| 281 | return 1; |
---|
| 282 | } |
---|
| 283 | |
---|
[64] | 284 | if ( (sources_set_client(g_self_client)) == -1 ) { |
---|
| 285 | ROAR_ERR("Can not init set source client!"); |
---|
| 286 | return 1; |
---|
| 287 | } |
---|
| 288 | |
---|
[0] | 289 | for (i = 1; i < argc; i++) { |
---|
| 290 | k = argv[i]; |
---|
| 291 | |
---|
| 292 | if ( strcmp(k, "-h") == 0 || strcmp(k, "--help") == 0 ) { |
---|
| 293 | usage(); |
---|
| 294 | return 0; |
---|
| 295 | |
---|
[579] | 296 | } else if ( strcmp(k, "--restart") == 0 ) { |
---|
| 297 | if ( restart_server(server) == -1 ) { |
---|
| 298 | ROAR_WARN("Can not terminate old server (not running at %s?), tring to continue anyway", server); |
---|
| 299 | } |
---|
| 300 | |
---|
[775] | 301 | } else if ( strcmp(k, "--demon") == 0 || strcmp(k, "--daemon") == 0 ) { |
---|
| 302 | daemon = 1; |
---|
[71] | 303 | } else if ( strcmp(k, "--terminate") == 0 ) { |
---|
| 304 | g_terminate = 1; |
---|
[905] | 305 | } else if ( strcmp(k, "--sysclocksync") == 0 ) { |
---|
| 306 | sysclocksync = 1000; |
---|
[275] | 307 | } else if ( strcmp(k, "--realtime") == 0 ) { |
---|
[276] | 308 | realtime++; |
---|
[444] | 309 | } else if ( strcmp(k, "--chroot") == 0 ) { |
---|
| 310 | chrootdir = argv[++i]; |
---|
| 311 | } else if ( strcmp(k, "--setgid") == 0 ) { |
---|
| 312 | setids |= R_SETGID; |
---|
| 313 | } else if ( strcmp(k, "--setuid") == 0 ) { |
---|
| 314 | setids |= R_SETUID; |
---|
[68] | 315 | |
---|
[280] | 316 | } else if ( strcmp(k, "--list-cf") == 0 ) { |
---|
| 317 | print_codecfilterlist(); |
---|
| 318 | return 0; |
---|
| 319 | |
---|
[0] | 320 | } else if ( strcmp(k, "-R") == 0 || strcmp(k, "--rate") == 0 ) { |
---|
| 321 | sa.rate = atoi(argv[++i]); |
---|
| 322 | } else if ( strcmp(k, "-B") == 0 || strcmp(k, "--bits") == 0 ) { |
---|
| 323 | sa.bits = atoi(argv[++i]); |
---|
| 324 | } else if ( strcmp(k, "-C") == 0 || strcmp(k, "--chans") == 0 ) { |
---|
| 325 | sa.channels = atoi(argv[++i]); |
---|
| 326 | |
---|
| 327 | } else if ( strcmp(k, "-d") == 0 || strcmp(k, "--driver") == 0 ) { |
---|
| 328 | driver = argv[++i]; |
---|
| 329 | if ( strcmp(driver, "list") == 0 ) { |
---|
[974] | 330 | ROAR_WARN("The option is obsolete, use --list-driver!"); |
---|
[0] | 331 | print_driverlist(); |
---|
[973] | 332 | return 0; |
---|
[0] | 333 | } |
---|
| 334 | } else if ( strcmp(k, "-D") == 0 || strcmp(k, "--device") == 0 ) { |
---|
| 335 | device = argv[++i]; |
---|
| 336 | } else if ( strcmp(k, "-dO") == 0 ) { |
---|
| 337 | opts = argv[++i]; |
---|
[973] | 338 | } else if ( strcmp(k, "--list-driver") == 0 ) { |
---|
| 339 | print_driverlist(); |
---|
| 340 | return 0; |
---|
[0] | 341 | |
---|
[932] | 342 | } else if ( strcmp(k, "-o") == 0 || strcmp(k, "--odriver") == 0 ) { |
---|
| 343 | o_drv = argv[++i]; |
---|
| 344 | } else if ( strcmp(k, "-O") == 0 || strcmp(k, "--odevice") == 0 ) { |
---|
| 345 | o_dev = argv[++i]; |
---|
| 346 | } else if ( strcmp(k, "-oO") == 0 ) { |
---|
| 347 | o_opts = argv[++i]; |
---|
[961] | 348 | } else if ( strcmp(k, "-oP") == 0 ) { |
---|
| 349 | o_prim = 1; |
---|
[932] | 350 | } else if ( strcmp(k, "-oN") == 0 ) { |
---|
[961] | 351 | add_output(o_drv, o_dev, o_opts, o_prim); |
---|
| 352 | o_drv = o_dev = o_opts = NULL; |
---|
| 353 | o_prim = 0; |
---|
[932] | 354 | |
---|
[0] | 355 | } else if ( strcmp(k, "-s") == 0 || strcmp(k, "--source") == 0 ) { |
---|
[550] | 356 | s_drv = argv[++i]; |
---|
| 357 | } else if ( strcmp(k, "-S") == 0 ) { |
---|
[0] | 358 | k = argv[++i]; |
---|
[550] | 359 | if ( sources_add(s_drv, k, s_con, s_opt, s_prim) == -1 ) { |
---|
| 360 | ROAR_ERR("main(*): adding source '%s' via '%s' failed!", k, s_drv); |
---|
[0] | 361 | } |
---|
[550] | 362 | s_opt = s_con = NULL; |
---|
[0] | 363 | s_prim = 0; |
---|
| 364 | } else if ( strcmp(k, "-sO") == 0 ) { |
---|
| 365 | s_opt = argv[++i]; |
---|
| 366 | } else if ( strcmp(k, "-sC") == 0 ) { |
---|
| 367 | s_con = argv[++i]; |
---|
| 368 | } else if ( strcmp(k, "-sP") == 0 ) { |
---|
| 369 | s_prim = 1; |
---|
| 370 | |
---|
| 371 | } else if ( strcmp(k, "-p") == 0 || strcmp(k, "--port") == 0 ) { |
---|
[447] | 372 | // This is only usefull in INET not UNIX mode. |
---|
| 373 | if ( *server == '/' ) |
---|
| 374 | server = ROAR_DEFAULT_HOST; |
---|
| 375 | |
---|
[446] | 376 | errno = 0; |
---|
| 377 | if ( (port = atoi(argv[++i])) < 1 ) { |
---|
| 378 | if ( (serv = getservbyname(argv[i], "tcp")) == NULL ) { |
---|
| 379 | ROAR_ERR("Unknown service: %s: %s", argv[i], strerror(errno)); |
---|
| 380 | return 1; |
---|
| 381 | } |
---|
| 382 | // NOTE: we need to use ROAR_NET2HOST16() here even if s_port is of type int! |
---|
| 383 | ROAR_DBG("main(*): serv = {s_name='%s', s_aliases={...}, s_port=%i, s_proto='%s'}", |
---|
| 384 | serv->s_name, ROAR_NET2HOST16(serv->s_port), serv->s_proto); |
---|
| 385 | port = ROAR_NET2HOST16(serv->s_port); |
---|
| 386 | } |
---|
[68] | 387 | } else if ( strcmp(k, "-b") == 0 || strcmp(k, "--bind") == 0 || strcmp(k, "-s") == 0 || strcmp(k, "--sock") == 0 ) { |
---|
[0] | 388 | server = argv[++i]; |
---|
[518] | 389 | |
---|
[573] | 390 | } else if ( strcmp(k, "-t") == 0 || strcmp(k, "--tcp") == 0 ) { |
---|
[518] | 391 | if ( sock_type != ROAR_SOCKET_TYPE_TCP && sock_type != ROAR_SOCKET_TYPE_TCP6 ) |
---|
| 392 | sock_type = ROAR_SOCKET_TYPE_TCP; |
---|
| 393 | |
---|
| 394 | if ( *server == '/' ) |
---|
| 395 | server = ROAR_DEFAULT_HOST; |
---|
| 396 | |
---|
| 397 | } else if ( strcmp(k, "-4") == 0 ) { |
---|
[517] | 398 | sock_type = ROAR_SOCKET_TYPE_TCP; |
---|
[447] | 399 | if ( *server == '/' ) |
---|
| 400 | server = ROAR_DEFAULT_HOST; |
---|
[518] | 401 | } else if ( strcmp(k, "-6") == 0 ) { |
---|
[519] | 402 | #ifdef PF_INET6 |
---|
[518] | 403 | sock_type = ROAR_SOCKET_TYPE_TCP6; |
---|
| 404 | if ( *server == '/' ) |
---|
| 405 | server = ROAR_DEFAULT_HOST; |
---|
[519] | 406 | #else |
---|
| 407 | ROAR_ERR("No IPv6 support compiled in!"); |
---|
| 408 | return 1; |
---|
| 409 | #endif |
---|
[518] | 410 | |
---|
[573] | 411 | } else if ( strcmp(k, "-u") == 0 || strcmp(k, "--unix") == 0 ) { |
---|
[62] | 412 | // ignore this case as it is the default behavor. |
---|
[517] | 413 | sock_type = ROAR_SOCKET_TYPE_UNIX; |
---|
[518] | 414 | |
---|
[573] | 415 | } else if ( strcmp(k, "-n") == 0 || strcmp(k, "--decnet") == 0 ) { |
---|
[508] | 416 | #ifdef ROAR_HAVE_LIBDNET |
---|
| 417 | port = ROAR_DEFAULT_NUM; |
---|
| 418 | strcpy(decnethost, ROAR_DEFAULT_LISTEN_OBJECT); |
---|
| 419 | server = decnethost; |
---|
[517] | 420 | sock_type = ROAR_SOCKET_TYPE_DECNET; |
---|
[508] | 421 | #else |
---|
| 422 | ROAR_ERR("No DECnet support compiled in!"); |
---|
| 423 | return 1; |
---|
| 424 | #endif |
---|
[518] | 425 | |
---|
[60] | 426 | } else if ( strcmp(k, "-G") == 0 ) { |
---|
[444] | 427 | sock_grp = argv[++i]; |
---|
| 428 | } else if ( strcmp(k, "-U") == 0 ) { |
---|
| 429 | sock_user = argv[++i]; |
---|
[0] | 430 | |
---|
[68] | 431 | } else if ( strcmp(k, "--no-listen") == 0 ) { |
---|
[548] | 432 | *server = 0; |
---|
| 433 | g_terminate = 1; |
---|
[68] | 434 | } else if ( strcmp(k, "--client-fh") == 0 ) { |
---|
| 435 | if ( clients_set_fh(clients_new(), atoi(argv[++i])) == -1 ) { |
---|
| 436 | ROAR_ERR("main(*): Can not set client's fh"); |
---|
| 437 | return 1; |
---|
| 438 | } |
---|
[501] | 439 | } else if ( strcmp(k, "--close-fh") == 0 ) { |
---|
| 440 | close(atoi(argv[++i])); |
---|
[68] | 441 | |
---|
[920] | 442 | } else if ( strcmp(k, "--standby") == 0 ) { |
---|
| 443 | g_standby = 1; |
---|
| 444 | } else if ( strcmp(k, "--auto-standby") == 0 ) { |
---|
| 445 | g_autostandby = 1; |
---|
[0] | 446 | } else { |
---|
| 447 | usage(); |
---|
| 448 | return 1; |
---|
| 449 | } |
---|
| 450 | |
---|
| 451 | } |
---|
| 452 | |
---|
[932] | 453 | if ( o_drv != NULL ) |
---|
[961] | 454 | add_output(o_drv, o_dev, o_opts, o_prim); |
---|
[932] | 455 | |
---|
[0] | 456 | ROAR_DBG("Server config: rate=%i, bits=%i, chans=%i", sa.rate, sa.bits, sa.channels); |
---|
| 457 | |
---|
[281] | 458 | if ( midi_init() == -1 ) |
---|
| 459 | ROAR_ERR("Can not initialize MIDI subsystem"); |
---|
| 460 | |
---|
[68] | 461 | if ( *server != 0 ) { |
---|
[517] | 462 | if ( (g_listen_socket = roar_socket_listen(sock_type, server, port)) == -1 ) { |
---|
[286] | 463 | if ( *server == '/' ) { |
---|
| 464 | if ( (i = roar_socket_connect(server, port)) != -1 ) { |
---|
| 465 | close(i); |
---|
| 466 | ROAR_ERR("Can not open listen socket!"); |
---|
| 467 | return 1; |
---|
| 468 | } else { |
---|
| 469 | unlink(server); |
---|
[517] | 470 | if ( (g_listen_socket = roar_socket_listen(sock_type, server, port)) == -1 ) { |
---|
[286] | 471 | ROAR_ERR("Can not open listen socket!"); |
---|
| 472 | return 1; |
---|
| 473 | } |
---|
| 474 | } |
---|
| 475 | } else { |
---|
| 476 | ROAR_ERR("Can not open listen socket!"); |
---|
| 477 | return 1; |
---|
| 478 | } |
---|
[68] | 479 | } |
---|
[0] | 480 | |
---|
[523] | 481 | if ( (grp = getgrnam(sock_grp)) == NULL ) { |
---|
| 482 | ROAR_ERR("Can not get GID for group %s: %s", sock_grp, strerror(errno)); |
---|
| 483 | } |
---|
| 484 | if ( sock_user || (setids & R_SETUID) ) { |
---|
| 485 | if ( (pwd = getpwnam(sock_user)) == NULL ) { |
---|
| 486 | ROAR_ERR("Can not get UID for user %s: %s", sock_user, strerror(errno)); |
---|
| 487 | } |
---|
| 488 | } |
---|
| 489 | |
---|
[68] | 490 | if ( *server == '/' ) { |
---|
[523] | 491 | if ( grp ) { |
---|
[444] | 492 | if ( pwd ) { |
---|
| 493 | chown(server, pwd->pw_uid, grp->gr_gid); |
---|
| 494 | } else { |
---|
| 495 | chown(server, -1, grp->gr_gid); |
---|
| 496 | } |
---|
[68] | 497 | if ( getuid() == 0 ) |
---|
| 498 | chmod(server, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP); |
---|
| 499 | } |
---|
[60] | 500 | } |
---|
| 501 | } |
---|
| 502 | |
---|
[0] | 503 | if ( output_buffer_init(&sa) == -1 ) { |
---|
| 504 | ROAR_ERR("Can not init output buffer!"); |
---|
| 505 | return 1; |
---|
| 506 | } |
---|
| 507 | |
---|
| 508 | if ( driver_open(&drvinst, &drvid, driver, device, &sa) == -1 ) { |
---|
| 509 | ROAR_ERR("Can not open output driver!"); |
---|
| 510 | return 1; |
---|
| 511 | } |
---|
| 512 | |
---|
[44] | 513 | if ( samples_init() == -1 ) { |
---|
| 514 | ROAR_ERR("Can not init samples!"); |
---|
| 515 | return 1; |
---|
| 516 | } |
---|
| 517 | |
---|
| 518 | |
---|
[0] | 519 | signal(SIGINT, on_sig_int); |
---|
[285] | 520 | signal(SIGCHLD, on_sig_chld); |
---|
[0] | 521 | signal(SIGPIPE, SIG_IGN); // ignore broken pipes |
---|
| 522 | |
---|
[275] | 523 | if ( realtime ) { |
---|
[278] | 524 | #ifdef DEBUG |
---|
| 525 | ROAR_WARN("compiled with -DDEBUG but realtime is enabled: for real realtime support compiel without -DDEBUG"); |
---|
| 526 | #endif |
---|
| 527 | |
---|
[275] | 528 | errno = 0; |
---|
[276] | 529 | nice(-5*realtime); // -5 for each --realtime |
---|
[275] | 530 | if ( errno ) |
---|
| 531 | ROAR_WARN("Can not decrease nice value by 5: %s", strerror(errno)); |
---|
[277] | 532 | /* |
---|
[276] | 533 | #ifdef __linux__ |
---|
[277] | 534 | if ( ioprio_set(IOPRIO_WHO_PROCESS, getpid(), IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 0)) == -1 ) |
---|
| 535 | ROAR_WARN("Can not set io priority: %s", strerror(errno)); |
---|
[276] | 536 | #endif |
---|
[277] | 537 | */ |
---|
[275] | 538 | } |
---|
| 539 | |
---|
[444] | 540 | if ( setids & R_SETGID ) { |
---|
| 541 | if ( setgroups(0, (const gid_t *) NULL) == -1 ) { |
---|
| 542 | ROAR_ERR("Can not clear supplementary group IDs: %s", strerror(errno)); |
---|
| 543 | } |
---|
[523] | 544 | if ( !grp || setgid(grp->gr_gid) == -1 ) { |
---|
[444] | 545 | ROAR_ERR("Can not set GroupID: %s", strerror(errno)); |
---|
| 546 | } |
---|
| 547 | } |
---|
| 548 | |
---|
[0] | 549 | |
---|
[39] | 550 | clients_set_pid(g_self_client, getpid()); |
---|
[440] | 551 | clients_set_uid(g_self_client, getuid()); |
---|
| 552 | clients_set_gid(g_self_client, getgid()); |
---|
[39] | 553 | clients_get(g_self_client, &self); |
---|
[37] | 554 | |
---|
[39] | 555 | if ( self == NULL ) { |
---|
| 556 | ROAR_ERR("Can not get self client!"); |
---|
| 557 | return 1; |
---|
| 558 | } |
---|
| 559 | |
---|
[775] | 560 | strcpy(self->name, "RoarAudio daemon internal"); |
---|
[68] | 561 | |
---|
[775] | 562 | if ( daemon ) { |
---|
[68] | 563 | close(ROAR_STDIN ); |
---|
| 564 | close(ROAR_STDOUT); |
---|
| 565 | close(ROAR_STDERR); |
---|
[422] | 566 | setsid(); |
---|
[68] | 567 | if ( fork() ) |
---|
| 568 | _exit(0); |
---|
| 569 | } |
---|
| 570 | |
---|
[444] | 571 | if (chrootdir) { |
---|
| 572 | if ( chroot(chrootdir) == -1 ) { |
---|
| 573 | ROAR_ERR("Can not chroot to %s: %s", chrootdir, strerror(errno)); |
---|
| 574 | return 2; |
---|
| 575 | } |
---|
| 576 | if ( chdir("/") == -1 ) { |
---|
| 577 | ROAR_ERR("Can not chdir to /: %s", strerror(errno)); |
---|
| 578 | return 2; |
---|
| 579 | } |
---|
| 580 | } |
---|
| 581 | |
---|
| 582 | if ( setids & R_SETUID ) { |
---|
| 583 | if ( !pwd || setuid(pwd->pw_uid) == -1 ) { |
---|
| 584 | ROAR_ERR("Can not set UserID: %s", strerror(errno)); |
---|
| 585 | return 3; |
---|
| 586 | } |
---|
| 587 | clients_set_uid(g_self_client, getuid()); |
---|
| 588 | } |
---|
| 589 | |
---|
[0] | 590 | // start main loop... |
---|
[905] | 591 | main_loop(drvid, drvinst, &sa, sysclocksync); |
---|
[0] | 592 | |
---|
| 593 | // clean up. |
---|
| 594 | clean_quit_prep(); |
---|
| 595 | driver_close(drvinst, drvid); |
---|
| 596 | output_buffer_free(); |
---|
| 597 | |
---|
| 598 | return 0; |
---|
| 599 | } |
---|
| 600 | |
---|
[574] | 601 | void cleanup_listen_socket (int terminate) { |
---|
[580] | 602 | |
---|
| 603 | if ( g_listen_socket != -1 ) { |
---|
| 604 | close(g_listen_socket); |
---|
[60] | 605 | |
---|
[580] | 606 | g_listen_socket = -1; |
---|
[576] | 607 | |
---|
[580] | 608 | if ( *server == '/' ) |
---|
| 609 | unlink(server); |
---|
| 610 | } |
---|
[60] | 611 | |
---|
[574] | 612 | if ( terminate ) |
---|
| 613 | g_terminate = 1; |
---|
| 614 | } |
---|
| 615 | |
---|
| 616 | void clean_quit_prep (void) { |
---|
| 617 | cleanup_listen_socket(0); |
---|
[60] | 618 | |
---|
[0] | 619 | sources_free(); |
---|
| 620 | streams_free(); |
---|
| 621 | clients_free(); |
---|
[282] | 622 | midi_cb_stop(); // stop console beep |
---|
[281] | 623 | midi_free(); |
---|
[0] | 624 | } |
---|
| 625 | |
---|
| 626 | void clean_quit (void) { |
---|
| 627 | clean_quit_prep(); |
---|
| 628 | // driver_close(drvinst, drvid); |
---|
| 629 | // output_buffer_free(); |
---|
| 630 | exit(0); |
---|
| 631 | } |
---|
| 632 | |
---|
| 633 | //ll |
---|