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