main.c 8.64 KB
Newer Older
Jack Moffitt's avatar
Jack Moffitt committed
1 2 3 4 5 6 7 8 9 10
#include <stdio.h>
#include <string.h>

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

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"
31
#include "geturl.h"
Jack Moffitt's avatar
Jack Moffitt committed
32

33 34
#include <libxml/xmlmemory.h>

35 36
#ifdef _WIN32
#define snprintf _snprintf
37
#endif
38

Jack Moffitt's avatar
Jack Moffitt committed
39 40 41
#undef CATMODULE
#define CATMODULE "main"

42
static void _print_usage()
Jack Moffitt's avatar
Jack Moffitt committed
43
{
44 45
	printf("Usage:\n");
	printf("\ticecast -c <file>\t\tSpecify configuration file\n");
Jack Moffitt's avatar
Jack Moffitt committed
46 47 48
	printf("\n");
}

49 50 51 52 53 54
static void _stop_logging(void)
{
	log_close(errorlog);
	log_close(accesslog);
}

55
static void _initialize_subsystems(void)
Jack Moffitt's avatar
Jack Moffitt committed
56 57 58 59 60 61 62 63 64
{
	log_initialize();
	thread_initialize();
	sock_initialize();
	resolver_initialize();
	config_initialize();
	connection_initialize();
	global_initialize();
	refbuf_initialize();
65
    xslt_initialize();
66
	curl_initialize();
Jack Moffitt's avatar
Jack Moffitt committed
67 68
}

69
static void _shutdown_subsystems(void)
Jack Moffitt's avatar
Jack Moffitt committed
70
{
71
	curl_shutdown();
72
    fserve_shutdown();
73
    xslt_shutdown();
Jack Moffitt's avatar
Jack Moffitt committed
74 75
	refbuf_shutdown();
	stats_shutdown();
76
	slave_shutdown();
77 78 79 80

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

Jack Moffitt's avatar
Jack Moffitt committed
81 82 83 84 85 86 87
	global_shutdown();
	connection_shutdown();
	config_shutdown();
	resolver_shutdown();
	sock_shutdown();
	thread_shutdown();
	log_shutdown();
88 89

    xmlCleanupParser();
Jack Moffitt's avatar
Jack Moffitt committed
90 91
}

92
static int _parse_config_file(int argc, char **argv, char *filename, int size)
Jack Moffitt's avatar
Jack Moffitt committed
93 94
{
	int i = 1;
95
	int	processID = 0;
Jack Moffitt's avatar
Jack Moffitt committed
96

97
	if (argc < 3) return -1;
Jack Moffitt's avatar
Jack Moffitt committed
98 99

	while (i < argc) {
100 101 102 103 104 105 106 107 108
		if (strcmp(argv[i], "-b") == 0) {
#ifndef WIN32
				fprintf(stdout, "Starting icecast2\nDetaching from the console\n");
				if ((processID = (int)fork()) > 0) {
						/* exit the parent */
						_exit(0);
				}
#endif
		}
Jack Moffitt's avatar
Jack Moffitt committed
109 110
		if (strcmp(argv[i], "-c") == 0) {
			if (i + 1 < argc) {
111 112
				strncpy(filename, argv[i + 1], size-1);
                filename[size-1] = 0;
Jack Moffitt's avatar
Jack Moffitt committed
113 114 115 116 117 118 119 120
				return 1;
			} else {
				return -1;
			}
		}
		i++;
	}

121
	return -1;
Jack Moffitt's avatar
Jack Moffitt committed
122 123
}

