[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; |
---|
[1486] | 274 | #ifdef ROAR_HAVE_FORK |
---|
[905] | 275 | int daemon = 0; |
---|
[1486] | 276 | #endif |
---|
[905] | 277 | int realtime = 0; |
---|
| 278 | int sysclocksync = 0; |
---|
[1207] | 279 | char * driver = NULL; |
---|
| 280 | char * device = NULL; |
---|
| 281 | char * opts = NULL; |
---|
[60] | 282 | // char * server = ROAR_DEFAULT_SOCK_GLOBAL; |
---|
[1207] | 283 | int port = ROAR_DEFAULT_PORT; |
---|
[0] | 284 | int drvid; |
---|
[550] | 285 | char * s_drv = "cf"; |
---|
[1110] | 286 | char * s_dev = NULL; |
---|
[444] | 287 | char * s_con = NULL; |
---|
| 288 | char * s_opt = NULL; |
---|
| 289 | int s_prim = 0; |
---|
[1207] | 290 | char * o_drv = getenv("ROAR_DRIVER"); |
---|
| 291 | char * o_dev = getenv("ROAR_DEVICE"); |
---|
[932] | 292 | char * o_opts = NULL; |
---|
[961] | 293 | int o_prim = 0; |
---|
[1145] | 294 | int o_count = 0; |
---|
[450] | 295 | char * sock_grp = ROAR_DEFAULT_SOCKGRP; |
---|
[444] | 296 | char * sock_user = NULL; |
---|
[517] | 297 | int sock_type = ROAR_SOCKET_TYPE_UNKNOWN; |
---|
[1486] | 298 | #ifdef ROAR_HAVE_CHROOT |
---|
[444] | 299 | char * chrootdir = NULL; |
---|
[1486] | 300 | #endif |
---|
| 301 | #if defined(ROAR_HAVE_SETGID) || defined(ROAR_HAVE_SETUID) |
---|
[444] | 302 | int setids = 0; |
---|
[1486] | 303 | #endif |
---|
| 304 | #ifdef ROAR_HAVE_UNIX |
---|
[1011] | 305 | char * env_roar_proxy_backup; |
---|
[1486] | 306 | #endif |
---|
| 307 | #if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX) |
---|
[446] | 308 | struct group * grp = NULL; |
---|
[1486] | 309 | #endif |
---|
| 310 | #if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX) |
---|
[446] | 311 | struct passwd * pwd = NULL; |
---|
[1486] | 312 | #endif |
---|
| 313 | #ifdef ROAR_HAVE_GETSERVBYNAME |
---|
[446] | 314 | struct servent * serv = NULL; |
---|
[1486] | 315 | #endif |
---|
[0] | 316 | DRIVER_USERDATA_T drvinst; |
---|
[39] | 317 | struct roar_client * self = NULL; |
---|
[508] | 318 | #ifdef ROAR_HAVE_LIBDNET |
---|
| 319 | char decnethost[80]; |
---|
| 320 | #endif |
---|
[0] | 321 | |
---|
| 322 | g_listen_socket = -1; |
---|
| 323 | g_standby = 0; |
---|
[920] | 324 | g_autostandby = 0; |
---|
[982] | 325 | alive = 1; |
---|
[1155] | 326 | g_no_listen = 0; |
---|
[0] | 327 | |
---|
| 328 | sa.bits = ROAR_BITS_DEFAULT; |
---|
| 329 | sa.channels = ROAR_CHANNELS_DEFAULT; |
---|
| 330 | sa.rate = ROAR_RATE_DEFAULT; |
---|
| 331 | sa.codec = ROAR_CODEC_DEFAULT; |
---|
| 332 | |
---|
| 333 | g_sa = &sa; |
---|
| 334 | |
---|
[60] | 335 | |
---|
[1486] | 336 | if ( getuid() != 0 && getenv("HOME") != NULL ) { |
---|
| 337 | snprintf(user_sock, 79, "%s/%s", (char*)getenv("HOME"), ROAR_DEFAULT_SOCK_USER); |
---|
[60] | 338 | server = user_sock; |
---|
| 339 | } |
---|
| 340 | |
---|
[279] | 341 | if ( getenv("ROAR_SERVER") != NULL ) |
---|
| 342 | server = getenv("ROAR_SERVER"); |
---|
| 343 | |
---|
[63] | 344 | if ( clients_init() == -1 ) { |
---|
| 345 | ROAR_ERR("Can not init clients!"); |
---|
| 346 | return 1; |
---|
| 347 | } |
---|
| 348 | |
---|
| 349 | if ( streams_init() == -1 ) { |
---|
| 350 | ROAR_ERR("Can not init streams!"); |
---|
| 351 | return 1; |
---|
| 352 | } |
---|
| 353 | |
---|
[64] | 354 | if ( (g_self_client = clients_new()) == -1 ) { |
---|
| 355 | ROAR_ERR("Can not create self client!"); |
---|
| 356 | return 1; |
---|
| 357 | } |
---|
| 358 | |
---|
[0] | 359 | if ( sources_init() == -1 ) { |
---|
| 360 | ROAR_ERR("Can not init sources!"); |
---|
| 361 | return 1; |
---|
| 362 | } |
---|
| 363 | |
---|
[64] | 364 | if ( (sources_set_client(g_self_client)) == -1 ) { |
---|
| 365 | ROAR_ERR("Can not init set source client!"); |
---|
| 366 | return 1; |
---|
| 367 | } |
---|
| 368 | |
---|
[0] | 369 | for (i = 1; i < argc; i++) { |
---|
| 370 | k = argv[i]; |
---|
| 371 | |
---|
| 372 | if ( strcmp(k, "-h") == 0 || strcmp(k, "--help") == 0 ) { |
---|
| 373 | usage(); |
---|
| 374 | return 0; |
---|
| 375 | |
---|
[579] | 376 | } else if ( strcmp(k, "--restart") == 0 ) { |
---|
| 377 | if ( restart_server(server) == -1 ) { |
---|
| 378 | ROAR_WARN("Can not terminate old server (not running at %s?), tring to continue anyway", server); |
---|
| 379 | } |
---|
| 380 | |
---|
[775] | 381 | } else if ( strcmp(k, "--demon") == 0 || strcmp(k, "--daemon") == 0 ) { |
---|
[1486] | 382 | #ifdef ROAR_HAVE_FORK |
---|
[775] | 383 | daemon = 1; |
---|
[1486] | 384 | #else |
---|
| 385 | ROAR_ERR("--daemon not supported"); |
---|
| 386 | #endif |
---|
[71] | 387 | } else if ( strcmp(k, "--terminate") == 0 ) { |
---|
| 388 | g_terminate = 1; |
---|
[905] | 389 | } else if ( strcmp(k, "--sysclocksync") == 0 ) { |
---|
| 390 | sysclocksync = 1000; |
---|
[275] | 391 | } else if ( strcmp(k, "--realtime") == 0 ) { |
---|
[276] | 392 | realtime++; |
---|
[444] | 393 | } else if ( strcmp(k, "--chroot") == 0 ) { |
---|
[1486] | 394 | #ifdef ROAR_HAVE_CHROOT |
---|
[444] | 395 | chrootdir = argv[++i]; |
---|
[1486] | 396 | #else |
---|
| 397 | ROAR_ERR("--chroot not supported"); |
---|
| 398 | i++; |
---|
| 399 | #endif |
---|
[444] | 400 | } else if ( strcmp(k, "--setgid") == 0 ) { |
---|
[1486] | 401 | #ifdef ROAR_HAVE_SETGID |
---|
[444] | 402 | setids |= R_SETGID; |
---|
[1486] | 403 | #else |
---|
| 404 | ROAR_ERR("--setgid not supported"); |
---|
| 405 | #endif |
---|
[444] | 406 | } else if ( strcmp(k, "--setuid") == 0 ) { |
---|
[1486] | 407 | #ifdef ROAR_HAVE_SETUID |
---|
[444] | 408 | setids |= R_SETUID; |
---|
[1486] | 409 | #else |
---|
| 410 | ROAR_ERR("--setuid not supported"); |
---|
| 411 | #endif |
---|
[68] | 412 | |
---|
[280] | 413 | } else if ( strcmp(k, "--list-cf") == 0 ) { |
---|
| 414 | print_codecfilterlist(); |
---|
| 415 | return 0; |
---|
| 416 | |
---|
[0] | 417 | } else if ( strcmp(k, "-R") == 0 || strcmp(k, "--rate") == 0 ) { |
---|
| 418 | sa.rate = atoi(argv[++i]); |
---|
| 419 | } else if ( strcmp(k, "-B") == 0 || strcmp(k, "--bits") == 0 ) { |
---|
| 420 | sa.bits = atoi(argv[++i]); |
---|
| 421 | } else if ( strcmp(k, "-C") == 0 || strcmp(k, "--chans") == 0 ) { |
---|
| 422 | sa.channels = atoi(argv[++i]); |
---|
| 423 | |
---|
| 424 | } else if ( strcmp(k, "-d") == 0 || strcmp(k, "--driver") == 0 ) { |
---|
| 425 | driver = argv[++i]; |
---|
| 426 | if ( strcmp(driver, "list") == 0 ) { |
---|
[974] | 427 | ROAR_WARN("The option is obsolete, use --list-driver!"); |
---|
[0] | 428 | print_driverlist(); |
---|
[973] | 429 | return 0; |
---|
[0] | 430 | } |
---|
| 431 | } else if ( strcmp(k, "-D") == 0 || strcmp(k, "--device") == 0 ) { |
---|
| 432 | device = argv[++i]; |
---|
| 433 | } else if ( strcmp(k, "-dO") == 0 ) { |
---|
| 434 | opts = argv[++i]; |
---|
[973] | 435 | } else if ( strcmp(k, "--list-driver") == 0 ) { |
---|
| 436 | print_driverlist(); |
---|
| 437 | return 0; |
---|
[0] | 438 | |
---|
[932] | 439 | } else if ( strcmp(k, "-o") == 0 || strcmp(k, "--odriver") == 0 ) { |
---|
| 440 | o_drv = argv[++i]; |
---|
| 441 | } else if ( strcmp(k, "-O") == 0 || strcmp(k, "--odevice") == 0 ) { |
---|
| 442 | o_dev = argv[++i]; |
---|
| 443 | } else if ( strcmp(k, "-oO") == 0 ) { |
---|
| 444 | o_opts = argv[++i]; |
---|
[961] | 445 | } else if ( strcmp(k, "-oP") == 0 ) { |
---|
| 446 | o_prim = 1; |
---|
[932] | 447 | } else if ( strcmp(k, "-oN") == 0 ) { |
---|
[1145] | 448 | if ( add_output(o_drv, o_dev, o_opts, o_prim, o_count) != -1 ) |
---|
| 449 | o_count++; |
---|
| 450 | |
---|
[961] | 451 | o_drv = o_dev = o_opts = NULL; |
---|
| 452 | o_prim = 0; |
---|
[932] | 453 | |
---|
[0] | 454 | } else if ( strcmp(k, "-s") == 0 || strcmp(k, "--source") == 0 ) { |
---|
[550] | 455 | s_drv = argv[++i]; |
---|
| 456 | } else if ( strcmp(k, "-S") == 0 ) { |
---|
[1110] | 457 | s_dev = argv[++i]; |
---|
[0] | 458 | } else if ( strcmp(k, "-sO") == 0 ) { |
---|
| 459 | s_opt = argv[++i]; |
---|
| 460 | } else if ( strcmp(k, "-sC") == 0 ) { |
---|
| 461 | s_con = argv[++i]; |
---|
| 462 | } else if ( strcmp(k, "-sP") == 0 ) { |
---|
| 463 | s_prim = 1; |
---|
[1110] | 464 | } else if ( strcmp(k, "-sN") == 0 ) { |
---|
| 465 | if ( sources_add(s_drv, s_dev, s_con, s_opt, s_prim) == -1 ) { |
---|
| 466 | ROAR_ERR("main(*): adding source '%s' via '%s' failed!", s_dev, s_drv); |
---|
| 467 | } |
---|
| 468 | s_opt = s_dev = s_con = NULL; |
---|
| 469 | s_drv = "cf"; |
---|
| 470 | s_prim = 0; |
---|
[0] | 471 | |
---|
| 472 | } else if ( strcmp(k, "-p") == 0 || strcmp(k, "--port") == 0 ) { |
---|
[447] | 473 | // This is only usefull in INET not UNIX mode. |
---|
| 474 | if ( *server == '/' ) |
---|
| 475 | server = ROAR_DEFAULT_HOST; |
---|
| 476 | |
---|
[446] | 477 | errno = 0; |
---|
| 478 | if ( (port = atoi(argv[++i])) < 1 ) { |
---|
[1486] | 479 | #ifdef ROAR_HAVE_GETSERVBYNAME |
---|
[446] | 480 | if ( (serv = getservbyname(argv[i], "tcp")) == NULL ) { |
---|
| 481 | ROAR_ERR("Unknown service: %s: %s", argv[i], strerror(errno)); |
---|
| 482 | return 1; |
---|
| 483 | } |
---|
| 484 | // NOTE: we need to use ROAR_NET2HOST16() here even if s_port is of type int! |
---|
| 485 | ROAR_DBG("main(*): serv = {s_name='%s', s_aliases={...}, s_port=%i, s_proto='%s'}", |
---|
| 486 | serv->s_name, ROAR_NET2HOST16(serv->s_port), serv->s_proto); |
---|
| 487 | port = ROAR_NET2HOST16(serv->s_port); |
---|
[1486] | 488 | #else |
---|
| 489 | ROAR_ERR("invalite port number: %s", argv[i]); |
---|
| 490 | return 1; |
---|
| 491 | #endif |
---|
[446] | 492 | } |
---|
[1115] | 493 | } else if ( strcmp(k, "-b") == 0 || strcmp(k, "--bind") == 0 || strcmp(k, "--sock") == 0 ) { |
---|
[0] | 494 | server = argv[++i]; |
---|
[518] | 495 | |
---|
[573] | 496 | } else if ( strcmp(k, "-t") == 0 || strcmp(k, "--tcp") == 0 ) { |
---|
[518] | 497 | if ( sock_type != ROAR_SOCKET_TYPE_TCP && sock_type != ROAR_SOCKET_TYPE_TCP6 ) |
---|
| 498 | sock_type = ROAR_SOCKET_TYPE_TCP; |
---|
| 499 | |
---|
| 500 | if ( *server == '/' ) |
---|
| 501 | server = ROAR_DEFAULT_HOST; |
---|
| 502 | |
---|
| 503 | } else if ( strcmp(k, "-4") == 0 ) { |
---|
[517] | 504 | sock_type = ROAR_SOCKET_TYPE_TCP; |
---|
[447] | 505 | if ( *server == '/' ) |
---|
| 506 | server = ROAR_DEFAULT_HOST; |
---|
[518] | 507 | } else if ( strcmp(k, "-6") == 0 ) { |
---|
[519] | 508 | #ifdef PF_INET6 |
---|
[518] | 509 | sock_type = ROAR_SOCKET_TYPE_TCP6; |
---|
| 510 | if ( *server == '/' ) |
---|
| 511 | server = ROAR_DEFAULT_HOST; |
---|
[519] | 512 | #else |
---|
| 513 | ROAR_ERR("No IPv6 support compiled in!"); |
---|
| 514 | return 1; |
---|
| 515 | #endif |
---|
[518] | 516 | |
---|
[573] | 517 | } else if ( strcmp(k, "-u") == 0 || strcmp(k, "--unix") == 0 ) { |
---|
[62] | 518 | // ignore this case as it is the default behavor. |
---|
[517] | 519 | sock_type = ROAR_SOCKET_TYPE_UNIX; |
---|
[518] | 520 | |
---|
[573] | 521 | } else if ( strcmp(k, "-n") == 0 || strcmp(k, "--decnet") == 0 ) { |
---|
[508] | 522 | #ifdef ROAR_HAVE_LIBDNET |
---|
| 523 | port = ROAR_DEFAULT_NUM; |
---|
| 524 | strcpy(decnethost, ROAR_DEFAULT_LISTEN_OBJECT); |
---|
| 525 | server = decnethost; |
---|
[517] | 526 | sock_type = ROAR_SOCKET_TYPE_DECNET; |
---|
[508] | 527 | #else |
---|
| 528 | ROAR_ERR("No DECnet support compiled in!"); |
---|
| 529 | return 1; |
---|
| 530 | #endif |
---|
[518] | 531 | |
---|
[60] | 532 | } else if ( strcmp(k, "-G") == 0 ) { |
---|
[444] | 533 | sock_grp = argv[++i]; |
---|
| 534 | } else if ( strcmp(k, "-U") == 0 ) { |
---|
| 535 | sock_user = argv[++i]; |
---|
[0] | 536 | |
---|
[68] | 537 | } else if ( strcmp(k, "--no-listen") == 0 ) { |
---|
[548] | 538 | *server = 0; |
---|
| 539 | g_terminate = 1; |
---|
[1155] | 540 | g_no_listen = 1; |
---|
[68] | 541 | } else if ( strcmp(k, "--client-fh") == 0 ) { |
---|
| 542 | if ( clients_set_fh(clients_new(), atoi(argv[++i])) == -1 ) { |
---|
| 543 | ROAR_ERR("main(*): Can not set client's fh"); |
---|
| 544 | return 1; |
---|
| 545 | } |
---|
[501] | 546 | } else if ( strcmp(k, "--close-fh") == 0 ) { |
---|
[1486] | 547 | #ifdef ROAR_HAVE_IO_POSIX |
---|
[501] | 548 | close(atoi(argv[++i])); |
---|
[1486] | 549 | #else |
---|
| 550 | i++; |
---|
| 551 | ROAR_WARN("can not close file handle %s (closing not supported)", argv[i]); |
---|
| 552 | #endif |
---|
[68] | 553 | |
---|
[920] | 554 | } else if ( strcmp(k, "--standby") == 0 ) { |
---|
| 555 | g_standby = 1; |
---|
| 556 | } else if ( strcmp(k, "--auto-standby") == 0 ) { |
---|
| 557 | g_autostandby = 1; |
---|
[0] | 558 | } else { |
---|
| 559 | usage(); |
---|
| 560 | return 1; |
---|
| 561 | } |
---|
| 562 | |
---|
| 563 | } |
---|
| 564 | |
---|
[1110] | 565 | if ( s_dev != NULL ) { |
---|
| 566 | if ( sources_add(s_drv, s_dev, s_con, s_opt, s_prim) == -1 ) { |
---|
| 567 | ROAR_ERR("main(*): adding source '%s' via '%s' failed!", s_dev, s_drv); |
---|
| 568 | } |
---|
| 569 | } |
---|
| 570 | |
---|
[1145] | 571 | add_output(o_drv, o_dev, o_opts, o_prim, o_count); |
---|
[932] | 572 | |
---|
[0] | 573 | ROAR_DBG("Server config: rate=%i, bits=%i, chans=%i", sa.rate, sa.bits, sa.channels); |
---|
| 574 | |
---|
[281] | 575 | if ( midi_init() == -1 ) |
---|
| 576 | ROAR_ERR("Can not initialize MIDI subsystem"); |
---|
| 577 | |
---|
[68] | 578 | if ( *server != 0 ) { |
---|
[517] | 579 | if ( (g_listen_socket = roar_socket_listen(sock_type, server, port)) == -1 ) { |
---|
[1486] | 580 | #ifdef ROAR_HAVE_UNIX |
---|
[286] | 581 | if ( *server == '/' ) { |
---|
[1011] | 582 | if ( (env_roar_proxy_backup = getenv("ROAR_PROXY")) != NULL ) { |
---|
| 583 | env_roar_proxy_backup = strdup(env_roar_proxy_backup); |
---|
| 584 | unsetenv("ROAR_PROXY"); |
---|
| 585 | } |
---|
[286] | 586 | if ( (i = roar_socket_connect(server, port)) != -1 ) { |
---|
| 587 | close(i); |
---|
| 588 | ROAR_ERR("Can not open listen socket!"); |
---|
| 589 | return 1; |
---|
| 590 | } else { |
---|
| 591 | unlink(server); |
---|
[517] | 592 | if ( (g_listen_socket = roar_socket_listen(sock_type, server, port)) == -1 ) { |
---|
[286] | 593 | ROAR_ERR("Can not open listen socket!"); |
---|
| 594 | return 1; |
---|
| 595 | } |
---|
| 596 | } |
---|
[1011] | 597 | if ( env_roar_proxy_backup != NULL ) { |
---|
| 598 | setenv("ROAR_PROXY", env_roar_proxy_backup, 0); |
---|
| 599 | free(env_roar_proxy_backup); |
---|
| 600 | } |
---|
[1486] | 601 | #else |
---|
| 602 | if (0) { // noop |
---|
| 603 | #endif |
---|
[286] | 604 | } else { |
---|
| 605 | ROAR_ERR("Can not open listen socket!"); |
---|
| 606 | return 1; |
---|
| 607 | } |
---|
[68] | 608 | } |
---|
[0] | 609 | |
---|
[1486] | 610 | #if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX) |
---|
[523] | 611 | if ( (grp = getgrnam(sock_grp)) == NULL ) { |
---|
| 612 | ROAR_ERR("Can not get GID for group %s: %s", sock_grp, strerror(errno)); |
---|
| 613 | } |
---|
[1486] | 614 | #endif |
---|
| 615 | #if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX) |
---|
[523] | 616 | if ( sock_user || (setids & R_SETUID) ) { |
---|
| 617 | if ( (pwd = getpwnam(sock_user)) == NULL ) { |
---|
| 618 | ROAR_ERR("Can not get UID for user %s: %s", sock_user, strerror(errno)); |
---|
| 619 | } |
---|
| 620 | } |
---|
[1486] | 621 | #endif |
---|
[523] | 622 | |
---|
[1486] | 623 | #ifdef ROAR_HAVE_IO_POSIX |
---|
[68] | 624 | if ( *server == '/' ) { |
---|
[523] | 625 | if ( grp ) { |
---|
[444] | 626 | if ( pwd ) { |
---|
| 627 | chown(server, pwd->pw_uid, grp->gr_gid); |
---|
| 628 | } else { |
---|
| 629 | chown(server, -1, grp->gr_gid); |
---|
| 630 | } |
---|
[68] | 631 | if ( getuid() == 0 ) |
---|
| 632 | chmod(server, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP); |
---|
| 633 | } |
---|
[60] | 634 | } |
---|
[1486] | 635 | #endif |
---|
[60] | 636 | } |
---|
| 637 | |
---|
[0] | 638 | if ( output_buffer_init(&sa) == -1 ) { |
---|
| 639 | ROAR_ERR("Can not init output buffer!"); |
---|
| 640 | return 1; |
---|
| 641 | } |
---|
| 642 | |
---|
[1145] | 643 | if ( driver == NULL ) { |
---|
| 644 | driver = "null"; |
---|
| 645 | } else { |
---|
| 646 | ROAR_WARN("Usage of old driver interface. use -o not -d!"); |
---|
| 647 | } |
---|
| 648 | |
---|
[0] | 649 | if ( driver_open(&drvinst, &drvid, driver, device, &sa) == -1 ) { |
---|
| 650 | ROAR_ERR("Can not open output driver!"); |
---|
| 651 | return 1; |
---|
| 652 | } |
---|
| 653 | |
---|
[44] | 654 | if ( samples_init() == -1 ) { |
---|
| 655 | ROAR_ERR("Can not init samples!"); |
---|
| 656 | return 1; |
---|
| 657 | } |
---|
| 658 | |
---|
| 659 | |
---|
[1486] | 660 | // we should handle this on microcontrollers, too. |
---|
| 661 | #if !defined(ROAR_TARGET_MICROCONTROLLER) |
---|
[0] | 662 | signal(SIGINT, on_sig_int); |
---|
[285] | 663 | signal(SIGCHLD, on_sig_chld); |
---|
[0] | 664 | signal(SIGPIPE, SIG_IGN); // ignore broken pipes |
---|
[1486] | 665 | #endif |
---|
[0] | 666 | |
---|
[275] | 667 | if ( realtime ) { |
---|
[278] | 668 | #ifdef DEBUG |
---|
| 669 | ROAR_WARN("compiled with -DDEBUG but realtime is enabled: for real realtime support compiel without -DDEBUG"); |
---|
| 670 | #endif |
---|
| 671 | |
---|
[1486] | 672 | #ifdef ROAR_HAVE_NICE |
---|
[275] | 673 | errno = 0; |
---|
[276] | 674 | nice(-5*realtime); // -5 for each --realtime |
---|
[1486] | 675 | if ( errno ) { |
---|
| 676 | ROAR_WARN("Can not decrease nice value by %i: %s", 5*realtime, strerror(errno)); |
---|
| 677 | } |
---|
| 678 | #else |
---|
| 679 | ROAR_WARN("Can not decrease nice value by %i: %s", 5*realtime, strerror(errno)); |
---|
| 680 | #endif |
---|
[277] | 681 | /* |
---|
[276] | 682 | #ifdef __linux__ |
---|
[277] | 683 | if ( ioprio_set(IOPRIO_WHO_PROCESS, getpid(), IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 0)) == -1 ) |
---|
| 684 | ROAR_WARN("Can not set io priority: %s", strerror(errno)); |
---|
[276] | 685 | #endif |
---|
[277] | 686 | */ |
---|
[275] | 687 | } |
---|
| 688 | |
---|
[1486] | 689 | #ifdef ROAR_HAVE_SETGID |
---|
[444] | 690 | if ( setids & R_SETGID ) { |
---|
| 691 | if ( setgroups(0, (const gid_t *) NULL) == -1 ) { |
---|
| 692 | ROAR_ERR("Can not clear supplementary group IDs: %s", strerror(errno)); |
---|
| 693 | } |
---|
[523] | 694 | if ( !grp || setgid(grp->gr_gid) == -1 ) { |
---|
[444] | 695 | ROAR_ERR("Can not set GroupID: %s", strerror(errno)); |
---|
| 696 | } |
---|
| 697 | } |
---|
[1486] | 698 | #endif |
---|
[444] | 699 | |
---|
[0] | 700 | |
---|
[39] | 701 | clients_set_pid(g_self_client, getpid()); |
---|
[440] | 702 | clients_set_uid(g_self_client, getuid()); |
---|
| 703 | clients_set_gid(g_self_client, getgid()); |
---|
[39] | 704 | clients_get(g_self_client, &self); |
---|
[37] | 705 | |
---|
[39] | 706 | if ( self == NULL ) { |
---|
| 707 | ROAR_ERR("Can not get self client!"); |
---|
| 708 | return 1; |
---|
| 709 | } |
---|
| 710 | |
---|
[775] | 711 | strcpy(self->name, "RoarAudio daemon internal"); |
---|
[68] | 712 | |
---|
[1486] | 713 | #ifdef ROAR_HAVE_FORK |
---|
[775] | 714 | if ( daemon ) { |
---|
[68] | 715 | close(ROAR_STDIN ); |
---|
| 716 | close(ROAR_STDOUT); |
---|
| 717 | close(ROAR_STDERR); |
---|
[422] | 718 | setsid(); |
---|
[68] | 719 | if ( fork() ) |
---|
[1486] | 720 | ROAR_U_EXIT(0); |
---|
[1046] | 721 | clients_set_pid(g_self_client, getpid()); // reset pid as it changed |
---|
[68] | 722 | } |
---|
[1486] | 723 | #endif |
---|
[68] | 724 | |
---|
[1486] | 725 | #ifdef ROAR_HAVE_CHROOT |
---|
[444] | 726 | if (chrootdir) { |
---|
| 727 | if ( chroot(chrootdir) == -1 ) { |
---|
| 728 | ROAR_ERR("Can not chroot to %s: %s", chrootdir, strerror(errno)); |
---|
| 729 | return 2; |
---|
| 730 | } |
---|
| 731 | if ( chdir("/") == -1 ) { |
---|
| 732 | ROAR_ERR("Can not chdir to /: %s", strerror(errno)); |
---|
| 733 | return 2; |
---|
| 734 | } |
---|
| 735 | } |
---|
[1486] | 736 | #endif |
---|
[444] | 737 | |
---|
[1486] | 738 | #ifdef ROAR_HAVE_SETUID |
---|
[444] | 739 | if ( setids & R_SETUID ) { |
---|
| 740 | if ( !pwd || setuid(pwd->pw_uid) == -1 ) { |
---|
| 741 | ROAR_ERR("Can not set UserID: %s", strerror(errno)); |
---|
| 742 | return 3; |
---|
| 743 | } |
---|
| 744 | clients_set_uid(g_self_client, getuid()); |
---|
| 745 | } |
---|
[1486] | 746 | #endif |
---|
[444] | 747 | |
---|
[0] | 748 | // start main loop... |
---|
[905] | 749 | main_loop(drvid, drvinst, &sa, sysclocksync); |
---|
[0] | 750 | |
---|
| 751 | // clean up. |
---|
| 752 | clean_quit_prep(); |
---|
| 753 | driver_close(drvinst, drvid); |
---|
| 754 | output_buffer_free(); |
---|
| 755 | |
---|
| 756 | return 0; |
---|
| 757 | } |
---|
| 758 | |
---|
[574] | 759 | void cleanup_listen_socket (int terminate) { |
---|
[580] | 760 | |
---|
| 761 | if ( g_listen_socket != -1 ) { |
---|
[1486] | 762 | #ifdef ROAR_HAVE_IO_POSIX |
---|
[580] | 763 | close(g_listen_socket); |
---|
[1486] | 764 | #endif // #else is useless because we are in void context. |
---|
[60] | 765 | |
---|
[580] | 766 | g_listen_socket = -1; |
---|
[576] | 767 | |
---|
[1486] | 768 | #ifdef ROAR_HAVE_UNIX |
---|
[580] | 769 | if ( *server == '/' ) |
---|
| 770 | unlink(server); |
---|
[1486] | 771 | #endif |
---|
[580] | 772 | } |
---|
[60] | 773 | |
---|
[574] | 774 | if ( terminate ) |
---|
| 775 | g_terminate = 1; |
---|
| 776 | } |
---|
| 777 | |
---|
| 778 | void clean_quit_prep (void) { |
---|
| 779 | cleanup_listen_socket(0); |
---|
[60] | 780 | |
---|
[0] | 781 | sources_free(); |
---|
| 782 | streams_free(); |
---|
| 783 | clients_free(); |
---|
[282] | 784 | midi_cb_stop(); // stop console beep |
---|
[281] | 785 | midi_free(); |
---|
[0] | 786 | } |
---|
| 787 | |
---|
| 788 | void clean_quit (void) { |
---|
| 789 | clean_quit_prep(); |
---|
| 790 | // driver_close(drvinst, drvid); |
---|
| 791 | // output_buffer_free(); |
---|
| 792 | exit(0); |
---|
| 793 | } |
---|
| 794 | |
---|
| 795 | //ll |
---|