main.c 10 KB
Newer Older
1
/* -*- c-basic-offset: 4; indent-tabs-mode: nil; -*- */
2 3 4 5
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

Jack Moffitt's avatar
Jack Moffitt committed
6 7 8
#include <stdio.h>
#include <string.h>

Karl Heyes's avatar
Karl Heyes committed
9 10 11 12 13
#include "thread/thread.h"
#include "avl/avl.h"
#include "net/sock.h"
#include "net/resolver.h"
#include "httpp/httpp.h"
Jack Moffitt's avatar
Jack Moffitt committed
14

15 16 17 18 19 20
#ifdef CHUID
#include <sys/types.h>
#include <grp.h>
#include <pwd.h>
#include <errno.h>
#endif
Jack Moffitt's avatar
Jack Moffitt committed
21

brendan's avatar
brendan committed
22
#ifdef CHROOT
brendan's avatar
brendan committed
23 24 25
#include <unistd.h>
#endif

26
#include "cfgfile.h"
Jack Moffitt's avatar
Jack Moffitt committed
27 28 29 30 31 32 33
#include "sighandler.h"

#include "global.h"
#include "os.h"
#include "connection.h"
#include "refbuf.h"
#include "client.h"
34
#include "slave.h"
Jack Moffitt's avatar
Jack Moffitt committed
35 36
#include "stats.h"
#include "logging.h"
37
#include "xslt.h"
38
#include "fserve.h"
brendan's avatar
brendan committed
39
#ifdef USE_YP
40
#include "geturl.h"
41
#endif
Jack Moffitt's avatar
Jack Moffitt committed
42

43 44
#include <libxml/xmlmemory.h>

45 46
#ifdef _WIN32
#define snprintf _snprintf
47
#endif
48

Jack Moffitt's avatar
Jack Moffitt committed
49 50 51
#undef CATMODULE
#define CATMODULE "main"

52 53 54 55 56 57 58 59 60
static void _fatal_error(char *perr)
{
#ifdef WIN32
    MessageBox(NULL, perr, "Error", MB_OK);
#else
    fprintf(stdout, "%s\n", perr);
#endif
}

61
static void _print_usage()
Jack Moffitt's avatar
Jack Moffitt committed
62
{
63 64 65
    printf("Usage:\n");
    printf("\ticecast -c <file>\t\tSpecify configuration file\n");
    printf("\n");
Jack Moffitt's avatar
Jack Moffitt committed
66 67
}

68 69
static void _stop_logging(void)
{
70 71
    log_close(errorlog);
    log_close(accesslog);
72 73
}

74
static void _initialize_subsystems(void)
Jack Moffitt's avatar
Jack Moffitt committed
75
{
76 77 78 79 80 81 82 83
    log_initialize();
    thread_initialize();
    sock_initialize();
    resolver_initialize();
    config_initialize();
    connection_initialize();
    global_initialize();
    refbuf_initialize();
84
    xslt_initialize();
brendan's avatar
brendan committed
85
#ifdef USE_YP
86
    curl_initialize();
87
#endif
Jack Moffitt's avatar
Jack Moffitt committed
88 89
}

90
static void _shutdown_subsystems(void)
Jack Moffitt's avatar
Jack Moffitt committed
91
{
brendan's avatar
brendan committed
92
#ifdef USE_YP
93
    curl_shutdown();
94
#endif
95
    fserve_shutdown();
96
    xslt_shutdown();
97 98 99
    refbuf_shutdown();
    stats_shutdown();
    slave_shutdown();
100 101

    /* Now that these are done, we can stop the loggers. */
102
    _stop_logging();
103

104 105 106 107 108 109 110
    global_shutdown();
    connection_shutdown();
    config_shutdown();
    resolver_shutdown();
    sock_shutdown();
    thread_shutdown();
    log_shutdown();
111 112

    xmlCleanupParser();
Jack Moffitt's avatar
Jack Moffitt committed
113 114
}

