[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 | |
---|
[60] | 27 | char * server = ROAR_DEFAULT_SOCK_GLOBAL; // global server address |
---|
| 28 | |
---|
[0] | 29 | void usage (void) { |
---|
| 30 | printf("Usage: roard [OPTIONS]...\n\n"); |
---|
| 31 | |
---|
[68] | 32 | printf("Misc Options:\n\n"); |
---|
| 33 | printf( |
---|
[775] | 34 | " --daemon - Bring the server into background after init\n" |
---|
[71] | 35 | " --terminate - Terminate after last client quited\n" |
---|
[274] | 36 | " --restart - Trys to stop an old instance and start a new with new settings\n" |
---|
[276] | 37 | " --realtime - Trys to get realtime priority,\n" |
---|
| 38 | " give multible times for being more realtime\n" |
---|
[444] | 39 | " --chroot DIR - chroots to the given dir\n" |
---|
| 40 | " --setgid - GroupID to the audio group as specified via -G\n" |
---|
| 41 | " --setuid - UserID to the audio user as specified via -U\n" |
---|
[905] | 42 | " --sysclocksync - calculate exact sample rate using the system clock\n" |
---|
[68] | 43 | ); |
---|
| 44 | |
---|
| 45 | printf("\nAudio Options:\n\n"); |
---|
[0] | 46 | printf( |
---|
| 47 | " -R --rate RATE - Set server rate\n" |
---|
| 48 | " -B --bits BITS - Set server bits\n" |
---|
| 49 | " -C --chans CHANNELS - Set server channels\n" |
---|
| 50 | ); |
---|
| 51 | |
---|
| 52 | printf("\nDriver Options:\n\n"); |
---|
| 53 | printf(" -d --driver DRV - Set the driver, use '-d list' to get a list (default: %s)\n", ROAR_DRIVER_DEFAULT); |
---|
| 54 | printf(" -D --device DEV - Set the device\n"); |
---|
| 55 | printf(" -dO OPTS - Set output options\n"); |
---|
[973] | 56 | printf(" --list-driver - List all drivers\n"); |
---|
[0] | 57 | |
---|
[932] | 58 | printf("\nOutput Options:\n\n"); |
---|
| 59 | printf(" -o --odriver DRV - Set the driver, use '-d list' to get a list\n"); |
---|
| 60 | printf(" -O --odevice DEV - Set the device\n"); |
---|
| 61 | printf(" -oO OPTS - Set output options\n"); |
---|
| 62 | printf(" -oN - Adds another output\n"); |
---|
[961] | 63 | printf(" -oP - Mark output as primary\n"); |
---|
[932] | 64 | |
---|
[0] | 65 | printf("\nSource Options:\n\n"); |
---|
| 66 | printf(" -s --source DRV - Use DRV as input driver\n" |
---|
| 67 | " -S DEV - Use DEV as input device\n" |
---|
| 68 | " -sO OPTS - Use OPTS as input options\n" |
---|
| 69 | " -sP - Make souce as primary\n" |
---|
| 70 | ); |
---|
| 71 | |
---|
[280] | 72 | printf("\nCodec Filter Options:\n\n"); |
---|
| 73 | printf(" --list-cf - List all codec filter\n" |
---|
| 74 | ); |
---|
[0] | 75 | |
---|
| 76 | printf("\nServer Options:\n\n"); |
---|
| 77 | printf(" -t --tcp - Use TCP listen socket\n" |
---|
| 78 | " -u --unix - Use UNIX Domain listen socket (default)\n" |
---|
[508] | 79 | #ifdef ROAR_HAVE_LIBDNET |
---|
| 80 | " -n --decnet - use DECnet listen socket\n" |
---|
| 81 | #endif |
---|
[518] | 82 | " -4 - Use IPv4 connections (implies -t)\n" |
---|
| 83 | #ifdef PF_INET6 |
---|
| 84 | " -6 - Use IPv6 connections (implies -t)\n" |
---|
| 85 | #endif |
---|
| 86 | #ifdef IPV6_ADDRFORM |
---|
| 87 | " -64 - Try to downgrade sockets from IPv6 into IPv4,\n" |
---|
| 88 | " this is normaly not usefull.\n" |
---|
| 89 | #endif |
---|
[0] | 90 | " -p --port - TCP Port to bind to\n" |
---|
| 91 | " -b --bind - IP/Hostname to bind to\n" |
---|
| 92 | " -s --sock - Filename for UNIX Domain Socket\n" |
---|
[450] | 93 | " -G GROUP - Sets the group for the UNIX Domain Socket, (default: %s)\n" |
---|
[60] | 94 | " You need the permittions to change the GID\n" |
---|
[444] | 95 | " -U USER - Sets the user for the UNIX Domain Socket, (default: do not set)\n" |
---|
| 96 | " You need the permittions to change the UID (normaly only root has)\n" |
---|
[548] | 97 | " --no-listen - Do not listen for new clients\n" |
---|
| 98 | " (only usefull for relaing, impleys --terminate)\n" |
---|
[274] | 99 | " --client-fh - Comunicate with a client over this handle\n" |
---|
[501] | 100 | " (only usefull for relaing)\n" |
---|
[920] | 101 | " --close-fh - Closes the given fh\n" |
---|
| 102 | " --standby - Start in standby state\n" |
---|
| 103 | " --auto-standby - Automatical goes into standby if there are no streams\n", |
---|
[450] | 104 | ROAR_DEFAULT_SOCKGRP |
---|
[0] | 105 | ); |
---|
| 106 | // printf("\n Options:\n\n"); |
---|
| 107 | printf("\n"); |
---|
| 108 | } |
---|
| 109 | |
---|
[579] | 110 | int restart_server (char * server) { |
---|
| 111 | struct roar_connection con; |
---|
| 112 | if ( roar_connect(&con, server) == -1 ) { |
---|
| 113 | return -1; |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | if ( roar_terminate(&con, 1) == -1 ) { |
---|
| 117 | return -1; |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | return roar_disconnect(&con); |
---|
| 121 | } |
---|
| 122 | |
---|
[444] | 123 | #define R_SETUID 1 |
---|
| 124 | #define R_SETGID 2 |
---|
| 125 | |
---|
[961] | 126 | int add_output (char * drv, char * dev, char * opts, int prim) { |
---|
[933] | 127 | int stream; |
---|
| 128 | struct roar_stream * s; |
---|
| 129 | struct roar_stream_server * ss; |
---|
[938] | 130 | char * k, * v; |
---|
| 131 | int codec; |
---|
[933] | 132 | |
---|
[938] | 133 | ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = ?", drv, dev, opts); |
---|
[933] | 134 | |
---|
| 135 | if ( (stream = streams_new()) == -1 ) { |
---|
[938] | 136 | ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = -1", drv, dev, opts); |
---|
[933] | 137 | return -1; |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | streams_get(stream, &ss); |
---|
| 141 | s = ROAR_STREAM(ss); |
---|
| 142 | |
---|
| 143 | memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info)); |
---|
| 144 | |
---|
| 145 | s->dir = ROAR_DIR_OUTPUT; |
---|
| 146 | s->pos_rel_id = -1; |
---|
| 147 | // s->info.codec = codec; |
---|
| 148 | |
---|
[938] | 149 | codec = s->info.codec; |
---|
| 150 | |
---|
| 151 | k = strtok(opts, ","); |
---|
| 152 | while (k != NULL) { |
---|
| 153 | // ROAR_WARN("add_output(*): opts: %s", k); |
---|
| 154 | |
---|
| 155 | if ( (v = strstr(k, "=")) != NULL ) { |
---|
| 156 | *v++ = 0; |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | ROAR_DBG("add_output(*): opts: k='%s', v='%s'", k, v); |
---|
| 160 | if ( strcmp(k, "rate") == 0 ) { |
---|
| 161 | s->info.rate = atoi(v); |
---|
| 162 | } else if ( strcmp(k, "channels") == 0 ) { |
---|
| 163 | s->info.channels = atoi(v); |
---|
| 164 | } else if ( strcmp(k, "bits") == 0 ) { |
---|
| 165 | s->info.bits = atoi(v); |
---|
| 166 | } else if ( strcmp(k, "codec") == 0 ) { |
---|
| 167 | if ( (codec = roar_str2codec(v)) == -1 ) { |
---|
| 168 | ROAR_ERR("add_output(*): unknown codec '%s'", v); |
---|
| 169 | streams_delete(stream); |
---|
| 170 | return -1; |
---|
| 171 | } |
---|
| 172 | } else { |
---|
| 173 | ROAR_ERR("add_output(*): unknown option '%s'", k); |
---|
| 174 | streams_delete(stream); |
---|
| 175 | return -1; |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | k = strtok(NULL, ","); |
---|
| 179 | } |
---|
| 180 | |
---|
[941] | 181 | if ( codec == ROAR_CODEC_ALAW || codec == ROAR_CODEC_MULAW ) |
---|
| 182 | s->info.bits = 8; // needed to open OSS driver, will be overriden by codecfilter |
---|
| 183 | |
---|
[938] | 184 | s->info.codec = codec; |
---|
| 185 | ROAR_STREAM_SERVER(s)->codec_orgi = codec; |
---|
[933] | 186 | |
---|
| 187 | if ( driver_openvio(&(ss->vio), &(ss->driver_id), drv, dev, &(s->info), -1) ) { |
---|
| 188 | streams_delete(stream); |
---|
[938] | 189 | ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = -1", drv, dev, opts); |
---|
[933] | 190 | return -1; |
---|
| 191 | } |
---|
| 192 | |
---|
[938] | 193 | streams_set_fh(stream, -1); // update some internal structures |
---|
| 194 | |
---|
[933] | 195 | client_stream_add(g_source_client, stream); |
---|
| 196 | |
---|
[961] | 197 | if ( prim ) |
---|
| 198 | streams_mark_primary(stream); |
---|
| 199 | |
---|
[933] | 200 | return 0; |
---|
[932] | 201 | } |
---|
| 202 | |
---|
[0] | 203 | int main (int argc, char * argv[]) { |
---|
| 204 | int i; |
---|
| 205 | char * k; |
---|
[905] | 206 | char user_sock[80] = {0}; |
---|
[0] | 207 | struct roar_audio_info sa; |
---|
[905] | 208 | int daemon = 0; |
---|
| 209 | int realtime = 0; |
---|
| 210 | int sysclocksync = 0; |
---|
[74] | 211 | char * driver = getenv("ROAR_DRIVER"); |
---|
| 212 | char * device = getenv("ROAR_DEVICE"); |
---|
[0] | 213 | char * opts = NULL; |
---|
[60] | 214 | // char * server = ROAR_DEFAULT_SOCK_GLOBAL; |
---|
[0] | 215 | int port = ROAR_DEFAULT_PORT; |
---|
| 216 | int drvid; |
---|
[550] | 217 | char * s_drv = "cf"; |
---|
[444] | 218 | char * s_con = NULL; |
---|
| 219 | char * s_opt = NULL; |
---|
| 220 | int s_prim = 0; |
---|
[932] | 221 | char * o_drv = NULL; |
---|
| 222 | char * o_dev = NULL; |
---|
| 223 | char * o_opts = NULL; |
---|
[961] | 224 | int o_prim = 0; |
---|
[450] | 225 | char * sock_grp = ROAR_DEFAULT_SOCKGRP; |
---|
[444] | 226 | char * sock_user = NULL; |
---|
[517] | 227 | int sock_type = ROAR_SOCKET_TYPE_UNKNOWN; |
---|
[444] | 228 | char * chrootdir = NULL; |
---|
| 229 | int setids = 0; |
---|
[446] | 230 | struct group * grp = NULL; |
---|
| 231 | struct passwd * pwd = NULL; |
---|
| 232 | struct servent * serv = NULL; |
---|
[0] | 233 | DRIVER_USERDATA_T drvinst; |
---|
[39] | 234 | struct roar_client * self = NULL; |
---|
[508] | 235 | #ifdef ROAR_HAVE_LIBDNET |
---|
| 236 | char decnethost[80]; |
---|
| 237 | #endif |
---|
[0] | 238 | |
---|
| 239 | g_listen_socket = -1; |
---|
| 240 | g_standby = 0; |
---|
[920] | 241 | g_autostandby = 0; |
---|
[0] | 242 | |
---|
| 243 | sa.bits = ROAR_BITS_DEFAULT; |
---|
| 244 | sa.channels = ROAR_CHANNELS_DEFAULT; |
---|
| 245 | sa.rate = ROAR_RATE_DEFAULT; |
---|
| 246 | sa.codec = ROAR_CODEC_DEFAULT; |
---|
| 247 | |
---|
| 248 | g_sa = &sa; |
---|
| 249 | |
---|
[60] | 250 | |
---|
| 251 | if ( getuid() != 0 && getenv("HOME") ) { |
---|
| 252 | snprintf(user_sock, 79, "%s/%s", getenv("HOME"), ROAR_DEFAULT_SOCK_USER); |
---|
| 253 | server = user_sock; |
---|
| 254 | } |
---|
| 255 | |
---|
[279] | 256 | if ( getenv("ROAR_SERVER") != NULL ) |
---|
| 257 | server = getenv("ROAR_SERVER"); |
---|
| 258 | |
---|
[63] | 259 | if ( clients_init() == -1 ) { |
---|
| 260 | ROAR_ERR("Can not init clients!"); |
---|
| 261 | return 1; |
---|
| 262 | } |
---|
| 263 | |
---|
| 264 | if ( streams_init() == -1 ) { |
---|
| 265 | ROAR_ERR("Can not init streams!"); |
---|
| 266 | return 1; |
---|
| 267 | } |
---|
| 268 | |
---|
[64] | 269 | if ( (g_self_client = clients_new()) == -1 ) { |
---|
| 270 | ROAR_ERR("Can not create self client!"); |
---|
| 271 | return 1; |
---|
| 272 | } |
---|
| 273 | |
---|
[0] | 274 | if ( sources_init() == -1 ) { |
---|
| 275 | ROAR_ERR("Can not init sources!"); |
---|
| 276 | return 1; |
---|
| 277 | } |
---|
| 278 | |
---|
[64] | 279 | if ( (sources_set_client(g_self_client)) == -1 ) { |
---|
| 280 | ROAR_ERR("Can not init set source client!"); |
---|
| 281 | return 1; |
---|
| 282 | } |
---|
| 283 | |
---|
[0] | 284 | for (i = 1; i < argc; i++) { |
---|
| 285 | k = argv[i]; |
---|
| 286 | |
---|
| 287 | if ( strcmp(k, "-h") == 0 || strcmp(k, "--help") == 0 ) { |
---|
| 288 | usage(); |
---|
| 289 | return 0; |
---|
| 290 | |
---|
[579] | 291 | } else if ( strcmp(k, "--restart") == 0 ) { |
---|
| 292 | if ( restart_server(server) == -1 ) { |
---|
| 293 | ROAR_WARN("Can not terminate old server (not running at %s?), tring to continue anyway", server); |
---|
| 294 | } |
---|
| 295 | |
---|
[775] | 296 | } else if ( strcmp(k, "--demon") == 0 || strcmp(k, "--daemon") == 0 ) { |
---|
| 297 | daemon = 1; |
---|
[71] | 298 | } else if ( strcmp(k, "--terminate") == 0 ) { |
---|
| 299 | g_terminate = 1; |
---|
[905] | 300 | } else if ( strcmp(k, "--sysclocksync") == 0 ) { |
---|
| 301 | sysclocksync = 1000; |
---|
[275] | 302 | } else if ( strcmp(k, "--realtime") == 0 ) { |
---|
[276] | 303 | realtime++; |
---|
[444] | 304 | } else if ( strcmp(k, "--chroot") == 0 ) { |
---|
| 305 | chrootdir = argv[++i]; |
---|
| 306 | } else if ( strcmp(k, "--setgid") == 0 ) { |
---|
| 307 | setids |= R_SETGID; |
---|
| 308 | } else if ( strcmp(k, "--setuid") == 0 ) { |
---|
| 309 | setids |= R_SETUID; |
---|
[68] | 310 | |
---|
[280] | 311 | } else if ( strcmp(k, "--list-cf") == 0 ) { |
---|
| 312 | print_codecfilterlist(); |
---|
| 313 | return 0; |
---|
| 314 | |
---|
[0] | 315 | } else if ( strcmp(k, "-R") == 0 || strcmp(k, "--rate") == 0 ) { |
---|
| 316 | sa.rate = atoi(argv[++i]); |
---|
| 317 | } else if ( strcmp(k, "-B") == 0 || strcmp(k, "--bits") == 0 ) { |
---|
| 318 | sa.bits = atoi(argv[++i]); |
---|
| 319 | } else if ( strcmp(k, "-C") == 0 || strcmp(k, "--chans") == 0 ) { |
---|
| 320 | sa.channels = atoi(argv[++i]); |
---|
| 321 | |
---|
| 322 | } else if ( strcmp(k, "-d") == 0 || strcmp(k, "--driver") == 0 ) { |
---|
| 323 | driver = argv[++i]; |
---|
| 324 | if ( strcmp(driver, "list") == 0 ) { |
---|
| 325 | print_driverlist(); |
---|
[973] | 326 | return 0; |
---|
[0] | 327 | } |
---|
| 328 | } else if ( strcmp(k, "-D") == 0 || strcmp(k, "--device") == 0 ) { |
---|
| 329 | device = argv[++i]; |
---|
| 330 | } else if ( strcmp(k, "-dO") == 0 ) { |
---|
| 331 | opts = argv[++i]; |
---|
[973] | 332 | } else if ( strcmp(k, "--list-driver") == 0 ) { |
---|
| 333 | print_driverlist(); |
---|
| 334 | return 0; |
---|
[0] | 335 | |
---|
[932] | 336 | } else if ( strcmp(k, "-o") == 0 || strcmp(k, "--odriver") == 0 ) { |
---|
| 337 | o_drv = argv[++i]; |
---|
| 338 | } else if ( strcmp(k, "-O") == 0 || strcmp(k, "--odevice") == 0 ) { |
---|
| 339 | o_dev = argv[++i]; |
---|
| 340 | } else if ( strcmp(k, "-oO") == 0 ) { |
---|
| 341 | o_opts = argv[++i]; |
---|
[961] | 342 | } else if ( strcmp(k, "-oP") == 0 ) { |
---|
| 343 | o_prim = 1; |
---|
[932] | 344 | } else if ( strcmp(k, "-oN") == 0 ) { |
---|
[961] | 345 | add_output(o_drv, o_dev, o_opts, o_prim); |
---|
| 346 | o_drv = o_dev = o_opts = NULL; |
---|
| 347 | o_prim = 0; |
---|
[932] | 348 | |
---|
[0] | 349 | } else if ( strcmp(k, "-s") == 0 || strcmp(k, "--source") == 0 ) { |
---|
[550] | 350 | s_drv = argv[++i]; |
---|
| 351 | } else if ( strcmp(k, "-S") == 0 ) { |
---|
[0] | 352 | k = argv[++i]; |
---|
[550] | 353 | if ( sources_add(s_drv, k, s_con, s_opt, s_prim) == -1 ) { |
---|
| 354 | ROAR_ERR("main(*): adding source '%s' via '%s' failed!", k, s_drv); |
---|
[0] | 355 | } |
---|
[550] | 356 | s_opt = s_con = NULL; |
---|
[0] | 357 | s_prim = 0; |
---|
| 358 | } else if ( strcmp(k, "-sO") == 0 ) { |
---|
| 359 | s_opt = argv[++i]; |
---|
| 360 | } else if ( strcmp(k, "-sC") == 0 ) { |
---|
| 361 | s_con = argv[++i]; |
---|
| 362 | } else if ( strcmp(k, "-sP") == 0 ) { |
---|
| 363 | s_prim = 1; |
---|
| 364 | |
---|
| 365 | } else if ( strcmp(k, "-p") == 0 || strcmp(k, "--port") == 0 ) { |
---|
[447] | 366 | // This is only usefull in INET not UNIX mode. |
---|
| 367 | if ( *server == '/' ) |
---|
| 368 | server = ROAR_DEFAULT_HOST; |
---|
| 369 | |
---|
[446] | 370 | errno = 0; |
---|
| 371 | if ( (port = atoi(argv[++i])) < 1 ) { |
---|
| 372 | if ( (serv = getservbyname(argv[i], "tcp")) == NULL ) { |
---|
| 373 | ROAR_ERR("Unknown service: %s: %s", argv[i], strerror(errno)); |
---|
| 374 | return 1; |
---|
| 375 | } |
---|
| 376 | // NOTE: we need to use ROAR_NET2HOST16() here even if s_port is of type int! |
---|
| 377 | ROAR_DBG("main(*): serv = {s_name='%s', s_aliases={...}, s_port=%i, s_proto='%s'}", |
---|
| 378 | serv->s_name, ROAR_NET2HOST16(serv->s_port), serv->s_proto); |
---|
| 379 | port = ROAR_NET2HOST16(serv->s_port); |
---|
| 380 | } |
---|
[68] | 381 | } else if ( strcmp(k, "-b") == 0 || strcmp(k, "--bind") == 0 || strcmp(k, "-s") == 0 || strcmp(k, "--sock") == 0 ) { |
---|
[0] | 382 | server = argv[++i]; |
---|
[518] | 383 | |
---|
[573] | 384 | } else if ( strcmp(k, "-t") == 0 || strcmp(k, "--tcp") == 0 ) { |
---|
[518] | 385 | if ( sock_type != ROAR_SOCKET_TYPE_TCP && sock_type != ROAR_SOCKET_TYPE_TCP6 ) |
---|
| 386 | sock_type = ROAR_SOCKET_TYPE_TCP; |
---|
| 387 | |
---|
| 388 | if ( *server == '/' ) |
---|
| 389 | server = ROAR_DEFAULT_HOST; |
---|
| 390 | |
---|
| 391 | } else if ( strcmp(k, "-4") == 0 ) { |
---|
[517] | 392 | sock_type = ROAR_SOCKET_TYPE_TCP; |
---|
[447] | 393 | if ( *server == '/' ) |
---|
| 394 | server = ROAR_DEFAULT_HOST; |
---|
[518] | 395 | } else if ( strcmp(k, "-6") == 0 ) { |
---|
[519] | 396 | #ifdef PF_INET6 |
---|
[518] | 397 | sock_type = ROAR_SOCKET_TYPE_TCP6; |
---|
| 398 | if ( *server == '/' ) |
---|
| 399 | server = ROAR_DEFAULT_HOST; |
---|
[519] | 400 | #else |
---|
| 401 | ROAR_ERR("No IPv6 support compiled in!"); |
---|
| 402 | return 1; |
---|
| 403 | #endif |
---|
[518] | 404 | |
---|
[573] | 405 | } else if ( strcmp(k, "-u") == 0 || strcmp(k, "--unix") == 0 ) { |
---|
[62] | 406 | // ignore this case as it is the default behavor. |
---|
[517] | 407 | sock_type = ROAR_SOCKET_TYPE_UNIX; |
---|
[518] | 408 | |
---|
[573] | 409 | } else if ( strcmp(k, "-n") == 0 || strcmp(k, "--decnet") == 0 ) { |
---|
[508] | 410 | #ifdef ROAR_HAVE_LIBDNET |
---|
| 411 | port = ROAR_DEFAULT_NUM; |
---|
| 412 | strcpy(decnethost, ROAR_DEFAULT_LISTEN_OBJECT); |
---|
| 413 | server = decnethost; |
---|
[517] | 414 | sock_type = ROAR_SOCKET_TYPE_DECNET; |
---|
[508] | 415 | #else |
---|
| 416 | ROAR_ERR("No DECnet support compiled in!"); |
---|
| 417 | return 1; |
---|
| 418 | #endif |
---|
[518] | 419 | |
---|
[60] | 420 | } else if ( strcmp(k, "-G") == 0 ) { |
---|
[444] | 421 | sock_grp = argv[++i]; |
---|
| 422 | } else if ( strcmp(k, "-U") == 0 ) { |
---|
| 423 | sock_user = argv[++i]; |
---|
[0] | 424 | |
---|
[68] | 425 | } else if ( strcmp(k, "--no-listen") == 0 ) { |
---|
[548] | 426 | *server = 0; |
---|
| 427 | g_terminate = 1; |
---|
[68] | 428 | } else if ( strcmp(k, "--client-fh") == 0 ) { |
---|
| 429 | if ( clients_set_fh(clients_new(), atoi(argv[++i])) == -1 ) { |
---|
| 430 | ROAR_ERR("main(*): Can not set client's fh"); |
---|
| 431 | return 1; |
---|
| 432 | } |
---|
[501] | 433 | } else if ( strcmp(k, "--close-fh") == 0 ) { |
---|
| 434 | close(atoi(argv[++i])); |
---|
[68] | 435 | |
---|
[920] | 436 | } else if ( strcmp(k, "--standby") == 0 ) { |
---|
| 437 | g_standby = 1; |
---|
| 438 | } else if ( strcmp(k, "--auto-standby") == 0 ) { |
---|
| 439 | g_autostandby = 1; |
---|
[0] | 440 | } else { |
---|
| 441 | usage(); |
---|
| 442 | return 1; |
---|
| 443 | } |
---|
| 444 | |
---|
| 445 | } |
---|
| 446 | |
---|
[932] | 447 | if ( o_drv != NULL ) |
---|
[961] | 448 | add_output(o_drv, o_dev, o_opts, o_prim); |
---|
[932] | 449 | |
---|
[0] | 450 | ROAR_DBG("Server config: rate=%i, bits=%i, chans=%i", sa.rate, sa.bits, sa.channels); |
---|
| 451 | |
---|
[281] | 452 | if ( midi_init() == -1 ) |
---|
| 453 | ROAR_ERR("Can not initialize MIDI subsystem"); |
---|
| 454 | |
---|
[68] | 455 | if ( *server != 0 ) { |
---|
[517] | 456 | if ( (g_listen_socket = roar_socket_listen(sock_type, server, port)) == -1 ) { |
---|
[286] | 457 | if ( *server == '/' ) { |
---|
| 458 | if ( (i = roar_socket_connect(server, port)) != -1 ) { |
---|
| 459 | close(i); |
---|
| 460 | ROAR_ERR("Can not open listen socket!"); |
---|
| 461 | return 1; |
---|
| 462 | } else { |
---|
| 463 | unlink(server); |
---|
[517] | 464 | if ( (g_listen_socket = roar_socket_listen(sock_type, server, port)) == -1 ) { |
---|
[286] | 465 | ROAR_ERR("Can not open listen socket!"); |
---|
| 466 | return 1; |
---|
| 467 | } |
---|
| 468 | } |
---|
| 469 | } else { |
---|
| 470 | ROAR_ERR("Can not open listen socket!"); |
---|
| 471 | return 1; |
---|
| 472 | } |
---|
[68] | 473 | } |
---|
[0] | 474 | |
---|
[523] | 475 | if ( (grp = getgrnam(sock_grp)) == NULL ) { |
---|
| 476 | ROAR_ERR("Can not get GID for group %s: %s", sock_grp, strerror(errno)); |
---|
| 477 | } |
---|
| 478 | if ( sock_user || (setids & R_SETUID) ) { |
---|
| 479 | if ( (pwd = getpwnam(sock_user)) == NULL ) { |
---|
| 480 | ROAR_ERR("Can not get UID for user %s: %s", sock_user, strerror(errno)); |
---|
| 481 | } |
---|
| 482 | } |
---|
| 483 | |
---|
[68] | 484 | if ( *server == '/' ) { |
---|
[523] | 485 | if ( grp ) { |
---|
[444] | 486 | if ( pwd ) { |
---|
| 487 | chown(server, pwd->pw_uid, grp->gr_gid); |
---|
| 488 | } else { |
---|
| 489 | chown(server, -1, grp->gr_gid); |
---|
| 490 | } |
---|
[68] | 491 | if ( getuid() == 0 ) |
---|
| 492 | chmod(server, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP); |
---|
| 493 | } |
---|
[60] | 494 | } |
---|
| 495 | } |
---|
| 496 | |
---|
[0] | 497 | if ( output_buffer_init(&sa) == -1 ) { |
---|
| 498 | ROAR_ERR("Can not init output buffer!"); |
---|
| 499 | return 1; |
---|
| 500 | } |
---|
| 501 | |
---|
| 502 | if ( driver_open(&drvinst, &drvid, driver, device, &sa) == -1 ) { |
---|
| 503 | ROAR_ERR("Can not open output driver!"); |
---|
| 504 | return 1; |
---|
| 505 | } |
---|
| 506 | |
---|
[44] | 507 | if ( samples_init() == -1 ) { |
---|
| 508 | ROAR_ERR("Can not init samples!"); |
---|
| 509 | return 1; |
---|
| 510 | } |
---|
| 511 | |
---|
| 512 | |
---|
[0] | 513 | signal(SIGINT, on_sig_int); |
---|
[285] | 514 | signal(SIGCHLD, on_sig_chld); |
---|
[0] | 515 | signal(SIGPIPE, SIG_IGN); // ignore broken pipes |
---|
| 516 | |
---|
[275] | 517 | if ( realtime ) { |
---|
[278] | 518 | #ifdef DEBUG |
---|
| 519 | ROAR_WARN("compiled with -DDEBUG but realtime is enabled: for real realtime support compiel without -DDEBUG"); |
---|
| 520 | #endif |
---|
| 521 | |
---|
[275] | 522 | errno = 0; |
---|
[276] | 523 | nice(-5*realtime); // -5 for each --realtime |
---|
[275] | 524 | if ( errno ) |
---|
| 525 | ROAR_WARN("Can not decrease nice value by 5: %s", strerror(errno)); |
---|
[277] | 526 | /* |
---|
[276] | 527 | #ifdef __linux__ |
---|
[277] | 528 | if ( ioprio_set(IOPRIO_WHO_PROCESS, getpid(), IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 0)) == -1 ) |
---|
| 529 | ROAR_WARN("Can not set io priority: %s", strerror(errno)); |
---|
[276] | 530 | #endif |
---|
[277] | 531 | */ |
---|
[275] | 532 | } |
---|
| 533 | |
---|
[444] | 534 | if ( setids & R_SETGID ) { |
---|
| 535 | if ( setgroups(0, (const gid_t *) NULL) == -1 ) { |
---|
| 536 | ROAR_ERR("Can not clear supplementary group IDs: %s", strerror(errno)); |
---|
| 537 | } |
---|
[523] | 538 | if ( !grp || setgid(grp->gr_gid) == -1 ) { |
---|
[444] | 539 | ROAR_ERR("Can not set GroupID: %s", strerror(errno)); |
---|
| 540 | } |
---|
| 541 | } |
---|
| 542 | |
---|
[0] | 543 | |
---|
[39] | 544 | clients_set_pid(g_self_client, getpid()); |
---|
[440] | 545 | clients_set_uid(g_self_client, getuid()); |
---|
| 546 | clients_set_gid(g_self_client, getgid()); |
---|
[39] | 547 | clients_get(g_self_client, &self); |
---|
[37] | 548 | |
---|
[39] | 549 | if ( self == NULL ) { |
---|
| 550 | ROAR_ERR("Can not get self client!"); |
---|
| 551 | return 1; |
---|
| 552 | } |
---|
| 553 | |
---|
[775] | 554 | strcpy(self->name, "RoarAudio daemon internal"); |
---|
[68] | 555 | |
---|
[775] | 556 | if ( daemon ) { |
---|
[68] | 557 | close(ROAR_STDIN ); |
---|
| 558 | close(ROAR_STDOUT); |
---|
| 559 | close(ROAR_STDERR); |
---|
[422] | 560 | setsid(); |
---|
[68] | 561 | if ( fork() ) |
---|
| 562 | _exit(0); |
---|
| 563 | } |
---|
| 564 | |
---|
[444] | 565 | if (chrootdir) { |
---|
| 566 | if ( chroot(chrootdir) == -1 ) { |
---|
| 567 | ROAR_ERR("Can not chroot to %s: %s", chrootdir, strerror(errno)); |
---|
| 568 | return 2; |
---|
| 569 | } |
---|
| 570 | if ( chdir("/") == -1 ) { |
---|
| 571 | ROAR_ERR("Can not chdir to /: %s", strerror(errno)); |
---|
| 572 | return 2; |
---|
| 573 | } |
---|
| 574 | } |
---|
| 575 | |
---|
| 576 | if ( setids & R_SETUID ) { |
---|
| 577 | if ( !pwd || setuid(pwd->pw_uid) == -1 ) { |
---|
| 578 | ROAR_ERR("Can not set UserID: %s", strerror(errno)); |
---|
| 579 | return 3; |
---|
| 580 | } |
---|
| 581 | clients_set_uid(g_self_client, getuid()); |
---|
| 582 | } |
---|
| 583 | |
---|
[0] | 584 | // start main loop... |
---|
[905] | 585 | main_loop(drvid, drvinst, &sa, sysclocksync); |
---|
[0] | 586 | |
---|
| 587 | // clean up. |
---|
| 588 | clean_quit_prep(); |
---|
| 589 | driver_close(drvinst, drvid); |
---|
| 590 | output_buffer_free(); |
---|
| 591 | |
---|
| 592 | return 0; |
---|
| 593 | } |
---|
| 594 | |
---|
[574] | 595 | void cleanup_listen_socket (int terminate) { |
---|
[580] | 596 | |
---|
| 597 | if ( g_listen_socket != -1 ) { |
---|
| 598 | close(g_listen_socket); |
---|
[60] | 599 | |
---|
[580] | 600 | g_listen_socket = -1; |
---|
[576] | 601 | |
---|
[580] | 602 | if ( *server == '/' ) |
---|
| 603 | unlink(server); |
---|
| 604 | } |
---|
[60] | 605 | |
---|
[574] | 606 | if ( terminate ) |
---|
| 607 | g_terminate = 1; |
---|
| 608 | } |
---|
| 609 | |
---|
| 610 | void clean_quit_prep (void) { |
---|
| 611 | cleanup_listen_socket(0); |
---|
[60] | 612 | |
---|
[0] | 613 | sources_free(); |
---|
| 614 | streams_free(); |
---|
| 615 | clients_free(); |
---|
[282] | 616 | midi_cb_stop(); // stop console beep |
---|
[281] | 617 | midi_free(); |
---|
[0] | 618 | } |
---|
| 619 | |
---|
| 620 | void clean_quit (void) { |
---|
| 621 | clean_quit_prep(); |
---|
| 622 | // driver_close(drvinst, drvid); |
---|
| 623 | // output_buffer_free(); |
---|
| 624 | exit(0); |
---|
| 625 | } |
---|
| 626 | |
---|
| 627 | //ll |
---|