main.c 9.96 KB
Newer Older
1
/* -*- c-basic-offset: 4; indent-tabs-mode: nil; -*- */
Jack Moffitt's avatar
Jack Moffitt committed
2 3 4
#include <stdio.h>
#include <string.h>

Karl Heyes's avatar
Karl Heyes committed
5 6 7 8 9
#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
10

11 12 13 14 15 16
#ifdef CHUID
#include <sys/types.h>
#include <grp.h>
#include <pwd.h>
#include <errno.h>
#endif
Jack Moffitt's avatar
Jack Moffitt committed
17 18 19 20 21 22 23 24 25

#include "config.h"
#include "sighandler.h"

#include "global.h"
#include "os.h"
#include "connection.h"
#include "refbuf.h"
#include "client.h"
26
#include "slave.h"
Jack Moffitt's avatar
Jack Moffitt committed
27 28
#include "stats.h"
#include "logging.h"
29
#include "xslt.h"
30
#include "fserve.h"
brendan's avatar
brendan committed
31
#ifdef USE_YP
32
#include "geturl.h"
33
#endif
Jack Moffitt's avatar
Jack Moffitt committed
34

35 36
#include <libxml/xmlmemory.h>

37 38
#ifdef _WIN32
#define snprintf _snprintf
39
#endif
40

Jack Moffitt's avatar
Jack Moffitt committed
41 42 43
#undef CATMODULE
#define CATMODULE "main"

44 45 46 47 48 49 50 51 52
static void _fatal_error(char *perr)
{
#ifdef WIN32
    MessageBox(NULL, perr, "Error", MB_OK);
#else
    fprintf(stdout, "%s\n", perr);
#endif
}

53
static void _print_usage()
Jack Moffitt's avatar
Jack Moffitt committed
54
{
55 56 57
    printf("Usage:\n");
    printf("\ticecast -c <file>\t\tSpecify configuration file\n");
    printf("\n");
Jack Moffitt's avatar
Jack Moffitt committed
58 59
}

60 61
static void _stop_logging(void)
{
62 63
    log_close(errorlog);
    log_close(accesslog);
64 65
}

66
static void _initialize_subsystems(void)
Jack Moffitt's avatar
Jack Moffitt committed
67
{
68 69 70 71 72 73 74 75
    log_initialize();
    thread_initialize();
    sock_initialize();
    resolver_initialize();
    config_initialize();
    connection_initialize();
    global_initialize();
    refbuf_initialize();
76
    xslt_initialize();
brendan's avatar
brendan committed
77
#ifdef USE_YP
78
    curl_initialize();
79
#endif
Jack Moffitt's avatar
Jack Moffitt committed
80 81
}

82
static void _shutdown_subsystems(void)
Jack Moffitt's avatar
Jack Moffitt committed
83
{
brendan's avatar
brendan committed
84
#ifdef USE_YP
85
    curl_shutdown();
86
#endif
87
    fserve_shutdown();
88
    xslt_shutdown();
89 90 91
    refbuf_shutdown();
    stats_shutdown();
    slave_shutdown();
92 93

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

96 97 98 99 100 101 102
    global_shutdown();
    connection_shutdown();
    config_shutdown();
    resolver_shutdown();
    sock_shutdown();
    thread_shutdown();
    log_shutdown();
103 104

    xmlCleanupParser();
Jack Moffitt's avatar
Jack Moffitt committed
105 106
}

107
static int _parse_config_file(int argc, char **argv, char *filename, int size)
Jack Moffitt's avatar
Jack Moffitt committed
108
{
109 110
    int i = 1;
    int    processID = 0;
Jack Moffitt's avatar
Jack Moffitt committed
111

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

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

    return -1;
Jack Moffitt's avatar
Jack Moffitt committed
137 138
}