115
static int _parse_config_file(int argc, char **argv, char *filename, int size)
Jack Moffitt's avatar
Jack Moffitt committed
116
{
117 118
    int i = 1;
    int    processID = 0;
Jack Moffitt's avatar
Jack Moffitt committed
119

120
    if (argc < 3) return -1;
Jack Moffitt's avatar
Jack Moffitt committed
121

122 123
    while (i < argc) {
        if (strcmp(argv[i], "-b") == 0) {
124
#ifndef WIN32
125 126 127 128 129
                fprintf(stdout, "Starting icecast2\nDetaching from the console\n");
                if ((processID = (int)fork()) > 0) {
                        /* exit the parent */
                        _exit(0);
                }
130
#endif
131 132 133 134
        }
        if (strcmp(argv[i], "-c") == 0) {
            if (i + 1 < argc) {
                strncpy(filename, argv[i + 1], size-1);
135
                filename[size-1] = 0;
136 137 138 139 140 141 142 143 144
                return 1;
            } else {
                return -1;
            }
        }
        i++;
    }

    return -1;
Jack Moffitt's avatar
Jack Moffitt committed
145 146
}

147
static int _start_logging(void)
Jack Moffitt's avatar
Jack Moffitt committed
148
{
149 150
    char fn_error[FILENAME_MAX];
    char fn_access[FILENAME_MAX];
151 152
    char pbuf[1024];

153
    ice_config_t *config = config_get_config_unlocked();
Jack Moffitt's avatar
Jack Moffitt committed
154

155
    if(strcmp(config->error_log, "-")) {
156
        snprintf(fn_error, FILENAME_MAX, "%s%s%s", config->log_dir, PATH_SEPARATOR, config->error_log);
157
        errorlog = log_open(fn_error);
158 159 160
    } else {
        errorlog = log_open_file(stderr);
    }
161
    if(strcmp(config->access_log, "-")) {
162
        snprintf(fn_access, FILENAME_MAX, "%s%s%s", config->log_dir, PATH_SEPARATOR, config->access_log);
163
        accesslog = log_open(fn_access);
164 165 166
    } else {
        accesslog = log_open_file(stderr);
    }
167 168 169 170
    
    log_set_level(errorlog, config->loglevel);
    log_set_level(accesslog, 4);

171 172 173 174 175 176 177 178
    if (errorlog < 0) {
        _fatal_error("FATAL: could not open error logging");
    }
    if (accesslog < 0) {
        memset(pbuf, '\000', sizeof(pbuf));
        snprintf(pbuf, sizeof(pbuf)-1, "FATAL: could not open access logging");
        _fatal_error(pbuf);
    }
179 180 181
    if (errorlog >= 0 && accesslog >= 0) return 1;
    
    return 0;
Jack Moffitt's avatar
Jack Moffitt committed
182 183
}

184
static int _setup_sockets(void)
Jack Moffitt's avatar
Jack Moffitt committed
185
{
186
    ice_config_t *config;
187 188 189
    int i = 0;
    int ret = 0;
    int successful = 0;
190
    char pbuf[1024];
Jack Moffitt's avatar
Jack Moffitt committed
191

192
    config = config_get_config_unlocked();
Jack Moffitt's avatar
Jack Moffitt committed
193

194 195 196 197 198 199 200
    for(i = 0; i < MAX_LISTEN_SOCKETS; i++) {
        if(config->listeners[i].port <= 0)
            break;

        global.serversock[i] = sock_get_server_socket(
                config->listeners[i].port, config->listeners[i].bind_address);

201
        if (global.serversock[i] == SOCK_ERROR) {
202 203 204 205 206
            memset(pbuf, '\000', sizeof(pbuf));
            snprintf(pbuf, sizeof(pbuf)-1, 
                "Could not create listener socket on port %d", 
                config->listeners[i].port);
            _fatal_error(pbuf);
207 208 209 210 211 212 213
            return 0;
        }
        else {
            ret = 1;
            successful++;
        }
    }
Michael Smith's avatar
Michael Smith committed
214

215
    global.server_sockets = successful;
216 217
    
    return ret;
Jack Moffitt's avatar
Jack Moffitt committed
218 219
}

