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