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