[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 | |
---|
[1494] | 27 | #ifdef ROAR_SUPPORT_LISTEN |
---|
[60] | 28 | char * server = ROAR_DEFAULT_SOCK_GLOBAL; // global server address |
---|
[1494] | 29 | #endif |
---|
[60] | 30 | |
---|
[2104] | 31 | #if defined(ROAR_HAVE_IO_POSIX) && defined(ROAR_HAVE_FS_POSIX) |
---|
| 32 | #define SUPPORT_PIDFILE |
---|
| 33 | char * pidfile = NULL; |
---|
| 34 | #endif |
---|
| 35 | |
---|
[1503] | 36 | #ifdef ROAR_HAVE_MAIN_ARGS |
---|
[0] | 37 | void usage (void) { |
---|
| 38 | printf("Usage: roard [OPTIONS]...\n\n"); |
---|
| 39 | |
---|
[68] | 40 | printf("Misc Options:\n\n"); |
---|
| 41 | printf( |
---|
[775] | 42 | " --daemon - Bring the server into background after init\n" |
---|
[71] | 43 | " --terminate - Terminate after last client quited\n" |
---|
[2109] | 44 | " --start - No op parameter (starting roard is default operation)\n" |
---|
[274] | 45 | " --restart - Trys to stop an old instance and start a new with new settings\n" |
---|
[2109] | 46 | " --stop - Stops a running roard (provide --pidfile!)\n" |
---|
| 47 | " --shutdown - Terminates a running roard (provide --pidfile!)\n" |
---|
[276] | 48 | " --realtime - Trys to get realtime priority,\n" |
---|
| 49 | " give multible times for being more realtime\n" |
---|
[444] | 50 | " --chroot DIR - chroots to the given dir\n" |
---|
| 51 | " --setgid - GroupID to the audio group as specified via -G\n" |
---|
| 52 | " --setuid - UserID to the audio user as specified via -U\n" |
---|
[905] | 53 | " --sysclocksync - calculate exact sample rate using the system clock\n" |
---|
[2017] | 54 | " --location LOC - Set lion readable location of server\n" |
---|
[2104] | 55 | #ifdef SUPPORT_PIDFILE |
---|
| 56 | " --pidfile PIDFILE - Write a pidfile at PIDFILE\n" |
---|
| 57 | #endif |
---|
[68] | 58 | ); |
---|
| 59 | |
---|
| 60 | printf("\nAudio Options:\n\n"); |
---|
[0] | 61 | printf( |
---|
| 62 | " -R --rate RATE - Set server rate\n" |
---|
| 63 | " -B --bits BITS - Set server bits\n" |
---|
| 64 | " -C --chans CHANNELS - Set server channels\n" |
---|
| 65 | ); |
---|
| 66 | |
---|
[1923] | 67 | printf("\nDriver Options: (obsolete, do not use, Use Ouput Options)\n\n"); |
---|
[974] | 68 | printf(" -d --driver DRV - Set the driver (default: %s)\n", ROAR_DRIVER_DEFAULT); |
---|
[0] | 69 | printf(" -D --device DEV - Set the device\n"); |
---|
| 70 | printf(" -dO OPTS - Set output options\n"); |
---|
[973] | 71 | printf(" --list-driver - List all drivers\n"); |
---|
[0] | 72 | |
---|
[932] | 73 | printf("\nOutput Options:\n\n"); |
---|
[974] | 74 | printf(" -o --odriver DRV - Set the driver, use '--list-driver' to get a list\n"); |
---|
[932] | 75 | printf(" -O --odevice DEV - Set the device\n"); |
---|
| 76 | printf(" -oO OPTS - Set output options\n"); |
---|
| 77 | printf(" -oN - Adds another output\n"); |
---|
[961] | 78 | printf(" -oP - Mark output as primary\n"); |
---|
[932] | 79 | |
---|
[0] | 80 | printf("\nSource Options:\n\n"); |
---|
| 81 | printf(" -s --source DRV - Use DRV as input driver\n" |
---|
| 82 | " -S DEV - Use DEV as input device\n" |
---|
| 83 | " -sO OPTS - Use OPTS as input options\n" |
---|
[1512] | 84 | " -sN - Adds another source\n" |
---|
[0] | 85 | " -sP - Make souce as primary\n" |
---|
| 86 | ); |
---|
[2270] | 87 | printf(" --list-sources - List all sources\n"); |
---|
[0] | 88 | |
---|
[280] | 89 | printf("\nCodec Filter Options:\n\n"); |
---|
| 90 | printf(" --list-cf - List all codec filter\n" |
---|
| 91 | ); |
---|
[0] | 92 | |
---|
[1923] | 93 | printf("\nMIDI Options:\n\n"); |
---|
[1924] | 94 | printf(" --midi-no-console - Disable console based MIDI synth\n" |
---|
| 95 | " --midi-console DEV - Set device for MIDI console\n" |
---|
| 96 | ); |
---|
[1923] | 97 | |
---|
| 98 | printf("\nLight Control Options:\n\n"); |
---|
| 99 | printf(" --light-channels NUM - Sets the number of channels for Light control (default: %i)\n", |
---|
| 100 | LIGHT_CHANNELS_DEFAULT |
---|
| 101 | ); |
---|
| 102 | |
---|
[0] | 103 | printf("\nServer Options:\n\n"); |
---|
| 104 | printf(" -t --tcp - Use TCP listen socket\n" |
---|
| 105 | " -u --unix - Use UNIX Domain listen socket (default)\n" |
---|
[508] | 106 | #ifdef ROAR_HAVE_LIBDNET |
---|
| 107 | " -n --decnet - use DECnet listen socket\n" |
---|
| 108 | #endif |
---|
[518] | 109 | " -4 - Use IPv4 connections (implies -t)\n" |
---|
| 110 | #ifdef PF_INET6 |
---|
| 111 | " -6 - Use IPv6 connections (implies -t)\n" |
---|
| 112 | #endif |
---|
| 113 | #ifdef IPV6_ADDRFORM |
---|
| 114 | " -64 - Try to downgrade sockets from IPv6 into IPv4,\n" |
---|
| 115 | " this is normaly not usefull.\n" |
---|
| 116 | #endif |
---|
[0] | 117 | " -p --port - TCP Port to bind to\n" |
---|
| 118 | " -b --bind - IP/Hostname to bind to\n" |
---|
[1115] | 119 | " --sock - Filename for UNIX Domain Socket\n" |
---|
[1993] | 120 | #ifdef ROAR_HAVE_LIBSLP |
---|
| 121 | " --slp - Enable OpenSLP support\n" |
---|
| 122 | #endif |
---|
[450] | 123 | " -G GROUP - Sets the group for the UNIX Domain Socket, (default: %s)\n" |
---|
[60] | 124 | " You need the permittions to change the GID\n" |
---|
[444] | 125 | " -U USER - Sets the user for the UNIX Domain Socket, (default: do not set)\n" |
---|
| 126 | " You need the permittions to change the UID (normaly only root has)\n" |
---|
[548] | 127 | " --no-listen - Do not listen for new clients\n" |
---|
| 128 | " (only usefull for relaing, impleys --terminate)\n" |
---|
[274] | 129 | " --client-fh - Comunicate with a client over this handle\n" |
---|
[501] | 130 | " (only usefull for relaing)\n" |
---|
[920] | 131 | " --close-fh - Closes the given fh\n" |
---|
| 132 | " --standby - Start in standby state\n" |
---|
| 133 | " --auto-standby - Automatical goes into standby if there are no streams\n", |
---|
[450] | 134 | ROAR_DEFAULT_SOCKGRP |
---|
[0] | 135 | ); |
---|
| 136 | // printf("\n Options:\n\n"); |
---|
| 137 | printf("\n"); |
---|
| 138 | } |
---|
[1503] | 139 | #endif |
---|
[0] | 140 | |
---|
[2109] | 141 | int restart_server (char * server, int terminate) { |
---|
[579] | 142 | struct roar_connection con; |
---|
[2111] | 143 | #ifdef ROAR_HAVE_KILL |
---|
| 144 | char buf[80]; |
---|
| 145 | ssize_t l; |
---|
| 146 | struct roar_vio_calls fh; |
---|
| 147 | pid_t pid; |
---|
| 148 | int ok; |
---|
| 149 | |
---|
| 150 | if ( pidfile != NULL ) { |
---|
| 151 | if ( roar_vio_open_file(&fh, pidfile, O_RDONLY, 0644) == -1 ) { |
---|
| 152 | ROAR_WARN("restart_server(*): Can not read pidfile: %s", pidfile); |
---|
| 153 | } else { |
---|
| 154 | l = roar_vio_read(&fh, buf, 80); |
---|
| 155 | roar_vio_close(&fh); |
---|
| 156 | if ( l > 0 ) { |
---|
| 157 | buf[l-1] = 0; |
---|
| 158 | buf[79] = 0; |
---|
| 159 | pid = atoi(buf); |
---|
| 160 | if ( terminate ) { |
---|
[2113] | 161 | ok = kill(pid, SIGUSR1); |
---|
[2111] | 162 | } else { |
---|
| 163 | ok = kill(pid, SIGINT); |
---|
| 164 | } |
---|
| 165 | if ( ok == 0 ) { |
---|
| 166 | return 0; |
---|
| 167 | } else { |
---|
| 168 | ROAR_WARN("restart_server(*): Can not kill roard by pidfile"); |
---|
| 169 | } |
---|
| 170 | } else { |
---|
| 171 | ROAR_WARN("restart_server(*): Can not find a PID in the pidfile"); |
---|
| 172 | } |
---|
| 173 | } |
---|
| 174 | } |
---|
| 175 | #endif |
---|
[2109] | 176 | |
---|
[579] | 177 | if ( roar_connect(&con, server) == -1 ) { |
---|
| 178 | return -1; |
---|
| 179 | } |
---|
| 180 | |
---|
[2109] | 181 | if ( roar_terminate(&con, terminate) == -1 ) { |
---|
[579] | 182 | return -1; |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | return roar_disconnect(&con); |
---|
| 186 | } |
---|
| 187 | |
---|
[444] | 188 | #define R_SETUID 1 |
---|
| 189 | #define R_SETGID 2 |
---|
| 190 | |
---|
[1609] | 191 | int init_config (void) { |
---|
| 192 | int i; |
---|
| 193 | |
---|
| 194 | memset(g_config, 0, sizeof(struct roard_config)); |
---|
| 195 | |
---|
| 196 | for (i = 0; i < ROAR_DIR_DIRIDS; i++) { |
---|
| 197 | g_config->streams[i].mixer_channels = 1; |
---|
| 198 | g_config->streams[i].mixer.rpg_mul = 1; |
---|
| 199 | g_config->streams[i].mixer.rpg_div = 1; |
---|
| 200 | g_config->streams[i].mixer.scale = 65535; |
---|
| 201 | g_config->streams[i].mixer.mixer[0] = g_config->streams[i].mixer.scale; |
---|
| 202 | } |
---|
| 203 | |
---|
[2264] | 204 | g_config->streams[ROAR_DIR_FILTER ].flags = ROAR_FLAG_SYNC; |
---|
[1899] | 205 | g_config->streams[ROAR_DIR_MIDI_OUT].flags = ROAR_FLAG_SYNC; |
---|
[2160] | 206 | g_config->streams[ROAR_DIR_BIDIR ].flags = ROAR_FLAG_ANTIECHO; |
---|
[1899] | 207 | |
---|
[2017] | 208 | g_config->location = "***default***"; |
---|
| 209 | |
---|
[1609] | 210 | return 0; |
---|
| 211 | } |
---|
| 212 | |
---|
[1145] | 213 | int add_output (char * drv, char * dev, char * opts, int prim, int count) { |
---|
[933] | 214 | int stream; |
---|
| 215 | struct roar_stream * s; |
---|
| 216 | struct roar_stream_server * ss; |
---|
[938] | 217 | char * k, * v; |
---|
[1208] | 218 | #ifdef ROAR_DRIVER_CODEC |
---|
| 219 | char * to_free = NULL; |
---|
| 220 | #endif |
---|
[938] | 221 | int codec; |
---|
[1926] | 222 | int sync = 0, f_mmap = 0; |
---|
[1522] | 223 | int32_t blocks = -1, blocksize = -1; |
---|
[1920] | 224 | int dir = ROAR_DIR_OUTPUT; |
---|
[1991] | 225 | int error = 0; |
---|
| 226 | // DMX: |
---|
| 227 | int32_t channel = -1; |
---|
| 228 | int32_t universe = -1; |
---|
| 229 | uint16_t tu16; |
---|
[2085] | 230 | float q = -32e6; |
---|
[933] | 231 | |
---|
[938] | 232 | ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = ?", drv, dev, opts); |
---|
[933] | 233 | |
---|
[1145] | 234 | if ( drv == NULL && count == 0 ) { |
---|
| 235 | drv = ROAR_DRIVER_DEFAULT; |
---|
| 236 | prim = 1; |
---|
| 237 | sync = 1; |
---|
[1208] | 238 | |
---|
| 239 | #ifdef ROAR_DRIVER_CODEC |
---|
| 240 | if ( opts == NULL ) { |
---|
| 241 | opts = to_free = strdup("codec=" ROAR_DRIVER_CODEC); |
---|
| 242 | } |
---|
| 243 | #endif |
---|
[1145] | 244 | } |
---|
| 245 | |
---|
[1227] | 246 | if ( opts == NULL && count == 0 ) { |
---|
| 247 | sync = 1; |
---|
| 248 | prim = 1; // if ( prim == 0 ) prim = 1; -> prim allways = 1 |
---|
| 249 | } |
---|
| 250 | |
---|
[933] | 251 | if ( (stream = streams_new()) == -1 ) { |
---|
[938] | 252 | ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = -1", drv, dev, opts); |
---|
[982] | 253 | if ( prim ) alive = 0; |
---|
[933] | 254 | return -1; |
---|
| 255 | } |
---|
| 256 | |
---|
| 257 | streams_get(stream, &ss); |
---|
| 258 | s = ROAR_STREAM(ss); |
---|
| 259 | |
---|
| 260 | memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info)); |
---|
| 261 | |
---|
| 262 | s->pos_rel_id = -1; |
---|
| 263 | // s->info.codec = codec; |
---|
| 264 | |
---|
[938] | 265 | codec = s->info.codec; |
---|
| 266 | |
---|
| 267 | k = strtok(opts, ","); |
---|
| 268 | while (k != NULL) { |
---|
| 269 | // ROAR_WARN("add_output(*): opts: %s", k); |
---|
| 270 | |
---|
| 271 | if ( (v = strstr(k, "=")) != NULL ) { |
---|
| 272 | *v++ = 0; |
---|
| 273 | } |
---|
| 274 | |
---|
| 275 | ROAR_DBG("add_output(*): opts: k='%s', v='%s'", k, v); |
---|
| 276 | if ( strcmp(k, "rate") == 0 ) { |
---|
| 277 | s->info.rate = atoi(v); |
---|
| 278 | } else if ( strcmp(k, "channels") == 0 ) { |
---|
| 279 | s->info.channels = atoi(v); |
---|
| 280 | } else if ( strcmp(k, "bits") == 0 ) { |
---|
| 281 | s->info.bits = atoi(v); |
---|
| 282 | } else if ( strcmp(k, "codec") == 0 ) { |
---|
| 283 | if ( (codec = roar_str2codec(v)) == -1 ) { |
---|
| 284 | ROAR_ERR("add_output(*): unknown codec '%s'", v); |
---|
[1991] | 285 | error++; |
---|
[938] | 286 | } |
---|
[2085] | 287 | } else if ( strcmp(k, "q") == 0 ) { |
---|
| 288 | q = atof(v); |
---|
[1221] | 289 | } else if ( strcmp(k, "blocks") == 0 ) { |
---|
| 290 | blocks = atoi(v); |
---|
[1522] | 291 | } else if ( strcmp(k, "blocksize") == 0 ) { |
---|
| 292 | blocksize = atoi(v); |
---|
[1926] | 293 | } else if ( strcmp(k, "mmap") == 0 ) { |
---|
| 294 | f_mmap = 1; |
---|
[1919] | 295 | } else if ( strcmp(k, "subsystem") == 0 ) { |
---|
| 296 | if ( !strcasecmp(v, "wave") || !strcasecmp(v, "waveform") ) { |
---|
[1920] | 297 | dir = ROAR_DIR_OUTPUT; |
---|
[1919] | 298 | } else if ( !strcasecmp(v, "midi") ) { |
---|
| 299 | dir = ROAR_DIR_MIDI_OUT; |
---|
| 300 | } else if ( !strcasecmp(v, "light") ) { |
---|
| 301 | dir = ROAR_DIR_LIGHT_OUT; |
---|
[2234] | 302 | } else if ( !strcasecmp(v, "raw") ) { |
---|
| 303 | dir = ROAR_DIR_RAW_OUT; |
---|
[1919] | 304 | } else { |
---|
| 305 | ROAR_ERR("add_output(*): unknown subsystem '%s'", k); |
---|
[1991] | 306 | error++; |
---|
| 307 | } |
---|
| 308 | // DMX: |
---|
| 309 | } else if ( strcmp(k, "channel") == 0 ) { |
---|
| 310 | channel = atoi(v); |
---|
| 311 | if ( channel < 0 || channel > 65535 ) { |
---|
| 312 | ROAR_ERR("add_output(*): Invalide channel (not within 0..65535): %i", channel); |
---|
| 313 | channel = -1; |
---|
| 314 | error++; |
---|
[1919] | 315 | } |
---|
[1991] | 316 | } else if ( strcmp(k, "universe") == 0 ) { |
---|
| 317 | universe = atoi(v); |
---|
| 318 | if ( universe < 0 || universe > 255 ) { |
---|
| 319 | ROAR_ERR("add_output(*): Invalide universe (not within 0..255): %i", universe); |
---|
| 320 | universe = -1; |
---|
| 321 | error++; |
---|
| 322 | } |
---|
| 323 | |
---|
[2115] | 324 | } else if ( strcmp(k, "name") == 0 ) { |
---|
| 325 | if ( streams_set_name(stream, v) == -1 ) { |
---|
| 326 | ROAR_ERR("add_output(*): Can not set Stream name"); |
---|
| 327 | error++; |
---|
| 328 | } |
---|
[1991] | 329 | |
---|
[1032] | 330 | } else if ( strcmp(k, "meta") == 0 ) { |
---|
| 331 | streams_set_flag(stream, ROAR_FLAG_META); |
---|
[1117] | 332 | } else if ( strcmp(k, "sync") == 0 ) { |
---|
| 333 | sync = 1; |
---|
[1221] | 334 | } else if ( strcmp(k, "primary") == 0 ) { |
---|
| 335 | prim = 1; |
---|
| 336 | |
---|
| 337 | } else if ( strcmp(k, "cleanmeta") == 0 ) { |
---|
| 338 | streams_set_flag(stream, ROAR_FLAG_CLEANMETA); |
---|
| 339 | } else if ( strcmp(k, "autoconf") == 0 ) { |
---|
[1531] | 340 | streams_set_flag(stream, ROAR_FLAG_AUTOCONF); |
---|
[938] | 341 | } else { |
---|
| 342 | ROAR_ERR("add_output(*): unknown option '%s'", k); |
---|
[1991] | 343 | error++; |
---|
| 344 | } |
---|
| 345 | |
---|
| 346 | if ( error ) { |
---|
[938] | 347 | streams_delete(stream); |
---|
[982] | 348 | if ( prim ) alive = 0; |
---|
[1208] | 349 | #ifdef ROAR_DRIVER_CODEC |
---|
| 350 | if ( to_free != NULL ) |
---|
| 351 | free(to_free); |
---|
| 352 | #endif |
---|
[938] | 353 | return -1; |
---|
| 354 | } |
---|
| 355 | |
---|
| 356 | k = strtok(NULL, ","); |
---|
| 357 | } |
---|
| 358 | |
---|
[1919] | 359 | if ( streams_set_dir(stream, dir, 1) == -1 ) { |
---|
| 360 | streams_delete(stream); |
---|
| 361 | return -1; |
---|
| 362 | } |
---|
| 363 | |
---|
[1208] | 364 | #ifdef ROAR_DRIVER_CODEC |
---|
| 365 | if ( to_free != NULL ) |
---|
| 366 | free(to_free); |
---|
| 367 | #endif |
---|
| 368 | |
---|
[941] | 369 | if ( codec == ROAR_CODEC_ALAW || codec == ROAR_CODEC_MULAW ) |
---|
| 370 | s->info.bits = 8; // needed to open OSS driver, will be overriden by codecfilter |
---|
| 371 | |
---|
[938] | 372 | s->info.codec = codec; |
---|
| 373 | ROAR_STREAM_SERVER(s)->codec_orgi = codec; |
---|
[933] | 374 | |
---|
[1145] | 375 | if ( driver_openvio(&(ss->vio), &(ss->driver_id), drv, dev, &(s->info), -1) == -1 ) { |
---|
[1581] | 376 | ss->driver_id = -1; // don't close a driver not opened... |
---|
| 377 | memset(&(ss->vio), 0, sizeof(struct roar_vio_calls)); |
---|
[933] | 378 | streams_delete(stream); |
---|
[938] | 379 | ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = -1", drv, dev, opts); |
---|
[982] | 380 | if ( prim ) alive = 0; |
---|
[933] | 381 | return -1; |
---|
| 382 | } |
---|
| 383 | |
---|
[1528] | 384 | roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_SSTREAMID, &stream); // ignore errors here |
---|
| 385 | roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_SSTREAM, s); // ignore errors here |
---|
| 386 | |
---|
[1221] | 387 | if ( blocks != -1 ) |
---|
| 388 | roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DBLOCKS, &blocks); |
---|
| 389 | |
---|
[1522] | 390 | if ( blocksize != -1 ) |
---|
| 391 | roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DBLKSIZE, &blocksize); |
---|
| 392 | |
---|
[1991] | 393 | // TODO: we shoudld *really* check for errors here... |
---|
| 394 | if ( channel != -1 ) { |
---|
| 395 | tu16 = channel; |
---|
| 396 | roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DMXSCHAN, &tu16); |
---|
| 397 | } |
---|
| 398 | if ( universe != -1 ) { |
---|
| 399 | tu16 = universe; |
---|
| 400 | roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DMXUNIV, &tu16); |
---|
| 401 | } |
---|
| 402 | |
---|
[1156] | 403 | ROAR_DBG("add_output(*): ss->driver_id=%i", ss->driver_id); |
---|
| 404 | |
---|
[938] | 405 | streams_set_fh(stream, -1); // update some internal structures |
---|
| 406 | |
---|
[2087] | 407 | if ( q > -1e6 ) { |
---|
| 408 | ROAR_DBG("add_output(*): setting q=%f", q); |
---|
| 409 | streams_ctl(stream, ROAR_CODECFILTER_CTL_SET_Q|ROAR_STREAM_CTL_TYPE_FLOAT, &q); |
---|
| 410 | } |
---|
| 411 | |
---|
[933] | 412 | client_stream_add(g_source_client, stream); |
---|
| 413 | |
---|
[1200] | 414 | if ( prim ) { |
---|
[961] | 415 | streams_mark_primary(stream); |
---|
[1200] | 416 | s->pos_rel_id = stream; |
---|
| 417 | } |
---|
[961] | 418 | |
---|
[1120] | 419 | if ( sync ) { |
---|
[1117] | 420 | streams_set_flag(stream, ROAR_FLAG_SYNC); |
---|
[1120] | 421 | } else { |
---|
| 422 | streams_reset_flag(stream, ROAR_FLAG_SYNC); |
---|
| 423 | } |
---|
[1117] | 424 | |
---|
[1926] | 425 | if ( f_mmap ) |
---|
| 426 | streams_set_flag(stream, ROAR_FLAG_MMAP); |
---|
| 427 | |
---|
[933] | 428 | return 0; |
---|
[932] | 429 | } |
---|
| 430 | |
---|
[1993] | 431 | |
---|
| 432 | // SLP: |
---|
| 433 | void register_slp_callback(SLPHandle hslp, SLPError errcode, void * cookie) { |
---|
| 434 | /* return the error code in the cookie */ |
---|
| 435 | *(SLPError*)cookie = errcode; |
---|
| 436 | } |
---|
| 437 | |
---|
| 438 | int register_slp (int unreg, char * sockname) { |
---|
| 439 | #ifdef ROAR_HAVE_LIBSLP |
---|
| 440 | static int regged = 0; |
---|
| 441 | static char * sn = NULL; |
---|
| 442 | SLPError err; |
---|
| 443 | SLPError callbackerr; |
---|
| 444 | SLPHandle hslp; |
---|
| 445 | char addr[1024]; |
---|
| 446 | char attr[1024] = ""; |
---|
[2017] | 447 | char * location; |
---|
[1993] | 448 | |
---|
| 449 | if ( sockname != NULL ) |
---|
| 450 | sn = sockname; |
---|
| 451 | |
---|
[2016] | 452 | snprintf(addr, sizeof(addr), ROAR_SLP_URL_TYPE "://%s", sn); |
---|
[1993] | 453 | |
---|
| 454 | err = SLPOpen("en", SLP_FALSE, &hslp); |
---|
| 455 | |
---|
| 456 | if (err != SLP_OK) { |
---|
| 457 | ROAR_ERR("Error opening slp handle: Error #%i", err); |
---|
| 458 | return -1; |
---|
| 459 | } |
---|
| 460 | |
---|
| 461 | if (!unreg) { |
---|
[2017] | 462 | |
---|
| 463 | if ( SLPEscape(g_config->location, &location, SLP_FALSE) != SLP_OK ) { |
---|
| 464 | ROAR_ERR("Error using SLPEscape() on server location, really bad!"); |
---|
| 465 | SLPClose(hslp); |
---|
| 466 | return -1; |
---|
| 467 | } |
---|
| 468 | |
---|
[1995] | 469 | snprintf(attr, sizeof(attr), "(wave-rate=%i),(wave-channels=%i),(wave-bits=%i)," |
---|
[2017] | 470 | "(light-channels=%i),(location=%s)", |
---|
[1995] | 471 | g_sa->rate, g_sa->channels, g_sa->bits, |
---|
[2017] | 472 | g_light_state.channels, |
---|
| 473 | location |
---|
[1995] | 474 | ); |
---|
| 475 | |
---|
[1993] | 476 | /* Register a service with SLP */ |
---|
| 477 | err = SLPReg(hslp, |
---|
| 478 | addr, |
---|
| 479 | SLP_LIFETIME_MAXIMUM, |
---|
| 480 | 0, |
---|
| 481 | attr, |
---|
| 482 | SLP_TRUE, |
---|
| 483 | register_slp_callback, |
---|
| 484 | &callbackerr); |
---|
[1994] | 485 | regged = 1; |
---|
| 486 | } else if ( unreg && regged ) { |
---|
| 487 | err = SLPDereg(hslp, addr, register_slp_callback, &callbackerr); |
---|
| 488 | regged = 0; |
---|
| 489 | } else { |
---|
| 490 | SLPClose(hslp); |
---|
| 491 | return -1; |
---|
| 492 | } |
---|
[1993] | 493 | |
---|
[1994] | 494 | /* err may contain an error code that occurred as the slp library */ |
---|
| 495 | /* _prepared_ to make the call. */ |
---|
| 496 | if ( err != SLP_OK ) { |
---|
| 497 | ROAR_ERR("Error (de)registering service with slp: Error #%i", err); |
---|
| 498 | return -1; |
---|
| 499 | } |
---|
| 500 | |
---|
| 501 | /* callbackerr may contain an error code (that was assigned through */ |
---|
| 502 | /* the callback cookie) that occurred as slp packets were sent on */ |
---|
| 503 | /* the wire */ |
---|
| 504 | if (callbackerr != SLP_OK) { |
---|
| 505 | ROAR_ERR("Error (de)registering service with slp: Error #%i", callbackerr); |
---|
| 506 | return -1; |
---|
[1993] | 507 | } |
---|
| 508 | |
---|
| 509 | SLPClose(hslp); |
---|
| 510 | return 0; |
---|
| 511 | #else |
---|
| 512 | return -1; |
---|
| 513 | #endif |
---|
| 514 | } |
---|
| 515 | |
---|
| 516 | |
---|
| 517 | // MAIN: |
---|
| 518 | |
---|
[1503] | 519 | #ifdef ROAR_HAVE_MAIN_ARGS |
---|
[0] | 520 | int main (int argc, char * argv[]) { |
---|
[1503] | 521 | #else |
---|
| 522 | int main (void) { |
---|
| 523 | #endif |
---|
| 524 | #ifdef ROAR_HAVE_MAIN_ARGS |
---|
[0] | 525 | int i; |
---|
| 526 | char * k; |
---|
[1503] | 527 | #endif |
---|
[1494] | 528 | #ifdef ROAR_SUPPORT_LISTEN |
---|
[905] | 529 | char user_sock[80] = {0}; |
---|
[1494] | 530 | #endif |
---|
[1609] | 531 | struct roar_audio_info sa, max_sa; |
---|
| 532 | struct roard_config config; |
---|
[1486] | 533 | #ifdef ROAR_HAVE_FORK |
---|
[905] | 534 | int daemon = 0; |
---|
[1486] | 535 | #endif |
---|
[905] | 536 | int realtime = 0; |
---|
| 537 | int sysclocksync = 0; |
---|
[1207] | 538 | char * driver = NULL; |
---|
| 539 | char * device = NULL; |
---|
[1503] | 540 | #ifdef ROAR_HAVE_MAIN_ARGS |
---|
[1207] | 541 | char * opts = NULL; |
---|
[1503] | 542 | #endif |
---|
[60] | 543 | // char * server = ROAR_DEFAULT_SOCK_GLOBAL; |
---|
[1494] | 544 | #ifdef ROAR_SUPPORT_LISTEN |
---|
[1207] | 545 | int port = ROAR_DEFAULT_PORT; |
---|
[1494] | 546 | #endif |
---|
[0] | 547 | int drvid; |
---|
[550] | 548 | char * s_drv = "cf"; |
---|
[1110] | 549 | char * s_dev = NULL; |
---|
[444] | 550 | char * s_con = NULL; |
---|
| 551 | char * s_opt = NULL; |
---|
| 552 | int s_prim = 0; |
---|
[1207] | 553 | char * o_drv = getenv("ROAR_DRIVER"); |
---|
| 554 | char * o_dev = getenv("ROAR_DEVICE"); |
---|
[932] | 555 | char * o_opts = NULL; |
---|
[961] | 556 | int o_prim = 0; |
---|
[1145] | 557 | int o_count = 0; |
---|
[1923] | 558 | int light_channels = LIGHT_CHANNELS_DEFAULT; |
---|
[450] | 559 | char * sock_grp = ROAR_DEFAULT_SOCKGRP; |
---|
[444] | 560 | char * sock_user = NULL; |
---|
[1494] | 561 | #ifdef ROAR_SUPPORT_LISTEN |
---|
[517] | 562 | int sock_type = ROAR_SOCKET_TYPE_UNKNOWN; |
---|
[1494] | 563 | #endif |
---|
[1993] | 564 | #ifdef ROAR_HAVE_LIBSLP |
---|
| 565 | int reg_slp = 0; |
---|
| 566 | #endif |
---|
[1486] | 567 | #ifdef ROAR_HAVE_CHROOT |
---|
[444] | 568 | char * chrootdir = NULL; |
---|
[1486] | 569 | #endif |
---|
| 570 | #if defined(ROAR_HAVE_SETGID) || defined(ROAR_HAVE_SETUID) |
---|
[444] | 571 | int setids = 0; |
---|
[1486] | 572 | #endif |
---|
| 573 | #ifdef ROAR_HAVE_UNIX |
---|
[1011] | 574 | char * env_roar_proxy_backup; |
---|
[1486] | 575 | #endif |
---|
| 576 | #if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX) |
---|
[446] | 577 | struct group * grp = NULL; |
---|
[1486] | 578 | #endif |
---|
| 579 | #if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX) |
---|
[446] | 580 | struct passwd * pwd = NULL; |
---|
[1486] | 581 | #endif |
---|
| 582 | #ifdef ROAR_HAVE_GETSERVBYNAME |
---|
[446] | 583 | struct servent * serv = NULL; |
---|
[1486] | 584 | #endif |
---|
[0] | 585 | DRIVER_USERDATA_T drvinst; |
---|
[39] | 586 | struct roar_client * self = NULL; |
---|
[508] | 587 | #ifdef ROAR_HAVE_LIBDNET |
---|
| 588 | char decnethost[80]; |
---|
| 589 | #endif |
---|
[2104] | 590 | #ifdef SUPPORT_PIDFILE |
---|
| 591 | struct roar_vio_calls pidfile_vio; |
---|
| 592 | #endif |
---|
[0] | 593 | |
---|
| 594 | g_standby = 0; |
---|
[920] | 595 | g_autostandby = 0; |
---|
[982] | 596 | alive = 1; |
---|
[1494] | 597 | #ifdef ROAR_SUPPORT_LISTEN |
---|
[1155] | 598 | g_no_listen = 0; |
---|
[1494] | 599 | g_listen_socket = -1; |
---|
| 600 | #else |
---|
| 601 | g_terminate = 1; |
---|
| 602 | #endif |
---|
[0] | 603 | |
---|
| 604 | sa.bits = ROAR_BITS_DEFAULT; |
---|
| 605 | sa.channels = ROAR_CHANNELS_DEFAULT; |
---|
| 606 | sa.rate = ROAR_RATE_DEFAULT; |
---|
| 607 | sa.codec = ROAR_CODEC_DEFAULT; |
---|
| 608 | |
---|
[1609] | 609 | g_sa = &sa; |
---|
| 610 | g_max_sa = &max_sa; |
---|
| 611 | |
---|
| 612 | memcpy(g_max_sa, g_sa, sizeof(max_sa)); |
---|
| 613 | |
---|
| 614 | g_config = &config; |
---|
| 615 | |
---|
| 616 | if ( init_config() == -1 ) { |
---|
| 617 | ROAR_ERR("Can not init default config!"); |
---|
| 618 | return 1; |
---|
| 619 | } |
---|
[0] | 620 | |
---|
[1924] | 621 | if ( midi_init_config() == -1 ) { |
---|
| 622 | ROAR_ERR("Can not init MIDI config!"); |
---|
| 623 | return 1; |
---|
| 624 | } |
---|
[60] | 625 | |
---|
[1494] | 626 | #ifdef ROAR_SUPPORT_LISTEN |
---|
[1753] | 627 | #ifdef ROAR_HAVE_GETUID |
---|
[1486] | 628 | if ( getuid() != 0 && getenv("HOME") != NULL ) { |
---|
| 629 | snprintf(user_sock, 79, "%s/%s", (char*)getenv("HOME"), ROAR_DEFAULT_SOCK_USER); |
---|
[60] | 630 | server = user_sock; |
---|
| 631 | } |
---|
[1753] | 632 | #endif |
---|
[60] | 633 | |
---|
[279] | 634 | if ( getenv("ROAR_SERVER") != NULL ) |
---|
| 635 | server = getenv("ROAR_SERVER"); |
---|
[1494] | 636 | #endif |
---|
[279] | 637 | |
---|
[63] | 638 | if ( clients_init() == -1 ) { |
---|
| 639 | ROAR_ERR("Can not init clients!"); |
---|
| 640 | return 1; |
---|
| 641 | } |
---|
| 642 | |
---|
| 643 | if ( streams_init() == -1 ) { |
---|
| 644 | ROAR_ERR("Can not init streams!"); |
---|
| 645 | return 1; |
---|
| 646 | } |
---|
| 647 | |
---|
[64] | 648 | if ( (g_self_client = clients_new()) == -1 ) { |
---|
| 649 | ROAR_ERR("Can not create self client!"); |
---|
| 650 | return 1; |
---|
| 651 | } |
---|
| 652 | |
---|
[0] | 653 | if ( sources_init() == -1 ) { |
---|
| 654 | ROAR_ERR("Can not init sources!"); |
---|
| 655 | return 1; |
---|
| 656 | } |
---|
| 657 | |
---|
[64] | 658 | if ( (sources_set_client(g_self_client)) == -1 ) { |
---|
| 659 | ROAR_ERR("Can not init set source client!"); |
---|
| 660 | return 1; |
---|
| 661 | } |
---|
| 662 | |
---|
[1503] | 663 | #ifdef ROAR_HAVE_MAIN_ARGS |
---|
[0] | 664 | for (i = 1; i < argc; i++) { |
---|
| 665 | k = argv[i]; |
---|
| 666 | |
---|
| 667 | if ( strcmp(k, "-h") == 0 || strcmp(k, "--help") == 0 ) { |
---|
| 668 | usage(); |
---|
| 669 | return 0; |
---|
| 670 | |
---|
[2109] | 671 | } else if ( strcmp(k, "--start") == 0 ) { |
---|
| 672 | // this is a no op |
---|
[579] | 673 | } else if ( strcmp(k, "--restart") == 0 ) { |
---|
[1494] | 674 | #ifdef ROAR_SUPPORT_LISTEN |
---|
[2109] | 675 | if ( restart_server(server, 1) == -1 ) { |
---|
[579] | 676 | ROAR_WARN("Can not terminate old server (not running at %s?), tring to continue anyway", server); |
---|
| 677 | } |
---|
[1494] | 678 | #else |
---|
| 679 | ROAR_ERR("--restart not supported"); |
---|
| 680 | #endif |
---|
[2109] | 681 | } else if ( strcmp(k, "--shutdown") == 0 ) { |
---|
| 682 | #ifdef ROAR_SUPPORT_LISTEN |
---|
| 683 | if ( restart_server(server, 1) == -1 ) { |
---|
| 684 | ROAR_WARN("Can not terminate old server (not running at %s?)", server); |
---|
| 685 | return 1; |
---|
| 686 | } |
---|
| 687 | return 0; |
---|
| 688 | #else |
---|
| 689 | ROAR_ERR("--shutdown not supported"); |
---|
| 690 | return 1; |
---|
| 691 | #endif |
---|
| 692 | } else if ( strcmp(k, "--stop") == 0 ) { |
---|
| 693 | #ifdef ROAR_SUPPORT_LISTEN |
---|
[2111] | 694 | if ( restart_server(server, 0) == -1 ) { |
---|
[2109] | 695 | ROAR_WARN("Can not stop old server (not running at %s?)", server); |
---|
| 696 | return 1; |
---|
| 697 | } |
---|
| 698 | return 0; |
---|
| 699 | #else |
---|
| 700 | ROAR_ERR("--stop not supported"); |
---|
| 701 | return 1; |
---|
| 702 | #endif |
---|
| 703 | |
---|
[579] | 704 | |
---|
[775] | 705 | } else if ( strcmp(k, "--demon") == 0 || strcmp(k, "--daemon") == 0 ) { |
---|
[1486] | 706 | #ifdef ROAR_HAVE_FORK |
---|
[775] | 707 | daemon = 1; |
---|
[1486] | 708 | #else |
---|
| 709 | ROAR_ERR("--daemon not supported"); |
---|
| 710 | #endif |
---|
[71] | 711 | } else if ( strcmp(k, "--terminate") == 0 ) { |
---|
| 712 | g_terminate = 1; |
---|
[905] | 713 | } else if ( strcmp(k, "--sysclocksync") == 0 ) { |
---|
| 714 | sysclocksync = 1000; |
---|
[275] | 715 | } else if ( strcmp(k, "--realtime") == 0 ) { |
---|
[276] | 716 | realtime++; |
---|
[444] | 717 | } else if ( strcmp(k, "--chroot") == 0 ) { |
---|
[1486] | 718 | #ifdef ROAR_HAVE_CHROOT |
---|
[444] | 719 | chrootdir = argv[++i]; |
---|
[1486] | 720 | #else |
---|
| 721 | ROAR_ERR("--chroot not supported"); |
---|
| 722 | i++; |
---|
| 723 | #endif |
---|
[444] | 724 | } else if ( strcmp(k, "--setgid") == 0 ) { |
---|
[1486] | 725 | #ifdef ROAR_HAVE_SETGID |
---|
[444] | 726 | setids |= R_SETGID; |
---|
[1486] | 727 | #else |
---|
| 728 | ROAR_ERR("--setgid not supported"); |
---|
| 729 | #endif |
---|
[444] | 730 | } else if ( strcmp(k, "--setuid") == 0 ) { |
---|
[1486] | 731 | #ifdef ROAR_HAVE_SETUID |
---|
[444] | 732 | setids |= R_SETUID; |
---|
[1486] | 733 | #else |
---|
| 734 | ROAR_ERR("--setuid not supported"); |
---|
| 735 | #endif |
---|
[2017] | 736 | } else if ( strcmp(k, "--location") == 0 ) { |
---|
| 737 | g_config->location = argv[++i]; |
---|
[2104] | 738 | } else if ( strcmp(k, "--pidfile") == 0 ) { |
---|
| 739 | #ifdef SUPPORT_PIDFILE |
---|
| 740 | pidfile = argv[++i]; |
---|
| 741 | #else |
---|
| 742 | ROAR_ERR("--pidfile not supported"); |
---|
| 743 | i++; |
---|
| 744 | #endif |
---|
[68] | 745 | |
---|
[280] | 746 | } else if ( strcmp(k, "--list-cf") == 0 ) { |
---|
| 747 | print_codecfilterlist(); |
---|
| 748 | return 0; |
---|
| 749 | |
---|
[0] | 750 | } else if ( strcmp(k, "-R") == 0 || strcmp(k, "--rate") == 0 ) { |
---|
| 751 | sa.rate = atoi(argv[++i]); |
---|
| 752 | } else if ( strcmp(k, "-B") == 0 || strcmp(k, "--bits") == 0 ) { |
---|
| 753 | sa.bits = atoi(argv[++i]); |
---|
| 754 | } else if ( strcmp(k, "-C") == 0 || strcmp(k, "--chans") == 0 ) { |
---|
| 755 | sa.channels = atoi(argv[++i]); |
---|
| 756 | |
---|
| 757 | } else if ( strcmp(k, "-d") == 0 || strcmp(k, "--driver") == 0 ) { |
---|
| 758 | driver = argv[++i]; |
---|
| 759 | if ( strcmp(driver, "list") == 0 ) { |
---|
[974] | 760 | ROAR_WARN("The option is obsolete, use --list-driver!"); |
---|
[0] | 761 | print_driverlist(); |
---|
[973] | 762 | return 0; |
---|
[0] | 763 | } |
---|
| 764 | } else if ( strcmp(k, "-D") == 0 || strcmp(k, "--device") == 0 ) { |
---|
| 765 | device = argv[++i]; |
---|
| 766 | } else if ( strcmp(k, "-dO") == 0 ) { |
---|
| 767 | opts = argv[++i]; |
---|
[973] | 768 | } else if ( strcmp(k, "--list-driver") == 0 ) { |
---|
| 769 | print_driverlist(); |
---|
| 770 | return 0; |
---|
[0] | 771 | |
---|
[932] | 772 | } else if ( strcmp(k, "-o") == 0 || strcmp(k, "--odriver") == 0 ) { |
---|
| 773 | o_drv = argv[++i]; |
---|
| 774 | } else if ( strcmp(k, "-O") == 0 || strcmp(k, "--odevice") == 0 ) { |
---|
| 775 | o_dev = argv[++i]; |
---|
| 776 | } else if ( strcmp(k, "-oO") == 0 ) { |
---|
| 777 | o_opts = argv[++i]; |
---|
[961] | 778 | } else if ( strcmp(k, "-oP") == 0 ) { |
---|
| 779 | o_prim = 1; |
---|
[932] | 780 | } else if ( strcmp(k, "-oN") == 0 ) { |
---|
[1145] | 781 | if ( add_output(o_drv, o_dev, o_opts, o_prim, o_count) != -1 ) |
---|
| 782 | o_count++; |
---|
| 783 | |
---|
[961] | 784 | o_drv = o_dev = o_opts = NULL; |
---|
| 785 | o_prim = 0; |
---|
[932] | 786 | |
---|
[0] | 787 | } else if ( strcmp(k, "-s") == 0 || strcmp(k, "--source") == 0 ) { |
---|
[550] | 788 | s_drv = argv[++i]; |
---|
| 789 | } else if ( strcmp(k, "-S") == 0 ) { |
---|
[1110] | 790 | s_dev = argv[++i]; |
---|
[0] | 791 | } else if ( strcmp(k, "-sO") == 0 ) { |
---|
| 792 | s_opt = argv[++i]; |
---|
| 793 | } else if ( strcmp(k, "-sC") == 0 ) { |
---|
| 794 | s_con = argv[++i]; |
---|
| 795 | } else if ( strcmp(k, "-sP") == 0 ) { |
---|
| 796 | s_prim = 1; |
---|
[1110] | 797 | } else if ( strcmp(k, "-sN") == 0 ) { |
---|
| 798 | if ( sources_add(s_drv, s_dev, s_con, s_opt, s_prim) == -1 ) { |
---|
| 799 | ROAR_ERR("main(*): adding source '%s' via '%s' failed!", s_dev, s_drv); |
---|
| 800 | } |
---|
| 801 | s_opt = s_dev = s_con = NULL; |
---|
| 802 | s_drv = "cf"; |
---|
| 803 | s_prim = 0; |
---|
[2270] | 804 | } else if ( strcmp(k, "--list-sources") == 0 ) { |
---|
| 805 | print_sourcelist(); |
---|
| 806 | return 0; |
---|
[0] | 807 | |
---|
[1923] | 808 | } else if ( strcmp(k, "--light-channels") == 0 ) { |
---|
| 809 | light_channels = atoi(argv[++i]); |
---|
| 810 | |
---|
[1924] | 811 | } else if ( strcmp(k, "--midi-no-console") == 0 ) { |
---|
| 812 | midi_config.init_cb = 0; |
---|
| 813 | } else if ( strcmp(k, "--midi-console") == 0 ) { |
---|
| 814 | midi_config.console_dev = argv[++i]; |
---|
| 815 | |
---|
[0] | 816 | } else if ( strcmp(k, "-p") == 0 || strcmp(k, "--port") == 0 ) { |
---|
[447] | 817 | // This is only usefull in INET not UNIX mode. |
---|
[1494] | 818 | #ifdef ROAR_SUPPORT_LISTEN |
---|
[447] | 819 | if ( *server == '/' ) |
---|
| 820 | server = ROAR_DEFAULT_HOST; |
---|
| 821 | |
---|
[446] | 822 | errno = 0; |
---|
| 823 | if ( (port = atoi(argv[++i])) < 1 ) { |
---|
[1486] | 824 | #ifdef ROAR_HAVE_GETSERVBYNAME |
---|
[446] | 825 | if ( (serv = getservbyname(argv[i], "tcp")) == NULL ) { |
---|
| 826 | ROAR_ERR("Unknown service: %s: %s", argv[i], strerror(errno)); |
---|
| 827 | return 1; |
---|
| 828 | } |
---|
| 829 | // NOTE: we need to use ROAR_NET2HOST16() here even if s_port is of type int! |
---|
| 830 | ROAR_DBG("main(*): serv = {s_name='%s', s_aliases={...}, s_port=%i, s_proto='%s'}", |
---|
| 831 | serv->s_name, ROAR_NET2HOST16(serv->s_port), serv->s_proto); |
---|
| 832 | port = ROAR_NET2HOST16(serv->s_port); |
---|
[1486] | 833 | #else |
---|
| 834 | ROAR_ERR("invalite port number: %s", argv[i]); |
---|
| 835 | return 1; |
---|
| 836 | #endif |
---|
[446] | 837 | } |
---|
[1494] | 838 | #endif |
---|
[1115] | 839 | } else if ( strcmp(k, "-b") == 0 || strcmp(k, "--bind") == 0 || strcmp(k, "--sock") == 0 ) { |
---|
[1494] | 840 | #ifdef ROAR_SUPPORT_LISTEN |
---|
[0] | 841 | server = argv[++i]; |
---|
[1494] | 842 | #endif |
---|
[518] | 843 | |
---|
[573] | 844 | } else if ( strcmp(k, "-t") == 0 || strcmp(k, "--tcp") == 0 ) { |
---|
[1494] | 845 | #ifdef ROAR_SUPPORT_LISTEN |
---|
[518] | 846 | if ( sock_type != ROAR_SOCKET_TYPE_TCP && sock_type != ROAR_SOCKET_TYPE_TCP6 ) |
---|
| 847 | sock_type = ROAR_SOCKET_TYPE_TCP; |
---|
| 848 | |
---|
| 849 | if ( *server == '/' ) |
---|
| 850 | server = ROAR_DEFAULT_HOST; |
---|
[1494] | 851 | #endif |
---|
[518] | 852 | |
---|
| 853 | } else if ( strcmp(k, "-4") == 0 ) { |
---|
[1494] | 854 | #ifdef ROAR_SUPPORT_LISTEN |
---|
[517] | 855 | sock_type = ROAR_SOCKET_TYPE_TCP; |
---|
[447] | 856 | if ( *server == '/' ) |
---|
| 857 | server = ROAR_DEFAULT_HOST; |
---|
[1494] | 858 | #endif |
---|
[518] | 859 | } else if ( strcmp(k, "-6") == 0 ) { |
---|
[1494] | 860 | #ifdef ROAR_SUPPORT_LISTEN |
---|
[519] | 861 | #ifdef PF_INET6 |
---|
[518] | 862 | sock_type = ROAR_SOCKET_TYPE_TCP6; |
---|
| 863 | if ( *server == '/' ) |
---|
| 864 | server = ROAR_DEFAULT_HOST; |
---|
[519] | 865 | #else |
---|
| 866 | ROAR_ERR("No IPv6 support compiled in!"); |
---|
| 867 | return 1; |
---|
| 868 | #endif |
---|
[1494] | 869 | #endif |
---|
[518] | 870 | |
---|
[573] | 871 | } else if ( strcmp(k, "-u") == 0 || strcmp(k, "--unix") == 0 ) { |
---|
[1494] | 872 | #ifdef ROAR_SUPPORT_LISTEN |
---|
[62] | 873 | // ignore this case as it is the default behavor. |
---|
[517] | 874 | sock_type = ROAR_SOCKET_TYPE_UNIX; |
---|
[1494] | 875 | #endif |
---|
[518] | 876 | |
---|
[573] | 877 | } else if ( strcmp(k, "-n") == 0 || strcmp(k, "--decnet") == 0 ) { |
---|
[1494] | 878 | #ifdef ROAR_SUPPORT_LISTEN |
---|
[508] | 879 | #ifdef ROAR_HAVE_LIBDNET |
---|
| 880 | port = ROAR_DEFAULT_NUM; |
---|
| 881 | strcpy(decnethost, ROAR_DEFAULT_LISTEN_OBJECT); |
---|
| 882 | server = decnethost; |
---|
[517] | 883 | sock_type = ROAR_SOCKET_TYPE_DECNET; |
---|
[508] | 884 | #else |
---|
| 885 | ROAR_ERR("No DECnet support compiled in!"); |
---|
| 886 | return 1; |
---|
| 887 | #endif |
---|
[1494] | 888 | #endif |
---|
[518] | 889 | |
---|
[1993] | 890 | } else if ( strcmp(k, "--slp") == 0 ) { |
---|
| 891 | #ifdef ROAR_HAVE_LIBSLP |
---|
| 892 | reg_slp = 1; |
---|
| 893 | #else |
---|
| 894 | ROAR_ERR("No OpenSLP support compiled in!"); |
---|
| 895 | return 1; |
---|
| 896 | #endif |
---|
| 897 | |
---|
[60] | 898 | } else if ( strcmp(k, "-G") == 0 ) { |
---|
[444] | 899 | sock_grp = argv[++i]; |
---|
| 900 | } else if ( strcmp(k, "-U") == 0 ) { |
---|
| 901 | sock_user = argv[++i]; |
---|
[0] | 902 | |
---|
[68] | 903 | } else if ( strcmp(k, "--no-listen") == 0 ) { |
---|
[1494] | 904 | #ifdef ROAR_SUPPORT_LISTEN |
---|
[1787] | 905 | server = ""; |
---|
[548] | 906 | g_terminate = 1; |
---|
[1155] | 907 | g_no_listen = 1; |
---|
[1494] | 908 | #endif |
---|
[68] | 909 | } else if ( strcmp(k, "--client-fh") == 0 ) { |
---|
| 910 | if ( clients_set_fh(clients_new(), atoi(argv[++i])) == -1 ) { |
---|
| 911 | ROAR_ERR("main(*): Can not set client's fh"); |
---|
| 912 | return 1; |
---|
| 913 | } |
---|
[501] | 914 | } else if ( strcmp(k, "--close-fh") == 0 ) { |
---|
[1486] | 915 | #ifdef ROAR_HAVE_IO_POSIX |
---|
[501] | 916 | close(atoi(argv[++i])); |
---|
[1486] | 917 | #else |
---|
| 918 | i++; |
---|
| 919 | ROAR_WARN("can not close file handle %s (closing not supported)", argv[i]); |
---|
| 920 | #endif |
---|
[68] | 921 | |
---|
[920] | 922 | } else if ( strcmp(k, "--standby") == 0 ) { |
---|
| 923 | g_standby = 1; |
---|
| 924 | } else if ( strcmp(k, "--auto-standby") == 0 ) { |
---|
| 925 | g_autostandby = 1; |
---|
[0] | 926 | } else { |
---|
| 927 | usage(); |
---|
| 928 | return 1; |
---|
| 929 | } |
---|
| 930 | |
---|
| 931 | } |
---|
[1503] | 932 | #endif |
---|
[0] | 933 | |
---|
[1110] | 934 | if ( s_dev != NULL ) { |
---|
| 935 | if ( sources_add(s_drv, s_dev, s_con, s_opt, s_prim) == -1 ) { |
---|
| 936 | ROAR_ERR("main(*): adding source '%s' via '%s' failed!", s_dev, s_drv); |
---|
| 937 | } |
---|
| 938 | } |
---|
| 939 | |
---|
[1145] | 940 | add_output(o_drv, o_dev, o_opts, o_prim, o_count); |
---|
[932] | 941 | |
---|
[0] | 942 | ROAR_DBG("Server config: rate=%i, bits=%i, chans=%i", sa.rate, sa.bits, sa.channels); |
---|
| 943 | |
---|
[1819] | 944 | if ( midi_init() == -1 ) { |
---|
[281] | 945 | ROAR_ERR("Can not initialize MIDI subsystem"); |
---|
[1819] | 946 | } |
---|
| 947 | |
---|
| 948 | if ( light_init(light_channels) == -1 ) { |
---|
| 949 | ROAR_ERR("Can not initialize light control subsystem"); |
---|
| 950 | } |
---|
[281] | 951 | |
---|
[1494] | 952 | #ifdef ROAR_SUPPORT_LISTEN |
---|
[68] | 953 | if ( *server != 0 ) { |
---|
[517] | 954 | if ( (g_listen_socket = roar_socket_listen(sock_type, server, port)) == -1 ) { |
---|
[1486] | 955 | #ifdef ROAR_HAVE_UNIX |
---|
[286] | 956 | if ( *server == '/' ) { |
---|
[1011] | 957 | if ( (env_roar_proxy_backup = getenv("ROAR_PROXY")) != NULL ) { |
---|
| 958 | env_roar_proxy_backup = strdup(env_roar_proxy_backup); |
---|
| 959 | unsetenv("ROAR_PROXY"); |
---|
| 960 | } |
---|
[286] | 961 | if ( (i = roar_socket_connect(server, port)) != -1 ) { |
---|
| 962 | close(i); |
---|
| 963 | ROAR_ERR("Can not open listen socket!"); |
---|
| 964 | return 1; |
---|
| 965 | } else { |
---|
| 966 | unlink(server); |
---|
[517] | 967 | if ( (g_listen_socket = roar_socket_listen(sock_type, server, port)) == -1 ) { |
---|
[286] | 968 | ROAR_ERR("Can not open listen socket!"); |
---|
| 969 | return 1; |
---|
| 970 | } |
---|
| 971 | } |
---|
[1011] | 972 | if ( env_roar_proxy_backup != NULL ) { |
---|
| 973 | setenv("ROAR_PROXY", env_roar_proxy_backup, 0); |
---|
| 974 | free(env_roar_proxy_backup); |
---|
| 975 | } |
---|
[1486] | 976 | #else |
---|
| 977 | if (0) { // noop |
---|
| 978 | #endif |
---|
[286] | 979 | } else { |
---|
| 980 | ROAR_ERR("Can not open listen socket!"); |
---|
| 981 | return 1; |
---|
| 982 | } |
---|
[68] | 983 | } |
---|
[0] | 984 | |
---|
[1486] | 985 | #if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX) |
---|
[523] | 986 | if ( (grp = getgrnam(sock_grp)) == NULL ) { |
---|
| 987 | ROAR_ERR("Can not get GID for group %s: %s", sock_grp, strerror(errno)); |
---|
| 988 | } |
---|
[1486] | 989 | #endif |
---|
| 990 | #if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX) |
---|
[523] | 991 | if ( sock_user || (setids & R_SETUID) ) { |
---|
| 992 | if ( (pwd = getpwnam(sock_user)) == NULL ) { |
---|
| 993 | ROAR_ERR("Can not get UID for user %s: %s", sock_user, strerror(errno)); |
---|
| 994 | } |
---|
| 995 | } |
---|
[1486] | 996 | #endif |
---|
[523] | 997 | |
---|
[1516] | 998 | #if defined(ROAR_HAVE_IO_POSIX) && defined(ROAR_HAVE_UNIX) |
---|
[68] | 999 | if ( *server == '/' ) { |
---|
[523] | 1000 | if ( grp ) { |
---|
[444] | 1001 | if ( pwd ) { |
---|
[1691] | 1002 | if ( chown(server, pwd->pw_uid, grp->gr_gid) == -1 ) |
---|
| 1003 | return 1; |
---|
[444] | 1004 | } else { |
---|
[1691] | 1005 | if ( chown(server, -1, grp->gr_gid) == -1 ) |
---|
| 1006 | return 1; |
---|
[444] | 1007 | } |
---|
[1753] | 1008 | #ifdef ROAR_HAVE_GETUID |
---|
[68] | 1009 | if ( getuid() == 0 ) |
---|
[1691] | 1010 | if ( chmod(server, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) == -1 ) |
---|
| 1011 | return 1; |
---|
[1753] | 1012 | #endif |
---|
[68] | 1013 | } |
---|
[60] | 1014 | } |
---|
[1486] | 1015 | #endif |
---|
[60] | 1016 | } |
---|
[1494] | 1017 | #endif |
---|
[60] | 1018 | |
---|
[0] | 1019 | if ( output_buffer_init(&sa) == -1 ) { |
---|
| 1020 | ROAR_ERR("Can not init output buffer!"); |
---|
| 1021 | return 1; |
---|
| 1022 | } |
---|
| 1023 | |
---|
[1145] | 1024 | if ( driver == NULL ) { |
---|
| 1025 | driver = "null"; |
---|
| 1026 | } else { |
---|
| 1027 | ROAR_WARN("Usage of old driver interface. use -o not -d!"); |
---|
| 1028 | } |
---|
| 1029 | |
---|
[0] | 1030 | if ( driver_open(&drvinst, &drvid, driver, device, &sa) == -1 ) { |
---|
| 1031 | ROAR_ERR("Can not open output driver!"); |
---|
| 1032 | return 1; |
---|
| 1033 | } |
---|
| 1034 | |
---|
[44] | 1035 | if ( samples_init() == -1 ) { |
---|
| 1036 | ROAR_ERR("Can not init samples!"); |
---|
| 1037 | return 1; |
---|
| 1038 | } |
---|
| 1039 | |
---|
| 1040 | |
---|
[1486] | 1041 | // we should handle this on microcontrollers, too. |
---|
[1753] | 1042 | #if !defined(ROAR_TARGET_MICROCONTROLLER) && !defined(ROAR_TARGET_WIN32) |
---|
[0] | 1043 | signal(SIGINT, on_sig_int); |
---|
[285] | 1044 | signal(SIGCHLD, on_sig_chld); |
---|
[2112] | 1045 | signal(SIGUSR1, on_sig_usr1); |
---|
[0] | 1046 | signal(SIGPIPE, SIG_IGN); // ignore broken pipes |
---|
[1486] | 1047 | #endif |
---|
[0] | 1048 | |
---|
[275] | 1049 | if ( realtime ) { |
---|
[278] | 1050 | #ifdef DEBUG |
---|
| 1051 | ROAR_WARN("compiled with -DDEBUG but realtime is enabled: for real realtime support compiel without -DDEBUG"); |
---|
| 1052 | #endif |
---|
| 1053 | |
---|
[1486] | 1054 | #ifdef ROAR_HAVE_NICE |
---|
[275] | 1055 | errno = 0; |
---|
[276] | 1056 | nice(-5*realtime); // -5 for each --realtime |
---|
[1486] | 1057 | if ( errno ) { |
---|
| 1058 | ROAR_WARN("Can not decrease nice value by %i: %s", 5*realtime, strerror(errno)); |
---|
| 1059 | } |
---|
| 1060 | #else |
---|
| 1061 | ROAR_WARN("Can not decrease nice value by %i: %s", 5*realtime, strerror(errno)); |
---|
| 1062 | #endif |
---|
[277] | 1063 | /* |
---|
[276] | 1064 | #ifdef __linux__ |
---|
[277] | 1065 | if ( ioprio_set(IOPRIO_WHO_PROCESS, getpid(), IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 0)) == -1 ) |
---|
| 1066 | ROAR_WARN("Can not set io priority: %s", strerror(errno)); |
---|
[276] | 1067 | #endif |
---|
[277] | 1068 | */ |
---|
[275] | 1069 | } |
---|
| 1070 | |
---|
[1486] | 1071 | #ifdef ROAR_HAVE_SETGID |
---|
[444] | 1072 | if ( setids & R_SETGID ) { |
---|
| 1073 | if ( setgroups(0, (const gid_t *) NULL) == -1 ) { |
---|
| 1074 | ROAR_ERR("Can not clear supplementary group IDs: %s", strerror(errno)); |
---|
| 1075 | } |
---|
[523] | 1076 | if ( !grp || setgid(grp->gr_gid) == -1 ) { |
---|
[444] | 1077 | ROAR_ERR("Can not set GroupID: %s", strerror(errno)); |
---|
| 1078 | } |
---|
| 1079 | } |
---|
[1486] | 1080 | #endif |
---|
[444] | 1081 | |
---|
[0] | 1082 | |
---|
[39] | 1083 | clients_set_pid(g_self_client, getpid()); |
---|
[1753] | 1084 | #ifdef ROAR_HAVE_GETUID |
---|
[440] | 1085 | clients_set_uid(g_self_client, getuid()); |
---|
[1753] | 1086 | #endif |
---|
| 1087 | #ifdef ROAR_HAVE_GETGID |
---|
[440] | 1088 | clients_set_gid(g_self_client, getgid()); |
---|
[1753] | 1089 | #endif |
---|
[39] | 1090 | clients_get(g_self_client, &self); |
---|
[37] | 1091 | |
---|
[39] | 1092 | if ( self == NULL ) { |
---|
| 1093 | ROAR_ERR("Can not get self client!"); |
---|
| 1094 | return 1; |
---|
| 1095 | } |
---|
| 1096 | |
---|
[775] | 1097 | strcpy(self->name, "RoarAudio daemon internal"); |
---|
[68] | 1098 | |
---|
[1486] | 1099 | #ifdef ROAR_HAVE_FORK |
---|
[775] | 1100 | if ( daemon ) { |
---|
[68] | 1101 | close(ROAR_STDIN ); |
---|
| 1102 | close(ROAR_STDOUT); |
---|
| 1103 | close(ROAR_STDERR); |
---|
[1753] | 1104 | |
---|
[68] | 1105 | if ( fork() ) |
---|
[1486] | 1106 | ROAR_U_EXIT(0); |
---|
[1753] | 1107 | |
---|
| 1108 | #ifdef ROAR_HAVE_SETSID |
---|
| 1109 | setsid(); |
---|
| 1110 | #endif |
---|
[1046] | 1111 | clients_set_pid(g_self_client, getpid()); // reset pid as it changed |
---|
[68] | 1112 | } |
---|
[1486] | 1113 | #endif |
---|
[68] | 1114 | |
---|
[2104] | 1115 | #ifdef SUPPORT_PIDFILE |
---|
[2106] | 1116 | if ( pidfile != NULL ) { |
---|
| 1117 | if ( roar_vio_open_file(&pidfile_vio, pidfile, O_WRONLY|O_CREAT, 0644) == -1 ) { |
---|
| 1118 | ROAR_ERR("Can not write pidfile: %s", pidfile); |
---|
| 1119 | } else { |
---|
| 1120 | roar_vio_printf(&pidfile_vio, "%i\n", getpid()); |
---|
| 1121 | roar_vio_close(&pidfile_vio); |
---|
| 1122 | } |
---|
[2104] | 1123 | } |
---|
| 1124 | #endif |
---|
| 1125 | |
---|
[1486] | 1126 | #ifdef ROAR_HAVE_CHROOT |
---|
[444] | 1127 | if (chrootdir) { |
---|
| 1128 | if ( chroot(chrootdir) == -1 ) { |
---|
| 1129 | ROAR_ERR("Can not chroot to %s: %s", chrootdir, strerror(errno)); |
---|
| 1130 | return 2; |
---|
| 1131 | } |
---|
| 1132 | if ( chdir("/") == -1 ) { |
---|
| 1133 | ROAR_ERR("Can not chdir to /: %s", strerror(errno)); |
---|
| 1134 | return 2; |
---|
| 1135 | } |
---|
| 1136 | } |
---|
[1486] | 1137 | #endif |
---|
[444] | 1138 | |
---|
[1486] | 1139 | #ifdef ROAR_HAVE_SETUID |
---|
[444] | 1140 | if ( setids & R_SETUID ) { |
---|
| 1141 | if ( !pwd || setuid(pwd->pw_uid) == -1 ) { |
---|
| 1142 | ROAR_ERR("Can not set UserID: %s", strerror(errno)); |
---|
| 1143 | return 3; |
---|
| 1144 | } |
---|
[1753] | 1145 | #ifdef ROAR_HAVE_GETUID |
---|
[444] | 1146 | clients_set_uid(g_self_client, getuid()); |
---|
[1753] | 1147 | #endif |
---|
[444] | 1148 | } |
---|
[1486] | 1149 | #endif |
---|
[444] | 1150 | |
---|
[1993] | 1151 | // Register with OpenSLP: |
---|
[2028] | 1152 | #ifdef ROAR_HAVE_LIBSLP |
---|
[1993] | 1153 | if ( reg_slp ) { |
---|
| 1154 | register_slp(0, server); |
---|
| 1155 | } |
---|
[2028] | 1156 | #endif |
---|
[1993] | 1157 | |
---|
[0] | 1158 | // start main loop... |
---|
[905] | 1159 | main_loop(drvid, drvinst, &sa, sysclocksync); |
---|
[0] | 1160 | |
---|
| 1161 | // clean up. |
---|
| 1162 | clean_quit_prep(); |
---|
| 1163 | driver_close(drvinst, drvid); |
---|
| 1164 | output_buffer_free(); |
---|
| 1165 | |
---|
| 1166 | return 0; |
---|
| 1167 | } |
---|
| 1168 | |
---|
[574] | 1169 | void cleanup_listen_socket (int terminate) { |
---|
[1993] | 1170 | // Deregister from SLP: |
---|
[2028] | 1171 | #ifdef ROAR_HAVE_LIBSLP |
---|
[1993] | 1172 | register_slp(1, NULL); |
---|
[2028] | 1173 | #endif |
---|
[580] | 1174 | |
---|
[1494] | 1175 | #ifdef ROAR_SUPPORT_LISTEN |
---|
[580] | 1176 | if ( g_listen_socket != -1 ) { |
---|
[1486] | 1177 | #ifdef ROAR_HAVE_IO_POSIX |
---|
[580] | 1178 | close(g_listen_socket); |
---|
[1486] | 1179 | #endif // #else is useless because we are in void context. |
---|
[60] | 1180 | |
---|
[580] | 1181 | g_listen_socket = -1; |
---|
[576] | 1182 | |
---|
[1486] | 1183 | #ifdef ROAR_HAVE_UNIX |
---|
[580] | 1184 | if ( *server == '/' ) |
---|
| 1185 | unlink(server); |
---|
[1486] | 1186 | #endif |
---|
[580] | 1187 | } |
---|
[60] | 1188 | |
---|
[1494] | 1189 | #endif |
---|
| 1190 | |
---|
[574] | 1191 | if ( terminate ) |
---|
| 1192 | g_terminate = 1; |
---|
| 1193 | } |
---|
| 1194 | |
---|
| 1195 | void clean_quit_prep (void) { |
---|
| 1196 | cleanup_listen_socket(0); |
---|
[60] | 1197 | |
---|
[0] | 1198 | sources_free(); |
---|
| 1199 | streams_free(); |
---|
| 1200 | clients_free(); |
---|
[282] | 1201 | midi_cb_stop(); // stop console beep |
---|
[281] | 1202 | midi_free(); |
---|
[1819] | 1203 | light_free(); |
---|
[2104] | 1204 | |
---|
| 1205 | #ifdef SUPPORT_PIDFILE |
---|
| 1206 | if ( pidfile != NULL ) |
---|
| 1207 | unlink(pidfile); |
---|
| 1208 | #endif |
---|
[0] | 1209 | } |
---|
| 1210 | |
---|
| 1211 | void clean_quit (void) { |
---|
| 1212 | clean_quit_prep(); |
---|
| 1213 | // driver_close(drvinst, drvid); |
---|
| 1214 | // output_buffer_free(); |
---|
| 1215 | exit(0); |
---|
| 1216 | } |
---|
| 1217 | |
---|
| 1218 | //ll |
---|