124
static int _start_logging(void)
Jack Moffitt's avatar
Jack Moffitt committed
125 126 127
{
	char fn_error[FILENAME_MAX];
	char fn_access[FILENAME_MAX];
Michael Smith's avatar
Michael Smith committed
128
	ice_config_t *config = config_get_config_unlocked();
Jack Moffitt's avatar
Jack Moffitt committed
129

130 131 132 133 134 135 136 137 138 139 140 141
	if(strcmp(config->error_log, "-")) {
        snprintf(fn_error, FILENAME_MAX, "%s%s%s", config->log_dir, PATH_SEPARATOR, config->error_log);
	    errorlog = log_open(fn_error);
    } else {
        errorlog = log_open_file(stderr);
    }
	if(strcmp(config->access_log, "-")) {
        snprintf(fn_access, FILENAME_MAX, "%s%s%s", config->log_dir, PATH_SEPARATOR, config->access_log);
	    accesslog = log_open(fn_access);
    } else {
        accesslog = log_open_file(stderr);
    }
Jack Moffitt's avatar
Jack Moffitt committed
142
	
143
	log_set_level(errorlog, config->loglevel);
Jack Moffitt's avatar
Jack Moffitt committed
144 145 146 147 148 149 150 151 152 153 154 155
	log_set_level(accesslog, 4);

	if (errorlog < 0)
		fprintf(stderr, "FATAL: could not open %s for error logging\n", fn_error);
	if (accesslog < 0)
		fprintf(stderr, "FATAL: could not open %s for access logging\n", fn_access);

	if (errorlog >= 0 && accesslog >= 0) return 1;
	
	return 0;
}

156
static int _setup_sockets(void)
Jack Moffitt's avatar
Jack Moffitt committed
157 158
{
	ice_config_t *config;
159 160 161
    int i = 0;
    int ret = 0;
    int successful = 0;
Jack Moffitt's avatar
Jack Moffitt committed
162

Michael Smith's avatar
Michael Smith committed
163
	config = config_get_config_unlocked();
Jack Moffitt's avatar
Jack Moffitt committed
164

165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
    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);

    	if (global.serversock[i] == SOCK_ERROR) {
		    fprintf(stderr, "Could not create listener socket on port %d\n", 
                    config->listeners[i].port);
            return 0;
        }
        else {
            ret = 1;
            successful++;
        }
    }
Michael Smith's avatar
Michael Smith committed
182

183
    global.server_sockets = successful;
Jack Moffitt's avatar
Jack Moffitt committed
184
	
185
	return ret;
Jack Moffitt's avatar
Jack Moffitt committed
186 187
}

188
static int _start_listening(void)
Jack Moffitt's avatar
Jack Moffitt committed
189
{
190 191 192 193
    int i;
    for(i=0; i < global.server_sockets; i++) {
    	if (sock_listen(global.serversock[i], ICE_LISTEN_QUEUE) == SOCK_ERROR)
	    	return 0;
Jack Moffitt's avatar
Jack Moffitt committed
194

195 196
	    sock_set_blocking(global.serversock[i], SOCK_NONBLOCK);
    }
Jack Moffitt's avatar
Jack Moffitt committed
197 198 199 200

	return 1;
}

201
/* bind the socket and start listening */
202
static int _server_proc_init(void)
Jack Moffitt's avatar
Jack Moffitt committed
203
{
204
	if (!_setup_sockets())
205
		return 0;
Jack Moffitt's avatar
Jack Moffitt committed
206 207

	if (!_start_listening()) {
Michael Smith's avatar
Michael Smith committed
208
		fprintf(stderr, "Failed trying to listen on server socket\n");
209
		return 0;
Jack Moffitt's avatar
Jack Moffitt committed
210
	}
211 212

    return 1;
213
}
Jack Moffitt's avatar
Jack Moffitt committed
214

215 216 217
/* this is the heart of the beast */
static void _server_proc(void)
{
218 219
    int i;

Jack Moffitt's avatar
Jack Moffitt committed
220 221
	connection_accept_loop();

222 223
    for(i=0; i < MAX_LISTEN_SOCKETS; i++)
    	sock_close(global.serversock[i]);
Jack Moffitt's avatar
Jack Moffitt committed
224 225
}

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