139
static int _start_logging(void)
Jack Moffitt's avatar
Jack Moffitt committed
140
{
141 142
    char fn_error[FILENAME_MAX];
    char fn_access[FILENAME_MAX];
143 144
    char pbuf[1024];

145
    ice_config_t *config = config_get_config_unlocked();
Jack Moffitt's avatar
Jack Moffitt committed
146

147
    if(strcmp(config->error_log, "-")) {
148
        snprintf(fn_error, FILENAME_MAX, "%s%s%s", config->log_dir, PATH_SEPARATOR, config->error_log);
149
        errorlog = log_open(fn_error);
150 151 152
    } else {
        errorlog = log_open_file(stderr);
    }
153
    if(strcmp(config->access_log, "-")) {
154
        snprintf(fn_access, FILENAME_MAX, "%s%s%s", config->log_dir, PATH_SEPARATOR, config->access_log);
155
        accesslog = log_open(fn_access);
156 157 158
    } else {
        accesslog = log_open_file(stderr);
    }
159 160 161 162
    
    log_set_level(errorlog, config->loglevel);
    log_set_level(accesslog, 4);

163 164 165 166 167 168 169 170
    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);
    }
171 172 173
    if (errorlog >= 0 && accesslog >= 0) return 1;
    
    return 0;
Jack Moffitt's avatar
Jack Moffitt committed
174 175
}

176
static int _setup_sockets(void)
Jack Moffitt's avatar
Jack Moffitt committed
177
{
178
    ice_config_t *config;
179 180 181
    int i = 0;
    int ret = 0;
    int successful = 0;
182
    char pbuf[1024];
Jack Moffitt's avatar
Jack Moffitt committed
183

184
    config = config_get_config_unlocked();
Jack Moffitt's avatar
Jack Moffitt committed
185

186 187 188 189 190 191 192
    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);

193
        if (global.serversock[i] == SOCK_ERROR) {
194 195 196 197 198
            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);
199 200 201 202 203 204 205
            return 0;
        }
        else {
            ret = 1;
            successful++;
        }
    }
206

207
    global.server_sockets = successful;
208 209
    
    return ret;
Jack Moffitt's avatar
Jack Moffitt committed
210 211
}

212
static int _start_listening(void)
Jack Moffitt's avatar
Jack Moffitt committed
213
{
214 215
    int i;
    for(i=0; i < global.server_sockets; i++) {
216 217
        if (sock_listen(global.serversock[i], ICE_LISTEN_QUEUE) == SOCK_ERROR)
            return 0;
Jack Moffitt's avatar
Jack Moffitt committed
218

219
        sock_set_blocking(global.serversock[i], SOCK_NONBLOCK);
220
    }
Jack Moffitt's avatar
Jack Moffitt committed
221

222
    return 1;
Jack Moffitt's avatar
Jack Moffitt committed
223 224
}

225
/* bind the socket and start listening */
226
static int _server_proc_init(void)
Jack Moffitt's avatar
Jack Moffitt committed
227
{
228 229
    if (!_setup_sockets())
        return 0;
Jack Moffitt's avatar
Jack Moffitt committed
230

231
    if (!_start_listening()) {
232
        _fatal_error("Failed trying to listen on server socket");
233 234
        return 0;
    }
235 236

    return 1;
237
}
Jack Moffitt's avatar
Jack Moffitt committed
238

239 240 241
/* this is the heart of the beast */
static void _server_proc(void)
{
242 243
    int i;

244
    connection_accept_loop();
Jack Moffitt's avatar
Jack Moffitt committed
245

246
    for(i=0; i < MAX_LISTEN_SOCKETS; i++)
247
        sock_close(global.serversock[i]);
Jack Moffitt's avatar
Jack Moffitt committed
248 249
}

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