220
static int _start_listening(void)
Jack Moffitt's avatar
Jack Moffitt committed
221
{
222 223
    int i;
    for(i=0; i < global.server_sockets; i++) {
224 225
        if (sock_listen(global.serversock[i], ICE_LISTEN_QUEUE) == SOCK_ERROR)
            return 0;
Jack Moffitt's avatar
Jack Moffitt committed
226

227
        sock_set_blocking(global.serversock[i], SOCK_NONBLOCK);
228
    }
Jack Moffitt's avatar
Jack Moffitt committed
229

230
    return 1;
Jack Moffitt's avatar
Jack Moffitt committed
231 232
}

233
/* bind the socket and start listening */
234
static int _server_proc_init(void)
Jack Moffitt's avatar
Jack Moffitt committed
235
{
236 237
    if (!_setup_sockets())
        return 0;
Jack Moffitt's avatar
Jack Moffitt committed
238

239
    if (!_start_listening()) {
240
        _fatal_error("Failed trying to listen on server socket");
241 242
        return 0;
    }
243 244

    return 1;
245
}
Jack Moffitt's avatar
Jack Moffitt committed
246

247 248 249
/* this is the heart of the beast */
static void _server_proc(void)
{
250 251
    int i;

252
    connection_accept_loop();
Jack Moffitt's avatar
Jack Moffitt committed
253

254
    for(i=0; i < MAX_LISTEN_SOCKETS; i++)
255
        sock_close(global.serversock[i]);
Jack Moffitt's avatar
Jack Moffitt committed
256 257
}

258
/* chroot the process. Watch out - we need to do this before starting other
259
 * threads. Change uid as well, after figuring out uid _first_ */
260

Michael Smith's avatar
Michael Smith committed
261
static void _ch_root_uid_setup(void)
262
{
Michael Smith's avatar
Michael Smith committed
263
   ice_config_t *conf = config_get_config_unlocked();
264 265 266 267 268 269 270 271
#ifdef CHUID
   struct passwd *user;
   struct group *group;
   uid_t uid=-1;
   gid_t gid=-1;

   if(conf->chuid)
   {
272 273 274 275 276 277 278 279 280
       if(conf->user) {
           user = getpwnam(conf->user);
           if(user)
               uid = user->pw_uid;
           else
               fprintf(stderr, "Couldn't find user \"%s\" in password file\n", conf->user);
       }
       if(conf->group) {
           group = getgrnam(conf->group);
281

282 283 284 285 286
           if(group)
               gid = group->gr_gid;
           else
               fprintf(stderr, "Couldn't find group \"%s\" in groups file\n", conf->group);
       }
287 288 289 290
   }
#endif

#ifdef CHROOT
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
   if (conf->chroot)
   {
       if(getuid()) /* root check */
       {
           fprintf(stderr, "WARNING: Cannot change server root unless running as root.\n");
           return;
       }
       if(chroot(conf->base_dir))
       {
           fprintf(stderr,"WARNING: Couldn't change server root: %s\n", strerror(errno));
           return;
       }
       else
           fprintf(stdout, "Changed root successfully to \"%s\".\n", conf->base_dir);

   }   
#endif
#ifdef CHUID
309

310 311 312 313 314 315 316 317
   if(conf->chuid)
   {
       if(getuid()) /* root check */
       {
           fprintf(stderr, "WARNING: Can't change user id unless you are root.\n");
           return;
       }

318 319
       if(gid != -1) {
           if(!setgid(gid))
320
               fprintf(stdout, "Changed groupid to %i.\n", gid);
321 322 323
           else
               fprintf(stdout, "Error changing groupid: %s.\n", strerror(errno));
       }
324

325 326
       if(uid != -1) {
           if(!setuid(uid))
327
               fprintf(stdout, "Changed userid to %i.\n", uid);
328 329 330
           else
               fprintf(stdout, "Error changing userid: %s.\n", strerror(errno));
       }
331 332
   }
#endif
333
}
334

