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