Michael Smith's avatar
Michael Smith committed
229
static void _ch_root_uid_setup(void)
230
{
Michael Smith's avatar
Michael Smith committed
231
   ice_config_t *conf = config_get_config_unlocked();
232 233 234 235 236 237 238 239
#ifdef CHUID
   struct passwd *user;
   struct group *group;
   uid_t uid=-1;
   gid_t gid=-1;

   if(conf->chuid)
   {
240 241 242 243 244 245 246 247 248
       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);
249

250 251 252 253 254
           if(group)
               gid = group->gr_gid;
           else
               fprintf(stderr, "Couldn't find group \"%s\" in groups file\n", conf->group);
       }
255 256 257 258
   }
#endif

#ifdef CHROOT
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
   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
277

278 279 280 281 282 283 284 285
   if(conf->chuid)
   {
       if(getuid()) /* root check */
       {
           fprintf(stderr, "WARNING: Can't change user id unless you are root.\n");
           return;
       }

286 287
       if(gid != -1) {
           if(!setgid(gid))
288
               fprintf(stdout, "Changed groupid to %i.\n", gid);
289 290 291
           else
               fprintf(stdout, "Error changing groupid: %s.\n", strerror(errno));
       }
292

293 294
       if(uid != -1) {
           if(!setuid(uid))
295
               fprintf(stdout, "Changed userid to %i.\n", uid);
296 297 298
           else
               fprintf(stdout, "Error changing userid: %s.\n", strerror(errno));
       }
299 300
   }
#endif
301
}
302

Jack Moffitt's avatar
Jack Moffitt committed
303 304 305
int main(int argc, char **argv)
{
	int res, ret;
306
	char filename[512];
Jack Moffitt's avatar
Jack Moffitt committed
307 308 309 310

	/* parse the '-c icecast.xml' option
	** only, so that we can read a configfile
	*/
311
	res = _parse_config_file(argc, argv, filename, 512);
Jack Moffitt's avatar
Jack Moffitt committed
312
	if (res == 1) {
313 314 315
	    /* startup all the modules */
    	_initialize_subsystems();

Jack Moffitt's avatar
Jack Moffitt committed
316
		/* parse the config file */
Michael Smith's avatar
Michael Smith committed
317 318 319
        config_get_config();
		ret = config_initial_parse_file(filename);
        config_release_config();
Jack Moffitt's avatar
Jack Moffitt committed
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
		if (ret < 0) {
			fprintf(stderr, "FATAL: error parsing config file:");
			switch (ret) {
			case CONFIG_EINSANE:
				fprintf(stderr, "filename was null or blank\n");
				break;
			case CONFIG_ENOROOT:
				fprintf(stderr, "no root element found\n");
				break;
			case CONFIG_EBADROOT:
				fprintf(stderr, "root element is not <icecast>\n");
				break;
			default:
				fprintf(stderr, "parse error\n");
				break;
			}
336 337
            _shutdown_subsystems();
            return 1;
Jack Moffitt's avatar
Jack Moffitt committed
338 339 340 341 342 343 344 345 346
		}
	} else if (res == -1) {
		_print_usage();
		return 1;
	}
	
	/* override config file options with commandline options */
	config_parse_cmdline(argc, argv);

347 348 349 350 351 352
    /* Bind socket, before we change userid */
    if(!_server_proc_init()) {
        fprintf(stderr, "Server startup failed. Exiting.\n");
        _shutdown_subsystems();
        return 1;
    }
353

Michael Smith's avatar
Michael Smith committed
354
    _ch_root_uid_setup(); /* Change user id and root if requested/possible */
355 356

    stats_initialize(); /* We have to do this later on because of threading */
357
    fserve_initialize(); /* This too */
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373

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

Jack Moffitt's avatar
Jack Moffitt committed
374 375 376 377 378 379
	if (!_start_logging()) {
		fprintf(stderr, "FATAL: Could not start logging\n");
		_shutdown_subsystems();
		return 1;
	}

380 381 382
    /* Do this after logging init */
    slave_initialize();

Jack Moffitt's avatar
Jack Moffitt committed
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
	INFO0("icecast server started");

	/* REM 3D Graphics */

	/* let her rip */
	global.running = ICE_RUNNING;
	_server_proc();

	INFO0("Shutting down");

	_shutdown_subsystems();

	return 0;
}