main.c 4.21 KB
Newer Older
Jack Moffitt's avatar
Jack Moffitt committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#include <stdio.h>
#include <string.h>

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


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

#include "global.h"
#include "os.h"
#include "connection.h"
#include "refbuf.h"
#include "client.h"
#include "stats.h"
#include "logging.h"

23 24
#ifdef _WIN32
#define snprintf _snprintf
25
#endif
26

Jack Moffitt's avatar
Jack Moffitt committed
27 28 29
#undef CATMODULE
#define CATMODULE "main"

30
static void _print_usage()
Jack Moffitt's avatar
Jack Moffitt committed
31
{
32 33
	printf("Usage:\n");
	printf("\ticecast -c <file>\t\tSpecify configuration file\n");
Jack Moffitt's avatar
Jack Moffitt committed
34 35 36
	printf("\n");
}

37
static void _initialize_subsystems(void)
Jack Moffitt's avatar
Jack Moffitt committed
38 39 40 41 42 43 44 45 46 47 48 49
{
	log_initialize();
	thread_initialize();
	sock_initialize();
	resolver_initialize();
	config_initialize();
	connection_initialize();
	global_initialize();
	stats_initialize();
	refbuf_initialize();
}

50
static void _shutdown_subsystems(void)
Jack Moffitt's avatar
Jack Moffitt committed
51 52 53 54 55 56 57 58 59 60 61 62
{
	refbuf_shutdown();
	stats_shutdown();
	global_shutdown();
	connection_shutdown();
	config_shutdown();
	resolver_shutdown();
	sock_shutdown();
	thread_shutdown();
	log_shutdown();
}

63
static int _parse_config_file(int argc, char **argv, char *filename, int size)
Jack Moffitt's avatar
Jack Moffitt committed
64 65 66
{
	int i = 1;

67
	if (argc < 3) return -1;
Jack Moffitt's avatar
Jack Moffitt committed
68 69 70 71 72 73 74 75 76 77 78 79 80

	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++;
	}

81
	return -1;
Jack Moffitt's avatar
Jack Moffitt committed
82 83
}

84
static int _start_logging(void)
Jack Moffitt's avatar
Jack Moffitt committed
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
{
	char fn_error[FILENAME_MAX];
	char fn_access[FILENAME_MAX];
	ice_config_t *config = config_get_config();

	snprintf(fn_error, FILENAME_MAX, "%s%s%s", config->log_dir, PATH_SEPARATOR, config->error_log);
	snprintf(fn_access, FILENAME_MAX, "%s%s%s", config->log_dir, PATH_SEPARATOR, config->access_log);

	errorlog = log_open(fn_error);
	accesslog = log_open(fn_access);
	
	log_set_level(errorlog, 4);
	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;
}

109
static void _stop_logging(void)
Jack Moffitt's avatar
Jack Moffitt committed
110 111 112 113 114
{
	log_close(errorlog);
	log_close(accesslog);
}

115
static int _setup_socket(void)
Jack Moffitt's avatar
Jack Moffitt committed
116 117 118 119 120 121 122 123 124 125 126 127
{
	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;
}

128
static int _start_listening(void)
Jack Moffitt's avatar
Jack Moffitt committed
129 130 131 132 133 134 135 136 137 138
{
	if (sock_listen(global.serversock, ICE_LISTEN_QUEUE) == SOCK_ERROR)
		return 0;

	sock_set_blocking(global.serversock, SOCK_NONBLOCK);

	return 1;
}

/* this is the heart of the beast */
139
static void _server_proc(void)
Jack Moffitt's avatar
Jack Moffitt committed
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
{
	if (!_setup_socket()) {
		ERROR1("Could not create listener socket on port %d", config_get_config()->port);
		return;
	}

	if (!_start_listening()) {
		ERROR0("Failed trying to listen on server socket");
		return;
	}

	connection_accept_loop();

	sock_close(global.serversock);
}

int main(int argc, char **argv)
{
	int res, ret;
	char filename[256];

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

	/* setup the default signal handlers */
	sighandler_initialize();

	/* 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;
			}
		}
	} else if (res == -1) {
		_print_usage();
		_shutdown_subsystems();
		return 1;
	}
	
	/* override config file options with commandline options */
	config_parse_cmdline(argc, argv);

	if (!_start_logging()) {
		fprintf(stderr, "FATAL: Could not start logging\n");
		_shutdown_subsystems();
		return 1;
	}

	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;
}