main.c 12.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/* Icecast
 *
 * This program is distributed under the GNU General Public License, version 2.
 * A copy of this license is included with this source.
 *
 * Copyright 2000-2004, Jack Moffitt <jack@xiph.org, 
 *                      Michael Smith <msmith@xiph.org>,
 *                      oddsock <oddsock@xiph.org>,
 *                      Karl Heyes <karl@xiph.org>
 *                      and others (see AUTHORS for details).
 */

13
/* -*- c-basic-offset: 4; indent-tabs-mode: nil; -*- */
14 15 16 17
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

Jack Moffitt's avatar
Jack Moffitt committed
18 19 20
#include <stdio.h>
#include <string.h>

21 22 23 24
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif

Karl Heyes's avatar
Karl Heyes committed
25 26 27 28 29
#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
30

31 32 33 34 35 36
#ifdef CHUID
#include <sys/types.h>
#include <grp.h>
#include <pwd.h>
#include <errno.h>
#endif
Jack Moffitt's avatar
Jack Moffitt committed
37

38
#include "cfgfile.h"
Jack Moffitt's avatar
Jack Moffitt committed
39 40 41 42 43 44 45
#include "sighandler.h"

#include "global.h"
#include "os.h"
#include "connection.h"
#include "refbuf.h"
#include "client.h"
46
#include "slave.h"
Jack Moffitt's avatar
Jack Moffitt committed
47 48
#include "stats.h"
#include "logging.h"
49
#include "xslt.h"
50
#include "fserve.h"
51
#include "yp.h"
Jack Moffitt's avatar
Jack Moffitt committed
52

53 54
#include <libxml/xmlmemory.h>

55
#ifdef _WIN32
56 57
/* For getpid() */
#include <process.h>
58
#define snprintf _snprintf
59
#define getpid _getpid
60
#endif
61

Jack Moffitt's avatar
Jack Moffitt committed
62 63 64
#undef CATMODULE
#define CATMODULE "main"

65 66 67 68 69 70 71 72 73
static void _fatal_error(char *perr)
{
#ifdef WIN32
    MessageBox(NULL, perr, "Error", MB_OK);
#else
    fprintf(stdout, "%s\n", perr);
#endif
}

74
static void _print_usage()
Jack Moffitt's avatar
Jack Moffitt committed
75
{
76
    printf(ICECAST_VERSION_STRING "\n\n");
77 78
    printf("usage: icecast [-h -b -v] -c <file>\n");
    printf("options:\n");
79 80 81 82
    printf("\t-c <file>\tSpecify configuration file\n");
    printf("\t-h\t\tDisplay usage\n");
    printf("\t-v\t\tDisplay version info\n");
    printf("\t-b\t\tRun icecast in the background\n");
83
    printf("\n");
Jack Moffitt's avatar
Jack Moffitt committed
84 85
}

86 87
static void _stop_logging(void)
{
88 89
    log_close(errorlog);
    log_close(accesslog);
90
    log_close(playlistlog);
91 92
}

93
static void _initialize_subsystems(void)
Jack Moffitt's avatar
Jack Moffitt committed
94
{
95 96 97 98 99 100 101 102
    log_initialize();
    thread_initialize();
    sock_initialize();
    resolver_initialize();
    config_initialize();
    connection_initialize();
    global_initialize();
    refbuf_initialize();
103
    xslt_initialize();
Jack Moffitt's avatar
Jack Moffitt committed
104 105
}

106
static void _shutdown_subsystems(void)
Jack Moffitt's avatar
Jack Moffitt committed
107
{
108
    fserve_shutdown();
109
    xslt_shutdown();
110 111
    refbuf_shutdown();
    slave_shutdown();
112
    yp_shutdown();
113
    stats_shutdown();
114 115

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

118 119 120 121 122 123 124
    global_shutdown();
    connection_shutdown();
    config_shutdown();
    resolver_shutdown();
    sock_shutdown();
    thread_shutdown();
    log_shutdown();
125 126

    xmlCleanupParser();
Jack Moffitt's avatar
Jack Moffitt committed
127 128
}

