[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 | |
---|
| 179 | s->info.codec = codec; |
---|
| 180 | ROAR_STREAM_SERVER(s)->codec_orgi = codec; |
---|
[933] | 181 | |
---|
| 182 | if ( driver_openvio(&(ss->vio), &(ss->driver_id), drv, dev, &(s->info), -1) ) { |
---|
| 183 | streams_delete(stream); |
---|
[938] | 184 | ROAR_DBG("add_output(drv='%s', dev='%s', opts='%s') = -1", drv, dev, opts); |
---|
[933] | 185 | return -1; |
---|
| 186 | } |
---|
| 187 | |
---|
[938] | 188 | streams_set_fh(stream, -1); // update some internal structures |
---|
| 189 | |
---|
[933] | 190 | client_stream_add(g_source_client, stream); |
---|
| 191 | |
---|
| 192 | return 0; |
---|
[932] | 193 | } |
---|
| 194 | |
---|
[0] | 195 | int main (int argc, char * argv[]) { |
---|
| 196 | int i; |
---|
| 197 | char * k; |
---|
[905] | 198 | char user_sock[80] = {0}; |
---|
[0] | 199 | struct roar_audio_info sa; |
---|
[905] | 200 | int daemon = 0; |
---|
| 201 | int realtime = 0; |
---|
| 202 | int sysclocksync = 0; |
---|
[74] | 203 | char * driver = getenv("ROAR_DRIVER"); |
---|
| 204 | char * device = getenv("ROAR_DEVICE"); |
---|
[0] | 205 | char * opts = NULL; |
---|
[60] | 206 | // char * server = ROAR_DEFAULT_SOCK_GLOBAL; |
---|
[0] | 207 | int port = ROAR_DEFAULT_PORT; |
---|
| 208 | int drvid; |
---|
[550] | 209 | char * s_drv = "cf"; |
---|
[444] | 210 | char * s_con = NULL; |
---|
| 211 | char * s_opt = NULL; |
---|
| 212 | int s_prim = 0; |
---|
[932] | 213 | char * o_drv = NULL; |
---|
| 214 | char * o_dev = NULL; |
---|
| 215 | char * o_opts = NULL; |
---|
[450] | 216 | char * sock_grp = ROAR_DEFAULT_SOCKGRP; |
---|
[444] | 217 | char * sock_user = NULL; |
---|
[517] | 218 | int sock_type = ROAR_SOCKET_TYPE_UNKNOWN; |
---|
[444] | 219 | char * chrootdir = NULL; |
---|
| 220 | int setids = 0; |
---|
[446] | 221 | struct group * grp = NULL; |
---|
| 222 | struct passwd * pwd = NULL; |
---|
| 223 | struct servent * serv = NULL; |
---|
[0] | 224 | DRIVER_USERDATA_T drvinst; |
---|
[39] | 225 | struct roar_client * self = NULL; |
---|
[508] | 226 | #ifdef ROAR_HAVE_LIBDNET |
---|
| 227 | char decnethost[80]; |
---|
| 228 | #endif |
---|
[0] | 229 | |
---|
| 230 | g_listen_socket = -1; |
---|
| 231 | g_standby = 0; |
---|
[920] | 232 | g_autostandby = 0; |
---|
[0] | 233 | |
---|
| 234 | sa.bits = ROAR_BITS_DEFAULT; |
---|
| 235 | sa.channels = ROAR_CHANNELS_DEFAULT; |
---|
| 236 | sa.rate = ROAR_RATE_DEFAULT; |
---|
| 237 | sa.codec = ROAR_CODEC_DEFAULT; |
---|
| 238 | |
---|
| 239 | g_sa = &sa; |
---|
| 240 | |
---|
[60] | 241 | |
---|
| 242 | if ( getuid() != 0 && getenv("HOME") ) { |
---|
| 243 | snprintf(user_sock, 79, "%s/%s", getenv("HOME"), ROAR_DEFAULT_SOCK_USER); |
---|
| 244 | server = user_sock; |
---|
| 245 | } |
---|
| 246 | |
---|
[279] | 247 | if ( getenv("ROAR_SERVER") != NULL ) |
---|
| 248 | server = getenv("ROAR_SERVER"); |
---|
| 249 | |
---|
[63] | 250 | if ( clients_init() == -1 ) { |
---|
| 251 | ROAR_ERR("Can not init clients!"); |
---|
| 252 | return 1; |
---|
| 253 | } |
---|
| 254 | |
---|
| 255 | if ( streams_init() == -1 ) { |
---|
| 256 | ROAR_ERR("Can not init streams!"); |
---|
| 257 | return 1; |
---|
| 258 | } |
---|
| 259 | |
---|
[64] | 260 | if ( (g_self_client = clients_new()) == -1 ) { |
---|
| 261 | ROAR_ERR("Can not create self client!"); |
---|
| 262 | return 1; |
---|
| 263 | } |
---|
| 264 | |
---|
[0] | 265 | if ( sources_init() == -1 ) { |
---|
| 266 | ROAR_ERR("Can not init sources!"); |
---|
| 267 | return 1; |
---|
| 268 | } |
---|
| 269 | |
---|
[64] | 270 | if ( (sources_set_client(g_self_client)) == -1 ) { |
---|
| 271 | ROAR_ERR("Can not init set source client!"); |
---|
| 272 | return 1; |
---|
| 273 | } |
---|
| 274 | |
---|
[0] | 275 | for (i = 1; i < argc; i++) { |
---|
| 276 | k = argv[i]; |
---|
| 277 | |
---|
| 278 | if ( strcmp(k, "-h") == 0 || strcmp(k, "--help") == 0 ) { |
---|
| 279 | usage(); |
---|
| 280 | return 0; |
---|
| 281 | |
---|
[579] | 282 | } else if ( strcmp(k, "--restart") == 0 ) { |
---|
| 283 | if ( restart_server(server) == -1 ) { |
---|
| 284 | ROAR_WARN("Can not terminate old server (not running at %s?), tring to continue anyway", server); |
---|
| 285 | } |
---|
| 286 | |
---|
[775] | 287 | } else if ( strcmp(k, "--demon") == 0 || strcmp(k, "--daemon") == 0 ) { |
---|
| 288 | daemon = 1; |
---|
[71] | 289 | } else if ( strcmp(k, "--terminate") == 0 ) { |
---|
| 290 | g_terminate = 1; |
---|
[905] | 291 | } else if ( strcmp(k, "--sysclocksync") == 0 ) { |
---|
| 292 | sysclocksync = 1000; |
---|
[275] | 293 | } else if ( strcmp(k, "--realtime") == 0 ) { |
---|
[276] | 294 | realtime++; |
---|
[444] | 295 | } else if ( strcmp(k, "--chroot") == 0 ) { |
---|
| 296 | chrootdir = argv[++i]; |
---|
| 297 | } else if ( strcmp(k, "--setgid") == 0 ) { |
---|
| 298 | setids |= R_SETGID; |
---|
| 299 | } else if ( strcmp(k, "--setuid") == 0 ) { |
---|
| 300 | setids |= R_SETUID; |
---|
[68] | 301 | |
---|
[280] | 302 | } else if ( strcmp(k, "--list-cf") == 0 ) { |
---|
| 303 | print_codecfilterlist(); |
---|
| 304 | return 0; |
---|
| 305 | |
---|
[0] | 306 | } else if ( strcmp(k, "-R") == 0 || strcmp(k, "--rate") == 0 ) { |
---|
| 307 | sa.rate = atoi(argv[++i]); |
---|
| 308 | } else if ( strcmp(k, "-B") == 0 || strcmp(k, "--bits") == 0 ) { |
---|
| 309 | sa.bits = atoi(argv[++i]); |
---|
| 310 | } else if ( strcmp(k, "-C") == 0 || strcmp(k, "--chans") == 0 ) { |
---|
| 311 | sa.channels = atoi(argv[++i]); |
---|
| 312 | |
---|
| 313 | } else if ( strcmp(k, "-d") == 0 || strcmp(k, "--driver") == 0 ) { |
---|
| 314 | driver = argv[++i]; |
---|
| 315 | if ( strcmp(driver, "list") == 0 ) { |
---|
| 316 | print_driverlist(); |
---|
| 317 | } |
---|
| 318 | } else if ( strcmp(k, "-D") == 0 || strcmp(k, "--device") == 0 ) { |
---|
| 319 | device = argv[++i]; |
---|
| 320 | } else if ( strcmp(k, "-dO") == 0 ) { |
---|
| 321 | opts = argv[++i]; |
---|
| 322 | |
---|
[932] | 323 | } else if ( strcmp(k, "-o") == 0 || strcmp(k, "--odriver") == 0 ) { |
---|
| 324 | o_drv = argv[++i]; |
---|
| 325 | } else if ( strcmp(k, "-O") == 0 || strcmp(k, "--odevice") == 0 ) { |
---|
| 326 | o_dev = argv[++i]; |
---|
| 327 | } else if ( strcmp(k, "-oO") == 0 ) { |
---|
| 328 | o_opts = argv[++i]; |
---|
| 329 | } else if ( strcmp(k, "-oN") == 0 ) { |
---|
| 330 | add_output(o_drv, o_dev, o_opts); |
---|
[933] | 331 | o_drv = o_dev = o_opts = NULL; |
---|
[932] | 332 | |
---|
[0] | 333 | } else if ( strcmp(k, "-s") == 0 || strcmp(k, "--source") == 0 ) { |
---|
[550] | 334 | s_drv = argv[++i]; |
---|
| 335 | } else if ( strcmp(k, "-S") == 0 ) { |
---|
[0] | 336 | k = argv[++i]; |
---|
[550] | 337 | if ( sources_add(s_drv, k, s_con, s_opt, s_prim) == -1 ) { |
---|
| 338 | ROAR_ERR("main(*): adding source '%s' via '%s' failed!", k, s_drv); |
---|
[0] | 339 | } |
---|
[550] | 340 | s_opt = s_con = NULL; |
---|
[0] | 341 | s_prim = 0; |
---|
| 342 | } else if ( strcmp(k, "-sO") == 0 ) { |
---|
| 343 | s_opt = argv[++i]; |
---|
| 344 | } else if ( strcmp(k, "-sC") == 0 ) { |
---|
| 345 | s_con = argv[++i]; |
---|
| 346 | } else if ( strcmp(k, "-sP") == 0 ) { |
---|
| 347 | s_prim = 1; |
---|
| 348 | |
---|
| 349 | } else if ( strcmp(k, "-p") == 0 || strcmp(k, "--port") == 0 ) { |
---|
[447] | 350 | // This is only usefull in INET not UNIX mode. |
---|
| 351 | if ( *server == '/' ) |
---|
| 352 | server = ROAR_DEFAULT_HOST; |
---|
| 353 | |
---|
[446] | 354 | errno = 0; |
---|
| 355 | if ( (port = atoi(argv[++i])) < 1 ) { |
---|
| 356 | if ( (serv = getservbyname(argv[i], "tcp")) == NULL ) { |
---|
| 357 | ROAR_ERR("Unknown service: %s: %s", argv[i], strerror(errno)); |
---|
| 358 | return 1; |
---|
| 359 | } |
---|
| 360 | // NOTE: we need to use ROAR_NET2HOST16() here even if s_port is of type int! |
---|
| 361 | ROAR_DBG("main(*): serv = {s_name='%s', s_aliases={...}, s_port=%i, s_proto='%s'}", |
---|
| 362 | serv->s_name, ROAR_NET2HOST16(serv->s_port), serv->s_proto); |
---|
| 363 | port = ROAR_NET2HOST16(serv->s_port); |
---|
| 364 | } |
---|
[68] | 365 | } else if ( strcmp(k, "-b") == 0 || strcmp(k, "--bind") == 0 || strcmp(k, "-s") == 0 || strcmp(k, "--sock") == 0 ) { |
---|
[0] | 366 | server = argv[++i]; |
---|
[518] | 367 | |
---|
[573] | 368 | } else if ( strcmp(k, "-t") == 0 || strcmp(k, "--tcp") == 0 ) { |
---|
[518] | 369 | if ( sock_type != ROAR_SOCKET_TYPE_TCP && sock_type != ROAR_SOCKET_TYPE_TCP6 ) |
---|
| 370 | sock_type = ROAR_SOCKET_TYPE_TCP; |
---|
| 371 | |
---|
| 372 | if ( *server == '/' ) |
---|
| 373 | server = ROAR_DEFAULT_HOST; |
---|
| 374 | |
---|
| 375 | } else if ( strcmp(k, "-4") == 0 ) { |
---|
[517] | 376 | sock_type = ROAR_SOCKET_TYPE_TCP; |
---|
[447] | 377 | if ( *server == '/' ) |
---|
| 378 | server = ROAR_DEFAULT_HOST; |
---|
[518] | 379 | } else if ( strcmp(k, "-6") == 0 ) { |
---|
[519] | 380 | #ifdef PF_INET6 |
---|
[518] | 381 | sock_type = ROAR_SOCKET_TYPE_TCP6; |
---|
| 382 | if ( *server == '/' ) |
---|
| 383 | server = ROAR_DEFAULT_HOST; |
---|
[519] | 384 | #else |
---|
| 385 | ROAR_ERR("No IPv6 support compiled in!"); |
---|
| 386 | return 1; |
---|
| 387 | #endif |
---|
[518] | 388 | |
---|
[573] | 389 | } else if ( strcmp(k, "-u") == 0 || strcmp(k, "--unix") == 0 ) { |
---|
[62] | 390 | // ignore this case as it is the default behavor. |
---|
[517] | 391 | sock_type = ROAR_SOCKET_TYPE_UNIX; |
---|
[518] | 392 | |
---|
[573] | 393 | } else if ( strcmp(k, "-n") == 0 || strcmp(k, "--decnet") == 0 ) { |
---|
[508] | 394 | #ifdef ROAR_HAVE_LIBDNET |
---|
| 395 | port = ROAR_DEFAULT_NUM; |
---|
| 396 | strcpy(decnethost, ROAR_DEFAULT_LISTEN_OBJECT); |
---|
| 397 | server = decnethost; |
---|
[517] | 398 | sock_type = ROAR_SOCKET_TYPE_DECNET; |
---|
[508] | 399 | #else |
---|
| 400 | ROAR_ERR("No DECnet support compiled in!"); |
---|
| 401 | return 1; |
---|
| 402 | #endif |
---|
[518] | 403 | |
---|
[60] | 404 | } else if ( strcmp(k, "-G") == 0 ) { |
---|
[444] | 405 | sock_grp = argv[++i]; |
---|
| 406 | } else if ( strcmp(k, "-U") == 0 ) { |
---|
| 407 | sock_user = argv[++i]; |
---|
[0] | 408 | |
---|
[68] | 409 | } else if ( strcmp(k, "--no-listen") == 0 ) { |
---|
[548] | 410 | *server = 0; |
---|
| 411 | g_terminate = 1; |
---|
[68] | 412 | } else if ( strcmp(k, "--client-fh") == 0 ) { |
---|
| 413 | if ( clients_set_fh(clients_new(), atoi(argv[++i])) == -1 ) { |
---|
| 414 | ROAR_ERR("main(*): Can not set client's fh"); |
---|
| 415 | return 1; |
---|
| 416 | } |
---|
[501] | 417 | } else if ( strcmp(k, "--close-fh") == 0 ) { |
---|
| 418 | close(atoi(argv[++i])); |
---|
[68] | 419 | |
---|
[920] | 420 | } else if ( strcmp(k, "--standby") == 0 ) { |
---|
| 421 | g_standby = 1; |
---|
| 422 | } else if ( strcmp(k, "--auto-standby") == 0 ) { |
---|
| 423 | g_autostandby = 1; |
---|
[0] | 424 | } else { |
---|
| 425 | usage(); |
---|
| 426 | return 1; |
---|
| 427 | } |
---|
| 428 | |
---|
| 429 | } |
---|
| 430 | |
---|
[932] | 431 | if ( o_drv != NULL ) |
---|
| 432 | add_output(o_drv, o_dev, o_opts); |
---|
| 433 | |
---|
[0] | 434 | ROAR_DBG("Server config: rate=%i, bits=%i, chans=%i", sa.rate, sa.bits, sa.channels); |
---|
| 435 | |
---|
[281] | 436 | if ( midi_init() == -1 ) |
---|
| 437 | ROAR_ERR("Can not initialize MIDI subsystem"); |
---|
| 438 | |
---|
[68] | 439 | if ( *server != 0 ) { |
---|
[517] | 440 | if ( (g_listen_socket = roar_socket_listen(sock_type, server, port)) == -1 ) { |
---|
[286] | 441 | if ( *server == '/' ) { |
---|
| 442 | if ( (i = roar_socket_connect(server, port)) != -1 ) { |
---|
| 443 | close(i); |
---|
| 444 | ROAR_ERR("Can not open listen socket!"); |
---|
| 445 | return 1; |
---|
| 446 | } else { |
---|
| 447 | unlink(server); |
---|
[517] | 448 | if ( (g_listen_socket = roar_socket_listen(sock_type, server, port)) == -1 ) { |
---|
[286] | 449 | ROAR_ERR("Can not open listen socket!"); |
---|
| 450 | return 1; |
---|
| 451 | } |
---|
| 452 | } |
---|
| 453 | } else { |
---|
| 454 | ROAR_ERR("Can not open listen socket!"); |
---|
| 455 | return 1; |
---|
| 456 | } |
---|
[68] | 457 | } |
---|
[0] | 458 | |
---|
[523] | 459 | if ( (grp = getgrnam(sock_grp)) == NULL ) { |
---|
| 460 | ROAR_ERR("Can not get GID for group %s: %s", sock_grp, strerror(errno)); |
---|
| 461 | } |
---|
| 462 | if ( sock_user || (setids & R_SETUID) ) { |
---|
| 463 | if ( (pwd = getpwnam(sock_user)) == NULL ) { |
---|
| 464 | ROAR_ERR("Can not get UID for user %s: %s", sock_user, strerror(errno)); |
---|
| 465 | } |
---|
| 466 | } |
---|
| 467 | |
---|
[68] | 468 | if ( *server == '/' ) { |
---|
[523] | 469 | if ( grp ) { |
---|
[444] | 470 | if ( pwd ) { |
---|
| 471 | chown(server, pwd->pw_uid, grp->gr_gid); |
---|
| 472 | } else { |
---|
| 473 | chown(server, -1, grp->gr_gid); |
---|
| 474 | } |
---|
[68] | 475 | if ( getuid() == 0 ) |
---|
| 476 | chmod(server, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP); |
---|
| 477 | } |
---|
[60] | 478 | } |
---|
| 479 | } |
---|
| 480 | |
---|
[0] | 481 | if ( output_buffer_init(&sa) == -1 ) { |
---|
| 482 | ROAR_ERR("Can not init output buffer!"); |
---|
| 483 | return 1; |
---|
| 484 | } |
---|
| 485 | |
---|
| 486 | if ( driver_open(&drvinst, &drvid, driver, device, &sa) == -1 ) { |
---|
| 487 | ROAR_ERR("Can not open output driver!"); |
---|
| 488 | return 1; |
---|
| 489 | } |
---|
| 490 | |
---|
[44] | 491 | if ( samples_init() == -1 ) { |
---|
| 492 | ROAR_ERR("Can not init samples!"); |
---|
| 493 | return 1; |
---|
| 494 | } |
---|
| 495 | |
---|
| 496 | |
---|
[0] | 497 | signal(SIGINT, on_sig_int); |
---|
[285] | 498 | signal(SIGCHLD, on_sig_chld); |
---|
[0] | 499 | signal(SIGPIPE, SIG_IGN); // ignore broken pipes |
---|
| 500 | |
---|
[275] | 501 | if ( realtime ) { |
---|
[278] | 502 | #ifdef DEBUG |
---|
| 503 | ROAR_WARN("compiled with -DDEBUG but realtime is enabled: for real realtime support compiel without -DDEBUG"); |
---|
| 504 | #endif |
---|
| 505 | |
---|
[275] | 506 | errno = 0; |
---|
[276] | 507 | nice(-5*realtime); // -5 for each --realtime |
---|
[275] | 508 | if ( errno ) |
---|
| 509 | ROAR_WARN("Can not decrease nice value by 5: %s", strerror(errno)); |
---|
[277] | 510 | /* |
---|
[276] | 511 | #ifdef __linux__ |
---|
[277] | 512 | if ( ioprio_set(IOPRIO_WHO_PROCESS, getpid(), IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 0)) == -1 ) |
---|
| 513 | ROAR_WARN("Can not set io priority: %s", strerror(errno)); |
---|
[276] | 514 | #endif |
---|
[277] | 515 | */ |
---|
[275] | 516 | } |
---|
| 517 | |
---|
[444] | 518 | if ( setids & R_SETGID ) { |
---|
| 519 | if ( setgroups(0, (const gid_t *) NULL) == -1 ) { |
---|
| 520 | ROAR_ERR("Can not clear supplementary group IDs: %s", strerror(errno)); |
---|
| 521 | } |
---|
[523] | 522 | if ( !grp || setgid(grp->gr_gid) == -1 ) { |
---|
[444] | 523 | ROAR_ERR("Can not set GroupID: %s", strerror(errno)); |
---|
| 524 | } |
---|
| 525 | } |
---|
| 526 | |
---|
[0] | 527 | |
---|
[39] | 528 | clients_set_pid(g_self_client, getpid()); |
---|
[440] | 529 | clients_set_uid(g_self_client, getuid()); |
---|
| 530 | clients_set_gid(g_self_client, getgid()); |
---|
[39] | 531 | clients_get(g_self_client, &self); |
---|
[37] | 532 | |
---|
[39] | 533 | if ( self == NULL ) { |
---|
| 534 | ROAR_ERR("Can not get self client!"); |
---|
| 535 | return 1; |
---|
| 536 | } |
---|
| 537 | |
---|
[775] | 538 | strcpy(self->name, "RoarAudio daemon internal"); |
---|
[68] | 539 | |
---|
[775] | 540 | if ( daemon ) { |
---|
[68] | 541 | close(ROAR_STDIN ); |
---|
| 542 | close(ROAR_STDOUT); |
---|
| 543 | close(ROAR_STDERR); |
---|
[422] | 544 | setsid(); |
---|
[68] | 545 | if ( fork() ) |
---|
| 546 | _exit(0); |
---|
| 547 | } |
---|
| 548 | |
---|
[444] | 549 | if (chrootdir) { |
---|
| 550 | if ( chroot(chrootdir) == -1 ) { |
---|
| 551 | ROAR_ERR("Can not chroot to %s: %s", chrootdir, strerror(errno)); |
---|
| 552 | return 2; |
---|
| 553 | } |
---|
| 554 | if ( chdir("/") == -1 ) { |
---|
| 555 | ROAR_ERR("Can not chdir to /: %s", strerror(errno)); |
---|
| 556 | return 2; |
---|
| 557 | } |
---|
| 558 | } |
---|
| 559 | |
---|
| 560 | if ( setids & R_SETUID ) { |
---|
| 561 | if ( !pwd || setuid(pwd->pw_uid) == -1 ) { |
---|
| 562 | ROAR_ERR("Can not set UserID: %s", strerror(errno)); |
---|
| 563 | return 3; |
---|
| 564 | } |
---|
| 565 | clients_set_uid(g_self_client, getuid()); |
---|
| 566 | } |
---|
| 567 | |
---|
[0] | 568 | // start main loop... |
---|
[905] | 569 | main_loop(drvid, drvinst, &sa, sysclocksync); |
---|
[0] | 570 | |
---|
| 571 | // clean up. |
---|
| 572 | clean_quit_prep(); |
---|
| 573 | driver_close(drvinst, drvid); |
---|
| 574 | output_buffer_free(); |
---|
| 575 | |
---|
| 576 | return 0; |
---|
| 577 | } |
---|
| 578 | |
---|
[574] | 579 | void cleanup_listen_socket (int terminate) { |
---|
[580] | 580 | |
---|
| 581 | if ( g_listen_socket != -1 ) { |
---|
| 582 | close(g_listen_socket); |
---|
[60] | 583 | |
---|
[580] | 584 | g_listen_socket = -1; |
---|
[576] | 585 | |
---|
[580] | 586 | if ( *server == '/' ) |
---|
| 587 | unlink(server); |
---|
| 588 | } |
---|
[60] | 589 | |
---|
[574] | 590 | if ( terminate ) |
---|
| 591 | g_terminate = 1; |
---|
| 592 | } |
---|
| 593 | |
---|
| 594 | void clean_quit_prep (void) { |
---|
| 595 | cleanup_listen_socket(0); |
---|
[60] | 596 | |
---|
[0] | 597 | sources_free(); |
---|
| 598 | streams_free(); |
---|
| 599 | clients_free(); |
---|
[282] | 600 | midi_cb_stop(); // stop console beep |
---|
[281] | 601 | midi_free(); |
---|
[0] | 602 | } |
---|
| 603 | |
---|
| 604 | void clean_quit (void) { |
---|
| 605 | clean_quit_prep(); |
---|
| 606 | // driver_close(drvinst, drvid); |
---|
| 607 | // output_buffer_free(); |
---|
| 608 | exit(0); |
---|
| 609 | } |
---|
| 610 | |
---|
| 611 | //ll |
---|