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