main.c 10.5 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>

9 10 11 12
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif

Karl Heyes's avatar
Karl Heyes committed
13 14 15 16 17
#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
18

19 20 21 22 23 24
#ifdef CHUID
#include <sys/types.h>
#include <grp.h>
#include <pwd.h>
#include <errno.h>
#endif
Jack Moffitt's avatar
Jack Moffitt committed
25

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
#include "yp.h"
42
#endif
Jack Moffitt's avatar
Jack Moffitt committed
43

44 45
#include <libxml/xmlmemory.h>

46 47
#ifdef _WIN32
#define snprintf _snprintf
48
#define getpid _getpid
49
#endif
50

Jack Moffitt's avatar
Jack Moffitt committed
51 52 53
#undef CATMODULE
#define CATMODULE "main"

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

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

70 71
static void _stop_logging(void)
{
72 73
    log_close(errorlog);
    log_close(accesslog);
74 75
}

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

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

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

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

    xmlCleanupParser();
Jack Moffitt's avatar
Jack Moffitt committed
115 116
}

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

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

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

    return -1;
Jack Moffitt's avatar
Jack Moffitt committed
147 148
}

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

155
    ice_config_t *config = config_get_config_unlocked();
Jack Moffitt's avatar
Jack Moffitt committed
156

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

173 174 175 176 177 178 179 180
    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);
    }
181 182 183
    if (errorlog >= 0 && accesslog >= 0) return 1;
    
    return 0;
Jack Moffitt's avatar
Jack Moffitt committed
184 185
}

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

194
    config = config_get_config_unlocked();
Jack Moffitt's avatar
Jack Moffitt committed
195

196 197 198 199 200 201 202
    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);

203
        if (global.serversock[i] == SOCK_ERROR) {
204 205 206 207 208
            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);
209 210 211 212 213 214 215
            return 0;
        }
        else {
            ret = 1;
            successful++;
        }
    }
216

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

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

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

232
    return 1;
Jack Moffitt's avatar
Jack Moffitt committed
233 234
}

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

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

    return 1;
247
}
Jack Moffitt's avatar
Jack Moffitt committed
248

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

254
    connection_accept_loop();
Jack Moffitt's avatar
Jack Moffitt committed
255

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

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

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

   if(conf->chuid)
   {
274 275 276 277 278 279 280 281 282
       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);
283

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

#ifdef CHROOT
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
   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
311

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

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

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

Jack Moffitt's avatar
Jack Moffitt committed
337 338
int main(int argc, char **argv)
{
339
    int res, ret;
340 341
    ice_config_t *config;
    char *pidfile = NULL;
342
    char filename[512];
343
    char pbuf[1024];
344 345 346 347 348 349 350 351 352 353

    /* 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 */
354
        config_get_config();
355
        ret = config_initial_parse_file(filename);
356
        config_release_config();
357
        if (ret < 0) {
358 359 360 361
            memset(pbuf, '\000', sizeof(pbuf));
            snprintf(pbuf, sizeof(pbuf)-1, 
                "FATAL: error parsing config file (%s)", filename);
            _fatal_error(pbuf);
362 363
            switch (ret) {
            case CONFIG_EINSANE:
364
                _fatal_error("filename was null of blank");
365 366
                break;
            case CONFIG_ENOROOT:
367
                _fatal_error("no root element found");
368 369
                break;
            case CONFIG_EBADROOT:
370
                _fatal_error("root element is not <icecast>");
371 372
                break;
            default:
373
                _fatal_error("XML config parsing error");
374 375
                break;
            }
376 377
            _shutdown_subsystems();
            return 1;
378 379 380 381 382 383 384 385
        }
    } 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
386

387 388
    /* Bind socket, before we change userid */
    if(!_server_proc_init()) {
389
        _fatal_error("Server startup failed. Exiting");
390 391 392
        _shutdown_subsystems();
        return 1;
    }
393

Michael Smith's avatar
Michael Smith committed
394
    _ch_root_uid_setup(); /* Change user id and root if requested/possible */
395 396

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

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

414
    if (!_start_logging()) {
415
        _fatal_error("FATAL: Could not start logging");
416 417 418
        _shutdown_subsystems();
        return 1;
    }
Jack Moffitt's avatar
Jack Moffitt committed
419

420 421 422 423 424 425 426 427 428 429 430 431
    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)
        {
            fprintf (f, "%d\n", getpid());
            fclose (f);
        }
    }
432 433 434
    /* Do this after logging init */
    slave_initialize();

435
    INFO0("icecast server started");
Jack Moffitt's avatar
Jack Moffitt committed
436

437
    /* REM 3D Graphics */
Jack Moffitt's avatar
Jack Moffitt committed
438

439 440
    /* let her rip */
    global.running = ICE_RUNNING;
441 442 443 444 445 446

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

447
    _server_proc();
Jack Moffitt's avatar
Jack Moffitt committed
448

449
    INFO0("Shutting down");
Jack Moffitt's avatar
Jack Moffitt committed
450

451
    _shutdown_subsystems();
Jack Moffitt's avatar
Jack Moffitt committed
452

453 454 455 456 457 458
    if (pidfile)
    {
        remove (pidfile);
        free (pidfile);
    }

459
    return 0;
Jack Moffitt's avatar
Jack Moffitt committed
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
}