main.c 11.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"
brendan's avatar
brendan committed
51
#ifdef USE_YP
52
#include "geturl.h"
53
#include "yp.h"
54
#endif
Jack Moffitt's avatar
Jack Moffitt committed
55

56 57
#include <libxml/xmlmemory.h>

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

Jack Moffitt's avatar
Jack Moffitt committed
65 66 67
#undef CATMODULE
#define CATMODULE "main"

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

77
static void _print_usage()
Jack Moffitt's avatar
Jack Moffitt committed
78
{
79
    printf(ICECAST_VERSION_STRING "\n\n");
80 81
    printf("usage: icecast [-h -b -v] -c <file>\n");
    printf("options:\n");
82 83 84 85
    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");
86
    printf("\n");
Jack Moffitt's avatar
Jack Moffitt committed
87 88
}

89 90
static void _stop_logging(void)
{
91 92
    log_close(errorlog);
    log_close(accesslog);
93 94
}

95
static void _initialize_subsystems(void)
Jack Moffitt's avatar
Jack Moffitt committed
96
{
97 98 99 100 101 102 103 104
    log_initialize();
    thread_initialize();
    sock_initialize();
    resolver_initialize();
    config_initialize();
    connection_initialize();
    global_initialize();
    refbuf_initialize();
105
    xslt_initialize();
brendan's avatar
brendan committed
106
#ifdef USE_YP
107
    curl_initialize();
108
#endif
Jack Moffitt's avatar
Jack Moffitt committed
109 110
}

111
static void _shutdown_subsystems(void)
Jack Moffitt's avatar
Jack Moffitt committed
112
{
113
    fserve_shutdown();
114
    xslt_shutdown();
115 116
    refbuf_shutdown();
    slave_shutdown();
117
    stats_shutdown();
118 119 120
#ifdef USE_YP
    curl_shutdown();
#endif
121 122

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

125 126 127 128 129 130 131
    global_shutdown();
    connection_shutdown();
    config_shutdown();
    resolver_shutdown();
    sock_shutdown();
    thread_shutdown();
    log_shutdown();
132 133

    xmlCleanupParser();
Jack Moffitt's avatar
Jack Moffitt committed
134 135
}

136
static int _parse_config_opts(int argc, char **argv, char *filename, int size)
Jack Moffitt's avatar
Jack Moffitt committed
137
{
138
    int i = 1;
139 140 141
    int processID = 0;
    int config_ok = 0;

Jack Moffitt's avatar
Jack Moffitt committed
142

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

145 146
    while (i < argc) {
        if (strcmp(argv[i], "-b") == 0) {
147
#ifndef WIN32
148 149 150
            fprintf(stdout, "Starting icecast2\nDetaching from the console\n");
            if ((processID = (int)fork()) > 0) {
                /* exit the parent */
151 152 153 154 155
                exit(0);
            }
            else {
                fprintf(stderr, "FATAL: Unable to fork child!");
                exit(1);
156
            }
157
#endif
158
        }
159 160
        if (strcmp(argv[i], "-v") == 0) {
            fprintf(stdout, "%s\n", ICECAST_VERSION_STRING);
161 162 163
            exit(0);
        }

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

176 177 178 179
    if(config_ok)
        return 1;
    else
        return -1;
Jack Moffitt's avatar
Jack Moffitt committed
180 181
}

182
static int _start_logging(void)
Jack Moffitt's avatar
Jack Moffitt committed
183
{
184 185
    char fn_error[FILENAME_MAX];
    char fn_access[FILENAME_MAX];
186 187
    char pbuf[1024];

188
    ice_config_t *config = config_get_config_unlocked();
Jack Moffitt's avatar
Jack Moffitt committed
189

190
    if(strcmp(config->error_log, "-")) {
191
        snprintf(fn_error, FILENAME_MAX, "%s%s%s", config->log_dir, PATH_SEPARATOR, config->error_log);
192
        errorlog = log_open(fn_error);
193 194 195
    } else {
        errorlog = log_open_file(stderr);
    }
196
    if(strcmp(config->access_log, "-")) {
197
        snprintf(fn_access, FILENAME_MAX, "%s%s%s", config->log_dir, PATH_SEPARATOR, config->access_log);
198
        accesslog = log_open(fn_access);
199 200 201
    } else {
        accesslog = log_open_file(stderr);
    }
202 203 204 205
    
    log_set_level(errorlog, config->loglevel);
    log_set_level(accesslog, 4);

206 207 208 209 210 211 212 213
    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);
    }
