[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 | |
---|
[1503] | 31 | #ifdef ROAR_HAVE_MAIN_ARGS |
---|
[0] | 32 | void usage (void) { |
---|
| 33 | printf("Usage: roard [OPTIONS]...\n\n"); |
---|
| 34 | |
---|
[68] | 35 | printf("Misc Options:\n\n"); |
---|
| 36 | printf( |
---|
[775] | 37 | " --daemon - Bring the server into background after init\n" |
---|
[71] | 38 | " --terminate - Terminate after last client quited\n" |
---|
[274] | 39 | " --restart - Trys to stop an old instance and start a new with new settings\n" |
---|
[276] | 40 | " --realtime - Trys to get realtime priority,\n" |
---|
| 41 | " give multible times for being more realtime\n" |
---|
[444] | 42 | " --chroot DIR - chroots to the given dir\n" |
---|
| 43 | " --setgid - GroupID to the audio group as specified via -G\n" |
---|
| 44 | " --setuid - UserID to the audio user as specified via -U\n" |
---|
[905] | 45 | " --sysclocksync - calculate exact sample rate using the system clock\n" |
---|
[68] | 46 | ); |
---|
| 47 | |
---|
| 48 | printf("\nAudio Options:\n\n"); |
---|
[0] | 49 | printf( |
---|
| 50 | " -R --rate RATE - Set server rate\n" |
---|
| 51 | " -B --bits BITS - Set server bits\n" |
---|
| 52 | " -C --chans CHANNELS - Set server channels\n" |
---|
| 53 | ); |
---|
| 54 | |
---|
| 55 | printf("\nDriver Options:\n\n"); |
---|
[974] | 56 | printf(" -d --driver DRV - Set the driver (default: %s)\n", ROAR_DRIVER_DEFAULT); |
---|
[0] | 57 | printf(" -D --device DEV - Set the device\n"); |
---|
| 58 | printf(" -dO OPTS - Set output options\n"); |
---|
[973] | 59 | printf(" --list-driver - List all drivers\n"); |
---|
[0] | 60 | |
---|
[932] | 61 | printf("\nOutput Options:\n\n"); |
---|
[974] | 62 | printf(" -o --odriver DRV - Set the driver, use '--list-driver' to get a list\n"); |
---|
[932] | 63 | printf(" -O --odevice DEV - Set the device\n"); |
---|
| 64 | printf(" -oO OPTS - Set output options\n"); |
---|
| 65 | printf(" -oN - Adds another output\n"); |
---|
[961] | 66 | printf(" -oP - Mark output as primary\n"); |
---|
[932] | 67 | |
---|
[0] | 68 | printf("\nSource Options:\n\n"); |
---|
| 69 | printf(" -s --source DRV - Use DRV as input driver\n" |
---|
| 70 | " -S DEV - Use DEV as input device\n" |
---|
| 71 | " -sO OPTS - Use OPTS as input options\n" |
---|
[1512] | 72 | " -sN - Adds another source\n" |
---|
[0] | 73 | " -sP - Make souce as primary\n" |
---|
| 74 | ); |
---|
| 75 | |
---|
[280] | 76 | printf("\nCodec Filter Options:\n\n"); |
---|
| 77 | printf(" --list-cf - List all codec filter\n" |
---|
| 78 | ); |
---|
[0] | 79 | |
---|
| 80 | printf("\nServer Options:\n\n"); |
---|
| 81 | printf(" -t --tcp - Use TCP listen socket\n" |
---|
| 82 | " -u --unix - Use UNIX Domain listen socket (default)\n" |
---|
[508] | 83 | #ifdef ROAR_HAVE_LIBDNET |
---|
| 84 | " -n --decnet - use DECnet listen socket\n" |
---|
| 85 | #endif |
---|
[518] | 86 | " -4 - Use IPv4 connections (implies -t)\n" |
---|
| 87 | #ifdef PF_INET6 |
---|
| 88 | " -6 - Use IPv6 connections (implies -t)\n" |
---|
| 89 | #endif |
---|
| 90 | #ifdef IPV6_ADDRFORM |
---|
| 91 | " -64 - Try to downgrade sockets from IPv6 into IPv4,\n" |
---|
| 92 | " this is normaly not usefull.\n" |
---|
| 93 | #endif |
---|
[0] | 94 | " -p --port - TCP Port to bind to\n" |
---|
| 95 | " -b --bind - IP/Hostname to bind to\n" |
---|
[1115] | 96 | " --sock - Filename for UNIX Domain Socket\n" |
---|
[450] | 97 | " -G GROUP - Sets the group for the UNIX Domain Socket, (default: %s)\n" |
---|
[60] | 98 | " You need the permittions to change the GID\n" |
---|
[444] | 99 | " -U USER - Sets the user for the UNIX Domain Socket, (default: do not set)\n" |
---|
| 100 | " You need the permittions to change the UID (normaly only root has)\n" |
---|
[548] | 101 | " --no-listen - Do not listen for new clients\n" |
---|
| 102 | " (only usefull for relaing, impleys --terminate)\n" |
---|
[274] | 103 | " --client-fh - Comunicate with a client over this handle\n" |
---|
[501] | 104 | " (only usefull for relaing)\n" |
---|
[920] | 105 | " --close-fh - Closes the given fh\n" |
---|
| 106 | " --standby - Start in standby state\n" |
---|
| 107 | " --auto-standby - Automatical goes into standby if there are no streams\n", |
---|
[450] | 108 | ROAR_DEFAULT_SOCKGRP |
---|
[0] | 109 | ); |
---|
| 110 | // printf("\n Options:\n\n"); |
---|
| 111 | printf("\n"); |
---|
| 112 | } |
---|
[1503] | 113 | #endif |
---|
[0] | 114 | |
---|
[579] | 115 | int restart_server (char * server) { |
---|
| 116 | struct roar_connection con; |
---|
| 117 | if ( roar_connect(&con, server) == -1 ) { |
---|
| 118 | return -1; |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | if ( roar_terminate(&con, 1) == -1 ) { |
---|
| 122 | return -1; |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | return roar_disconnect(&con); |
---|
| 126 | } |
---|
| 127 | |
---|
[444] | 128 | #define R_SETUID 1 |
---|
| 129 | #define R_SETGID 2 |
---|
| 130 | |
---|
[1609] | 131 | int init_config (void) { |
---|
| 132 | int i; |
---|
| 133 | |
---|
| 134 | memset(g_config, 0, sizeof(struct roard_config)); |
---|
| 135 | |
---|
| 136 | for (i = 0; i < ROAR_DIR_DIRIDS; i++) { |
---|
| 137 | g_config->streams[i].mixer_channels = 1; |
---|
| 138 | g_config->streams[i].mixer.rpg_mul = 1; |
---|
| 139 | g_config->streams[i].mixer.rpg_div = 1; |
---|
| 140 | g_config->streams[i].mixer.scale = 65535; |
---|
| 141 | g_config->streams[i].mixer.mixer[0] = g_config->streams[i].mixer.scale; |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | return 0; |
---|
| 145 | } |
---|
| 146 | |
---|
[1145] | 147 | int add_output (char * drv, char * dev, char * opts, int prim, int count) { |
---|
[933] | 148 | int stream; |
---|
| 149 | struct roar_stream * s; |
---|
| 150 | struct roar_stream_server * ss; |
---|
[938] | 151 | char * k, * v; |
---|
[1208] | 152 | #ifdef ROAR_DRIVER_CODEC |
---|
| 153 | char * to_free = NULL; |
---|
| 154 | #endif |
---|
[938] | 155 | int codec; |
---|
[1117] | 156 | int sync = 0; |
---|
[1522] | 157 | int32_t blocks = -1, blocksize = -1; |
---|
[933] | 158 | |
---|
[938] | 159 | ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = ?", drv, dev, opts); |
---|
[933] | 160 | |
---|
[1145] | 161 | if ( drv == NULL && count == 0 ) { |
---|
| 162 | drv = ROAR_DRIVER_DEFAULT; |
---|
| 163 | prim = 1; |
---|
| 164 | sync = 1; |
---|
[1208] | 165 | |
---|
| 166 | #ifdef ROAR_DRIVER_CODEC |
---|
| 167 | if ( opts == NULL ) { |
---|
| 168 | opts = to_free = strdup("codec=" ROAR_DRIVER_CODEC); |
---|
| 169 | } |
---|
| 170 | #endif |
---|
[1145] | 171 | } |
---|
| 172 | |
---|
[1227] | 173 | if ( opts == NULL && count == 0 ) { |
---|
| 174 | sync = 1; |
---|
| 175 | prim = 1; // if ( prim == 0 ) prim = 1; -> prim allways = 1 |
---|
| 176 | } |
---|
| 177 | |
---|
[933] | 178 | if ( (stream = streams_new()) == -1 ) { |
---|
[938] | 179 | ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = -1", drv, dev, opts); |
---|
[982] | 180 | if ( prim ) alive = 0; |
---|
[933] | 181 | return -1; |
---|
| 182 | } |
---|
| 183 | |
---|
| 184 | streams_get(stream, &ss); |
---|
| 185 | s = ROAR_STREAM(ss); |
---|
| 186 | |
---|
| 187 | memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info)); |
---|
| 188 | |
---|
[1609] | 189 | if ( streams_set_dir(stream, ROAR_DIR_OUTPUT, 1) == -1 ) { |
---|
| 190 | streams_delete(stream); |
---|
| 191 | return -1; |
---|
| 192 | } |
---|
[933] | 193 | s->pos_rel_id = -1; |
---|
| 194 | // s->info.codec = codec; |
---|
| 195 | |
---|
[938] | 196 | codec = s->info.codec; |
---|
| 197 | |
---|
| 198 | k = strtok(opts, ","); |
---|
| 199 | while (k != NULL) { |
---|
| 200 | // ROAR_WARN("add_output(*): opts: %s", k); |
---|
| 201 | |
---|
| 202 | if ( (v = strstr(k, "=")) != NULL ) { |
---|
| 203 | *v++ = 0; |
---|
| 204 | } |
---|
| 205 | |
---|
| 206 | ROAR_DBG("add_output(*): opts: k='%s', v='%s'", k, v); |
---|
| 207 | if ( strcmp(k, "rate") == 0 ) { |
---|
| 208 | s->info.rate = atoi(v); |
---|
| 209 | } else if ( strcmp(k, "channels") == 0 ) { |
---|
| 210 | s->info.channels = atoi(v); |
---|
| 211 | } else if ( strcmp(k, "bits") == 0 ) { |
---|
| 212 | s->info.bits = atoi(v); |
---|
| 213 | } else if ( strcmp(k, "codec") == 0 ) { |
---|
| 214 | if ( (codec = roar_str2codec(v)) == -1 ) { |
---|
| 215 | ROAR_ERR("add_output(*): unknown codec '%s'", v); |
---|
| 216 | streams_delete(stream); |
---|
[982] | 217 | if ( prim ) alive = 0; |
---|
[1208] | 218 | #ifdef ROAR_DRIVER_CODEC |
---|
| 219 | if ( to_free != NULL ) |
---|
| 220 | free(to_free); |
---|
| 221 | #endif |
---|
[938] | 222 | return -1; |
---|
| 223 | } |
---|
[1221] | 224 | } else if ( strcmp(k, "blocks") == 0 ) { |
---|
| 225 | blocks = atoi(v); |
---|
[1522] | 226 | } else if ( strcmp(k, "blocksize") == 0 ) { |
---|
| 227 | blocksize = atoi(v); |
---|
[1032] | 228 | } else if ( strcmp(k, "meta") == 0 ) { |
---|
| 229 | streams_set_flag(stream, ROAR_FLAG_META); |
---|
[1117] | 230 | } else if ( strcmp(k, "sync") == 0 ) { |
---|
| 231 | sync = 1; |
---|
[1221] | 232 | } else if ( strcmp(k, "primary") == 0 ) { |
---|
| 233 | prim = 1; |
---|
| 234 | |
---|
| 235 | } else if ( strcmp(k, "cleanmeta") == 0 ) { |
---|
| 236 | streams_set_flag(stream, ROAR_FLAG_CLEANMETA); |
---|
| 237 | } else if ( strcmp(k, "autoconf") == 0 ) { |
---|
[1531] | 238 | streams_set_flag(stream, ROAR_FLAG_AUTOCONF); |
---|
[938] | 239 | } else { |
---|
| 240 | ROAR_ERR("add_output(*): unknown option '%s'", k); |
---|
| 241 | streams_delete(stream); |
---|
[982] | 242 | if ( prim ) alive = 0; |
---|
[1208] | 243 | #ifdef ROAR_DRIVER_CODEC |
---|
| 244 | if ( to_free != NULL ) |
---|
| 245 | free(to_free); |
---|
| 246 | #endif |
---|
[938] | 247 | return -1; |
---|
| 248 | } |
---|
| 249 | |
---|
| 250 | k = strtok(NULL, ","); |
---|
| 251 | } |
---|
| 252 | |
---|
[1208] | 253 | #ifdef ROAR_DRIVER_CODEC |
---|
| 254 | if ( to_free != NULL ) |
---|
| 255 | free(to_free); |
---|
| 256 | #endif |
---|
| 257 | |
---|
[941] | 258 | if ( codec == ROAR_CODEC_ALAW || codec == ROAR_CODEC_MULAW ) |
---|
| 259 | s->info.bits = 8; // needed to open OSS driver, will be overriden by codecfilter |
---|
| 260 | |
---|
[938] | 261 | s->info.codec = codec; |
---|
| 262 | ROAR_STREAM_SERVER(s)->codec_orgi = codec; |
---|
[933] | 263 | |
---|
[1145] | 264 | if ( driver_openvio(&(ss->vio), &(ss->driver_id), drv, dev, &(s->info), -1) == -1 ) { |
---|
[1581] | 265 | ss->driver_id = -1; // don't close a driver not opened... |
---|
| 266 | memset(&(ss->vio), 0, sizeof(struct roar_vio_calls)); |
---|
[933] | 267 | streams_delete(stream); |
---|
[938] | 268 | ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = -1", drv, dev, opts); |
---|
[982] | 269 | if ( prim ) alive = 0; |
---|
[933] | 270 | return -1; |
---|
| 271 | } |
---|
| 272 | |
---|
[1528] | 273 | roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_SSTREAMID, &stream); // ignore errors here |
---|
| 274 | roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_SSTREAM, s); // ignore errors here |
---|
| 275 | |
---|
[1221] | 276 | if ( blocks != -1 ) |
---|
| 277 | roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DBLOCKS, &blocks); |
---|
| 278 | |
---|
[1522] | 279 | if ( blocksize != -1 ) |
---|
| 280 | roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_DBLKSIZE, &blocksize); |
---|
| 281 | |
---|
[1156] | 282 | ROAR_DBG("add_output(*): ss->driver_id=%i", ss->driver_id); |
---|
| 283 | |
---|
[938] | 284 | streams_set_fh(stream, -1); // update some internal structures |
---|
| 285 | |
---|
[933] | 286 | client_stream_add(g_source_client, stream); |
---|
| 287 | |
---|
[1200] | 288 | if ( prim ) { |
---|
[961] | 289 | streams_mark_primary(stream); |
---|
[1200] | 290 | s->pos_rel_id = stream; |
---|
| 291 | } |
---|
[961] | 292 | |
---|
[1120] | 293 | if ( sync ) { |
---|
[1117] | 294 | streams_set_flag(stream, ROAR_FLAG_SYNC); |
---|
[1120] | 295 | } else { |
---|
| 296 | streams_reset_flag(stream, ROAR_FLAG_SYNC); |
---|
| 297 | } |
---|
[1117] | 298 | |
---|
[933] | 299 | return 0; |
---|
[932] | 300 | } |
---|
| 301 | |
---|
[1503] | 302 | #ifdef ROAR_HAVE_MAIN_ARGS |
---|
[0] | 303 | int main (int argc, char * argv[]) { |
---|
[1503] | 304 | #else |
---|
| 305 | int main (void) { |
---|
| 306 | #endif |
---|
| 307 | #ifdef ROAR_HAVE_MAIN_ARGS |
---|
[0] | 308 | int i; |
---|
| 309 | char * k; |
---|
[1503] | 310 | #endif |
---|
[1494] | 311 | #ifdef ROAR_SUPPORT_LISTEN |
---|
[905] | 312 | char user_sock[80] = {0}; |
---|
[1494] | 313 | #endif |
---|
[1609] | 314 | struct roar_audio_info sa, max_sa; |
---|
| 315 | struct roard_config config; |
---|
[1486] | 316 | #ifdef ROAR_HAVE_FORK |
---|
[905] | 317 | int daemon = 0; |
---|
[1486] | 318 | #endif |
---|
[905] | 319 | int realtime = 0; |
---|
| 320 | int sysclocksync = 0; |
---|
[1207] | 321 | char * driver = NULL; |
---|
| 322 | char * device = NULL; |
---|
[1503] | 323 | #ifdef ROAR_HAVE_MAIN_ARGS |
---|
[1207] | 324 | char * opts = NULL; |
---|
[1503] | 325 | #endif |
---|
[60] | 326 | // char * server = ROAR_DEFAULT_SOCK_GLOBAL; |
---|
[1494] | 327 | #ifdef ROAR_SUPPORT_LISTEN |
---|
[1207] | 328 | int port = ROAR_DEFAULT_PORT; |
---|
[1494] | 329 | #endif |
---|
[0] | 330 | int drvid; |
---|
[550] | 331 | char * s_drv = "cf"; |
---|
[1110] | 332 | char * s_dev = NULL; |
---|
[444] | 333 | char * s_con = NULL; |
---|
| 334 | char * s_opt = NULL; |
---|
| 335 | int s_prim = 0; |
---|
[1207] | 336 | char * o_drv = getenv("ROAR_DRIVER"); |
---|
| 337 | char * o_dev = getenv("ROAR_DEVICE"); |
---|
[932] | 338 | char * o_opts = NULL; |
---|
[961] | 339 | int o_prim = 0; |
---|
[1145] | 340 | int o_count = 0; |
---|
[1819] | 341 | int light_channels = 512; |
---|
[450] | 342 | char * sock_grp = ROAR_DEFAULT_SOCKGRP; |
---|
[444] | 343 | char * sock_user = NULL; |
---|
[1494] | 344 | #ifdef ROAR_SUPPORT_LISTEN |
---|
[517] | 345 | int sock_type = ROAR_SOCKET_TYPE_UNKNOWN; |
---|
[1494] | 346 | #endif |
---|
[1486] | 347 | #ifdef ROAR_HAVE_CHROOT |
---|
[444] | 348 | char * chrootdir = NULL; |
---|
[1486] | 349 | #endif |
---|
| 350 | #if defined(ROAR_HAVE_SETGID) || defined(ROAR_HAVE_SETUID) |
---|
[444] | 351 | int setids = 0; |
---|
[1486] | 352 | #endif |
---|
| 353 | #ifdef ROAR_HAVE_UNIX |
---|
[1011] | 354 | char * env_roar_proxy_backup; |
---|
[1486] | 355 | #endif |
---|
| 356 | #if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX) |
---|
[446] | 357 | struct group * grp = NULL; |
---|
[1486] | 358 | #endif |
---|
| 359 | #if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX) |
---|
[446] | 360 | struct passwd * pwd = NULL; |
---|
[1486] | 361 | #endif |
---|
| 362 | #ifdef ROAR_HAVE_GETSERVBYNAME |
---|
[446] | 363 | struct servent * serv = NULL; |
---|
[1486] | 364 | #endif |
---|
[0] | 365 | DRIVER_USERDATA_T drvinst; |
---|
[39] | 366 | struct roar_client * self = NULL; |
---|
[508] | 367 | #ifdef ROAR_HAVE_LIBDNET |
---|
| 368 | char decnethost[80]; |
---|
| 369 | #endif |
---|
[0] | 370 | |
---|
| 371 | g_standby = 0; |
---|
[920] | 372 | g_autostandby = 0; |
---|
[982] | 373 | alive = 1; |
---|
[1494] | 374 | #ifdef ROAR_SUPPORT_LISTEN |
---|
[1155] | 375 | g_no_listen = 0; |
---|
[1494] | 376 | g_listen_socket = -1; |
---|
| 377 | #else |
---|
| 378 | g_terminate = 1; |
---|
| 379 | #endif |
---|
[0] | 380 | |
---|
| 381 | sa.bits = ROAR_BITS_DEFAULT; |
---|
| 382 | sa.channels = ROAR_CHANNELS_DEFAULT; |
---|
| 383 | sa.rate = ROAR_RATE_DEFAULT; |
---|
| 384 | sa.codec = ROAR_CODEC_DEFAULT; |
---|
| 385 | |
---|
[1609] | 386 | g_sa = &sa; |
---|
| 387 | g_max_sa = &max_sa; |
---|
| 388 | |
---|
| 389 | memcpy(g_max_sa, g_sa, sizeof(max_sa)); |
---|
| 390 | |
---|
| 391 | g_config = &config; |
---|
| 392 | |
---|
| 393 | if ( init_config() == -1 ) { |
---|
| 394 | ROAR_ERR("Can not init default config!"); |
---|
| 395 | return 1; |
---|
| 396 | } |
---|
[0] | 397 | |
---|
[60] | 398 | |
---|
[1494] | 399 | #ifdef ROAR_SUPPORT_LISTEN |
---|
[1753] | 400 | #ifdef ROAR_HAVE_GETUID |
---|
[1486] | 401 | if ( getuid() != 0 && getenv("HOME") != NULL ) { |
---|
| 402 | snprintf(user_sock, 79, "%s/%s", (char*)getenv("HOME"), ROAR_DEFAULT_SOCK_USER); |
---|
[60] | 403 | server = user_sock; |
---|
| 404 | } |
---|
[1753] | 405 | #endif |
---|
[60] | 406 | |
---|
[279] | 407 | if ( getenv("ROAR_SERVER") != NULL ) |
---|
| 408 | server = getenv("ROAR_SERVER"); |
---|
[1494] | 409 | #endif |
---|
[279] | 410 | |
---|
[63] | 411 | if ( clients_init() == -1 ) { |
---|
| 412 | ROAR_ERR("Can not init clients!"); |
---|
| 413 | return 1; |
---|
| 414 | } |
---|
| 415 | |
---|
| 416 | if ( streams_init() == -1 ) { |
---|
| 417 | ROAR_ERR("Can not init streams!"); |
---|
| 418 | return 1; |
---|
| 419 | } |
---|
| 420 | |
---|
[64] | 421 | if ( (g_self_client = clients_new()) == -1 ) { |
---|
| 422 | ROAR_ERR("Can not create self client!"); |
---|
| 423 | return 1; |
---|
| 424 | } |
---|
| 425 | |
---|
[0] | 426 | if ( sources_init() == -1 ) { |
---|
| 427 | ROAR_ERR("Can not init sources!"); |
---|
| 428 | return 1; |
---|
| 429 | } |
---|
| 430 | |
---|
[64] | 431 | if ( (sources_set_client(g_self_client)) == -1 ) { |
---|
| 432 | ROAR_ERR("Can not init set source client!"); |
---|
| 433 | return 1; |
---|
| 434 | } |
---|
| 435 | |
---|
[1503] | 436 | #ifdef ROAR_HAVE_MAIN_ARGS |
---|
[0] | 437 | for (i = 1; i < argc; i++) { |
---|
| 438 | k = argv[i]; |
---|
| 439 | |
---|
| 440 | if ( strcmp(k, "-h") == 0 || strcmp(k, "--help") == 0 ) { |
---|
| 441 | usage(); |
---|
| 442 | return 0; |
---|
| 443 | |
---|
[579] | 444 | } else if ( strcmp(k, "--restart") == 0 ) { |
---|
[1494] | 445 | #ifdef ROAR_SUPPORT_LISTEN |
---|
[579] | 446 | if ( restart_server(server) == -1 ) { |
---|
| 447 | ROAR_WARN("Can not terminate old server (not running at %s?), tring to continue anyway", server); |
---|
| 448 | } |
---|
[1494] | 449 | #else |
---|
| 450 | ROAR_ERR("--restart not supported"); |
---|
| 451 | #endif |
---|
[579] | 452 | |
---|
[775] | 453 | } else if ( strcmp(k, "--demon") == 0 || strcmp(k, "--daemon") == 0 ) { |
---|
[1486] | 454 | #ifdef ROAR_HAVE_FORK |
---|
[775] | 455 | daemon = 1; |
---|
[1486] | 456 | #else |
---|
| 457 | ROAR_ERR("--daemon not supported"); |
---|
| 458 | #endif |
---|
[71] | 459 | } else if ( strcmp(k, "--terminate") == 0 ) { |
---|
| 460 | g_terminate = 1; |
---|
[905] | 461 | } else if ( strcmp(k, "--sysclocksync") == 0 ) { |
---|
| 462 | sysclocksync = 1000; |
---|
[275] | 463 | } else if ( strcmp(k, "--realtime") == 0 ) { |
---|
[276] | 464 | realtime++; |
---|
[444] | 465 | } else if ( strcmp(k, "--chroot") == 0 ) { |
---|
[1486] | 466 | #ifdef ROAR_HAVE_CHROOT |
---|
[444] | 467 | chrootdir = argv[++i]; |
---|
[1486] | 468 | #else |
---|
| 469 | ROAR_ERR("--chroot not supported"); |
---|
| 470 | i++; |
---|
| 471 | #endif |
---|
[444] | 472 | } else if ( strcmp(k, "--setgid") == 0 ) { |
---|
[1486] | 473 | #ifdef ROAR_HAVE_SETGID |
---|
[444] | 474 | setids |= R_SETGID; |
---|
[1486] | 475 | #else |
---|
| 476 | ROAR_ERR("--setgid not supported"); |
---|
| 477 | #endif |
---|
[444] | 478 | } else if ( strcmp(k, "--setuid") == 0 ) { |
---|
[1486] | 479 | #ifdef ROAR_HAVE_SETUID |
---|
[444] | 480 | setids |= R_SETUID; |
---|
[1486] | 481 | #else |
---|
| 482 | ROAR_ERR("--setuid not supported"); |
---|
| 483 | #endif |
---|
[68] | 484 | |
---|
[280] | 485 | } else if ( strcmp(k, "--list-cf") == 0 ) { |
---|
| 486 | print_codecfilterlist(); |
---|
| 487 | return 0; |
---|
| 488 | |
---|
[0] | 489 | } else if ( strcmp(k, "-R") == 0 || strcmp(k, "--rate") == 0 ) { |
---|
| 490 | sa.rate = atoi(argv[++i]); |
---|
| 491 | } else if ( strcmp(k, "-B") == 0 || strcmp(k, "--bits") == 0 ) { |
---|
| 492 | sa.bits = atoi(argv[++i]); |
---|
| 493 | } else if ( strcmp(k, "-C") == 0 || strcmp(k, "--chans") == 0 ) { |
---|
| 494 | sa.channels = atoi(argv[++i]); |
---|
| 495 | |
---|
| 496 | } else if ( strcmp(k, "-d") == 0 || strcmp(k, "--driver") == 0 ) { |
---|
| 497 | driver = argv[++i]; |
---|
| 498 | if ( strcmp(driver, "list") == 0 ) { |
---|
[974] | 499 | ROAR_WARN("The option is obsolete, use --list-driver!"); |
---|
[0] | 500 | print_driverlist(); |
---|
[973] | 501 | return 0; |
---|
[0] | 502 | } |
---|
| 503 | } else if ( strcmp(k, "-D") == 0 || strcmp(k, "--device") == 0 ) { |
---|
| 504 | device = argv[++i]; |
---|
| 505 | } else if ( strcmp(k, "-dO") == 0 ) { |
---|
| 506 | opts = argv[++i]; |
---|
[973] | 507 | } else if ( strcmp(k, "--list-driver") == 0 ) { |
---|
| 508 | print_driverlist(); |
---|
| 509 | return 0; |
---|
[0] | 510 | |
---|
[932] | 511 | } else if ( strcmp(k, "-o") == 0 || strcmp(k, "--odriver") == 0 ) { |
---|
| 512 | o_drv = argv[++i]; |
---|
| 513 | } else if ( strcmp(k, "-O") == 0 || strcmp(k, "--odevice") == 0 ) { |
---|
| 514 | o_dev = argv[++i]; |
---|
| 515 | } else if ( strcmp(k, "-oO") == 0 ) { |
---|
| 516 | o_opts = argv[++i]; |
---|
[961] | 517 | } else if ( strcmp(k, "-oP") == 0 ) { |
---|
| 518 | o_prim = 1; |
---|
[932] | 519 | } else if ( strcmp(k, "-oN") == 0 ) { |
---|
[1145] | 520 | if ( add_output(o_drv, o_dev, o_opts, o_prim, o_count) != -1 ) |
---|
| 521 | o_count++; |
---|
| 522 | |
---|
[961] | 523 | o_drv = o_dev = o_opts = NULL; |
---|
| 524 | o_prim = 0; |
---|
[932] | 525 | |
---|
[0] | 526 | } else if ( strcmp(k, "-s") == 0 || strcmp(k, "--source") == 0 ) { |
---|
[550] | 527 | s_drv = argv[++i]; |
---|
| 528 | } else if ( strcmp(k, "-S") == 0 ) { |
---|
[1110] | 529 | s_dev = argv[++i]; |
---|
[0] | 530 | } else if ( strcmp(k, "-sO") == 0 ) { |
---|
| 531 | s_opt = argv[++i]; |
---|
| 532 | } else if ( strcmp(k, "-sC") == 0 ) { |
---|
| 533 | s_con = argv[++i]; |
---|
| 534 | } else if ( strcmp(k, "-sP") == 0 ) { |
---|
| 535 | s_prim = 1; |
---|
[1110] | 536 | } else if ( strcmp(k, "-sN") == 0 ) { |
---|
| 537 | if ( sources_add(s_drv, s_dev, s_con, s_opt, s_prim) == -1 ) { |
---|
| 538 | ROAR_ERR("main(*): adding source '%s' via '%s' failed!", s_dev, s_drv); |
---|
| 539 | } |
---|
| 540 | s_opt = s_dev = s_con = NULL; |
---|
| 541 | s_drv = "cf"; |
---|
| 542 | s_prim = 0; |
---|
[0] | 543 | |
---|
| 544 | } else if ( strcmp(k, "-p") == 0 || strcmp(k, "--port") == 0 ) { |
---|
[447] | 545 | // This is only usefull in INET not UNIX mode. |
---|
[1494] | 546 | #ifdef ROAR_SUPPORT_LISTEN |
---|
[447] | 547 | if ( *server == '/' ) |
---|
| 548 | server = ROAR_DEFAULT_HOST; |
---|
| 549 | |
---|
[446] | 550 | errno = 0; |
---|
| 551 | if ( (port = atoi(argv[++i])) < 1 ) { |
---|
[1486] | 552 | #ifdef ROAR_HAVE_GETSERVBYNAME |
---|
[446] | 553 | if ( (serv = getservbyname(argv[i], "tcp")) == NULL ) { |
---|
| 554 | ROAR_ERR("Unknown service: %s: %s", argv[i], strerror(errno)); |
---|
| 555 | return 1; |
---|
| 556 | } |
---|
| 557 | // NOTE: we need to use ROAR_NET2HOST16() here even if s_port is of type int! |
---|
| 558 | ROAR_DBG("main(*): serv = {s_name='%s', s_aliases={...}, s_port=%i, s_proto='%s'}", |
---|
| 559 | serv->s_name, ROAR_NET2HOST16(serv->s_port), serv->s_proto); |
---|
| 560 | port = ROAR_NET2HOST16(serv->s_port); |
---|
[1486] | 561 | #else |
---|
| 562 | ROAR_ERR("invalite port number: %s", argv[i]); |
---|
| 563 | return 1; |
---|
| 564 | #endif |
---|
[446] | 565 | } |
---|
[1494] | 566 | #endif |
---|
[1115] | 567 | } else if ( strcmp(k, "-b") == 0 || strcmp(k, "--bind") == 0 || strcmp(k, "--sock") == 0 ) { |
---|
[1494] | 568 | #ifdef ROAR_SUPPORT_LISTEN |
---|
[0] | 569 | server = argv[++i]; |
---|
[1494] | 570 | #endif |
---|
[518] | 571 | |
---|
[573] | 572 | } else if ( strcmp(k, "-t") == 0 || strcmp(k, "--tcp") == 0 ) { |
---|
[1494] | 573 | #ifdef ROAR_SUPPORT_LISTEN |
---|
[518] | 574 | if ( sock_type != ROAR_SOCKET_TYPE_TCP && sock_type != ROAR_SOCKET_TYPE_TCP6 ) |
---|
| 575 | sock_type = ROAR_SOCKET_TYPE_TCP; |
---|
| 576 | |
---|
| 577 | if ( *server == '/' ) |
---|
| 578 | server = ROAR_DEFAULT_HOST; |
---|
[1494] | 579 | #endif |
---|
[518] | 580 | |
---|
| 581 | } else if ( strcmp(k, "-4") == 0 ) { |
---|
[1494] | 582 | #ifdef ROAR_SUPPORT_LISTEN |
---|
[517] | 583 | sock_type = ROAR_SOCKET_TYPE_TCP; |
---|
[447] | 584 | if ( *server == '/' ) |
---|
| 585 | server = ROAR_DEFAULT_HOST; |
---|
[1494] | 586 | #endif |
---|
[518] | 587 | } else if ( strcmp(k, "-6") == 0 ) { |
---|
[1494] | 588 | #ifdef ROAR_SUPPORT_LISTEN |
---|
[519] | 589 | #ifdef PF_INET6 |
---|
[518] | 590 | sock_type = ROAR_SOCKET_TYPE_TCP6; |
---|
| 591 | if ( *server == '/' ) |
---|
| 592 | server = ROAR_DEFAULT_HOST; |
---|
[519] | 593 | #else |
---|
| 594 | ROAR_ERR("No IPv6 support compiled in!"); |
---|
| 595 | return 1; |
---|
| 596 | #endif |
---|
[1494] | 597 | #endif |
---|
[518] | 598 | |
---|
[573] | 599 | } else if ( strcmp(k, "-u") == 0 || strcmp(k, "--unix") == 0 ) { |
---|
[1494] | 600 | #ifdef ROAR_SUPPORT_LISTEN |
---|
[62] | 601 | // ignore this case as it is the default behavor. |
---|
[517] | 602 | sock_type = ROAR_SOCKET_TYPE_UNIX; |
---|
[1494] | 603 | #endif |
---|
[518] | 604 | |
---|
[573] | 605 | } else if ( strcmp(k, "-n") == 0 || strcmp(k, "--decnet") == 0 ) { |
---|
[1494] | 606 | #ifdef ROAR_SUPPORT_LISTEN |
---|
[508] | 607 | #ifdef ROAR_HAVE_LIBDNET |
---|
| 608 | port = ROAR_DEFAULT_NUM; |
---|
| 609 | strcpy(decnethost, ROAR_DEFAULT_LISTEN_OBJECT); |
---|
| 610 | server = decnethost; |
---|
[517] | 611 | sock_type = ROAR_SOCKET_TYPE_DECNET; |
---|
[508] | 612 | #else |
---|
| 613 | ROAR_ERR("No DECnet support compiled in!"); |
---|
| 614 | return 1; |
---|
| 615 | #endif |
---|
[1494] | 616 | #endif |
---|
[518] | 617 | |
---|
[60] | 618 | } else if ( strcmp(k, "-G") == 0 ) { |
---|
[444] | 619 | sock_grp = argv[++i]; |
---|
| 620 | } else if ( strcmp(k, "-U") == 0 ) { |
---|
| 621 | sock_user = argv[++i]; |
---|
[0] | 622 | |
---|
[68] | 623 | } else if ( strcmp(k, "--no-listen") == 0 ) { |
---|
[1494] | 624 | #ifdef ROAR_SUPPORT_LISTEN |
---|
[1787] | 625 | server = ""; |
---|
[548] | 626 | g_terminate = 1; |
---|
[1155] | 627 | g_no_listen = 1; |
---|
[1494] | 628 | #endif |
---|
[68] | 629 | } else if ( strcmp(k, "--client-fh") == 0 ) { |
---|
| 630 | if ( clients_set_fh(clients_new(), atoi(argv[++i])) == -1 ) { |
---|
| 631 | ROAR_ERR("main(*): Can not set client's fh"); |
---|
| 632 | return 1; |
---|
| 633 | } |
---|
[501] | 634 | } else if ( strcmp(k, "--close-fh") == 0 ) { |
---|
[1486] | 635 | #ifdef ROAR_HAVE_IO_POSIX |
---|
[501] | 636 | close(atoi(argv[++i])); |
---|
[1486] | 637 | #else |
---|
| 638 | i++; |
---|
| 639 | ROAR_WARN("can not close file handle %s (closing not supported)", argv[i]); |
---|
| 640 | #endif |
---|
[68] | 641 | |
---|
[920] | 642 | } else if ( strcmp(k, "--standby") == 0 ) { |
---|
| 643 | g_standby = 1; |
---|
| 644 | } else if ( strcmp(k, "--auto-standby") == 0 ) { |
---|
| 645 | g_autostandby = 1; |
---|
[0] | 646 | } else { |
---|
| 647 | usage(); |
---|
| 648 | return 1; |
---|
| 649 | } |
---|
| 650 | |
---|
| 651 | } |
---|
[1503] | 652 | #endif |
---|
[0] | 653 | |
---|
[1110] | 654 | if ( s_dev != NULL ) { |
---|
| 655 | if ( sources_add(s_drv, s_dev, s_con, s_opt, s_prim) == -1 ) { |
---|
| 656 | ROAR_ERR("main(*): adding source '%s' via '%s' failed!", s_dev, s_drv); |
---|
| 657 | } |
---|
| 658 | } |
---|
| 659 | |
---|
[1145] | 660 | add_output(o_drv, o_dev, o_opts, o_prim, o_count); |
---|
[932] | 661 | |
---|
[0] | 662 | ROAR_DBG("Server config: rate=%i, bits=%i, chans=%i", sa.rate, sa.bits, sa.channels); |
---|
| 663 | |
---|
[1819] | 664 | if ( midi_init() == -1 ) { |
---|
[281] | 665 | ROAR_ERR("Can not initialize MIDI subsystem"); |
---|
[1819] | 666 | } |
---|
| 667 | |
---|
| 668 | if ( light_init(light_channels) == -1 ) { |
---|
| 669 | ROAR_ERR("Can not initialize light control subsystem"); |
---|
| 670 | } |
---|
[281] | 671 | |
---|
[1494] | 672 | #ifdef ROAR_SUPPORT_LISTEN |
---|
[68] | 673 | if ( *server != 0 ) { |
---|
[517] | 674 | if ( (g_listen_socket = roar_socket_listen(sock_type, server, port)) == -1 ) { |
---|
[1486] | 675 | #ifdef ROAR_HAVE_UNIX |
---|
[286] | 676 | if ( *server == '/' ) { |
---|
[1011] | 677 | if ( (env_roar_proxy_backup = getenv("ROAR_PROXY")) != NULL ) { |
---|
| 678 | env_roar_proxy_backup = strdup(env_roar_proxy_backup); |
---|
| 679 | unsetenv("ROAR_PROXY"); |
---|
| 680 | } |
---|
[286] | 681 | if ( (i = roar_socket_connect(server, port)) != -1 ) { |
---|
| 682 | close(i); |
---|
| 683 | ROAR_ERR("Can not open listen socket!"); |
---|
| 684 | return 1; |
---|
| 685 | } else { |
---|
| 686 | unlink(server); |
---|
[517] | 687 | if ( (g_listen_socket = roar_socket_listen(sock_type, server, port)) == -1 ) { |
---|
[286] | 688 | ROAR_ERR("Can not open listen socket!"); |
---|
| 689 | return 1; |
---|
| 690 | } |
---|
| 691 | } |
---|
[1011] | 692 | if ( env_roar_proxy_backup != NULL ) { |
---|
| 693 | setenv("ROAR_PROXY", env_roar_proxy_backup, 0); |
---|
| 694 | free(env_roar_proxy_backup); |
---|
| 695 | } |
---|
[1486] | 696 | #else |
---|
| 697 | if (0) { // noop |
---|
| 698 | #endif |
---|
[286] | 699 | } else { |
---|
| 700 | ROAR_ERR("Can not open listen socket!"); |
---|
| 701 | return 1; |
---|
| 702 | } |
---|
[68] | 703 | } |
---|
[0] | 704 | |
---|
[1486] | 705 | #if defined(ROAR_HAVE_SETGID) && defined(ROAR_HAVE_IO_POSIX) |
---|
[523] | 706 | if ( (grp = getgrnam(sock_grp)) == NULL ) { |
---|
| 707 | ROAR_ERR("Can not get GID for group %s: %s", sock_grp, strerror(errno)); |
---|
| 708 | } |
---|
[1486] | 709 | #endif |
---|
| 710 | #if defined(ROAR_HAVE_SETUID) && defined(ROAR_HAVE_IO_POSIX) |
---|
[523] | 711 | if ( sock_user || (setids & R_SETUID) ) { |
---|
| 712 | if ( (pwd = getpwnam(sock_user)) == NULL ) { |
---|
| 713 | ROAR_ERR("Can not get UID for user %s: %s", sock_user, strerror(errno)); |
---|
| 714 | } |
---|
| 715 | } |
---|
[1486] | 716 | #endif |
---|
[523] | 717 | |
---|
[1516] | 718 | #if defined(ROAR_HAVE_IO_POSIX) && defined(ROAR_HAVE_UNIX) |
---|
[68] | 719 | if ( *server == '/' ) { |
---|
[523] | 720 | if ( grp ) { |
---|
[444] | 721 | if ( pwd ) { |
---|
[1691] | 722 | if ( chown(server, pwd->pw_uid, grp->gr_gid) == -1 ) |
---|
| 723 | return 1; |
---|
[444] | 724 | } else { |
---|
[1691] | 725 | if ( chown(server, -1, grp->gr_gid) == -1 ) |
---|
| 726 | return 1; |
---|
[444] | 727 | } |
---|
[1753] | 728 | #ifdef ROAR_HAVE_GETUID |
---|
[68] | 729 | if ( getuid() == 0 ) |
---|
[1691] | 730 | if ( chmod(server, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) == -1 ) |
---|
| 731 | return 1; |
---|
[1753] | 732 | #endif |
---|
[68] | 733 | } |
---|
[60] | 734 | } |
---|
[1486] | 735 | #endif |
---|
[60] | 736 | } |
---|
[1494] | 737 | #endif |
---|
[60] | 738 | |
---|
[0] | 739 | if ( output_buffer_init(&sa) == -1 ) { |
---|
| 740 | ROAR_ERR("Can not init output buffer!"); |
---|
| 741 | return 1; |
---|
| 742 | } |
---|
| 743 | |
---|
[1145] | 744 | if ( driver == NULL ) { |
---|
| 745 | driver = "null"; |
---|
| 746 | } else { |
---|
| 747 | ROAR_WARN("Usage of old driver interface. use -o not -d!"); |
---|
| 748 | } |
---|
| 749 | |
---|
[0] | 750 | if ( driver_open(&drvinst, &drvid, driver, device, &sa) == -1 ) { |
---|
| 751 | ROAR_ERR("Can not open output driver!"); |
---|
| 752 | return 1; |
---|
| 753 | } |
---|
| 754 | |
---|
[44] | 755 | if ( samples_init() == -1 ) { |
---|
| 756 | ROAR_ERR("Can not init samples!"); |
---|
| 757 | return 1; |
---|
| 758 | } |
---|
| 759 | |
---|
| 760 | |
---|
[1486] | 761 | // we should handle this on microcontrollers, too. |
---|
[1753] | 762 | #if !defined(ROAR_TARGET_MICROCONTROLLER) && !defined(ROAR_TARGET_WIN32) |
---|
[0] | 763 | signal(SIGINT, on_sig_int); |
---|
[285] | 764 | signal(SIGCHLD, on_sig_chld); |
---|
[0] | 765 | signal(SIGPIPE, SIG_IGN); // ignore broken pipes |
---|
[1486] | 766 | #endif |
---|
[0] | 767 | |
---|
[275] | 768 | if ( realtime ) { |
---|
[278] | 769 | #ifdef DEBUG |
---|
| 770 | ROAR_WARN("compiled with -DDEBUG but realtime is enabled: for real realtime support compiel without -DDEBUG"); |
---|
| 771 | #endif |
---|
| 772 | |
---|
[1486] | 773 | #ifdef ROAR_HAVE_NICE |
---|
[275] | 774 | errno = 0; |
---|
[276] | 775 | nice(-5*realtime); // -5 for each --realtime |
---|
[1486] | 776 | if ( errno ) { |
---|
| 777 | ROAR_WARN("Can not decrease nice value by %i: %s", 5*realtime, strerror(errno)); |
---|
| 778 | } |
---|
| 779 | #else |
---|
| 780 | ROAR_WARN("Can not decrease nice value by %i: %s", 5*realtime, strerror(errno)); |
---|
| 781 | #endif |
---|
[277] | 782 | /* |
---|
[276] | 783 | #ifdef __linux__ |
---|
[277] | 784 | if ( ioprio_set(IOPRIO_WHO_PROCESS, getpid(), IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 0)) == -1 ) |
---|
| 785 | ROAR_WARN("Can not set io priority: %s", strerror(errno)); |
---|
[276] | 786 | #endif |
---|
[277] | 787 | */ |
---|
[275] | 788 | } |
---|
| 789 | |
---|
[1486] | 790 | #ifdef ROAR_HAVE_SETGID |
---|
[444] | 791 | if ( setids & R_SETGID ) { |
---|
| 792 | if ( setgroups(0, (const gid_t *) NULL) == -1 ) { |
---|
| 793 | ROAR_ERR("Can not clear supplementary group IDs: %s", strerror(errno)); |
---|
| 794 | } |
---|
[523] | 795 | if ( !grp || setgid(grp->gr_gid) == -1 ) { |
---|
[444] | 796 | ROAR_ERR("Can not set GroupID: %s", strerror(errno)); |
---|
| 797 | } |
---|
| 798 | } |
---|
[1486] | 799 | #endif |
---|
[444] | 800 | |
---|
[0] | 801 | |
---|
[39] | 802 | clients_set_pid(g_self_client, getpid()); |
---|
[1753] | 803 | #ifdef ROAR_HAVE_GETUID |
---|
[440] | 804 | clients_set_uid(g_self_client, getuid()); |
---|
[1753] | 805 | #endif |
---|
| 806 | #ifdef ROAR_HAVE_GETGID |
---|
[440] | 807 | clients_set_gid(g_self_client, getgid()); |
---|
[1753] | 808 | #endif |
---|
[39] | 809 | clients_get(g_self_client, &self); |
---|
[37] | 810 | |
---|
[39] | 811 | if ( self == NULL ) { |
---|
| 812 | ROAR_ERR("Can not get self client!"); |
---|
| 813 | return 1; |
---|
| 814 | } |
---|
| 815 | |
---|
[775] | 816 | strcpy(self->name, "RoarAudio daemon internal"); |
---|
[68] | 817 | |
---|
[1486] | 818 | #ifdef ROAR_HAVE_FORK |
---|
[775] | 819 | if ( daemon ) { |
---|
[68] | 820 | close(ROAR_STDIN ); |
---|
| 821 | close(ROAR_STDOUT); |
---|
| 822 | close(ROAR_STDERR); |
---|
[1753] | 823 | |
---|
[68] | 824 | if ( fork() ) |
---|
[1486] | 825 | ROAR_U_EXIT(0); |
---|
[1753] | 826 | |
---|
| 827 | #ifdef ROAR_HAVE_SETSID |
---|
| 828 | setsid(); |
---|
| 829 | #endif |
---|
[1046] | 830 | clients_set_pid(g_self_client, getpid()); // reset pid as it changed |
---|
[68] | 831 | } |
---|
[1486] | 832 | #endif |
---|
[68] | 833 | |
---|
[1486] | 834 | #ifdef ROAR_HAVE_CHROOT |
---|
[444] | 835 | if (chrootdir) { |
---|
| 836 | if ( chroot(chrootdir) == -1 ) { |
---|
| 837 | ROAR_ERR("Can not chroot to %s: %s", chrootdir, strerror(errno)); |
---|
| 838 | return 2; |
---|
| 839 | } |
---|
| 840 | if ( chdir("/") == -1 ) { |
---|
| 841 | ROAR_ERR("Can not chdir to /: %s", strerror(errno)); |
---|
| 842 | return 2; |
---|
| 843 | } |
---|
| 844 | } |
---|
[1486] | 845 | #endif |
---|
[444] | 846 | |
---|
[1486] | 847 | #ifdef ROAR_HAVE_SETUID |
---|
[444] | 848 | if ( setids & R_SETUID ) { |
---|
| 849 | if ( !pwd || setuid(pwd->pw_uid) == -1 ) { |
---|
| 850 | ROAR_ERR("Can not set UserID: %s", strerror(errno)); |
---|
| 851 | return 3; |
---|
| 852 | } |
---|
[1753] | 853 | #ifdef ROAR_HAVE_GETUID |
---|
[444] | 854 | clients_set_uid(g_self_client, getuid()); |
---|
[1753] | 855 | #endif |
---|
[444] | 856 | } |
---|
[1486] | 857 | #endif |
---|
[444] | 858 | |
---|
[0] | 859 | // start main loop... |
---|
[905] | 860 | main_loop(drvid, drvinst, &sa, sysclocksync); |
---|
[0] | 861 | |
---|
| 862 | // clean up. |
---|
| 863 | clean_quit_prep(); |
---|
| 864 | driver_close(drvinst, drvid); |
---|
| 865 | output_buffer_free(); |
---|
| 866 | |
---|
| 867 | return 0; |
---|
| 868 | } |
---|
| 869 | |
---|
[574] | 870 | void cleanup_listen_socket (int terminate) { |
---|
[580] | 871 | |
---|
[1494] | 872 | #ifdef ROAR_SUPPORT_LISTEN |
---|
[580] | 873 | if ( g_listen_socket != -1 ) { |
---|
[1486] | 874 | #ifdef ROAR_HAVE_IO_POSIX |
---|
[580] | 875 | close(g_listen_socket); |
---|
[1486] | 876 | #endif // #else is useless because we are in void context. |
---|
[60] | 877 | |
---|
[580] | 878 | g_listen_socket = -1; |
---|
[576] | 879 | |
---|
[1486] | 880 | #ifdef ROAR_HAVE_UNIX |
---|
[580] | 881 | if ( *server == '/' ) |
---|
| 882 | unlink(server); |
---|
[1486] | 883 | #endif |
---|
[580] | 884 | } |
---|
[60] | 885 | |
---|
[1494] | 886 | #endif |
---|
| 887 | |
---|
[574] | 888 | if ( terminate ) |
---|
| 889 | g_terminate = 1; |
---|
| 890 | } |
---|
| 891 | |
---|
| 892 | void clean_quit_prep (void) { |
---|
| 893 | cleanup_listen_socket(0); |
---|
[60] | 894 | |
---|
[0] | 895 | sources_free(); |
---|
| 896 | streams_free(); |
---|
| 897 | clients_free(); |
---|
[282] | 898 | midi_cb_stop(); // stop console beep |
---|
[281] | 899 | midi_free(); |
---|
[1819] | 900 | light_free(); |
---|
[0] | 901 | } |
---|
| 902 | |
---|
| 903 | void clean_quit (void) { |
---|
| 904 | clean_quit_prep(); |
---|
| 905 | // driver_close(drvinst, drvid); |
---|
| 906 | // output_buffer_free(); |
---|
| 907 | exit(0); |
---|
| 908 | } |
---|
| 909 | |
---|
| 910 | //ll |
---|