Michael Smith's avatar
Michael Smith committed
253
static void _ch_root_uid_setup(void)
254
{
255
   ice_config_t *conf = config_get_config_unlocked();
256 257 258 259 260 261 262 263
#ifdef CHUID
   struct passwd *user;
   struct group *group;
   uid_t uid=-1;
   gid_t gid=-1;

   if(conf->chuid)
   {
264 265 266 267 268 269 270 271 272
       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);
273

274 275 276 277 278
           if(group)
               gid = group->gr_gid;
           else
               fprintf(stderr, "Couldn't find group \"%s\" in groups file\n", conf->group);
       }
279 280 281 282
   }
#endif

#ifdef CHROOT
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
   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
301

302 303 304 305 306 307 308 309
   if(conf->chuid)
   {
       if(getuid()) /* root check */
       {
           fprintf(stderr, "WARNING: Can't change user id unless you are root.\n");
           return;
       }

310 311
       if(gid != -1) {
           if(!setgid(gid))
312
               fprintf(stdout, "Changed groupid to %i.\n", gid);
313 314 315
           else
               fprintf(stdout, "Error changing groupid: %s.\n", strerror(errno));
       }
316

317 318
       if(uid != -1) {
           if(!setuid(uid))
319
               fprintf(stdout, "Changed userid to %i.\n", uid);
320 321 322
           else
               fprintf(stdout, "Error changing userid: %s.\n", strerror(errno));
       }
323 324
   }
#endif
325
}
326

Jack Moffitt's avatar
Jack Moffitt committed
327 328
int main(int argc, char **argv)
{
329 330
    int res, ret;
    char filename[512];
331
    char pbuf[1024];
332 333 334 335 336 337 338 339 340 341

    /* 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 */
342
        config_get_config();
343
        ret = config_initial_parse_file(filename);
344
        config_release_config();
345
        if (ret < 0) {
346 347 348 349
            memset(pbuf, '\000', sizeof(pbuf));
            snprintf(pbuf, sizeof(pbuf)-1, 
                "FATAL: error parsing config file (%s)", filename);
            _fatal_error(pbuf);
350 351
            switch (ret) {
            case CONFIG_EINSANE:
352
                _fatal_error("filename was null of blank");
353 354
                break;
            case CONFIG_ENOROOT:
355
                _fatal_error("no root element found");
356 357
                break;
            case CONFIG_EBADROOT:
358
                _fatal_error("root element is not <icecast>");
359 360
                break;
            default:
361
                _fatal_error("XML config parsing error");
362 363
                break;
            }
364 365
            _shutdown_subsystems();
            return 1;
366 367 368 369 370 371 372 373
        }
    } 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
374

375 376
    /* Bind socket, before we change userid */
    if(!_server_proc_init()) {
377
        _fatal_error("Server startup failed. Exiting");
378 379 380
        _shutdown_subsystems();
        return 1;
    }
381

Michael Smith's avatar
Michael Smith committed
382
    _ch_root_uid_setup(); /* Change user id and root if requested/possible */
383 384

    stats_initialize(); /* We have to do this later on because of threading */
385
    fserve_initialize(); /* This too */
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401

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

402
    if (!_start_logging()) {
403
        _fatal_error("FATAL: Could not start logging");
404 405 406
        _shutdown_subsystems();
        return 1;
    }
Jack Moffitt's avatar
Jack Moffitt committed
407

408 409 410
    /* Do this after logging init */
    slave_initialize();

411
    INFO0("icecast server started");
Jack Moffitt's avatar
Jack Moffitt committed
412

413
    /* REM 3D Graphics */
Jack Moffitt's avatar
Jack Moffitt committed
414

415 416
    /* let her rip */
    global.running = ICE_RUNNING;
417 418 419 420 421 422

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

423
    _server_proc();
Jack Moffitt's avatar
Jack Moffitt committed
424

425
    INFO0("Shutting down");
Jack Moffitt's avatar
Jack Moffitt committed
426

427
    _shutdown_subsystems();
Jack Moffitt's avatar
Jack Moffitt committed
428

429
    return 0;
Jack Moffitt's avatar
Jack Moffitt committed
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
}