main.c 9.89 KB
Newer Older
1
/* -*- c-basic-offset: 4; indent-tabs-mode: nil; -*- */
Jack Moffitt's avatar
Jack Moffitt committed
2 3 4 5 6 7 8 9 10 11
#include <stdio.h>
#include <string.h>

#include "thread.h"
#include "avl.h"
#include "log.h"
#include "sock.h"
#include "resolver.h"
#include "httpp.h"

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

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

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

36 37
#include <libxml/xmlmemory.h>

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

416 417 418
    /* let her rip */
    global.running = ICE_RUNNING;
    _server_proc();
Jack Moffitt's avatar
Jack Moffitt committed
419

420
    INFO0("Shutting down");
Jack Moffitt's avatar
Jack Moffitt committed
421

422
    _shutdown_subsystems();
Jack Moffitt's avatar
Jack Moffitt committed
423

424
    return 0;
Jack Moffitt's avatar
Jack Moffitt committed
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
}