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