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