214 215 216
    if (errorlog >= 0 && accesslog >= 0) return 1;
    
    return 0;
Jack Moffitt's avatar
Jack Moffitt committed
217 218
}

219
static int _setup_sockets(void)
Jack Moffitt's avatar
Jack Moffitt committed
220
{
221
    ice_config_t *config;
222 223 224
    int i = 0;
    int ret = 0;
    int successful = 0;
225
    char pbuf[1024];
Jack Moffitt's avatar
Jack Moffitt committed
226

227
    config = config_get_config_unlocked();
Jack Moffitt's avatar
Jack Moffitt committed
228

229 230 231 232 233 234 235
    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);

236
        if (global.serversock[i] == SOCK_ERROR) {
237 238 239 240 241
            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);
242 243 244 245 246 247 248
            return 0;
        }
        else {
            ret = 1;
            successful++;
        }
    }
Michael Smith's avatar
Michael Smith committed
249

250
    global.server_sockets = successful;
251 252
    
    return ret;
Jack Moffitt's avatar
Jack Moffitt committed
253 254
}

255
static int _start_listening(void)
Jack Moffitt's avatar
Jack Moffitt committed
256
{
257 258
    int i;
    for(i=0; i < global.server_sockets; i++) {
259 260
        if (sock_listen(global.serversock[i], ICE_LISTEN_QUEUE) == SOCK_ERROR)
            return 0;
Jack Moffitt's avatar
Jack Moffitt committed
261

262
        sock_set_blocking(global.serversock[i], SOCK_NONBLOCK);
263
    }
Jack Moffitt's avatar
Jack Moffitt committed
264

265
    return 1;
Jack Moffitt's avatar
Jack Moffitt committed
266 267
}

268
/* bind the socket and start listening */
269
static int _server_proc_init(void)
Jack Moffitt's avatar
Jack Moffitt committed
270
{
271 272
    if (!_setup_sockets())
        return 0;
Jack Moffitt's avatar
Jack Moffitt committed
273

274
    if (!_start_listening()) {
275
        _fatal_error("Failed trying to listen on server socket");
276 277
        return 0;
    }
278 279

    return 1;
280
}
Jack Moffitt's avatar
Jack Moffitt committed
281

282 283 284
/* this is the heart of the beast */
static void _server_proc(void)
{
285 286
    int i;

287
    connection_accept_loop();
Jack Moffitt's avatar
Jack Moffitt committed
288

289
    for(i=0; i < MAX_LISTEN_SOCKETS; i++)
290
        sock_close(global.serversock[i]);
Jack Moffitt's avatar
Jack Moffitt committed
291 292
}

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

Michael Smith's avatar
Michael Smith committed
296
static void _ch_root_uid_setup(void)
297
{
Michael Smith's avatar
Michael Smith committed
298
   ice_config_t *conf = config_get_config_unlocked();
299 300 301 302 303 304 305 306
#ifdef CHUID
   struct passwd *user;
   struct group *group;
   uid_t uid=-1;
   gid_t gid=-1;

   if(conf->chuid)
   {
307 308 309 310 311 312 313 314 315
       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);
316

317 318 319 320 321
           if(group)
               gid = group->gr_gid;
           else
               fprintf(stderr, "Couldn't find group \"%s\" in groups file\n", conf->group);
       }
322 323 324 325
   }
#endif

#ifdef CHROOT
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
   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
344

