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