Jack Moffitt's avatar
Jack Moffitt committed
335 336
int main(int argc, char **argv)
{
337 338
    int res, ret;
    char filename[512];
339
    char pbuf[1024];
340 341 342 343 344 345 346 347 348 349

    /* parse the '-c icecast.xml' option
    ** only, so that we can read a configfile
    */
    res = _parse_config_file(argc, argv, filename, 512);
    if (res == 1) {
        /* startup all the modules */
        _initialize_subsystems();

        /* parse the config file */
Michael Smith's avatar
Michael Smith committed
350
        config_get_config();
351
        ret = config_initial_parse_file(filename);
Michael Smith's avatar
Michael Smith committed
352
        config_release_config();
353
        if (ret < 0) {
354 355 356 357
            memset(pbuf, '\000', sizeof(pbuf));
            snprintf(pbuf, sizeof(pbuf)-1, 
                "FATAL: error parsing config file (%s)", filename);
            _fatal_error(pbuf);
358 359
            switch (ret) {
            case CONFIG_EINSANE:
360
                _fatal_error("filename was null of blank");
361 362
                break;
            case CONFIG_ENOROOT:
363
                _fatal_error("no root element found");
364 365
                break;
            case CONFIG_EBADROOT:
366
                _fatal_error("root element is not <icecast>");
367 368
                break;
            default:
369
                _fatal_error("XML config parsing error");
370 371
                break;
            }
372 373
            _shutdown_subsystems();
            return 1;
374 375 376 377 378 379 380 381
        }
    } else if (res == -1) {
        _print_usage();
        return 1;
    }
    
    /* override config file options with commandline options */
    config_parse_cmdline(argc, argv);
Jack Moffitt's avatar
Jack Moffitt committed
382

383 384
    /* Bind socket, before we change userid */
    if(!_server_proc_init()) {
385
        _fatal_error("Server startup failed. Exiting");
386 387 388
        _shutdown_subsystems();
        return 1;
    }
389

Michael Smith's avatar
Michael Smith committed
390
    _ch_root_uid_setup(); /* Change user id and root if requested/possible */
391 392

    stats_initialize(); /* We have to do this later on because of threading */
393
    fserve_initialize(); /* This too */
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409

#ifdef CHUID 
    /* We'll only have getuid() if we also have setuid(), it's reasonable to
     * assume */
    if(!getuid()) /* Running as root! Don't allow this */
    {
        fprintf(stderr, "WARNING: You should not run icecast2 as root\n");
        fprintf(stderr, "Use the changeowner directive in the config file\n");
        _shutdown_subsystems();
        return 1;
    }
#endif

    /* setup default signal handlers */
    sighandler_initialize();

410
    if (!_start_logging()) {
411
        _fatal_error("FATAL: Could not start logging");
412 413 414
        _shutdown_subsystems();
        return 1;
    }
Jack Moffitt's avatar
Jack Moffitt committed
415

416 417 418
    /* Do this after logging init */
    slave_initialize();

419
    INFO0("icecast server started");
Jack Moffitt's avatar
Jack Moffitt committed
420

421
    /* REM 3D Graphics */
Jack Moffitt's avatar
Jack Moffitt committed
422

423 424
    /* let her rip */
    global.running = ICE_RUNNING;
425 426 427 428 429 430

#ifdef USE_YP
    /* Startup yp thread */
    yp_initialize();
#endif

431
    _server_proc();
Jack Moffitt's avatar
Jack Moffitt committed
432

433
    INFO0("Shutting down");
Jack Moffitt's avatar
Jack Moffitt committed
434

435
    _shutdown_subsystems();
Jack Moffitt's avatar
Jack Moffitt committed
436

437
    return 0;
Jack Moffitt's avatar
Jack Moffitt committed
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
}