2001-09-09 22:21:46 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "thread.h"
|
|
|
|
#include "avl.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "sock.h"
|
|
|
|
#include "resolver.h"
|
|
|
|
#include "httpp.h"
|
|
|
|
|
2002-05-14 07:06:54 -04:00
|
|
|
#ifdef CHUID
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <grp.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#endif
|
2001-09-09 22:21:46 -04:00
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "sighandler.h"
|
|
|
|
|
|
|
|
#include "global.h"
|
|
|
|
#include "os.h"
|
|
|
|
#include "connection.h"
|
|
|
|
#include "refbuf.h"
|
|
|
|
#include "client.h"
|
2002-08-05 10:48:04 -04:00
|
|
|
#include "slave.h"
|
2001-09-09 22:21:46 -04:00
|
|
|
#include "stats.h"
|
|
|
|
#include "logging.h"
|
2002-08-13 08:46:45 -04:00
|
|
|
#include "xslt.h"
|
2001-09-09 22:21:46 -04:00
|
|
|
|
2001-10-20 02:43:04 -04:00
|
|
|
#ifdef _WIN32
|
|
|
|
#define snprintf _snprintf
|
2001-10-20 02:51:29 -04:00
|
|
|
#endif
|
2001-10-20 02:43:04 -04:00
|
|
|
|
2001-09-09 22:21:46 -04:00
|
|
|
#undef CATMODULE
|
|
|
|
#define CATMODULE "main"
|
|
|
|
|
2002-02-07 22:59:17 -05:00
|
|
|
static void _print_usage()
|
2001-09-09 22:21:46 -04:00
|
|
|
{
|
2002-02-07 22:59:17 -05:00
|
|
|
printf("Usage:\n");
|
|
|
|
printf("\ticecast -c <file>\t\tSpecify configuration file\n");
|
2001-09-09 22:21:46 -04:00
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
2002-02-07 22:59:17 -05:00
|
|
|
static void _initialize_subsystems(void)
|
2001-09-09 22:21:46 -04:00
|
|
|
{
|
|
|
|
log_initialize();
|
|
|
|
thread_initialize();
|
|
|
|
sock_initialize();
|
|
|
|
resolver_initialize();
|
|
|
|
config_initialize();
|
|
|
|
connection_initialize();
|
|
|
|
global_initialize();
|
|
|
|
refbuf_initialize();
|
2002-08-13 08:46:45 -04:00
|
|
|
xslt_initialize();
|
2001-09-09 22:21:46 -04:00
|
|
|
}
|
|
|
|
|
2002-02-07 22:59:17 -05:00
|
|
|
static void _shutdown_subsystems(void)
|
2001-09-09 22:21:46 -04:00
|
|
|
{
|
2002-08-13 08:46:45 -04:00
|
|
|
xslt_shutdown();
|
2001-09-09 22:21:46 -04:00
|
|
|
refbuf_shutdown();
|
|
|
|
stats_shutdown();
|
2002-08-05 10:48:04 -04:00
|
|
|
slave_shutdown();
|
2001-09-09 22:21:46 -04:00
|
|
|
global_shutdown();
|
|
|
|
connection_shutdown();
|
|
|
|
config_shutdown();
|
|
|
|
resolver_shutdown();
|
|
|
|
sock_shutdown();
|
|
|
|
thread_shutdown();
|
|
|
|
log_shutdown();
|
|
|
|
}
|
|
|
|
|
2002-02-07 22:59:17 -05:00
|
|
|
static int _parse_config_file(int argc, char **argv, char *filename, int size)
|
2001-09-09 22:21:46 -04:00
|
|
|
{
|
|
|
|
int i = 1;
|
|
|
|
|
2002-02-07 22:59:17 -05:00
|
|
|
if (argc < 3) return -1;
|
2001-09-09 22:21:46 -04:00
|
|
|
|
|
|
|
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++;
|
|
|
|
}
|
|
|
|
|
2002-02-07 22:59:17 -05:00
|
|
|
return -1;
|
2001-09-09 22:21:46 -04:00
|
|
|
}
|
|
|
|
|
2002-02-07 22:59:17 -05:00
|
|
|
static int _start_logging(void)
|
2001-09-09 22:21:46 -04:00
|
|
|
{
|
|
|
|
char fn_error[FILENAME_MAX];
|
|
|
|
char fn_access[FILENAME_MAX];
|
|
|
|
ice_config_t *config = config_get_config();
|
|
|
|
|
2002-05-20 20:05:31 -04:00
|
|
|
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);
|
|
|
|
}
|
2001-09-09 22:21:46 -04:00
|
|
|
|
2002-08-09 04:11:37 -04:00
|
|
|
log_set_level(errorlog, config->loglevel);
|
2001-09-09 22:21:46 -04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2002-02-07 22:59:17 -05:00
|
|
|
static void _stop_logging(void)
|
2001-09-09 22:21:46 -04:00
|
|
|
{
|
|
|
|
log_close(errorlog);
|
|
|
|
log_close(accesslog);
|
|
|
|
}
|
|
|
|
|
2002-02-07 22:59:17 -05:00
|
|
|
static int _setup_socket(void)
|
2001-09-09 22:21:46 -04:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2002-02-07 22:59:17 -05:00
|
|
|
static int _start_listening(void)
|
2001-09-09 22:21:46 -04:00
|
|
|
{
|
|
|
|
if (sock_listen(global.serversock, ICE_LISTEN_QUEUE) == SOCK_ERROR)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
sock_set_blocking(global.serversock, SOCK_NONBLOCK);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2002-05-14 07:06:54 -04:00
|
|
|
/* bind the socket and start listening */
|
2002-08-09 09:14:59 -04:00
|
|
|
static int _server_proc_init(void)
|
2001-09-09 22:21:46 -04:00
|
|
|
{
|
|
|
|
if (!_setup_socket()) {
|
2002-05-21 01:22:32 -04:00
|
|
|
fprintf(stderr, "Could not create listener socket on port %d\n", config_get_config()->port);
|
2002-08-09 09:14:59 -04:00
|
|
|
return 0;
|
2001-09-09 22:21:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!_start_listening()) {
|
2002-05-21 01:22:32 -04:00
|
|
|
fprintf(stderr, "Failed trying to listen on server socket\n");
|
2002-08-09 09:14:59 -04:00
|
|
|
return 0;
|
2001-09-09 22:21:46 -04:00
|
|
|
}
|
2002-08-09 09:14:59 -04:00
|
|
|
|
|
|
|
return 1;
|
2002-05-14 07:06:54 -04:00
|
|
|
}
|
2001-09-09 22:21:46 -04:00
|
|
|
|
2002-05-14 07:06:54 -04:00
|
|
|
/* this is the heart of the beast */
|
|
|
|
static void _server_proc(void)
|
|
|
|
{
|
2001-09-09 22:21:46 -04:00
|
|
|
connection_accept_loop();
|
|
|
|
|
|
|
|
sock_close(global.serversock);
|
|
|
|
}
|
|
|
|
|
2002-05-14 07:06:54 -04:00
|
|
|
/* chroot the process. Watch out - we need to do this before starting other
|
2002-05-20 09:25:31 -04:00
|
|
|
* threads. Change uid as well, after figuring out uid _first_ */
|
2002-05-14 07:06:54 -04:00
|
|
|
|
2002-07-23 11:15:11 -04:00
|
|
|
static void _ch_root_uid_setup(void)
|
2002-05-14 07:06:54 -04:00
|
|
|
{
|
|
|
|
ice_config_t *conf = config_get_config();
|
2002-05-20 09:25:31 -04:00
|
|
|
#ifdef CHUID
|
|
|
|
struct passwd *user;
|
|
|
|
struct group *group;
|
|
|
|
uid_t uid=-1;
|
|
|
|
gid_t gid=-1;
|
|
|
|
|
|
|
|
if(conf->chuid)
|
|
|
|
{
|
2002-05-20 20:05:31 -04:00
|
|
|
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);
|
2002-05-14 07:06:54 -04:00
|
|
|
|
2002-05-20 20:05:31 -04:00
|
|
|
if(group)
|
|
|
|
gid = group->gr_gid;
|
|
|
|
else
|
|
|
|
fprintf(stderr, "Couldn't find group \"%s\" in groups file\n", conf->group);
|
|
|
|
}
|
2002-05-20 09:25:31 -04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CHROOT
|
2002-05-14 07:06:54 -04:00
|
|
|
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
|
2002-05-20 09:25:31 -04:00
|
|
|
|
2002-05-14 07:06:54 -04:00
|
|
|
if(conf->chuid)
|
|
|
|
{
|
|
|
|
if(getuid()) /* root check */
|
|
|
|
{
|
|
|
|
fprintf(stderr, "WARNING: Can't change user id unless you are root.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2002-05-20 09:25:31 -04:00
|
|
|
if(gid != -1) {
|
|
|
|
if(!setgid(gid))
|
2002-05-20 20:05:31 -04:00
|
|
|
fprintf(stdout, "Changed groupid to %i.\n", gid);
|
2002-05-20 09:25:31 -04:00
|
|
|
else
|
|
|
|
fprintf(stdout, "Error changing groupid: %s.\n", strerror(errno));
|
|
|
|
}
|
2002-05-14 07:06:54 -04:00
|
|
|
|
2002-05-20 09:25:31 -04:00
|
|
|
if(uid != -1) {
|
|
|
|
if(!setuid(uid))
|
2002-05-20 20:05:31 -04:00
|
|
|
fprintf(stdout, "Changed userid to %i.\n", uid);
|
2002-05-20 09:25:31 -04:00
|
|
|
else
|
|
|
|
fprintf(stdout, "Error changing userid: %s.\n", strerror(errno));
|
|
|
|
}
|
2002-05-14 07:06:54 -04:00
|
|
|
}
|
|
|
|
#endif
|
2002-05-20 09:25:31 -04:00
|
|
|
}
|
2002-05-14 07:06:54 -04:00
|
|
|
|
2001-09-09 22:21:46 -04:00
|
|
|
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;
|
|
|
|
}
|
2002-07-31 11:00:12 -04:00
|
|
|
_shutdown_subsystems();
|
|
|
|
return 1;
|
2001-09-09 22:21:46 -04:00
|
|
|
}
|
|
|
|
} else if (res == -1) {
|
|
|
|
_print_usage();
|
|
|
|
_shutdown_subsystems();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* override config file options with commandline options */
|
|
|
|
config_parse_cmdline(argc, argv);
|
|
|
|
|
2002-08-09 09:14:59 -04:00
|
|
|
/* Bind socket, before we change userid */
|
|
|
|
if(!_server_proc_init()) {
|
|
|
|
fprintf(stderr, "Server startup failed. Exiting.\n");
|
|
|
|
_shutdown_subsystems();
|
|
|
|
return 1;
|
|
|
|
}
|
2002-05-14 07:06:54 -04:00
|
|
|
|
2002-07-23 11:15:11 -04:00
|
|
|
_ch_root_uid_setup(); /* Change user id and root if requested/possible */
|
2002-05-14 07:06:54 -04:00
|
|
|
|
|
|
|
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();
|
|
|
|
|
2001-09-09 22:21:46 -04:00
|
|
|
if (!_start_logging()) {
|
|
|
|
fprintf(stderr, "FATAL: Could not start logging\n");
|
|
|
|
_shutdown_subsystems();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2002-08-05 10:48:04 -04:00
|
|
|
/* Do this after logging init */
|
|
|
|
slave_initialize();
|
|
|
|
|
2001-09-09 22:21:46 -04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|