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