main.c 7.62 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"
Jack Moffitt's avatar
Jack Moffitt committed
31

32 33
#ifdef _WIN32
#define snprintf _snprintf
34
#endif
35

Jack Moffitt's avatar
Jack Moffitt committed
36 37 38
#undef CATMODULE
#define CATMODULE "main"

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

46
static void _initialize_subsystems(void)
Jack Moffitt's avatar
Jack Moffitt committed
47 48 49 50 51 52 53 54 55
{
	log_initialize();
	thread_initialize();
	sock_initialize();
	resolver_initialize();
	config_initialize();
	connection_initialize();
	global_initialize();
	refbuf_initialize();
56
    xslt_initialize();
57 58
    DEBUG0("Calling fserve_initialize()");
    fserve_initialize();
Jack Moffitt's avatar
Jack Moffitt committed
59 60
}

61
static void _shutdown_subsystems(void)
Jack Moffitt's avatar
Jack Moffitt committed
62
{
63
    fserve_shutdown();
64
    xslt_shutdown();
Jack Moffitt's avatar
Jack Moffitt committed
65 66
	refbuf_shutdown();
	stats_shutdown();
67
	slave_shutdown();
Jack Moffitt's avatar
Jack Moffitt committed
68 69 70 71 72 73 74 75 76
	global_shutdown();
	connection_shutdown();
	config_shutdown();
	resolver_shutdown();
	sock_shutdown();
	thread_shutdown();
	log_shutdown();
}

77
static int _parse_config_file(int argc, char **argv, char *filename, int size)
Jack Moffitt's avatar
Jack Moffitt committed
78 79 80
{
	int i = 1;

81
	if (argc < 3) return -1;
Jack Moffitt's avatar
Jack Moffitt committed
82 83 84 85 86 87 88 89 90 91 92 93 94

	while (i < argc) {
		if (strcmp(argv[i], "-c") == 0) {
			if (i + 1 < argc) {
				strncpy(filename, argv[i + 1], size);
				return 1;
			} else {
				return -1;
			}
		}
		i++;
	}

95
	return -1;
Jack Moffitt's avatar
Jack Moffitt committed
96 97
}

98
static int _start_logging(void)
Jack Moffitt's avatar
Jack Moffitt committed
99 100 101 102 103
{
	char fn_error[FILENAME_MAX];
	char fn_access[FILENAME_MAX];
	ice_config_t *config = config_get_config();

104 105 106 107 108 109 110 111 112 113 114 115
	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
116
	
117
	log_set_level(errorlog, config->loglevel);
Jack Moffitt's avatar
Jack Moffitt committed
118 119 120 121 122 123 124 125 126 127 128 129
	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;
}

130
static void _stop_logging(void)
Jack Moffitt's avatar
Jack Moffitt committed
131 132 133 134 135
{
	log_close(errorlog);
	log_close(accesslog);
}

136
static int _setup_socket(void)
Jack Moffitt's avatar
Jack Moffitt committed
137 138 139 140 141 142 143 144 145 146 147 148
{
	ice_config_t *config;

	config = config_get_config();

	global.serversock = sock_get_server_socket(config->port, config->bind_address);
	if (global.serversock == SOCK_ERROR)
		return 0;
	
	return 1;
}

149
static int _start_listening(void)
Jack Moffitt's avatar
Jack Moffitt committed
150 151 152 153 154 155 156 157 158
{
	if (sock_listen(global.serversock, ICE_LISTEN_QUEUE) == SOCK_ERROR)
		return 0;

	sock_set_blocking(global.serversock, SOCK_NONBLOCK);

	return 1;
}

