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