[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 |
---|
[2530] | 28 | char * server[ROAR_MAX_LISTEN_SOCKETS]; |
---|
[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 | |
---|
[2530] | 36 | #if defined(ROAR_HAVE_SETGID) || defined(ROAR_HAVE_SETUID) |
---|
| 37 | int setids = 0; |
---|
| 38 | #endif |
---|
| 39 | |
---|
[1503] | 40 | #ifdef ROAR_HAVE_MAIN_ARGS |
---|
[0] | 41 | void usage (void) { |
---|
| 42 | printf("Usage: roard [OPTIONS]...\n\n"); |
---|
| 43 | |
---|
[68] | 44 | printf("Misc Options:\n\n"); |
---|
| 45 | printf( |
---|
[775] | 46 | " --daemon - Bring the server into background after init\n" |
---|
[2970] | 47 | " --verbose - Be more verbose, can be used multiple times\n" |
---|
[71] | 48 | " --terminate - Terminate after last client quited\n" |
---|
[2109] | 49 | " --start - No op parameter (starting roard is default operation)\n" |
---|
[274] | 50 | " --restart - Trys to stop an old instance and start a new with new settings\n" |
---|
[2109] | 51 | " --stop - Stops a running roard (provide --pidfile!)\n" |
---|
| 52 | " --shutdown - Terminates a running roard (provide --pidfile!)\n" |
---|
[276] | 53 | " --realtime - Trys to get realtime priority,\n" |
---|
| 54 | " give multible times for being more realtime\n" |
---|
[444] | 55 | " --chroot DIR - chroots to the given dir\n" |
---|
| 56 | " --setgid - GroupID to the audio group as specified via -G\n" |
---|
| 57 | " --setuid - UserID to the audio user as specified via -U\n" |
---|
[905] | 58 | " --sysclocksync - calculate exact sample rate using the system clock\n" |
---|
[2017] | 59 | " --location LOC - Set lion readable location of server\n" |
---|
[2104] | 60 | #ifdef SUPPORT_PIDFILE |
---|
| 61 | " --pidfile PIDFILE - Write a pidfile at PIDFILE\n" |
---|
| 62 | #endif |
---|
[68] | 63 | ); |
---|
| 64 | |
---|
| 65 | printf("\nAudio Options:\n\n"); |
---|
[0] | 66 | printf( |
---|
| 67 | " -R --rate RATE - Set server rate\n" |
---|
| 68 | " -B --bits BITS - Set server bits\n" |
---|
| 69 | " -C --chans CHANNELS - Set server channels\n" |
---|
| 70 | ); |
---|
| 71 | |
---|
[2339] | 72 | printf("\nStream Options:\n\n"); |
---|
| 73 | printf( |
---|
| 74 | " --stream-flags D=F - Set default flags for stream directions\n" |
---|
| 75 | " D is the stream direction and F is a comma seperated\n" |
---|
| 76 | " list of flags in form +flag or -flag to set or unset\n" |
---|
| 77 | " a flag as default or remove it from the default\n" |
---|
| 78 | ); |
---|
| 79 | |
---|
[1923] | 80 | printf("\nDriver Options: (obsolete, do not use, Use Ouput Options)\n\n"); |
---|
[974] | 81 | printf(" -d --driver DRV - Set the driver (default: %s)\n", ROAR_DRIVER_DEFAULT); |
---|
[0] | 82 | printf(" -D --device DEV - Set the device\n"); |
---|
| 83 | printf(" -dO OPTS - Set output options\n"); |
---|
[973] | 84 | printf(" --list-driver - List all drivers\n"); |
---|
[0] | 85 | |
---|
[932] | 86 | printf("\nOutput Options:\n\n"); |
---|
[974] | 87 | printf(" -o --odriver DRV - Set the driver, use '--list-driver' to get a list\n"); |
---|
[932] | 88 | printf(" -O --odevice DEV - Set the device\n"); |
---|
| 89 | printf(" -oO OPTS - Set output options\n"); |
---|
| 90 | printf(" -oN - Adds another output\n"); |
---|
[961] | 91 | printf(" -oP - Mark output as primary\n"); |
---|
[932] | 92 | |
---|
[2485] | 93 | #ifndef ROAR_WITHOUT_DCOMP_SOURCES |
---|
[0] | 94 | printf("\nSource Options:\n\n"); |
---|
| 95 | printf(" -s --source DRV - Use DRV as input driver\n" |
---|
| 96 | " -S DEV - Use DEV as input device\n" |
---|
| 97 | " -sO OPTS - Use OPTS as input options\n" |
---|
[1512] | 98 | " -sN - Adds another source\n" |
---|
[0] | 99 | " -sP - Make souce as primary\n" |
---|
| 100 | ); |
---|
[2270] | 101 | printf(" --list-sources - List all sources\n"); |
---|
[2485] | 102 | #endif |
---|
[0] | 103 | |
---|
[280] | 104 | printf("\nCodec Filter Options:\n\n"); |
---|
| 105 | printf(" --list-cf - List all codec filter\n" |
---|
| 106 | ); |
---|
[0] | 107 | |
---|
[2500] | 108 | #ifndef ROAR_WITHOUT_DCOMP_MIDI |
---|
[1923] | 109 | printf("\nMIDI Options:\n\n"); |
---|
[1924] | 110 | printf(" --midi-no-console - Disable console based MIDI synth\n" |
---|
[2444] | 111 | " --midi-console-enable - Enables the console based MIDI synth\n" |
---|
[1924] | 112 | " --midi-console DEV - Set device for MIDI console\n" |
---|
[2489] | 113 | #ifndef ROAR_WITHOUT_DCOMP_SSYNTH |
---|
[2451] | 114 | " --ssynth-enable - Enable simple software synth\n" |
---|
| 115 | " --ssynth-disable - Disable simple software synth\n" |
---|
[2489] | 116 | #endif |
---|
[1924] | 117 | ); |
---|
[2500] | 118 | #endif |
---|
[1923] | 119 | |
---|
[2494] | 120 | #ifndef ROAR_WITHOUT_DCOMP_LIGHT |
---|
[1923] | 121 | printf("\nLight Control Options:\n\n"); |
---|
| 122 | printf(" --light-channels NUM - Sets the number of channels for Light control (default: %i)\n", |
---|
| 123 | LIGHT_CHANNELS_DEFAULT |
---|
| 124 | ); |
---|
[2494] | 125 | #endif |
---|
[1923] | 126 | |
---|
[2725] | 127 | #ifndef ROAR_WITHOUT_DCOMP_RDTCS |
---|
| 128 | printf("\nRadio Date and Transmitter Control System Options:\n\n"); |
---|
| 129 | printf(" --rds-pi PI - Sets the RDS Programme Identification (PI)\n" |
---|
| 130 | " --rds-ps PS - Sets the RDS Programme Service Name (PS)\n" |
---|
| 131 | " --rds-pty PTY - Sets the RDS Programme Type (PTY)\n" |
---|
| 132 | " --rds-tp - Sets the RDS Traffic Programme (TP) flag\n" |
---|
| 133 | " --rds-ct - Enables sending of RDS Clock Time (CT)\n" |
---|
| 134 | ); |
---|
| 135 | #endif |
---|
| 136 | |
---|
[0] | 137 | printf("\nServer Options:\n\n"); |
---|
| 138 | printf(" -t --tcp - Use TCP listen socket\n" |
---|
| 139 | " -u --unix - Use UNIX Domain listen socket (default)\n" |
---|
[508] | 140 | #ifdef ROAR_HAVE_LIBDNET |
---|
| 141 | " -n --decnet - use DECnet listen socket\n" |
---|
| 142 | #endif |
---|
[518] | 143 | " -4 - Use IPv4 connections (implies -t)\n" |
---|
| 144 | #ifdef PF_INET6 |
---|
| 145 | " -6 - Use IPv6 connections (implies -t)\n" |
---|
| 146 | #endif |
---|
| 147 | #ifdef IPV6_ADDRFORM |
---|
| 148 | " -64 - Try to downgrade sockets from IPv6 into IPv4,\n" |
---|
| 149 | " this is normaly not usefull.\n" |
---|
| 150 | #endif |
---|
[0] | 151 | " -p --port - TCP Port to bind to\n" |
---|
| 152 | " -b --bind - IP/Hostname to bind to\n" |
---|
[1115] | 153 | " --sock - Filename for UNIX Domain Socket\n" |
---|
[2530] | 154 | " --proto PROTO - Use PROTO as protocol on Socket\n" |
---|
[3252] | 155 | " --proto-dir DIR - Set direction parameter for protocol\n" |
---|
| 156 | " --proto-rate RATE - Set sample rate parameter for protocol\n" |
---|
| 157 | " --proto-bits BITS - Set bits per sample parameter for protocol\n" |
---|
| 158 | " --proto-codec E - Set codec parameter for protocol\n" |
---|
| 159 | " --proto-chans C - Set number of channels paramter for protocol\n" |
---|
[3127] | 160 | " --list-proto - List supported protocols\n" |
---|
[2530] | 161 | " --new-sock - Parameters for new socket follows\n" |
---|
[1993] | 162 | #ifdef ROAR_HAVE_LIBSLP |
---|
| 163 | " --slp - Enable OpenSLP support\n" |
---|
| 164 | #endif |
---|
[3039] | 165 | " --jumbo-mtu MTU - Sets the MTU for Jumbo Packets\n" |
---|
[450] | 166 | " -G GROUP - Sets the group for the UNIX Domain Socket, (default: %s)\n" |
---|
[2737] | 167 | " You need the permissions to change the GID\n" |
---|
[444] | 168 | " -U USER - Sets the user for the UNIX Domain Socket, (default: do not set)\n" |
---|
[2737] | 169 | " You need the permissions to change the UID (normaly only root has)\n" |
---|
[548] | 170 | " --no-listen - Do not listen for new clients\n" |
---|
| 171 | " (only usefull for relaing, impleys --terminate)\n" |
---|
[274] | 172 | " --client-fh - Comunicate with a client over this handle\n" |
---|
[501] | 173 | " (only usefull for relaing)\n" |
---|
[920] | 174 | " --close-fh - Closes the given fh\n" |
---|
| 175 | " --standby - Start in standby state\n" |
---|
| 176 | " --auto-standby - Automatical goes into standby if there are no streams\n", |
---|
[2755] | 177 | #ifdef ROAR_DEFAULT_SOCKGRP |
---|
[450] | 178 | ROAR_DEFAULT_SOCKGRP |
---|
[2755] | 179 | #else |
---|
| 180 | "(none)" |
---|
| 181 | #endif |
---|
[0] | 182 | ); |
---|
| 183 | // printf("\n Options:\n\n"); |
---|
| 184 | printf("\n"); |
---|
| 185 | } |
---|
[3127] | 186 | |
---|
| 187 | static void list_proto (void) { |
---|
| 188 | printf(" Protocol Flag Subsys - Description\n"); |
---|
| 189 | printf("------------------------------------------------------\n"); |
---|
| 190 | printf(" roar WM LRX - RoarAudio native protocol\n"); |
---|
| 191 | #if !defined(ROAR_WITHOUT_DCOMP_EMUL_ESD) && defined(ROAR_HAVE_H_ESD) |
---|
| 192 | printf(" esd W - EsounD emulation\n"); |
---|
| 193 | #endif |
---|
[3257] | 194 | printf(" simple WM LRX - PulseAudio simple protocol\n"); |
---|
[3127] | 195 | } |
---|
| 196 | |
---|
[1503] | 197 | #endif |
---|
[0] | 198 | |
---|
[2109] | 199 | int restart_server (char * server, int terminate) { |
---|
[579] | 200 | struct roar_connection con; |
---|
[2111] | 201 | #ifdef ROAR_HAVE_KILL |
---|
| 202 | char buf[80]; |
---|
| 203 | ssize_t l; |
---|
| 204 | struct roar_vio_calls fh; |
---|
| 205 | pid_t pid; |
---|
| 206 | int ok; |
---|
| 207 | |
---|
| 208 | if ( pidfile != NULL ) { |
---|
| 209 | if ( roar_vio_open_file(&fh, pidfile, O_RDONLY, 0644) == -1 ) { |
---|
| 210 | ROAR_WARN("restart_server(*): Can not read pidfile: %s", pidfile); |
---|
| 211 | } else { |
---|
| 212 | l = roar_vio_read(&fh, buf, 80); |
---|
| 213 | roar_vio_close(&fh); |
---|
| 214 | if ( l > 0 ) { |
---|
| 215 | buf[l-1] = 0; |
---|
| 216 | buf[79] = 0; |
---|
| 217 | pid = atoi(buf); |
---|
| 218 | if ( terminate ) { |
---|
[2113] | 219 | ok = kill(pid, SIGUSR1); |
---|
[2111] | 220 | } else { |
---|
| 221 | ok = kill(pid, SIGINT); |
---|
| 222 | } |
---|
| 223 | if ( ok == 0 ) { |
---|
| 224 | return 0; |
---|
| 225 | } else { |
---|
| 226 | ROAR_WARN("restart_server(*): Can not kill roard by pidfile"); |
---|
| 227 | } |
---|
| 228 | } else { |
---|
| 229 | ROAR_WARN("restart_server(*): Can not find a PID in the pidfile"); |
---|
| 230 | } |
---|
| 231 | } |
---|
| 232 | } |
---|
| 233 | #endif |
---|
[2109] | 234 | |
---|
[579] | 235 | if ( roar_connect(&con, server) == -1 ) { |
---|
| 236 | return -1; |
---|
| 237 | } |
---|
| 238 | |
---|
[2109] | 239 | if ( roar_terminate(&con, terminate) == -1 ) { |
---|
[579] | 240 | return -1; |
---|
| 241 | } |
---|
| 242 | |
---|
| 243 | return roar_disconnect(&con); |
---|
| 244 | } |
---|
| 245 | |
---|
[444] | 246 | #define R_SETUID 1 |
---|
| 247 | #define R_SETGID 2 |
---|
| 248 | |
---|
[1609] | 249 | int init_config (void) { |
---|
| 250 | int i; |
---|
| 251 | |
---|
| 252 | memset(g_config, 0, sizeof(struct roard_config)); |
---|
| 253 | |
---|
| 254 | for (i = 0; i < ROAR_DIR_DIRIDS; i++) { |
---|
| 255 | g_config->streams[i].mixer_channels = 1; |
---|
| 256 | g_config->streams[i].mixer.rpg_mul = 1; |
---|
| 257 | g_config->streams[i].mixer.rpg_div = 1; |
---|
| 258 | g_config->streams[i].mixer.scale = 65535; |
---|
| 259 | g_config->streams[i].mixer.mixer[0] = g_config->streams[i].mixer.scale; |
---|
| 260 | } |
---|
| 261 | |
---|
[2291] | 262 | g_config->streams[ROAR_DIR_PLAY ].flags = ROAR_FLAG_META; |
---|
[2413] | 263 | g_config->streams[ROAR_DIR_OUTPUT ].flags = ROAR_FLAG_PASSMIXER; |
---|
[2264] | 264 | g_config->streams[ROAR_DIR_FILTER ].flags = ROAR_FLAG_SYNC; |
---|
[1899] | 265 | g_config->streams[ROAR_DIR_MIDI_OUT].flags = ROAR_FLAG_SYNC; |
---|
[2160] | 266 | g_config->streams[ROAR_DIR_BIDIR ].flags = ROAR_FLAG_ANTIECHO; |
---|
[1899] | 267 | |
---|
[2017] | 268 | g_config->location = "***default***"; |
---|
| 269 | |
---|
[1609] | 270 | return 0; |
---|
| 271 | } |
---|
| 272 | |
---|
[2530] | 273 | #ifdef ROAR_SUPPORT_LISTEN |
---|
| 274 | int init_listening (void) { |
---|
| 275 | int i; |
---|
| 276 | |
---|
[3128] | 277 | memset(g_listen, 0, sizeof(g_listen)); |
---|
| 278 | |
---|
[2530] | 279 | for (i = 0; i < ROAR_MAX_LISTEN_SOCKETS; i++) { |
---|
[3129] | 280 | g_listen[i].socket = -1; |
---|
[3128] | 281 | g_listen[i].proto = ROAR_PROTO_ROARAUDIO; |
---|
[2530] | 282 | server[i] = NULL; |
---|
| 283 | } |
---|
| 284 | |
---|
| 285 | return 0; |
---|
| 286 | } |
---|
| 287 | |
---|
[3252] | 288 | int add_listen (char * addr, int port, int sock_type, char * user, char * group, int proto, int dir, struct roar_audio_info * info) { |
---|
[2530] | 289 | #if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX) |
---|
| 290 | struct group * grp = NULL; |
---|
| 291 | #endif |
---|
| 292 | #if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX) |
---|
| 293 | struct passwd * pwd = NULL; |
---|
| 294 | #endif |
---|
| 295 | #ifdef ROAR_HAVE_UNIX |
---|
| 296 | char * env_roar_proxy_backup; |
---|
| 297 | #endif |
---|
| 298 | int sockid = -1; |
---|
| 299 | int sock; |
---|
| 300 | int i; |
---|
| 301 | |
---|
| 302 | if ( *addr != 0 ) { |
---|
| 303 | for (i = 0; i < ROAR_MAX_LISTEN_SOCKETS; i++) { |
---|
[3129] | 304 | if ( g_listen[i].socket == -1 ) { |
---|
[2530] | 305 | sockid = i; |
---|
| 306 | break; |
---|
| 307 | } |
---|
| 308 | } |
---|
| 309 | |
---|
| 310 | if ( sockid == -1 ) |
---|
| 311 | return -1; |
---|
| 312 | |
---|
[3128] | 313 | g_listen[sockid].proto = proto; |
---|
[2530] | 314 | |
---|
| 315 | ROAR_DBG("add_listen(*): proto=0x%.4x", proto); |
---|
| 316 | |
---|
[3129] | 317 | if ( (g_listen[sockid].socket = roar_socket_listen(sock_type, addr, port)) == -1 ) { |
---|
[2530] | 318 | #ifdef ROAR_HAVE_UNIX |
---|
| 319 | if ( *addr == '/' ) { |
---|
| 320 | if ( (env_roar_proxy_backup = getenv("ROAR_PROXY")) != NULL ) { |
---|
| 321 | env_roar_proxy_backup = strdup(env_roar_proxy_backup); |
---|
| 322 | unsetenv("ROAR_PROXY"); |
---|
| 323 | } |
---|
| 324 | if ( (sock = roar_socket_connect(addr, port)) != -1 ) { |
---|
| 325 | close(sock); |
---|
[3040] | 326 | ROAR_ERR("Can not open listen socket: Socket allready in use"); |
---|
[2530] | 327 | return 1; |
---|
| 328 | } else { |
---|
| 329 | unlink(addr); |
---|
[3129] | 330 | if ( (g_listen[sockid].socket = roar_socket_listen(sock_type, addr, port)) == -1 ) { |
---|
[3040] | 331 | ROAR_ERR("Can not open listen socket: %s", strerror(errno)); |
---|
[2530] | 332 | return 1; |
---|
| 333 | } |
---|
| 334 | } |
---|
| 335 | if ( env_roar_proxy_backup != NULL ) { |
---|
| 336 | setenv("ROAR_PROXY", env_roar_proxy_backup, 0); |
---|
| 337 | free(env_roar_proxy_backup); |
---|
| 338 | } |
---|
| 339 | #else |
---|
| 340 | if (0) { // noop |
---|
| 341 | #endif |
---|
| 342 | } else { |
---|
[3041] | 343 | ROAR_ERR("Can not open listen socket: %s", strerror(errno)); |
---|
[2530] | 344 | return 1; |
---|
| 345 | } |
---|
| 346 | } |
---|
| 347 | |
---|
| 348 | #if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX) |
---|
[2753] | 349 | if ( group != NULL ) { |
---|
| 350 | if ( (grp = getgrnam(group)) == NULL ) { |
---|
| 351 | ROAR_ERR("Can not get GID for group %s: %s", group, strerror(errno)); |
---|
| 352 | } |
---|
[2530] | 353 | } |
---|
| 354 | #endif |
---|
| 355 | #if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX) |
---|
[2752] | 356 | if ( user ) { |
---|
[2530] | 357 | if ( (pwd = getpwnam(user)) == NULL ) { |
---|
| 358 | ROAR_ERR("Can not get UID for user %s: %s", user, strerror(errno)); |
---|
| 359 | } |
---|
| 360 | } |
---|
| 361 | #endif |
---|
| 362 | |
---|
| 363 | #if defined(ROAR_HAVE_IO_POSIX) && defined(ROAR_HAVE_UNIX) |
---|
| 364 | if ( *addr == '/' ) { |
---|
[2753] | 365 | if ( grp || pwd ) { |
---|
| 366 | if ( chown(addr, pwd ? pwd->pw_uid : -1, grp ? grp->gr_gid : -1) == -1 ) |
---|
[2530] | 367 | return 1; |
---|
[2753] | 368 | } |
---|
[2530] | 369 | #ifdef ROAR_HAVE_GETUID |
---|
[2753] | 370 | if ( grp ) { |
---|
[2530] | 371 | if ( getuid() == 0 ) |
---|
| 372 | if ( chmod(addr, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) == -1 ) |
---|
| 373 | return 1; |
---|
[2753] | 374 | } |
---|
[2530] | 375 | #endif |
---|
| 376 | } |
---|
| 377 | #endif |
---|
| 378 | } |
---|
| 379 | |
---|
[2736] | 380 | // in case we opened the listening socket correctly. |
---|
[3256] | 381 | if ( dir == -1 ) |
---|
| 382 | dir = ROAR_DIR_PLAY; |
---|
| 383 | |
---|
[3252] | 384 | g_listen[sockid].inst.stpl.dir = dir; |
---|
| 385 | memcpy(&(g_listen[sockid].inst.stpl.info), info, sizeof(struct roar_audio_info)); |
---|
[3256] | 386 | |
---|
| 387 | switch (dir) { |
---|
| 388 | case ROAR_DIR_PLAY: |
---|
| 389 | case ROAR_DIR_RECORD: |
---|
| 390 | case ROAR_DIR_MONITOR: |
---|
| 391 | case ROAR_DIR_FILTER: |
---|
| 392 | case ROAR_DIR_BIDIR: |
---|
| 393 | if ( !g_listen[sockid].inst.stpl.info.rate ) |
---|
| 394 | g_listen[sockid].inst.stpl.info.rate = g_sa->rate; |
---|
| 395 | |
---|
| 396 | if ( !g_listen[sockid].inst.stpl.info.bits ) |
---|
| 397 | g_listen[sockid].inst.stpl.info.bits = g_sa->bits; |
---|
| 398 | |
---|
| 399 | if ( !g_listen[sockid].inst.stpl.info.channels ) |
---|
| 400 | g_listen[sockid].inst.stpl.info.channels = g_sa->channels; |
---|
| 401 | |
---|
| 402 | if ( !g_listen[sockid].inst.stpl.info.codec ) |
---|
| 403 | g_listen[sockid].inst.stpl.info.codec = g_sa->codec; |
---|
| 404 | break; |
---|
| 405 | } |
---|
| 406 | |
---|
[2736] | 407 | server[sockid] = addr; |
---|
[2530] | 408 | return 0; |
---|
| 409 | } |
---|
| 410 | #endif |
---|
| 411 | |
---|
[2339] | 412 | int update_stream_flags (char * str) { |
---|
| 413 | int dir; |
---|
| 414 | char * flags; |
---|
| 415 | char * k; |
---|
| 416 | int op; |
---|
| 417 | int flag; |
---|
| 418 | |
---|
| 419 | if ( (flags = strstr(str, "=")) == NULL ) |
---|
| 420 | return -1; |
---|
| 421 | |
---|
| 422 | *flags = 0; |
---|
| 423 | flags++; |
---|
| 424 | |
---|
| 425 | if ( (dir = roar_str2dir(str)) == -1 ) |
---|
| 426 | return -1; |
---|
| 427 | |
---|
| 428 | while (flags != NULL) { |
---|
| 429 | k = flags; |
---|
| 430 | flags = strstr(flags, ","); |
---|
| 431 | |
---|
| 432 | if ( flags != NULL ) |
---|
| 433 | *(flags++) = 0; |
---|
| 434 | |
---|
| 435 | switch (*k) { |
---|
[2340] | 436 | case '+': k++; op = ROAR_SET_FLAG; break; |
---|
| 437 | case '-': k++; op = ROAR_RESET_FLAG; break; |
---|
[2339] | 438 | default: |
---|
[2340] | 439 | op = ROAR_SET_FLAG; |
---|
[2339] | 440 | } |
---|
| 441 | |
---|
| 442 | flag = 0; |
---|
| 443 | |
---|
| 444 | if ( !strcmp(k, "sync") ) { |
---|
| 445 | flag = ROAR_FLAG_SYNC; |
---|
| 446 | } else if ( !strcmp(k, "meta") ) { |
---|
| 447 | flag = ROAR_FLAG_META; |
---|
| 448 | } else if ( !strcmp(k, "cleanmeta") ) { |
---|
| 449 | flag = ROAR_FLAG_CLEANMETA; |
---|
| 450 | } else if ( !strcmp(k, "pause") ) { |
---|
| 451 | flag = ROAR_FLAG_PAUSE; |
---|
| 452 | } else if ( !strcmp(k, "mute") ) { |
---|
| 453 | flag = ROAR_FLAG_MUTE; |
---|
| 454 | } else if ( !strcmp(k, "antiecho") ) { |
---|
| 455 | flag = ROAR_FLAG_ANTIECHO; |
---|
[2413] | 456 | } else if ( !strcmp(k, "passmixer") ) { |
---|
| 457 | flag = ROAR_FLAG_PASSMIXER; |
---|
[2339] | 458 | } else { |
---|
| 459 | return -1; |
---|
| 460 | } |
---|
| 461 | |
---|
| 462 | g_config->streams[dir].flags |= flag; |
---|
| 463 | |
---|
[2340] | 464 | if ( op == ROAR_RESET_FLAG ) |
---|
[2339] | 465 | g_config->streams[dir].flags -= flag; |
---|
| 466 | } |
---|
| 467 | |
---|
| 468 | return 0; |
---|
| 469 | } |
---|
| 470 | |
---|
[1145] | 471 | int add_output (char * drv, char * dev, char * opts, int prim, int count) { |
---|
[933] | 472 | int stream; |
---|
| 473 | struct roar_stream * s; |
---|
| 474 | struct roar_stream_server * ss; |
---|
[938] | 475 | char * k, * v; |
---|
[1208] | 476 | #ifdef ROAR_DRIVER_CODEC |
---|
| 477 | char * to_free = NULL; |
---|
| 478 | #endif |
---|
[1926] | 479 | int sync = 0, f_mmap = 0; |
---|
[1522] | 480 | int32_t blocks = -1, blocksize = -1; |
---|
[1920] | 481 | int dir = ROAR_DIR_OUTPUT; |
---|
[1991] | 482 | int error = 0; |
---|
| 483 | // DMX: |
---|
| 484 | int32_t channel = -1; |
---|
| 485 | int32_t universe = -1; |
---|
| 486 | uint16_t tu16; |
---|
[2085] | 487 | float q = -32e6; |
---|
[933] | 488 | |
---|
[938] | 489 | ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = ?", drv, dev, opts); |
---|
[933] | 490 | |
---|
[1145] | 491 | if ( drv == NULL && count == 0 ) { |
---|
| 492 | drv = ROAR_DRIVER_DEFAULT; |
---|
| 493 | prim = 1; |
---|
| 494 | sync = 1; |
---|
[1208] | 495 | |
---|
| 496 | #ifdef ROAR_DRIVER_CODEC |
---|
| 497 | if ( opts == NULL ) { |
---|
| 498 | opts = to_free = strdup("codec=" ROAR_DRIVER_CODEC); |
---|
| 499 | } |
---|
| 500 | #endif |
---|
[1145] | 501 | } |
---|
| 502 | |
---|
[1227] | 503 | if ( opts == NULL && count == 0 ) { |
---|
| 504 | sync = 1; |
---|
| 505 | prim = 1; // if ( prim == 0 ) prim = 1; -> prim allways = 1 |
---|
| 506 | } |
---|
| 507 | |
---|
[2785] | 508 | ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = ?", drv, dev, opts); |
---|
| 509 | |
---|
[933] | 510 | if ( (stream = streams_new()) == -1 ) { |
---|
[938] | 511 | ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = -1", drv, dev, opts); |
---|
[982] | 512 | if ( prim ) alive = 0; |
---|
[933] | 513 | return -1; |
---|
| 514 | } |
---|
| 515 | |
---|
[2785] | 516 | ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = ?", drv, dev, opts); |
---|
| 517 | |
---|
[933] | 518 | streams_get(stream, &ss); |
---|
| 519 | s = ROAR_STREAM(ss); |
---|
| 520 | |
---|
[2785] | 521 | ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = ?", drv, dev, opts); |
---|
| 522 | |
---|
[2511] | 523 | memset(&(s->info), 0xFF, sizeof(struct roar_audio_info)); // set everything to -1 |
---|
[933] | 524 | |
---|
| 525 | s->pos_rel_id = -1; |
---|
| 526 | // s->info.codec = codec; |
---|
| 527 | |
---|
[2785] | 528 | ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = ?", drv, dev, opts); |
---|
| 529 | |
---|
[2788] | 530 | if ( opts == NULL ) { |
---|
| 531 | k = NULL; |
---|
| 532 | } else { |
---|
| 533 | k = strtok(opts, ","); |
---|
| 534 | } |
---|
[2786] | 535 | |
---|
| 536 | ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s'): initial k='%s'(%p)", drv, dev, opts, k, k); |
---|
| 537 | |
---|
[938] | 538 | while (k != NULL) { |
---|
| 539 | // ROAR_WARN("add_output(*): opts: %s", k); |
---|
| 540 | |
---|
| 541 | if ( (v = strstr(k, "=")) != NULL ) { |
---|
| 542 | *v++ = 0; |
---|
| 543 | } |
---|
| 544 | |
---|
| 545 | ROAR_DBG("add_output(*): opts: k='%s', v='%s'", k, v); |
---|
| 546 | if ( strcmp(k, "rate") == 0 ) { |
---|
| 547 | s->info.rate = atoi(v); |
---|
| 548 | } else if ( strcmp(k, "channels") == 0 ) { |
---|
| 549 | s->info.channels = atoi(v); |
---|
| 550 | } else if ( strcmp(k, "bits") == 0 ) { |
---|
| 551 | s->info.bits = atoi(v); |
---|
| 552 | } else if ( strcmp(k, "codec") == 0 ) { |
---|
[2511] | 553 | if ( (s->info.codec = roar_str2codec(v)) == -1 ) { |
---|
[938] | 554 | ROAR_ERR("add_output(*): unknown codec '%s'", v); |
---|
[1991] | 555 | error++; |
---|
[938] | 556 | } |
---|
[2085] | 557 | } else if ( strcmp(k, "q") == 0 ) { |
---|
| 558 | q = atof(v); |
---|
[1221] | 559 | } else if ( strcmp(k, "blocks") == 0 ) { |
---|
| 560 | blocks = atoi(v); |
---|
[1522] | 561 | } else if ( strcmp(k, "blocksize") == 0 ) { |
---|
| 562 | blocksize = atoi(v); |
---|
[1926] | 563 | } else if ( strcmp(k, "mmap") == 0 ) { |
---|
| 564 | f_mmap = 1; |
---|
[1919] | 565 | } else if ( strcmp(k, "subsystem") == 0 ) { |
---|
| 566 | if ( !strcasecmp(v, "wave") || !strcasecmp(v, "waveform") ) { |
---|
[1920] | 567 | dir = ROAR_DIR_OUTPUT; |
---|
[2494] | 568 | #ifndef ROAR_WITHOUT_DCOMP_MIDI |
---|
[1919] | 569 | } else if ( !strcasecmp(v, "midi") ) { |
---|
| 570 | dir = ROAR_DIR_MIDI_OUT; |
---|
[2494] | 571 | #endif |
---|
| 572 | #ifndef ROAR_WITHOUT_DCOMP_LIGHT |
---|
[1919] | 573 | } else if ( !strcasecmp(v, "light") ) { |
---|
| 574 | dir = ROAR_DIR_LIGHT_OUT; |
---|
[2494] | 575 | #endif |
---|
| 576 | #ifndef ROAR_WITHOUT_DCOMP_RAW |
---|
[2234] | 577 | } else if ( !strcasecmp(v, "raw") ) { |
---|
| 578 | dir = ROAR_DIR_RAW_OUT; |
---|
[2494] | 579 | #endif |
---|
[2681] | 580 | } else if ( !strcasecmp(v, "complex") ) { |
---|
| 581 | dir = ROAR_DIR_COMPLEX_OUT; |
---|
[1919] | 582 | } else { |
---|
[2494] | 583 | ROAR_ERR("add_output(*): unknown/unsupported subsystem '%s'", k); |
---|
[1991] | 584 | error++; |
---|
| 585 | } |
---|
| 586 | // DMX: |
---|
| 587 | } else if ( strcmp(k, "channel") == 0 ) { |
---|
| 588 | channel = atoi(v); |
---|
| 589 | if ( channel < 0 || channel > 65535 ) { |
---|
| 590 | ROAR_ERR("add_output(*): Invalide channel (not within 0..65535): %i", channel); |
---|
| 591 | channel = -1; |
---|
| 592 | error++; |
---|
[1919] | 593 | } |
---|
[1991] | 594 | } else if ( strcmp(k, "universe") == 0 ) { |
---|
| 595 | universe = atoi(v); |
---|
| 596 | if ( universe < 0 || universe > 255 ) { |
---|
| 597 | ROAR_ERR("add_output(*): Invalide universe (not within 0..255): %i", universe); |
---|
| 598 | universe = -1; |
---|
| 599 | error++; |
---|
| 600 | } |
---|
| 601 | |
---|
[2115] | 602 | } else if ( strcmp(k, "name") == 0 ) { |
---|
| 603 | if ( streams_set_name(stream, v) == -1 ) { |
---|
| 604 | ROAR_ERR("add_output(*): Can not set Stream name"); |
---|
| 605 | error++; |
---|
| 606 | } |
---|
[1991] | 607 | |
---|
[1032] | 608 | } else if ( strcmp(k, "meta") == 0 ) { |
---|
| 609 | streams_set_flag(stream, ROAR_FLAG_META); |
---|
[1117] | 610 | } else if ( strcmp(k, "sync") == 0 ) { |
---|
| 611 | sync = 1; |
---|
[1221] | 612 | } else if ( strcmp(k, "primary") == 0 ) { |
---|
| 613 | prim = 1; |
---|
| 614 | |
---|
| 615 | } else if ( strcmp(k, "cleanmeta") == 0 ) { |
---|
| 616 | streams_set_flag(stream, ROAR_FLAG_CLEANMETA); |
---|
| 617 | } else if ( strcmp(k, "autoconf") == 0 ) { |
---|
[1531] | 618 | streams_set_flag(stream, ROAR_FLAG_AUTOCONF); |
---|
[2413] | 619 | } else if ( strcmp(k, "recsource") == 0 ) { |
---|
| 620 | streams_set_flag(stream, ROAR_FLAG_RECSOURCE); |
---|
| 621 | } else if ( strcmp(k, "passmixer") == 0 ) { |
---|
| 622 | streams_set_flag(stream, ROAR_FLAG_PASSMIXER); |
---|
[938] | 623 | } else { |
---|
| 624 | ROAR_ERR("add_output(*): unknown option '%s'", k); |
---|
[1991] | 625 | error++; |
---|
| 626 | } |
---|
| 627 | |
---|
| 628 | if ( error ) { |
---|
[938] | 629 | streams_delete(stream); |
---|
[982] | 630 | if ( prim ) alive = 0; |
---|
[1208] | 631 | #ifdef ROAR_DRIVER_CODEC |
---|
| 632 | if ( to_free != NULL ) |
---|
| 633 | free(to_free); |
---|
| 634 | #endif |
---|
[938] | 635 | return -1; |
---|
| 636 | } |
---|
| 637 | |
---|
| 638 | k = strtok(NULL, ","); |
---|
| 639 | } |
---|
| 640 | |
---|
[2785] | 641 | ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = ?", drv, dev, opts); |
---|
| 642 | |
---|
[2511] | 643 | // set audio info... |
---|
| 644 | switch (dir) { |
---|
| 645 | case ROAR_DIR_LIGHT_OUT: |
---|
| 646 | switch (s->info.codec) { |
---|
| 647 | case ROAR_CODEC_DMX512: |
---|
| 648 | case -1: |
---|
| 649 | if ( s->info.rate == -1 ) |
---|
| 650 | s->info.rate = ROAR_OUTPUT_CFREQ; |
---|
| 651 | |
---|
| 652 | s->info.channels = 0; |
---|
| 653 | s->info.bits = 8; |
---|
| 654 | s->info.codec = ROAR_CODEC_DMX512; // in case codec == -1 |
---|
| 655 | break; |
---|
| 656 | } |
---|
| 657 | break; |
---|
| 658 | case ROAR_DIR_MIDI_OUT: |
---|
| 659 | switch (s->info.codec) { |
---|
| 660 | case ROAR_CODEC_MIDI: |
---|
| 661 | case -1: |
---|
| 662 | if ( s->info.rate == -1 ) |
---|
| 663 | s->info.rate = ROAR_MIDI_TICKS_PER_BEAT; |
---|
| 664 | |
---|
| 665 | s->info.channels = ROAR_MIDI_CHANNELS_DEFAULT; |
---|
| 666 | s->info.bits = ROAR_MIDI_BITS; |
---|
| 667 | s->info.codec = ROAR_CODEC_MIDI; // in case codec == -1 |
---|
| 668 | break; |
---|
| 669 | } |
---|
| 670 | break; |
---|
| 671 | case ROAR_DIR_RAW_OUT: |
---|
| 672 | if ( s->info.rate == -1 ) |
---|
| 673 | s->info.rate = 0; |
---|
| 674 | if ( s->info.bits == -1 ) |
---|
| 675 | s->info.bits = 0; |
---|
| 676 | if ( s->info.channels == -1 ) |
---|
| 677 | s->info.channels = 0; |
---|
| 678 | if ( s->info.codec == -1 ) |
---|
| 679 | s->info.codec = 0; |
---|
| 680 | break; |
---|
| 681 | } |
---|
| 682 | |
---|
| 683 | if ( s->info.rate == -1 ) |
---|
| 684 | s->info.rate = g_sa->rate; |
---|
| 685 | if ( s->info.bits == -1 ) |
---|
| 686 | s->info.bits = g_sa->bits; |
---|
| 687 | if ( s->info.channels == -1 ) |
---|
| 688 | s->info.channels = g_sa->channels; |
---|
| 689 | if ( s->info.codec == -1 ) |
---|
| 690 | s->info.codec = g_sa->codec; |
---|
| 691 | |
---|
| 692 | ROAR_DBG("add_output(*): s->info = {.rate=%i, .bits=%i, .channels=%i, .codec=%i}", s->info.rate, s->info.bits, s->info.channels, s->info.codec); |
---|
| 693 | |
---|
[1919] | 694 | if ( streams_set_dir(stream, dir, 1) == -1 ) { |
---|
| 695 | streams_delete(stream); |
---|
| 696 | return -1; |
---|
| 697 | } |
---|
| 698 | |
---|
[1208] | 699 | #ifdef ROAR_DRIVER_CODEC |
---|
| 700 | if ( to_free != NULL ) |
---|
| 701 | free(to_free); |
---|
| 702 | #endif |
---|
| 703 | |
---|
[2511] | 704 | if ( s->info.codec == ROAR_CODEC_ALAW || s->info.codec == ROAR_CODEC_MULAW ) |
---|
[941] | 705 | s->info.bits = 8; // needed to open OSS driver, will be overriden by codecfilter |
---|
| 706 | |
---|
[2511] | 707 | ROAR_STREAM_SERVER(s)->codec_orgi = s->info.codec; |
---|
[933] | 708 | |
---|
[2364] | 709 | if ( driver_openvio(&(ss->vio), &(ss->driver_id), drv, dev, &(s->info), -1, ss) == -1 ) { |
---|
[1581] | 710 | ss->driver_id = -1; // don't close a driver not opened... |
---|
| 711 | memset(&(ss->vio), 0, sizeof(struct roar_vio_calls)); |
---|
[933] | 712 | streams_delete(stream); |
---|
[938] | 713 | ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = -1", drv, dev, opts); |
---|
[982] | 714 | if ( prim ) alive = 0; |
---|
[933] | 715 | return -1; |
---|
| 716 | } |
---|
| 717 | |
---|
[1528] | 718 | roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_SSTREAMID, &stream); // ignore errors here |
---|
| 719 | roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_SSTREAM, s); // ignore errors here |
---|
| 720 | |
---|
[1221] | 721 | if ( blocks != -1 ) |
---|
| 722 | roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DBLOCKS, &blocks); |
---|
| 723 | |
---|
[1522] | 724 | if ( blocksize != -1 ) |
---|
| 725 | roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DBLKSIZE, &blocksize); |
---|
| 726 | |
---|
[1991] | 727 | // TODO: we shoudld *really* check for errors here... |
---|
| 728 | if ( channel != -1 ) { |
---|
| 729 | tu16 = channel; |
---|
| 730 | roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DMXSCHAN, &tu16); |
---|
| 731 | } |
---|
| 732 | if ( universe != -1 ) { |
---|
| 733 | tu16 = universe; |
---|
| 734 | roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DMXUNIV, &tu16); |
---|
| 735 | } |
---|
| 736 | |
---|
[1156] | 737 | ROAR_DBG("add_output(*): ss->driver_id=%i", ss->driver_id); |
---|
| 738 | |
---|
[938] | 739 | streams_set_fh(stream, -1); // update some internal structures |
---|
| 740 | |
---|
[2087] | 741 | if ( q > -1e6 ) { |
---|
| 742 | ROAR_DBG("add_output(*): setting q=%f", q); |
---|
| 743 | streams_ctl(stream, ROAR_CODECFILTER_CTL_SET_Q|ROAR_STREAM_CTL_TYPE_FLOAT, &q); |
---|
| 744 | } |
---|
| 745 | |
---|
[2490] | 746 | client_stream_add(g_self_client, stream); |
---|
[933] | 747 | |
---|
[1200] | 748 | if ( prim ) { |
---|
[961] | 749 | streams_mark_primary(stream); |
---|
[1200] | 750 | s->pos_rel_id = stream; |
---|
| 751 | } |
---|
[961] | 752 | |
---|
[1120] | 753 | if ( sync ) { |
---|
[1117] | 754 | streams_set_flag(stream, ROAR_FLAG_SYNC); |
---|
[1120] | 755 | } else { |
---|
| 756 | streams_reset_flag(stream, ROAR_FLAG_SYNC); |
---|
| 757 | } |
---|
[1117] | 758 | |
---|
[1926] | 759 | if ( f_mmap ) |
---|
| 760 | streams_set_flag(stream, ROAR_FLAG_MMAP); |
---|
| 761 | |
---|
[2511] | 762 | ROAR_DBG("add_output(*): s->info = {.rate=%i, .bits=%i, .channels=%i, .codec=%i}", s->info.rate, s->info.bits, s->info.channels, s->info.codec); |
---|
[933] | 763 | return 0; |
---|
[932] | 764 | } |
---|
| 765 | |
---|
[1993] | 766 | |
---|
| 767 | // SLP: |
---|
| 768 | void register_slp_callback(SLPHandle hslp, SLPError errcode, void * cookie) { |
---|
| 769 | /* return the error code in the cookie */ |
---|
| 770 | *(SLPError*)cookie = errcode; |
---|
| 771 | } |
---|
| 772 | |
---|
| 773 | int register_slp (int unreg, char * sockname) { |
---|
| 774 | #ifdef ROAR_HAVE_LIBSLP |
---|
| 775 | static int regged = 0; |
---|
| 776 | static char * sn = NULL; |
---|
| 777 | SLPError err; |
---|
| 778 | SLPError callbackerr; |
---|
| 779 | SLPHandle hslp; |
---|
| 780 | char addr[1024]; |
---|
| 781 | char attr[1024] = ""; |
---|
[2017] | 782 | char * location; |
---|
[1993] | 783 | |
---|
| 784 | if ( sockname != NULL ) |
---|
| 785 | sn = sockname; |
---|
| 786 | |
---|
[3076] | 787 | snprintf(addr, sizeof(addr), ROAR_SLP_URL_TYPE_ROAR "://%s", sn); |
---|
[1993] | 788 | |
---|
| 789 | err = SLPOpen("en", SLP_FALSE, &hslp); |
---|
| 790 | |
---|
| 791 | if (err != SLP_OK) { |
---|
| 792 | ROAR_ERR("Error opening slp handle: Error #%i", err); |
---|
| 793 | return -1; |
---|
| 794 | } |
---|
| 795 | |
---|
| 796 | if (!unreg) { |
---|
[2017] | 797 | |
---|
| 798 | if ( SLPEscape(g_config->location, &location, SLP_FALSE) != SLP_OK ) { |
---|
| 799 | ROAR_ERR("Error using SLPEscape() on server location, really bad!"); |
---|
| 800 | SLPClose(hslp); |
---|
| 801 | return -1; |
---|
| 802 | } |
---|
| 803 | |
---|
[1995] | 804 | snprintf(attr, sizeof(attr), "(wave-rate=%i),(wave-channels=%i),(wave-bits=%i)," |
---|
[2494] | 805 | #ifndef ROAR_WITHOUT_DCOMP_LIGHT |
---|
| 806 | "(light-channels=%i)," |
---|
| 807 | #endif |
---|
| 808 | "(location=%s)", |
---|
[1995] | 809 | g_sa->rate, g_sa->channels, g_sa->bits, |
---|
[2494] | 810 | #ifndef ROAR_WITHOUT_DCOMP_LIGHT |
---|
[2017] | 811 | g_light_state.channels, |
---|
[2494] | 812 | #endif |
---|
[2017] | 813 | location |
---|
[1995] | 814 | ); |
---|
| 815 | |
---|
[1993] | 816 | /* Register a service with SLP */ |
---|
| 817 | err = SLPReg(hslp, |
---|
| 818 | addr, |
---|
| 819 | SLP_LIFETIME_MAXIMUM, |
---|
| 820 | 0, |
---|
| 821 | attr, |
---|
| 822 | SLP_TRUE, |
---|
| 823 | register_slp_callback, |
---|
| 824 | &callbackerr); |
---|
[1994] | 825 | regged = 1; |
---|
| 826 | } else if ( unreg && regged ) { |
---|
| 827 | err = SLPDereg(hslp, addr, register_slp_callback, &callbackerr); |
---|
| 828 | regged = 0; |
---|
| 829 | } else { |
---|
| 830 | SLPClose(hslp); |
---|
| 831 | return -1; |
---|
| 832 | } |
---|
[1993] | 833 | |
---|
[1994] | 834 | /* err may contain an error code that occurred as the slp library */ |
---|
| 835 | /* _prepared_ to make the call. */ |
---|
| 836 | if ( err != SLP_OK ) { |
---|
| 837 | ROAR_ERR("Error (de)registering service with slp: Error #%i", err); |
---|
| 838 | return -1; |
---|
| 839 | } |
---|
| 840 | |
---|
| 841 | /* callbackerr may contain an error code (that was assigned through */ |
---|
| 842 | /* the callback cookie) that occurred as slp packets were sent on */ |
---|
| 843 | /* the wire */ |
---|
| 844 | if (callbackerr != SLP_OK) { |
---|
| 845 | ROAR_ERR("Error (de)registering service with slp: Error #%i", callbackerr); |
---|
| 846 | return -1; |
---|
[1993] | 847 | } |
---|
| 848 | |
---|
| 849 | SLPClose(hslp); |
---|
| 850 | return 0; |
---|
| 851 | #else |
---|
| 852 | return -1; |
---|
| 853 | #endif |
---|
| 854 | } |
---|
| 855 | |
---|
| 856 | |
---|
| 857 | // MAIN: |
---|
| 858 | |
---|
[1503] | 859 | #ifdef ROAR_HAVE_MAIN_ARGS |
---|
[0] | 860 | int main (int argc, char * argv[]) { |
---|
[1503] | 861 | #else |
---|
| 862 | int main (void) { |
---|
| 863 | #endif |
---|
| 864 | #ifdef ROAR_HAVE_MAIN_ARGS |
---|
[0] | 865 | int i; |
---|
| 866 | char * k; |
---|
[1503] | 867 | #endif |
---|
[2787] | 868 | #if defined(ROAR_SUPPORT_LISTEN) && defined(ROAR_HAVE_GETUID) |
---|
[905] | 869 | char user_sock[80] = {0}; |
---|
[1494] | 870 | #endif |
---|
[1609] | 871 | struct roar_audio_info sa, max_sa; |
---|
| 872 | struct roard_config config; |
---|
[1486] | 873 | #ifdef ROAR_HAVE_FORK |
---|
[905] | 874 | int daemon = 0; |
---|
[1486] | 875 | #endif |
---|
[905] | 876 | int realtime = 0; |
---|
| 877 | int sysclocksync = 0; |
---|
[1207] | 878 | char * driver = NULL; |
---|
| 879 | char * device = NULL; |
---|
[1503] | 880 | #ifdef ROAR_HAVE_MAIN_ARGS |
---|
[1207] | 881 | char * opts = NULL; |
---|
[1503] | 882 | #endif |
---|
[60] | 883 | // char * server = ROAR_DEFAULT_SOCK_GLOBAL; |
---|
[1494] | 884 | #ifdef ROAR_SUPPORT_LISTEN |
---|
[2530] | 885 | int port = ROAR_DEFAULT_PORT; |
---|
| 886 | char * sock_addr = NULL; |
---|
| 887 | int sock_proto = ROAR_PROTO_ROARAUDIO; |
---|
[3252] | 888 | int sock_dir = -1; |
---|
| 889 | struct roar_audio_info sock_info = {0, 0, 0, 0}; |
---|
[1494] | 890 | #endif |
---|
[0] | 891 | int drvid; |
---|
[2486] | 892 | #ifndef ROAR_WITHOUT_DCOMP_SOURCES |
---|
[550] | 893 | char * s_drv = "cf"; |
---|
[1110] | 894 | char * s_dev = NULL; |
---|
[444] | 895 | char * s_con = NULL; |
---|
| 896 | char * s_opt = NULL; |
---|
| 897 | int s_prim = 0; |
---|
[2486] | 898 | #endif |
---|
[1207] | 899 | char * o_drv = getenv("ROAR_DRIVER"); |
---|
| 900 | char * o_dev = getenv("ROAR_DEVICE"); |
---|
[932] | 901 | char * o_opts = NULL; |
---|
[961] | 902 | int o_prim = 0; |
---|
[1145] | 903 | int o_count = 0; |
---|
[2494] | 904 | #ifndef ROAR_WITHOUT_DCOMP_LIGHT |
---|
[1923] | 905 | int light_channels = LIGHT_CHANNELS_DEFAULT; |
---|
[2494] | 906 | #endif |
---|
[2751] | 907 | #ifdef ROAR_DEFAULT_SOCKGRP |
---|
[450] | 908 | char * sock_grp = ROAR_DEFAULT_SOCKGRP; |
---|
[2751] | 909 | #else |
---|
| 910 | char * sock_grp = NULL; |
---|
| 911 | #endif |
---|
[444] | 912 | char * sock_user = NULL; |
---|
[1494] | 913 | #ifdef ROAR_SUPPORT_LISTEN |
---|
[517] | 914 | int sock_type = ROAR_SOCKET_TYPE_UNKNOWN; |
---|
[1494] | 915 | #endif |
---|
[1993] | 916 | #ifdef ROAR_HAVE_LIBSLP |
---|
| 917 | int reg_slp = 0; |
---|
| 918 | #endif |
---|
[1486] | 919 | #ifdef ROAR_HAVE_CHROOT |
---|
[444] | 920 | char * chrootdir = NULL; |
---|
[1486] | 921 | #endif |
---|
| 922 | #if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX) |
---|
[446] | 923 | struct group * grp = NULL; |
---|
[1486] | 924 | #endif |
---|
| 925 | #if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX) |
---|
[446] | 926 | struct passwd * pwd = NULL; |
---|
[1486] | 927 | #endif |
---|
| 928 | #ifdef ROAR_HAVE_GETSERVBYNAME |
---|
[446] | 929 | struct servent * serv = NULL; |
---|
[1486] | 930 | #endif |
---|
[0] | 931 | DRIVER_USERDATA_T drvinst; |
---|
[39] | 932 | struct roar_client * self = NULL; |
---|
[508] | 933 | #ifdef ROAR_HAVE_LIBDNET |
---|
| 934 | char decnethost[80]; |
---|
| 935 | #endif |
---|
[2104] | 936 | #ifdef SUPPORT_PIDFILE |
---|
| 937 | struct roar_vio_calls pidfile_vio; |
---|
| 938 | #endif |
---|
[0] | 939 | |
---|
[2784] | 940 | ROAR_DBG("main(*): starting roard..."); |
---|
| 941 | |
---|
[0] | 942 | g_standby = 0; |
---|
[920] | 943 | g_autostandby = 0; |
---|
[982] | 944 | alive = 1; |
---|
[1494] | 945 | #ifdef ROAR_SUPPORT_LISTEN |
---|
[1155] | 946 | g_no_listen = 0; |
---|
[1494] | 947 | #else |
---|
| 948 | g_terminate = 1; |
---|
| 949 | #endif |
---|
[0] | 950 | |
---|
[2970] | 951 | g_verbose = ROAR_DBG_INFO_NONE; |
---|
| 952 | |
---|
[0] | 953 | sa.bits = ROAR_BITS_DEFAULT; |
---|
| 954 | sa.channels = ROAR_CHANNELS_DEFAULT; |
---|
| 955 | sa.rate = ROAR_RATE_DEFAULT; |
---|
| 956 | sa.codec = ROAR_CODEC_DEFAULT; |
---|
| 957 | |
---|
[1609] | 958 | g_sa = &sa; |
---|
| 959 | g_max_sa = &max_sa; |
---|
| 960 | |
---|
| 961 | memcpy(g_max_sa, g_sa, sizeof(max_sa)); |
---|
| 962 | |
---|
| 963 | g_config = &config; |
---|
| 964 | |
---|
| 965 | if ( init_config() == -1 ) { |
---|
| 966 | ROAR_ERR("Can not init default config!"); |
---|
| 967 | return 1; |
---|
| 968 | } |
---|
[0] | 969 | |
---|
[2968] | 970 | // load config |
---|
| 971 | roar_libroar_get_config(); |
---|
| 972 | |
---|
[2530] | 973 | #ifdef ROAR_SUPPORT_LISTEN |
---|
| 974 | if ( init_listening() == -1 ) { |
---|
| 975 | ROAR_ERR("Can not init listening sockets!"); |
---|
| 976 | return 1; |
---|
| 977 | } |
---|
| 978 | #endif |
---|
| 979 | |
---|
[2500] | 980 | #ifndef ROAR_WITHOUT_DCOMP_MIDI |
---|
[1924] | 981 | if ( midi_init_config() == -1 ) { |
---|
| 982 | ROAR_ERR("Can not init MIDI config!"); |
---|
| 983 | return 1; |
---|
| 984 | } |
---|
[2500] | 985 | #endif |
---|
[60] | 986 | |
---|
[2489] | 987 | #ifndef ROAR_WITHOUT_DCOMP_SSYNTH |
---|
[2451] | 988 | if ( ssynth_init_config() == -1 ) { |
---|
| 989 | ROAR_ERR("Can not init ssynth config!"); |
---|
| 990 | return 1; |
---|
| 991 | } |
---|
[2489] | 992 | #endif |
---|
[2451] | 993 | |
---|
[2718] | 994 | #ifndef ROAR_WITHOUT_DCOMP_RDTCS |
---|
| 995 | if ( rdtcs_init_config() == -1 ) { |
---|
| 996 | ROAR_ERR("Can not init RDTCS config!"); |
---|
| 997 | return 1; |
---|
| 998 | } |
---|
| 999 | #endif |
---|
| 1000 | |
---|
[1494] | 1001 | #ifdef ROAR_SUPPORT_LISTEN |
---|
[2767] | 1002 | #ifndef ROAR_TARGET_WIN32 |
---|
[2530] | 1003 | sock_addr = ROAR_DEFAULT_SOCK_GLOBAL; |
---|
[2767] | 1004 | #else |
---|
| 1005 | sock_addr = ROAR_DEFAULT_HOST; |
---|
| 1006 | #endif |
---|
| 1007 | |
---|
[1753] | 1008 | #ifdef ROAR_HAVE_GETUID |
---|
[1486] | 1009 | if ( getuid() != 0 && getenv("HOME") != NULL ) { |
---|
| 1010 | snprintf(user_sock, 79, "%s/%s", (char*)getenv("HOME"), ROAR_DEFAULT_SOCK_USER); |
---|
[2530] | 1011 | sock_addr = user_sock; |
---|
[60] | 1012 | } |
---|
[1753] | 1013 | #endif |
---|
[60] | 1014 | |
---|
[279] | 1015 | if ( getenv("ROAR_SERVER") != NULL ) |
---|
[2530] | 1016 | sock_addr = getenv("ROAR_SERVER"); |
---|
[1494] | 1017 | #endif |
---|
[279] | 1018 | |
---|
[63] | 1019 | if ( clients_init() == -1 ) { |
---|
| 1020 | ROAR_ERR("Can not init clients!"); |
---|
| 1021 | return 1; |
---|
| 1022 | } |
---|
| 1023 | |
---|
| 1024 | if ( streams_init() == -1 ) { |
---|
| 1025 | ROAR_ERR("Can not init streams!"); |
---|
| 1026 | return 1; |
---|
| 1027 | } |
---|
| 1028 | |
---|
[64] | 1029 | if ( (g_self_client = clients_new()) == -1 ) { |
---|
| 1030 | ROAR_ERR("Can not create self client!"); |
---|
| 1031 | return 1; |
---|
| 1032 | } |
---|
| 1033 | |
---|
[2485] | 1034 | #ifndef ROAR_WITHOUT_DCOMP_SOURCES |
---|
[0] | 1035 | if ( sources_init() == -1 ) { |
---|
| 1036 | ROAR_ERR("Can not init sources!"); |
---|
| 1037 | return 1; |
---|
| 1038 | } |
---|
| 1039 | |
---|
[64] | 1040 | if ( (sources_set_client(g_self_client)) == -1 ) { |
---|
| 1041 | ROAR_ERR("Can not init set source client!"); |
---|
| 1042 | return 1; |
---|
| 1043 | } |
---|
[2485] | 1044 | #endif |
---|
[64] | 1045 | |
---|
[1503] | 1046 | #ifdef ROAR_HAVE_MAIN_ARGS |
---|
[0] | 1047 | for (i = 1; i < argc; i++) { |
---|
| 1048 | k = argv[i]; |
---|
| 1049 | |
---|
| 1050 | if ( strcmp(k, "-h") == 0 || strcmp(k, "--help") == 0 ) { |
---|
| 1051 | usage(); |
---|
| 1052 | return 0; |
---|
| 1053 | |
---|
[2109] | 1054 | } else if ( strcmp(k, "--start") == 0 ) { |
---|
| 1055 | // this is a no op |
---|
[579] | 1056 | } else if ( strcmp(k, "--restart") == 0 ) { |
---|
[1494] | 1057 | #ifdef ROAR_SUPPORT_LISTEN |
---|
[2530] | 1058 | if ( restart_server(sock_addr, 1) == -1 ) { |
---|
| 1059 | ROAR_WARN("Can not terminate old server (not running at %s?), tring to continue anyway", sock_addr); |
---|
[579] | 1060 | } |
---|
[1494] | 1061 | #else |
---|
| 1062 | ROAR_ERR("--restart not supported"); |
---|
| 1063 | #endif |
---|
[2109] | 1064 | } else if ( strcmp(k, "--shutdown") == 0 ) { |
---|
| 1065 | #ifdef ROAR_SUPPORT_LISTEN |
---|
[2530] | 1066 | if ( restart_server(sock_addr, 1) == -1 ) { |
---|
| 1067 | ROAR_WARN("Can not terminate old server (not running at %s?)", sock_addr); |
---|
[2109] | 1068 | return 1; |
---|
| 1069 | } |
---|
| 1070 | return 0; |
---|
| 1071 | #else |
---|
| 1072 | ROAR_ERR("--shutdown not supported"); |
---|
| 1073 | return 1; |
---|
| 1074 | #endif |
---|
| 1075 | } else if ( strcmp(k, "--stop") == 0 ) { |
---|
| 1076 | #ifdef ROAR_SUPPORT_LISTEN |
---|
[2530] | 1077 | if ( restart_server(sock_addr, 0) == -1 ) { |
---|
| 1078 | ROAR_WARN("Can not stop old server (not running at %s?)", sock_addr); |
---|
[2109] | 1079 | return 1; |
---|
| 1080 | } |
---|
| 1081 | return 0; |
---|
| 1082 | #else |
---|
| 1083 | ROAR_ERR("--stop not supported"); |
---|
| 1084 | return 1; |
---|
| 1085 | #endif |
---|
| 1086 | |
---|
[579] | 1087 | |
---|
[775] | 1088 | } else if ( strcmp(k, "--demon") == 0 || strcmp(k, "--daemon") == 0 ) { |
---|
[1486] | 1089 | #ifdef ROAR_HAVE_FORK |
---|
[775] | 1090 | daemon = 1; |
---|
[1486] | 1091 | #else |
---|
| 1092 | ROAR_ERR("--daemon not supported"); |
---|
| 1093 | #endif |
---|
[2970] | 1094 | } else if ( strcmp(k, "--verbose") == 0 ) { |
---|
| 1095 | g_verbose++; |
---|
[71] | 1096 | } else if ( strcmp(k, "--terminate") == 0 ) { |
---|
| 1097 | g_terminate = 1; |
---|
[905] | 1098 | } else if ( strcmp(k, "--sysclocksync") == 0 ) { |
---|
| 1099 | sysclocksync = 1000; |
---|
[275] | 1100 | } else if ( strcmp(k, "--realtime") == 0 ) { |
---|
[276] | 1101 | realtime++; |
---|
[444] | 1102 | } else if ( strcmp(k, "--chroot") == 0 ) { |
---|
[1486] | 1103 | #ifdef ROAR_HAVE_CHROOT |
---|
[444] | 1104 | chrootdir = argv[++i]; |
---|
[1486] | 1105 | #else |
---|
| 1106 | ROAR_ERR("--chroot not supported"); |
---|
| 1107 | i++; |
---|
| 1108 | #endif |
---|
[444] | 1109 | } else if ( strcmp(k, "--setgid") == 0 ) { |
---|
[1486] | 1110 | #ifdef ROAR_HAVE_SETGID |
---|
[444] | 1111 | setids |= R_SETGID; |
---|
[1486] | 1112 | #else |
---|
| 1113 | ROAR_ERR("--setgid not supported"); |
---|
| 1114 | #endif |
---|
[444] | 1115 | } else if ( strcmp(k, "--setuid") == 0 ) { |
---|
[1486] | 1116 | #ifdef ROAR_HAVE_SETUID |
---|
[444] | 1117 | setids |= R_SETUID; |
---|
[1486] | 1118 | #else |
---|
| 1119 | ROAR_ERR("--setuid not supported"); |
---|
| 1120 | #endif |
---|
[2017] | 1121 | } else if ( strcmp(k, "--location") == 0 ) { |
---|
| 1122 | g_config->location = argv[++i]; |
---|
[2104] | 1123 | } else if ( strcmp(k, "--pidfile") == 0 ) { |
---|
| 1124 | #ifdef SUPPORT_PIDFILE |
---|
| 1125 | pidfile = argv[++i]; |
---|
| 1126 | #else |
---|
| 1127 | ROAR_ERR("--pidfile not supported"); |
---|
| 1128 | i++; |
---|
| 1129 | #endif |
---|
[68] | 1130 | |
---|
[280] | 1131 | } else if ( strcmp(k, "--list-cf") == 0 ) { |
---|
| 1132 | print_codecfilterlist(); |
---|
| 1133 | return 0; |
---|
| 1134 | |
---|
[0] | 1135 | } else if ( strcmp(k, "-R") == 0 || strcmp(k, "--rate") == 0 ) { |
---|
| 1136 | sa.rate = atoi(argv[++i]); |
---|
| 1137 | } else if ( strcmp(k, "-B") == 0 || strcmp(k, "--bits") == 0 ) { |
---|
| 1138 | sa.bits = atoi(argv[++i]); |
---|
| 1139 | } else if ( strcmp(k, "-C") == 0 || strcmp(k, "--chans") == 0 ) { |
---|
| 1140 | sa.channels = atoi(argv[++i]); |
---|
| 1141 | |
---|
[2339] | 1142 | } else if ( strcmp(k, "--stream-flags") == 0 ) { |
---|
| 1143 | if ( update_stream_flags(argv[++i]) == -1 ) { |
---|
| 1144 | ROAR_ERR("Can not set stream flags"); |
---|
| 1145 | return 1; |
---|
| 1146 | } |
---|
| 1147 | |
---|
[0] | 1148 | } else if ( strcmp(k, "-d") == 0 || strcmp(k, "--driver") == 0 ) { |
---|
| 1149 | driver = argv[++i]; |
---|
| 1150 | if ( strcmp(driver, "list") == 0 ) { |
---|
[974] | 1151 | ROAR_WARN("The option is obsolete, use --list-driver!"); |
---|
[0] | 1152 | print_driverlist(); |
---|
[973] | 1153 | return 0; |
---|
[0] | 1154 | } |
---|
| 1155 | } else if ( strcmp(k, "-D") == 0 || strcmp(k, "--device") == 0 ) { |
---|
| 1156 | device = argv[++i]; |
---|
| 1157 | } else if ( strcmp(k, "-dO") == 0 ) { |
---|
| 1158 | opts = argv[++i]; |
---|
[973] | 1159 | } else if ( strcmp(k, "--list-driver") == 0 ) { |
---|
| 1160 | print_driverlist(); |
---|
| 1161 | return 0; |
---|
[0] | 1162 | |
---|
[932] | 1163 | } else if ( strcmp(k, "-o") == 0 || strcmp(k, "--odriver") == 0 ) { |
---|
| 1164 | o_drv = argv[++i]; |
---|
| 1165 | } else if ( strcmp(k, "-O") == 0 || strcmp(k, "--odevice") == 0 ) { |
---|
| 1166 | o_dev = argv[++i]; |
---|
| 1167 | } else if ( strcmp(k, "-oO") == 0 ) { |
---|
| 1168 | o_opts = argv[++i]; |
---|
[961] | 1169 | } else if ( strcmp(k, "-oP") == 0 ) { |
---|
| 1170 | o_prim = 1; |
---|
[932] | 1171 | } else if ( strcmp(k, "-oN") == 0 ) { |
---|
[1145] | 1172 | if ( add_output(o_drv, o_dev, o_opts, o_prim, o_count) != -1 ) |
---|
| 1173 | o_count++; |
---|
| 1174 | |
---|
[961] | 1175 | o_drv = o_dev = o_opts = NULL; |
---|
| 1176 | o_prim = 0; |
---|
[932] | 1177 | |
---|
[0] | 1178 | } else if ( strcmp(k, "-s") == 0 || strcmp(k, "--source") == 0 ) { |
---|
[2485] | 1179 | #ifndef ROAR_WITHOUT_DCOMP_SOURCES |
---|
[550] | 1180 | s_drv = argv[++i]; |
---|
[2485] | 1181 | #else |
---|
| 1182 | ROAR_ERR("main(*): No support for sources compiled in"); |
---|
| 1183 | #endif |
---|
[550] | 1184 | } else if ( strcmp(k, "-S") == 0 ) { |
---|
[2485] | 1185 | #ifndef ROAR_WITHOUT_DCOMP_SOURCES |
---|
[1110] | 1186 | s_dev = argv[++i]; |
---|
[2485] | 1187 | #else |
---|
| 1188 | ROAR_ERR("main(*): No support for sources compiled in"); |
---|
| 1189 | #endif |
---|
[0] | 1190 | } else if ( strcmp(k, "-sO") == 0 ) { |
---|
[2485] | 1191 | #ifndef ROAR_WITHOUT_DCOMP_SOURCES |
---|
[0] | 1192 | s_opt = argv[++i]; |
---|
[2485] | 1193 | #else |
---|
| 1194 | ROAR_ERR("main(*): No support for sources compiled in"); |
---|
| 1195 | #endif |
---|
[0] | 1196 | } else if ( strcmp(k, "-sC") == 0 ) { |
---|
[2485] | 1197 | #ifndef ROAR_WITHOUT_DCOMP_SOURCES |
---|
[0] | 1198 | s_con = argv[++i]; |
---|
[2485] | 1199 | #else |
---|
| 1200 | ROAR_ERR("main(*): No support for sources compiled in"); |
---|
| 1201 | #endif |
---|
[0] | 1202 | } else if ( strcmp(k, "-sP") == 0 ) { |
---|
[2485] | 1203 | #ifndef ROAR_WITHOUT_DCOMP_SOURCES |
---|
[0] | 1204 | s_prim = 1; |
---|
[2485] | 1205 | #else |
---|
| 1206 | ROAR_ERR("main(*): No support for sources compiled in"); |
---|
| 1207 | #endif |
---|
[1110] | 1208 | } else if ( strcmp(k, "-sN") == 0 ) { |
---|
[2485] | 1209 | #ifndef ROAR_WITHOUT_DCOMP_SOURCES |
---|
[1110] | 1210 | if ( sources_add(s_drv, s_dev, s_con, s_opt, s_prim) == -1 ) { |
---|
| 1211 | ROAR_ERR("main(*): adding source '%s' via '%s' failed!", s_dev, s_drv); |
---|
| 1212 | } |
---|
| 1213 | s_opt = s_dev = s_con = NULL; |
---|
| 1214 | s_drv = "cf"; |
---|
| 1215 | s_prim = 0; |
---|
[2485] | 1216 | #else |
---|
| 1217 | ROAR_ERR("main(*): No support for sources compiled in"); |
---|
| 1218 | #endif |
---|
[2270] | 1219 | } else if ( strcmp(k, "--list-sources") == 0 ) { |
---|
[2485] | 1220 | #ifndef ROAR_WITHOUT_DCOMP_SOURCES |
---|
[2270] | 1221 | print_sourcelist(); |
---|
| 1222 | return 0; |
---|
[2485] | 1223 | #else |
---|
| 1224 | ROAR_ERR("main(*): No support for sources compiled in"); |
---|
[2504] | 1225 | return 1; |
---|
[2485] | 1226 | #endif |
---|
[0] | 1227 | |
---|
[1923] | 1228 | } else if ( strcmp(k, "--light-channels") == 0 ) { |
---|
[2494] | 1229 | #ifndef ROAR_WITHOUT_DCOMP_LIGHT |
---|
[1923] | 1230 | light_channels = atoi(argv[++i]); |
---|
[2494] | 1231 | #else |
---|
| 1232 | ROAR_WARN("main(*): no light subsystem compiled in"); |
---|
| 1233 | #endif |
---|
[1923] | 1234 | |
---|
[2725] | 1235 | } else if ( strcmp(k, "--rds-pi") == 0 ) { |
---|
| 1236 | #ifndef ROAR_WITHOUT_DCOMP_RDTCS |
---|
| 1237 | g_rdtcs.rds.pi = atoi(argv[++i]); |
---|
| 1238 | #else |
---|
| 1239 | ROAR_WARN("main(*): no RDTCS subsystem compiled in"); |
---|
| 1240 | #endif |
---|
[2728] | 1241 | } else if ( strcmp(k, "--rds-ps") == 0 ) { |
---|
| 1242 | #ifndef ROAR_WITHOUT_DCOMP_RDTCS |
---|
| 1243 | if ( rdtcs_rds_set_ps(argv[++i]) == -1 ) { |
---|
[2729] | 1244 | ROAR_ERR("Can not set RDS PS to '%s' (longer than 8 chars?)", argv[i]); |
---|
[2728] | 1245 | return 1; |
---|
| 1246 | } |
---|
| 1247 | #else |
---|
| 1248 | ROAR_WARN("main(*): no RDTCS subsystem compiled in"); |
---|
| 1249 | #endif |
---|
| 1250 | } else if ( strcmp(k, "--rds-pty") == 0 ) { |
---|
| 1251 | #ifndef ROAR_WITHOUT_DCOMP_RDTCS |
---|
| 1252 | if ( rdtcs_rds_set_pty(argv[++i]) == -1 ) { |
---|
[2729] | 1253 | ROAR_ERR("Can not set RDS PTY to '%s'", argv[i]); |
---|
[2728] | 1254 | return 1; |
---|
| 1255 | } |
---|
| 1256 | #else |
---|
| 1257 | ROAR_WARN("main(*): no RDTCS subsystem compiled in"); |
---|
| 1258 | #endif |
---|
| 1259 | } else if ( strcmp(k, "--rds-tp") == 0 ) { |
---|
| 1260 | #ifndef ROAR_WITHOUT_DCOMP_RDTCS |
---|
| 1261 | if ( rdtcs_rds_set_flag(RDTCS_RDS_FLAG_TP, 0) == -1 ) { |
---|
| 1262 | ROAR_ERR("Can not set RDS TP flag"); |
---|
| 1263 | return 1; |
---|
| 1264 | } |
---|
| 1265 | #else |
---|
| 1266 | ROAR_WARN("main(*): no RDTCS subsystem compiled in"); |
---|
| 1267 | #endif |
---|
| 1268 | } else if ( strcmp(k, "--rds-ct") == 0 ) { |
---|
| 1269 | #ifndef ROAR_WITHOUT_DCOMP_RDTCS |
---|
| 1270 | if ( rdtcs_rds_set_flag(RDTCS_RDS_FLAG_CT, 0) == -1 ) { |
---|
| 1271 | ROAR_ERR("Can not set RDS CT flag"); |
---|
| 1272 | return 1; |
---|
| 1273 | } |
---|
| 1274 | #else |
---|
| 1275 | ROAR_WARN("main(*): no RDTCS subsystem compiled in"); |
---|
| 1276 | #endif |
---|
| 1277 | |
---|
[2725] | 1278 | |
---|
[1924] | 1279 | } else if ( strcmp(k, "--midi-no-console") == 0 ) { |
---|
[2487] | 1280 | #ifndef ROAR_WITHOUT_DCOMP_CB |
---|
[1924] | 1281 | midi_config.init_cb = 0; |
---|
[2487] | 1282 | #else |
---|
| 1283 | // no warning here as this is the disable option |
---|
| 1284 | #endif |
---|
[2444] | 1285 | } else if ( strcmp(k, "--midi-console-enable") == 0 ) { |
---|
[2487] | 1286 | #ifndef ROAR_WITHOUT_DCOMP_CB |
---|
[2444] | 1287 | midi_config.init_cb = 1; |
---|
[2487] | 1288 | #else |
---|
| 1289 | ROAR_ERR("main(*): No support for MIDI subsystem part CB compiled in"); |
---|
| 1290 | #endif |
---|
[1924] | 1291 | } else if ( strcmp(k, "--midi-console") == 0 ) { |
---|
[2487] | 1292 | #ifndef ROAR_WITHOUT_DCOMP_CB |
---|
[1924] | 1293 | midi_config.console_dev = argv[++i]; |
---|
[2444] | 1294 | midi_config.init_cb = 1; |
---|
[2487] | 1295 | #else |
---|
| 1296 | ROAR_ERR("main(*): No support for MIDI subsystem part CB compiled in"); |
---|
| 1297 | #endif |
---|
[1924] | 1298 | |
---|
[2451] | 1299 | } else if ( strcmp(k, "--ssynth-enable") == 0 ) { |
---|
[2489] | 1300 | #ifndef ROAR_WITHOUT_DCOMP_SSYNTH |
---|
[2451] | 1301 | ssynth_conf.enable = 1; |
---|
[2489] | 1302 | #else |
---|
| 1303 | ROAR_ERR("main(*): No support for ssynth compiled in"); |
---|
| 1304 | #endif |
---|
[2451] | 1305 | } else if ( strcmp(k, "--ssynth-disable") == 0 ) { |
---|
[2489] | 1306 | #ifndef ROAR_WITHOUT_DCOMP_SSYNTH |
---|
[2451] | 1307 | ssynth_conf.enable = 0; |
---|
[2489] | 1308 | #else |
---|
| 1309 | // we can safely ignore the disable |
---|
| 1310 | #endif |
---|
[2451] | 1311 | |
---|
[0] | 1312 | } else if ( strcmp(k, "-p") == 0 || strcmp(k, "--port") == 0 ) { |
---|
[447] | 1313 | // This is only usefull in INET not UNIX mode. |
---|
[1494] | 1314 | #ifdef ROAR_SUPPORT_LISTEN |
---|
[2530] | 1315 | if ( *sock_addr == '/' ) |
---|
| 1316 | sock_addr = ROAR_DEFAULT_HOST; |
---|
[447] | 1317 | |
---|
[446] | 1318 | errno = 0; |
---|
| 1319 | if ( (port = atoi(argv[++i])) < 1 ) { |
---|
[1486] | 1320 | #ifdef ROAR_HAVE_GETSERVBYNAME |
---|
[446] | 1321 | if ( (serv = getservbyname(argv[i], "tcp")) == NULL ) { |
---|
| 1322 | ROAR_ERR("Unknown service: %s: %s", argv[i], strerror(errno)); |
---|
| 1323 | return 1; |
---|
| 1324 | } |
---|
| 1325 | // NOTE: we need to use ROAR_NET2HOST16() here even if s_port is of type int! |
---|
| 1326 | ROAR_DBG("main(*): serv = {s_name='%s', s_aliases={...}, s_port=%i, s_proto='%s'}", |
---|
| 1327 | serv->s_name, ROAR_NET2HOST16(serv->s_port), serv->s_proto); |
---|
| 1328 | port = ROAR_NET2HOST16(serv->s_port); |
---|
[1486] | 1329 | #else |
---|
| 1330 | ROAR_ERR("invalite port number: %s", argv[i]); |
---|
| 1331 | return 1; |
---|
| 1332 | #endif |
---|
[446] | 1333 | } |
---|
[1494] | 1334 | #endif |
---|
[1115] | 1335 | } else if ( strcmp(k, "-b") == 0 || strcmp(k, "--bind") == 0 || strcmp(k, "--sock") == 0 ) { |
---|
[1494] | 1336 | #ifdef ROAR_SUPPORT_LISTEN |
---|
[2530] | 1337 | sock_addr = argv[++i]; |
---|
| 1338 | #endif |
---|
| 1339 | |
---|
| 1340 | } else if ( strcmp(k, "--proto") == 0 ) { |
---|
| 1341 | #ifdef ROAR_SUPPORT_LISTEN |
---|
[2549] | 1342 | if ( (sock_proto = roar_str2proto(argv[++i])) == -1 ) { |
---|
[2530] | 1343 | ROAR_ERR("Unknown protocol: %s", argv[i]); |
---|
| 1344 | return 1; |
---|
| 1345 | } |
---|
[1494] | 1346 | #endif |
---|
[3252] | 1347 | } else if ( strcmp(k, "--proto-dir") == 0 ) { |
---|
| 1348 | #ifdef ROAR_SUPPORT_LISTEN |
---|
| 1349 | if ( (sock_dir = roar_str2dir(argv[++i])) == -1 ) { |
---|
| 1350 | ROAR_ERR("Unknown stream direction: %s", argv[i]); |
---|
| 1351 | return 1; |
---|
| 1352 | } |
---|
| 1353 | #endif |
---|
| 1354 | } else if ( strcmp(k, "--proto-rate") == 0 ) { |
---|
| 1355 | #ifdef ROAR_SUPPORT_LISTEN |
---|
| 1356 | sock_info.rate = atoi(argv[++i]); |
---|
| 1357 | #endif |
---|
| 1358 | } else if ( strcmp(k, "--proto-bits") == 0 ) { |
---|
| 1359 | #ifdef ROAR_SUPPORT_LISTEN |
---|
| 1360 | sock_info.bits = atoi(argv[++i]); |
---|
| 1361 | #endif |
---|
| 1362 | } else if ( strcmp(k, "--proto-chans") == 0 ) { |
---|
| 1363 | #ifdef ROAR_SUPPORT_LISTEN |
---|
| 1364 | sock_info.channels = atoi(argv[++i]); |
---|
| 1365 | #endif |
---|
| 1366 | } else if ( strcmp(k, "--proto-codec") == 0 ) { |
---|
| 1367 | #ifdef ROAR_SUPPORT_LISTEN |
---|
| 1368 | if ( (sock_info.codec = roar_str2codec(argv[++i])) == -1 ) { |
---|
| 1369 | ROAR_ERR("Unknown codec: %s", argv[i]); |
---|
| 1370 | return 1; |
---|
| 1371 | } |
---|
| 1372 | #endif |
---|
| 1373 | |
---|
| 1374 | |
---|
[3127] | 1375 | } else if ( strcmp(k, "--list-proto") == 0 ) { |
---|
| 1376 | list_proto(); |
---|
| 1377 | return 0; |
---|
[518] | 1378 | |
---|
[573] | 1379 | } else if ( strcmp(k, "-t") == 0 || strcmp(k, "--tcp") == 0 ) { |
---|
[1494] | 1380 | #ifdef ROAR_SUPPORT_LISTEN |
---|
[518] | 1381 | if ( sock_type != ROAR_SOCKET_TYPE_TCP && sock_type != ROAR_SOCKET_TYPE_TCP6 ) |
---|
| 1382 | sock_type = ROAR_SOCKET_TYPE_TCP; |
---|
| 1383 | |
---|
[2530] | 1384 | if ( *sock_addr == '/' ) |
---|
| 1385 | sock_addr = ROAR_DEFAULT_HOST; |
---|
[1494] | 1386 | #endif |
---|
[518] | 1387 | |
---|
| 1388 | } else if ( strcmp(k, "-4") == 0 ) { |
---|
[1494] | 1389 | #ifdef ROAR_SUPPORT_LISTEN |
---|
[517] | 1390 | sock_type = ROAR_SOCKET_TYPE_TCP; |
---|
[2530] | 1391 | if ( *sock_addr == '/' ) |
---|
| 1392 | sock_addr = ROAR_DEFAULT_HOST; |
---|
[1494] | 1393 | #endif |
---|
[518] | 1394 | } else if ( strcmp(k, "-6") == 0 ) { |
---|
[1494] | 1395 | #ifdef ROAR_SUPPORT_LISTEN |
---|
[519] | 1396 | #ifdef PF_INET6 |
---|
[518] | 1397 | sock_type = ROAR_SOCKET_TYPE_TCP6; |
---|
[2530] | 1398 | if ( *sock_addr == '/' ) |
---|
| 1399 | sock_addr = ROAR_DEFAULT_HOST; |
---|
[519] | 1400 | #else |
---|
| 1401 | ROAR_ERR("No IPv6 support compiled in!"); |
---|
| 1402 | return 1; |
---|
| 1403 | #endif |
---|
[1494] | 1404 | #endif |
---|
[518] | 1405 | |
---|
[573] | 1406 | } else if ( strcmp(k, "-u") == 0 || strcmp(k, "--unix") == 0 ) { |
---|
[1494] | 1407 | #ifdef ROAR_SUPPORT_LISTEN |
---|
[62] | 1408 | // ignore this case as it is the default behavor. |
---|
[517] | 1409 | sock_type = ROAR_SOCKET_TYPE_UNIX; |
---|
[1494] | 1410 | #endif |
---|
[518] | 1411 | |
---|
[573] | 1412 | } else if ( strcmp(k, "-n") == 0 || strcmp(k, "--decnet") == 0 ) { |
---|
[1494] | 1413 | #ifdef ROAR_SUPPORT_LISTEN |
---|
[508] | 1414 | #ifdef ROAR_HAVE_LIBDNET |
---|
| 1415 | port = ROAR_DEFAULT_NUM; |
---|
| 1416 | strcpy(decnethost, ROAR_DEFAULT_LISTEN_OBJECT); |
---|
[2530] | 1417 | sock_addr = decnethost; |
---|
[517] | 1418 | sock_type = ROAR_SOCKET_TYPE_DECNET; |
---|
[508] | 1419 | #else |
---|
| 1420 | ROAR_ERR("No DECnet support compiled in!"); |
---|
| 1421 | return 1; |
---|
| 1422 | #endif |
---|
[1494] | 1423 | #endif |
---|
[2530] | 1424 | } else if ( strcmp(k, "--new-sock") == 0 ) { |
---|
| 1425 | #ifdef ROAR_SUPPORT_LISTEN |
---|
[3252] | 1426 | if ( add_listen(sock_addr, port, sock_type, sock_user, sock_grp, sock_proto, sock_dir, &sock_info) != 0 ) { |
---|
[2530] | 1427 | ROAR_ERR("Can not open listen socket!"); |
---|
| 1428 | return 1; |
---|
| 1429 | } |
---|
| 1430 | #endif |
---|
[518] | 1431 | |
---|
[1993] | 1432 | } else if ( strcmp(k, "--slp") == 0 ) { |
---|
| 1433 | #ifdef ROAR_HAVE_LIBSLP |
---|
| 1434 | reg_slp = 1; |
---|
| 1435 | #else |
---|
| 1436 | ROAR_ERR("No OpenSLP support compiled in!"); |
---|
| 1437 | return 1; |
---|
| 1438 | #endif |
---|
| 1439 | |
---|
[3039] | 1440 | } else if ( strcmp(k, "--jumbo-mtu") == 0 ) { |
---|
| 1441 | g_config->jumbo_mtu = atoi(argv[++i]); |
---|
| 1442 | |
---|
[60] | 1443 | } else if ( strcmp(k, "-G") == 0 ) { |
---|
[444] | 1444 | sock_grp = argv[++i]; |
---|
| 1445 | } else if ( strcmp(k, "-U") == 0 ) { |
---|
| 1446 | sock_user = argv[++i]; |
---|
[0] | 1447 | |
---|
[68] | 1448 | } else if ( strcmp(k, "--no-listen") == 0 ) { |
---|
[1494] | 1449 | #ifdef ROAR_SUPPORT_LISTEN |
---|
[2530] | 1450 | sock_addr = ""; |
---|
[548] | 1451 | g_terminate = 1; |
---|
[1155] | 1452 | g_no_listen = 1; |
---|
[1494] | 1453 | #endif |
---|
[68] | 1454 | } else if ( strcmp(k, "--client-fh") == 0 ) { |
---|
| 1455 | if ( clients_set_fh(clients_new(), atoi(argv[++i])) == -1 ) { |
---|
| 1456 | ROAR_ERR("main(*): Can not set client's fh"); |
---|
| 1457 | return 1; |
---|
| 1458 | } |
---|
[501] | 1459 | } else if ( strcmp(k, "--close-fh") == 0 ) { |
---|
[1486] | 1460 | #ifdef ROAR_HAVE_IO_POSIX |
---|
[501] | 1461 | close(atoi(argv[++i])); |
---|
[1486] | 1462 | #else |
---|
| 1463 | i++; |
---|
| 1464 | ROAR_WARN("can not close file handle %s (closing not supported)", argv[i]); |
---|
| 1465 | #endif |
---|
[68] | 1466 | |
---|
[920] | 1467 | } else if ( strcmp(k, "--standby") == 0 ) { |
---|
| 1468 | g_standby = 1; |
---|
| 1469 | } else if ( strcmp(k, "--auto-standby") == 0 ) { |
---|
| 1470 | g_autostandby = 1; |
---|
[0] | 1471 | } else { |
---|
| 1472 | usage(); |
---|
| 1473 | return 1; |
---|
| 1474 | } |
---|
| 1475 | |
---|
| 1476 | } |
---|
[1503] | 1477 | #endif |
---|
[0] | 1478 | |
---|
[2485] | 1479 | #ifndef ROAR_WITHOUT_DCOMP_SOURCES |
---|
[1110] | 1480 | if ( s_dev != NULL ) { |
---|
| 1481 | if ( sources_add(s_drv, s_dev, s_con, s_opt, s_prim) == -1 ) { |
---|
| 1482 | ROAR_ERR("main(*): adding source '%s' via '%s' failed!", s_dev, s_drv); |
---|
| 1483 | } |
---|
| 1484 | } |
---|
[2485] | 1485 | #endif |
---|
[1110] | 1486 | |
---|
[1145] | 1487 | add_output(o_drv, o_dev, o_opts, o_prim, o_count); |
---|
[932] | 1488 | |
---|
[0] | 1489 | ROAR_DBG("Server config: rate=%i, bits=%i, chans=%i", sa.rate, sa.bits, sa.channels); |
---|
| 1490 | |
---|
[2945] | 1491 | if ( waveform_init() == -1 ) { |
---|
| 1492 | ROAR_ERR("Can not initialize Waveform subsystem"); |
---|
| 1493 | return 1; |
---|
| 1494 | } |
---|
| 1495 | |
---|
[2500] | 1496 | #ifndef ROAR_WITHOUT_DCOMP_MIDI |
---|
[1819] | 1497 | if ( midi_init() == -1 ) { |
---|
[281] | 1498 | ROAR_ERR("Can not initialize MIDI subsystem"); |
---|
[1819] | 1499 | } |
---|
[2500] | 1500 | #endif |
---|
[1819] | 1501 | |
---|
[2489] | 1502 | #ifndef ROAR_WITHOUT_DCOMP_SSYNTH |
---|
[2451] | 1503 | if ( ssynth_init() == -1 ) { |
---|
| 1504 | ROAR_ERR("Can not initialize ssynth subsystem"); |
---|
| 1505 | } |
---|
[2489] | 1506 | #endif |
---|
[2451] | 1507 | |
---|
[2494] | 1508 | #ifndef ROAR_WITHOUT_DCOMP_LIGHT |
---|
[1819] | 1509 | if ( light_init(light_channels) == -1 ) { |
---|
| 1510 | ROAR_ERR("Can not initialize light control subsystem"); |
---|
| 1511 | } |
---|
[2494] | 1512 | #endif |
---|
[281] | 1513 | |
---|
[2718] | 1514 | #ifndef ROAR_WITHOUT_DCOMP_RDTCS |
---|
| 1515 | if ( rdtcs_init() == -1 ) { |
---|
| 1516 | ROAR_ERR("Can not initialize RDTCS subsystem"); |
---|
| 1517 | } |
---|
| 1518 | #endif |
---|
| 1519 | |
---|
[1494] | 1520 | #ifdef ROAR_SUPPORT_LISTEN |
---|
[3252] | 1521 | if ( add_listen(sock_addr, port, sock_type, sock_user, sock_grp, sock_proto, sock_dir, &sock_info) != 0 ) { |
---|
[2530] | 1522 | ROAR_ERR("Can not open listen socket!"); |
---|
| 1523 | return 1; |
---|
[60] | 1524 | } |
---|
[1494] | 1525 | #endif |
---|
[60] | 1526 | |
---|
[0] | 1527 | if ( output_buffer_init(&sa) == -1 ) { |
---|
| 1528 | ROAR_ERR("Can not init output buffer!"); |
---|
| 1529 | return 1; |
---|
| 1530 | } |
---|
| 1531 | |
---|
[1145] | 1532 | if ( driver == NULL ) { |
---|
| 1533 | driver = "null"; |
---|
| 1534 | } else { |
---|
| 1535 | ROAR_WARN("Usage of old driver interface. use -o not -d!"); |
---|
| 1536 | } |
---|
| 1537 | |
---|
[0] | 1538 | if ( driver_open(&drvinst, &drvid, driver, device, &sa) == -1 ) { |
---|
| 1539 | ROAR_ERR("Can not open output driver!"); |
---|
| 1540 | return 1; |
---|
| 1541 | } |
---|
| 1542 | |
---|
[44] | 1543 | if ( samples_init() == -1 ) { |
---|
| 1544 | ROAR_ERR("Can not init samples!"); |
---|
| 1545 | return 1; |
---|
| 1546 | } |
---|
| 1547 | |
---|
| 1548 | |
---|
[1486] | 1549 | // we should handle this on microcontrollers, too. |
---|
[1753] | 1550 | #if !defined(ROAR_TARGET_MICROCONTROLLER) && !defined(ROAR_TARGET_WIN32) |
---|
[0] | 1551 | signal(SIGINT, on_sig_int); |
---|
[2732] | 1552 | signal(SIGTERM, on_sig_term); |
---|
[285] | 1553 | signal(SIGCHLD, on_sig_chld); |
---|
[2112] | 1554 | signal(SIGUSR1, on_sig_usr1); |
---|
[0] | 1555 | signal(SIGPIPE, SIG_IGN); // ignore broken pipes |
---|
[1486] | 1556 | #endif |
---|
[0] | 1557 | |
---|
[275] | 1558 | if ( realtime ) { |
---|
[278] | 1559 | #ifdef DEBUG |
---|
| 1560 | ROAR_WARN("compiled with -DDEBUG but realtime is enabled: for real realtime support compiel without -DDEBUG"); |
---|
| 1561 | #endif |
---|
| 1562 | |
---|
[1486] | 1563 | #ifdef ROAR_HAVE_NICE |
---|
[275] | 1564 | errno = 0; |
---|
[276] | 1565 | nice(-5*realtime); // -5 for each --realtime |
---|
[1486] | 1566 | if ( errno ) { |
---|
| 1567 | ROAR_WARN("Can not decrease nice value by %i: %s", 5*realtime, strerror(errno)); |
---|
| 1568 | } |
---|
| 1569 | #else |
---|
| 1570 | ROAR_WARN("Can not decrease nice value by %i: %s", 5*realtime, strerror(errno)); |
---|
| 1571 | #endif |
---|
[277] | 1572 | /* |
---|
[276] | 1573 | #ifdef __linux__ |
---|
[277] | 1574 | if ( ioprio_set(IOPRIO_WHO_PROCESS, getpid(), IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 0)) == -1 ) |
---|
| 1575 | ROAR_WARN("Can not set io priority: %s", strerror(errno)); |
---|
[276] | 1576 | #endif |
---|
[277] | 1577 | */ |
---|
[275] | 1578 | } |
---|
| 1579 | |
---|
[2756] | 1580 | #if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX) |
---|
[444] | 1581 | if ( setids & R_SETGID ) { |
---|
[2754] | 1582 | if ( sock_grp == NULL ) { |
---|
| 1583 | ROAR_ERR("Can not set GID if no groupname is supplied"); |
---|
| 1584 | return 1; |
---|
| 1585 | } |
---|
[2584] | 1586 | if ( (grp = getgrnam(sock_grp)) == NULL ) { |
---|
| 1587 | ROAR_ERR("Can not get GID for group %s: %s", sock_grp, strerror(errno)); |
---|
| 1588 | return 1; |
---|
| 1589 | } |
---|
[444] | 1590 | if ( setgroups(0, (const gid_t *) NULL) == -1 ) { |
---|
| 1591 | ROAR_ERR("Can not clear supplementary group IDs: %s", strerror(errno)); |
---|
| 1592 | } |
---|
[523] | 1593 | if ( !grp || setgid(grp->gr_gid) == -1 ) { |
---|
[444] | 1594 | ROAR_ERR("Can not set GroupID: %s", strerror(errno)); |
---|
| 1595 | } |
---|
| 1596 | } |
---|
[1486] | 1597 | #endif |
---|
[444] | 1598 | |
---|
[0] | 1599 | |
---|
[39] | 1600 | clients_set_pid(g_self_client, getpid()); |
---|
[1753] | 1601 | #ifdef ROAR_HAVE_GETUID |
---|
[440] | 1602 | clients_set_uid(g_self_client, getuid()); |
---|
[1753] | 1603 | #endif |
---|
| 1604 | #ifdef ROAR_HAVE_GETGID |
---|
[440] | 1605 | clients_set_gid(g_self_client, getgid()); |
---|
[1753] | 1606 | #endif |
---|
[39] | 1607 | clients_get(g_self_client, &self); |
---|
[37] | 1608 | |
---|
[39] | 1609 | if ( self == NULL ) { |
---|
| 1610 | ROAR_ERR("Can not get self client!"); |
---|
| 1611 | return 1; |
---|
| 1612 | } |
---|
| 1613 | |
---|
[775] | 1614 | strcpy(self->name, "RoarAudio daemon internal"); |
---|
[68] | 1615 | |
---|
[2815] | 1616 | if ( roar_nnode_free(&(self->nnode)) == -1 ) |
---|
| 1617 | return 1; |
---|
| 1618 | |
---|
| 1619 | // not fully correct but ok as workaorund |
---|
| 1620 | // so tools assume that roard runs on the same machine as |
---|
| 1621 | // they in case they use AF_UNIX: |
---|
| 1622 | if ( roar_nnode_new(&(self->nnode), ROAR_SOCKET_TYPE_UNIX) == -1 ) |
---|
| 1623 | return 1; |
---|
| 1624 | |
---|
[1486] | 1625 | #ifdef ROAR_HAVE_FORK |
---|
[775] | 1626 | if ( daemon ) { |
---|
[68] | 1627 | close(ROAR_STDIN ); |
---|
| 1628 | close(ROAR_STDOUT); |
---|
| 1629 | close(ROAR_STDERR); |
---|
[1753] | 1630 | |
---|
[68] | 1631 | if ( fork() ) |
---|
[1486] | 1632 | ROAR_U_EXIT(0); |
---|
[1753] | 1633 | |
---|
| 1634 | #ifdef ROAR_HAVE_SETSID |
---|
| 1635 | setsid(); |
---|
| 1636 | #endif |
---|
[1046] | 1637 | clients_set_pid(g_self_client, getpid()); // reset pid as it changed |
---|
[68] | 1638 | } |
---|
[1486] | 1639 | #endif |
---|
[68] | 1640 | |
---|
[2757] | 1641 | #if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX) |
---|
| 1642 | // early test for UID as we need this for the pidfile and the setuid() |
---|
| 1643 | if ( sock_user != NULL ) { |
---|
| 1644 | if ( (pwd = getpwnam(sock_user)) == NULL ) { |
---|
| 1645 | ROAR_ERR("Can not get UID for user %s: %s", sock_user, strerror(errno)); |
---|
| 1646 | return 1; |
---|
| 1647 | } |
---|
| 1648 | } |
---|
| 1649 | #endif |
---|
| 1650 | |
---|
[2104] | 1651 | #ifdef SUPPORT_PIDFILE |
---|
[2106] | 1652 | if ( pidfile != NULL ) { |
---|
| 1653 | if ( roar_vio_open_file(&pidfile_vio, pidfile, O_WRONLY|O_CREAT, 0644) == -1 ) { |
---|
| 1654 | ROAR_ERR("Can not write pidfile: %s", pidfile); |
---|
| 1655 | } else { |
---|
| 1656 | roar_vio_printf(&pidfile_vio, "%i\n", getpid()); |
---|
| 1657 | roar_vio_close(&pidfile_vio); |
---|
| 1658 | } |
---|
[2765] | 1659 | #if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX) |
---|
[2759] | 1660 | if ( pwd || grp ) { |
---|
| 1661 | if ( chown(pidfile, pwd ? pwd->pw_uid : -1, grp ? grp->gr_gid : -1) == -1 ) { |
---|
| 1662 | ROAR_WARN("Can not change ownership of pidfile: %s: %s", pidfile, strerror(errno)); |
---|
| 1663 | } |
---|
| 1664 | } |
---|
| 1665 | if ( chmod(pidfile, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) == -1 ) { |
---|
| 1666 | ROAR_WARN("Can not change permissions of pidfile: %s: %s", pidfile, strerror(errno)); |
---|
| 1667 | } |
---|
[2765] | 1668 | #endif |
---|
[2104] | 1669 | } |
---|
| 1670 | #endif |
---|
| 1671 | |
---|
[1486] | 1672 | #ifdef ROAR_HAVE_CHROOT |
---|
[444] | 1673 | if (chrootdir) { |
---|
| 1674 | if ( chroot(chrootdir) == -1 ) { |
---|
| 1675 | ROAR_ERR("Can not chroot to %s: %s", chrootdir, strerror(errno)); |
---|
| 1676 | return 2; |
---|
| 1677 | } |
---|
| 1678 | if ( chdir("/") == -1 ) { |
---|
| 1679 | ROAR_ERR("Can not chdir to /: %s", strerror(errno)); |
---|
| 1680 | return 2; |
---|
| 1681 | } |
---|
| 1682 | } |
---|
[1486] | 1683 | #endif |
---|
[444] | 1684 | |
---|
[2756] | 1685 | #if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX) |
---|
[444] | 1686 | if ( setids & R_SETUID ) { |
---|
[2752] | 1687 | if ( sock_user == NULL ) { |
---|
| 1688 | ROAR_ERR("Can not set UID if no username is supplied"); |
---|
| 1689 | return 1; |
---|
| 1690 | } |
---|
[444] | 1691 | if ( !pwd || setuid(pwd->pw_uid) == -1 ) { |
---|
| 1692 | ROAR_ERR("Can not set UserID: %s", strerror(errno)); |
---|
| 1693 | return 3; |
---|
| 1694 | } |
---|
[1753] | 1695 | #ifdef ROAR_HAVE_GETUID |
---|
[444] | 1696 | clients_set_uid(g_self_client, getuid()); |
---|
[1753] | 1697 | #endif |
---|
[444] | 1698 | } |
---|
[1486] | 1699 | #endif |
---|
[444] | 1700 | |
---|
[1993] | 1701 | // Register with OpenSLP: |
---|
[2028] | 1702 | #ifdef ROAR_HAVE_LIBSLP |
---|
[1993] | 1703 | if ( reg_slp ) { |
---|
[2530] | 1704 | register_slp(0, sock_addr); |
---|
[1993] | 1705 | } |
---|
[2028] | 1706 | #endif |
---|
[1993] | 1707 | |
---|
[0] | 1708 | // start main loop... |
---|
[905] | 1709 | main_loop(drvid, drvinst, &sa, sysclocksync); |
---|
[0] | 1710 | |
---|
| 1711 | // clean up. |
---|
| 1712 | clean_quit_prep(); |
---|
| 1713 | driver_close(drvinst, drvid); |
---|
| 1714 | output_buffer_free(); |
---|
| 1715 | |
---|
| 1716 | return 0; |
---|
| 1717 | } |
---|
| 1718 | |
---|
[574] | 1719 | void cleanup_listen_socket (int terminate) { |
---|
[2530] | 1720 | int i; |
---|
| 1721 | |
---|
[1993] | 1722 | // Deregister from SLP: |
---|
[2028] | 1723 | #ifdef ROAR_HAVE_LIBSLP |
---|
[1993] | 1724 | register_slp(1, NULL); |
---|
[2028] | 1725 | #endif |
---|
[580] | 1726 | |
---|
[1494] | 1727 | #ifdef ROAR_SUPPORT_LISTEN |
---|
[2530] | 1728 | for (i = 0; i < ROAR_MAX_LISTEN_SOCKETS; i++) { |
---|
[3129] | 1729 | if ( g_listen[i].socket != -1 ) { |
---|
[1486] | 1730 | #ifdef ROAR_HAVE_IO_POSIX |
---|
[3129] | 1731 | close(g_listen[i].socket); |
---|
[1486] | 1732 | #endif // #else is useless because we are in void context. |
---|
[60] | 1733 | |
---|
[3129] | 1734 | g_listen[i].socket = -1; |
---|
[576] | 1735 | |
---|
[1486] | 1736 | #ifdef ROAR_HAVE_UNIX |
---|
[2530] | 1737 | if ( server[i] != NULL ) |
---|
| 1738 | if ( *(server[i]) == '/' ) |
---|
| 1739 | unlink(server[i]); |
---|
[1486] | 1740 | #endif |
---|
[2530] | 1741 | } |
---|
[580] | 1742 | } |
---|
[60] | 1743 | |
---|
[1494] | 1744 | #endif |
---|
| 1745 | |
---|
[574] | 1746 | if ( terminate ) |
---|
| 1747 | g_terminate = 1; |
---|
| 1748 | } |
---|
| 1749 | |
---|
| 1750 | void clean_quit_prep (void) { |
---|
| 1751 | cleanup_listen_socket(0); |
---|
[60] | 1752 | |
---|
[2485] | 1753 | #ifndef ROAR_WITHOUT_DCOMP_SOURCES |
---|
[0] | 1754 | sources_free(); |
---|
[2485] | 1755 | #endif |
---|
[0] | 1756 | streams_free(); |
---|
| 1757 | clients_free(); |
---|
[2489] | 1758 | #ifndef ROAR_WITHOUT_DCOMP_SSYNTH |
---|
[2451] | 1759 | ssynth_free(); |
---|
[2489] | 1760 | #endif |
---|
[2487] | 1761 | #ifndef ROAR_WITHOUT_DCOMP_CB |
---|
[282] | 1762 | midi_cb_stop(); // stop console beep |
---|
[2487] | 1763 | #endif |
---|
[2500] | 1764 | #ifndef ROAR_WITHOUT_DCOMP_MIDI |
---|
[281] | 1765 | midi_free(); |
---|
[2500] | 1766 | #endif |
---|
[2494] | 1767 | #ifndef ROAR_WITHOUT_DCOMP_LIGHT |
---|
[1819] | 1768 | light_free(); |
---|
[2494] | 1769 | #endif |
---|
[2718] | 1770 | #ifndef ROAR_WITHOUT_DCOMP_RDTCS |
---|
| 1771 | rdtcs_free(); |
---|
| 1772 | #endif |
---|
[2104] | 1773 | |
---|
[2945] | 1774 | waveform_free(); |
---|
| 1775 | |
---|
[2104] | 1776 | #ifdef SUPPORT_PIDFILE |
---|
| 1777 | if ( pidfile != NULL ) |
---|
| 1778 | unlink(pidfile); |
---|
| 1779 | #endif |
---|
[0] | 1780 | } |
---|
| 1781 | |
---|
| 1782 | void clean_quit (void) { |
---|
| 1783 | clean_quit_prep(); |
---|
| 1784 | // driver_close(drvinst, drvid); |
---|
| 1785 | // output_buffer_free(); |
---|
| 1786 | exit(0); |
---|
| 1787 | } |
---|
| 1788 | |
---|
| 1789 | //ll |
---|