159
/* bind the socket and start listening */
160
static int _server_proc_init(void)
Jack Moffitt's avatar
Jack Moffitt committed
161 162
{
	if (!_setup_socket()) {
Michael Smith's avatar
Michael Smith committed
163
		fprintf(stderr, "Could not create listener socket on port %d\n", config_get_config()->port);
164
		return 0;
Jack Moffitt's avatar
Jack Moffitt committed
165 166 167
	}

	if (!_start_listening()) {
Michael Smith's avatar
Michael Smith committed
168
		fprintf(stderr, "Failed trying to listen on server socket\n");
169
		return 0;
Jack Moffitt's avatar
Jack Moffitt committed
170
	}
171 172

    return 1;
173
}
Jack Moffitt's avatar
Jack Moffitt committed
174

175 176 177
/* this is the heart of the beast */
static void _server_proc(void)
{
Jack Moffitt's avatar
Jack Moffitt committed
178 179 180 181 182
	connection_accept_loop();

	sock_close(global.serversock);
}

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

Michael Smith's avatar
Michael Smith committed
186
static void _ch_root_uid_setup(void)
187 188
{
   ice_config_t *conf = config_get_config();
189 190 191 192 193 194 195 196
#ifdef CHUID
   struct passwd *user;
   struct group *group;
   uid_t uid=-1;
   gid_t gid=-1;

   if(conf->chuid)
   {
197 198 199 200 201 202 203 204 205
       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);
206

207 208 209 210 211
           if(group)
               gid = group->gr_gid;
           else
               fprintf(stderr, "Couldn't find group \"%s\" in groups file\n", conf->group);
       }
212 213 214 215
   }
#endif

#ifdef CHROOT
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
   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
234

235 236 237 238 239 240 241 242
   if(conf->chuid)
   {
       if(getuid()) /* root check */
       {
           fprintf(stderr, "WARNING: Can't change user id unless you are root.\n");
           return;
       }

243 244
       if(gid != -1) {
           if(!setgid(gid))
245
               fprintf(stdout, "Changed groupid to %i.\n", gid);
246 247 248
           else
               fprintf(stdout, "Error changing groupid: %s.\n", strerror(errno));
       }
249

250 251
       if(uid != -1) {
           if(!setuid(uid))
252
               fprintf(stdout, "Changed userid to %i.\n", uid);
253 254 255
           else
               fprintf(stdout, "Error changing userid: %s.\n", strerror(errno));
       }
256 257
   }
#endif
258
}
259

Jack Moffitt's avatar
Jack Moffitt committed
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
int main(int argc, char **argv)
{
	int res, ret;
	char filename[256];

	/* startup all the modules */
	_initialize_subsystems();

	/* parse the '-c icecast.xml' option
	** only, so that we can read a configfile
	*/
	res = _parse_config_file(argc, argv, filename, 256);
	if (res == 1) {
		/* parse the config file */
		ret = config_parse_file(filename);
		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;
			}
291 292
            _shutdown_subsystems();
            return 1;
Jack Moffitt's avatar
Jack Moffitt committed
293 294 295 296 297 298 299 300 301 302
		}
	} else if (res == -1) {
		_print_usage();
		_shutdown_subsystems();
		return 1;
	}
	
	/* override config file options with commandline options */
	config_parse_cmdline(argc, argv);

303 304 305 306 307 308
    /* Bind socket, before we change userid */
    if(!_server_proc_init()) {
        fprintf(stderr, "Server startup failed. Exiting.\n");
        _shutdown_subsystems();
        return 1;
    }
309

Michael Smith's avatar
Michael Smith committed
310
    _ch_root_uid_setup(); /* Change user id and root if requested/possible */
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328

    stats_initialize(); /* We have to do this later on because of threading */

#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
329 330 331 332 333 334
	if (!_start_logging()) {
		fprintf(stderr, "FATAL: Could not start logging\n");
		_shutdown_subsystems();
		return 1;
	}

335 336 337
    /* Do this after logging init */
    slave_initialize();

Jack Moffitt's avatar
Jack Moffitt committed
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
	INFO0("icecast server started");

	/* REM 3D Graphics */

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

	INFO0("Shutting down");

	_stop_logging();

	_shutdown_subsystems();

	return 0;
}