129
static int _parse_config_opts(int argc, char **argv, char *filename, int size)
Jack Moffitt's avatar
Jack Moffitt committed
130
{
131
    int i = 1;
132 133
    int config_ok = 0;

Jack Moffitt's avatar
Jack Moffitt committed
134

135
    if (argc < 2) return -1;
Jack Moffitt's avatar
Jack Moffitt committed
136

137 138
    while (i < argc) {
        if (strcmp(argv[i], "-b") == 0) {
139
#ifndef WIN32
Michael Smith's avatar
Michael Smith committed
140
            pid_t pid;
141
            fprintf(stdout, "Starting icecast2\nDetaching from the console\n");
Michael Smith's avatar
Michael Smith committed
142 143 144 145

            pid = fork();

            if (pid > 0) {
146
                /* exit the parent */
147 148
                exit(0);
            }
Michael Smith's avatar
Michael Smith committed
149
            else if(pid < 0) {
150 151
                fprintf(stderr, "FATAL: Unable to fork child!");
                exit(1);
152
            }
153
#endif
154
        }
155 156
        if (strcmp(argv[i], "-v") == 0) {
            fprintf(stdout, "%s\n", ICECAST_VERSION_STRING);
157 158 159
            exit(0);
        }

160 161 162
        if (strcmp(argv[i], "-c") == 0) {
            if (i + 1 < argc) {
                strncpy(filename, argv[i + 1], size-1);
163
                filename[size-1] = 0;
164
                config_ok = 1;
165 166 167 168 169 170 171
            } else {
                return -1;
            }
        }
        i++;
    }

172 173 174 175
    if(config_ok)
        return 1;
    else
        return -1;
Jack Moffitt's avatar
Jack Moffitt committed
176 177
}

178
static int _start_logging(void)
Jack Moffitt's avatar
Jack Moffitt committed
179
{
180 181
    char fn_error[FILENAME_MAX];
    char fn_access[FILENAME_MAX];
182
    char fn_playlist[FILENAME_MAX];
183
    char buf[1024];
184
    int log_to_stderr;
185

186
    ice_config_t *config = config_get_config_unlocked();
Jack Moffitt's avatar
Jack Moffitt committed
187

188
    if(strcmp(config->error_log, "-")) {
189
        snprintf(fn_error, FILENAME_MAX, "%s%s%s", config->log_dir, PATH_SEPARATOR, config->error_log);
190
        errorlog = log_open(fn_error);
191
        log_to_stderr = 0;
192 193
    } else {
        errorlog = log_open_file(stderr);
194
        log_to_stderr = 1;
195
    }
196 197 198

    if (errorlog < 0) {
        buf[sizeof(buf)-1] = 0;
199 200 201
        snprintf(buf, sizeof(buf)-1, 
                "FATAL: could not open error logging (%s): %s",
                log_to_stderr?"standard error":fn_error,
202 203 204 205 206
                strerror(errno));
        _fatal_error(buf);
    }
    log_set_level(errorlog, config->loglevel);

207
    if(strcmp(config->access_log, "-")) {
208
        snprintf(fn_access, FILENAME_MAX, "%s%s%s", config->log_dir, PATH_SEPARATOR, config->access_log);
209
        accesslog = log_open(fn_access);
210
        log_to_stderr = 0;
211 212
    } else {
        accesslog = log_open_file(stderr);
213
        log_to_stderr = 1;
214
    }
215 216 217

    if (accesslog < 0) {
        buf[sizeof(buf)-1] = 0;
218 219 220
        snprintf(buf, sizeof(buf)-1, 
                "FATAL: could not open access logging (%s): %s",
                log_to_stderr?"standard error":fn_access,
221 222 223
                strerror(errno));
        _fatal_error(buf);
    }
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239

    if(config->playlist_log) {
        snprintf(fn_playlist, FILENAME_MAX, "%s%s%s", config->log_dir, PATH_SEPARATOR, config->playlist_log);
        playlistlog = log_open(fn_playlist);
        if (playlistlog < 0) {
            buf[sizeof(buf)-1] = 0;
            snprintf(buf, sizeof(buf)-1, 
                "FATAL: could not open playlist logging (%s): %s",
                log_to_stderr?"standard error":fn_playlist,
                strerror(errno));
            _fatal_error(buf);
        }
        log_to_stderr = 0;
    } else {
        playlistlog = -1;
    }
240 241 242
    
    log_set_level(errorlog, config->loglevel);
    log_set_level(accesslog, 4);
243
    log_set_level(playlistlog, 4);
244 245 246 247

    if (errorlog >= 0 && accesslog >= 0) return 1;
    
    return 0;
Jack Moffitt's avatar
Jack Moffitt committed
248 249
}