345 346 347 348 349 350 351 352
   if(conf->chuid)
   {
       if(getuid()) /* root check */
       {
           fprintf(stderr, "WARNING: Can't change user id unless you are root.\n");
           return;
       }

353 354
       if(gid != -1) {
           if(!setgid(gid))
355
               fprintf(stdout, "Changed groupid to %i.\n", (int)gid);
356 357 358
           else
               fprintf(stdout, "Error changing groupid: %s.\n", strerror(errno));
       }
359

360 361
       if(uid != -1) {
           if(!setuid(uid))
362
               fprintf(stdout, "Changed userid to %i.\n", (int)uid);
363 364 365
           else
               fprintf(stdout, "Error changing userid: %s.\n", strerror(errno));
       }
366 367
   }
#endif
368
}
369

Jack Moffitt's avatar
Jack Moffitt committed
370 371
int main(int argc, char **argv)
{
372
    int res, ret;
373 374
    ice_config_t *config;
    char *pidfile = NULL;
375
    char filename[512];
376
    char pbuf[1024];
377 378 379 380

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

        /* parse the config file */
Michael Smith's avatar
Michael Smith committed
387
        config_get_config();
388
        ret = config_initial_parse_file(filename);
Michael Smith's avatar
Michael Smith committed
389
        config_release_config();
390
        if (ret < 0) {
391 392 393 394
            memset(pbuf, '\000', sizeof(pbuf));
            snprintf(pbuf, sizeof(pbuf)-1, 
                "FATAL: error parsing config file (%s)", filename);
            _fatal_error(pbuf);
395 396
            switch (ret) {
            case CONFIG_EINSANE:
397
                _fatal_error("filename was null of blank");
398 399
                break;
            case CONFIG_ENOROOT:
400
                _fatal_error("no root element found");
401 402
                break;
            case CONFIG_EBADROOT:
403
                _fatal_error("root element is not <icecast>");
404 405
                break;
            default:
406
                _fatal_error("XML config parsing error");
407 408
                break;
            }
409 410
            _shutdown_subsystems();
            return 1;
411 412 413 414 415 416 417 418
        }
    } 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
419

420 421
    /* Bind socket, before we change userid */
    if(!_server_proc_init()) {
422
        _fatal_error("Server startup failed. Exiting");
423 424 425
        _shutdown_subsystems();
        return 1;
    }
426

Michael Smith's avatar
Michael Smith committed
427
    _ch_root_uid_setup(); /* Change user id and root if requested/possible */
428 429

    stats_initialize(); /* We have to do this later on because of threading */
430
    fserve_initialize(); /* This too */
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446

#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();

447
    if (!_start_logging()) {
448
        _fatal_error("FATAL: Could not start logging");
449 450 451
        _shutdown_subsystems();
        return 1;
    }
Jack Moffitt's avatar
Jack Moffitt committed
452

453 454 455 456 457 458 459 460
    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)
        {
461
            fprintf (f, "%d\n", (int)getpid());
462 463 464
            fclose (f);
        }
    }
465

466
    INFO0 (ICECAST_VERSION_STRING " server started");
Jack Moffitt's avatar
Jack Moffitt committed
467

468
    /* REM 3D Graphics */
Jack Moffitt's avatar
Jack Moffitt committed
469

470 471
    /* let her rip */
    global.running = ICE_RUNNING;
472 473 474 475 476 477

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

478 479 480
    /* Do this after logging init */
    slave_initialize();

481
    _server_proc();
Jack Moffitt's avatar
Jack Moffitt committed
482

483
    INFO0("Shutting down");
Jack Moffitt's avatar
Jack Moffitt committed
484

485
    _shutdown_subsystems();
Jack Moffitt's avatar
Jack Moffitt committed
486

487 488 489 490 491 492
    if (pidfile)
    {
        remove (pidfile);
        free (pidfile);
    }

493
    return 0;
Jack Moffitt's avatar
Jack Moffitt committed
494 495 496
}