250
static int _setup_sockets(void)
Jack Moffitt's avatar
Jack Moffitt committed
251
{
252
    ice_config_t *config;
253 254 255
    int i = 0;
    int ret = 0;
    int successful = 0;
256
    char pbuf[1024];
Jack Moffitt's avatar
Jack Moffitt committed
257

258
    config = config_get_config_unlocked();
Jack Moffitt's avatar
Jack Moffitt committed
259

260 261 262 263 264 265 266
    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);

267
        if (global.serversock[i] == SOCK_ERROR) {
268 269 270 271 272
            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);
273 274 275 276 277 278 279
            return 0;
        }
        else {
            ret = 1;
            successful++;
        }
    }
Michael Smith's avatar
Michael Smith committed
280

281
    global.server_sockets = successful;
282 283
    
    return ret;
Jack Moffitt's avatar
Jack Moffitt committed
284 285
}

286
static int _start_listening(void)
Jack Moffitt's avatar
Jack Moffitt committed
287
{
288 289
    int i;
    for(i=0; i < global.server_sockets; i++) {
290 291
        if (sock_listen(global.serversock[i], ICE_LISTEN_QUEUE) == SOCK_ERROR)
            return 0;
Jack Moffitt's avatar
Jack Moffitt committed
292

293
        sock_set_blocking(global.serversock[i], SOCK_NONBLOCK);
294
    }
Jack Moffitt's avatar
Jack Moffitt committed
295

296
    return 1;
Jack Moffitt's avatar
Jack Moffitt committed
297 298
}

299
/* bind the socket and start listening */
300
static int _server_proc_init(void)
Jack Moffitt's avatar
Jack Moffitt committed
301
{
302 303
    if (!_setup_sockets())
        return 0;
Jack Moffitt's avatar
Jack Moffitt committed
304

305
    if (!_start_listening()) {
306
        _fatal_error("Failed trying to listen on server socket");
307 308
        return 0;
    }
309 310

    return 1;
311
}
Jack Moffitt's avatar
Jack Moffitt committed
312

313 314 315
/* this is the heart of the beast */
static void _server_proc(void)
{
316 317
    int i;

318
    connection_accept_loop();
Jack Moffitt's avatar
Jack Moffitt committed
319

320
    for(i=0; i < MAX_LISTEN_SOCKETS; i++)
321
        sock_close(global.serversock[i]);
Jack Moffitt's avatar
Jack Moffitt committed
322 323
}

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

Michael Smith's avatar
Michael Smith committed
327
static void _ch_root_uid_setup(void)
328
{
Michael Smith's avatar
Michael Smith committed
329
   ice_config_t *conf = config_get_config_unlocked();
330 331 332 333 334 335 336 337
#ifdef CHUID
   struct passwd *user;
   struct group *group;
   uid_t uid=-1;
   gid_t gid=-1;

   if(conf->chuid)
   {
338 339 340 341 342 343 344 345 346
       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);
347

348 349 350 351 352
           if(group)
               gid = group->gr_gid;
           else
               fprintf(stderr, "Couldn't find group \"%s\" in groups file\n", conf->group);
       }
353 354 355 356
   }
#endif

#ifdef CHROOT
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
   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
375

376 377 378 379 380 381 382 383
   if(conf->chuid)
   {
       if(getuid()) /* root check */
       {
           fprintf(stderr, "WARNING: Can't change user id unless you are root.\n");
           return;
       }

384 385
       if(gid != -1) {
           if(!setgid(gid))
386
               fprintf(stdout, "Changed groupid to %i.\n", (int)gid);
387 388 389
           else
               fprintf(stdout, "Error changing groupid: %s.\n", strerror(errno));
       }
390

391 392
       if(uid != -1) {
           if(!setuid(uid))
393
               fprintf(stdout, "Changed userid to %i.\n", (int)uid);
394 395 396
           else
               fprintf(stdout, "Error changing userid: %s.\n", strerror(errno));
       }
397 398
   }
#endif
399
}
400

Jack Moffitt's avatar
Jack Moffitt committed
401 402
int main(int argc, char **argv)
{
403
    int res, ret;
404 405
    ice_config_t *config;
    char *pidfile = NULL;
406
    char filename[512];
407
    char pbuf[1024];
408 409 410 411

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

        /* parse the config file */
Michael Smith's avatar
Michael Smith committed
418
        config_get_config();
419
        ret = config_initial_parse_file(filename);
Michael Smith's avatar
Michael Smith committed
420
        config_release_config();
421
        if (ret < 0) {
422 423 424 425
            memset(pbuf, '\000', sizeof(pbuf));
            snprintf(pbuf, sizeof(pbuf)-1, 
                "FATAL: error parsing config file (%s)", filename);
            _fatal_error(pbuf);
426 427
            switch (ret) {
            case CONFIG_EINSANE:
428
                _fatal_error("filename was null or blank");
429 430
                break;
            case CONFIG_ENOROOT:
431
                _fatal_error("no root element found");
432 433
                break;
            case CONFIG_EBADROOT:
434
                _fatal_error("root element is not <icecast>");
435 436
                break;
            default:
437
                _fatal_error("XML config parsing error");
438 439
                break;
            }
440 441
            _shutdown_subsystems();
            return 1;
442 443 444 445 446 447 448 449
        }
    } 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
450

451 452
    /* Bind socket, before we change userid */
    if(!_server_proc_init()) {
453
        _fatal_error("Server startup failed. Exiting");
454 455 456
        _shutdown_subsystems();
        return 1;
    }
457

Michael Smith's avatar
Michael Smith committed
458
    _ch_root_uid_setup(); /* Change user id and root if requested/possible */
459 460

    stats_initialize(); /* We have to do this later on because of threading */
461
    fserve_initialize(); /* This too */
462 463 464 465 466 467

#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 */
    {
468
        fprintf(stderr, "ERROR: You should not run icecast2 as root\n");
469 470 471 472 473 474 475 476 477
        fprintf(stderr, "Use the changeowner directive in the config file\n");
        _shutdown_subsystems();
        return 1;
    }
#endif

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

478
    if (!_start_logging()) {
479
        _fatal_error("FATAL: Could not start logging");
480 481 482
        _shutdown_subsystems();
        return 1;
    }
Jack Moffitt's avatar
Jack Moffitt committed
483

484 485 486 487 488 489 490 491
    config = config_get_config_unlocked();
    /* recreate the pid file */
    if (config->pidfile)
    {
        FILE *f;
        pidfile = strdup (config->pidfile);
        if (pidfile && (f = fopen (config->pidfile, "w")) != NULL)
        {
492
            fprintf (f, "%d\n", (int)getpid());
493 494 495
            fclose (f);
        }
    }
496

497
    INFO0 (ICECAST_VERSION_STRING " server started");
Jack Moffitt's avatar
Jack Moffitt committed
498

499
    /* REM 3D Graphics */
Jack Moffitt's avatar
Jack Moffitt committed
500

501 502
    /* let her rip */
    global.running = ICE_RUNNING;
503 504 505 506

    /* Startup yp thread */
    yp_initialize();

507 508 509
    /* Do this after logging init */
    slave_initialize();

510
    _server_proc();
Jack Moffitt's avatar
Jack Moffitt committed
511

512
    INFO0("Shutting down");
Jack Moffitt's avatar
Jack Moffitt committed
513

514
    _shutdown_subsystems();
Jack Moffitt's avatar
Jack Moffitt committed
515

516 517 518 519 520 521
    if (pidfile)
    {
        remove (pidfile);
        free (pidfile);
    }

522
    return 0;
Jack Moffitt's avatar
Jack Moffitt committed
523 524 525
}