winix/main/main.cpp

172 lines
3.1 KiB
C++
Executable File

/*
* This file is a part of Winix
* and is not publicly distributed
*
* Copyright (c) 2008-2012, Tomasz Sowa
* All rights reserved.
*
*/
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <sys/param.h>
#include <fcntl.h>
#include "core/log.h"
#include "core/slog.h"
#include "core/app.h"
#include "core/plugin.h"
#include "core/version.h"
Log log;
Log nlog; // notify log (used by a notification thread)
SLog slog; // session logger
Plugin plugin;
/*
application object
*/
App app;
void print_syntax()
{
std::cout << "Syntax:" << std::endl;
std::cout << " winix config_file" << std::endl;
}
void CreateNewDescriptor(int des_dst, int flags)
{
int descriptor;
descriptor = open("/dev/null", flags | O_NOCTTY);
if( descriptor != -1 )
{
dup2(descriptor, des_dst);
if( descriptor != des_dst )
close(descriptor);
}
}
void CloseDescriptors()
{
close(0);
close(1);
close(2);
app.stdout_is_closed = true;
CreateNewDescriptor(0, O_RDONLY);
CreateNewDescriptor(1, O_WRONLY);
CreateNewDescriptor(2, O_WRONLY);
}
void LogInfo(LogManipulators log_level, const char * msg, bool put_version, const char * msg2)
{
log << log_level;
log.PrintDate(PT::Date(std::time(0)), app.config.log_time_zone_id);
log << ' ' << msg;
if( put_version )
{
log << ' ' << WINIX_VER_MAJOR
<< '.' << WINIX_VER_MINOR
<< '.' << WINIX_VER_REVISION;
}
log << ' ' << msg2 << logend;
}
int main(int argv, char ** argc)
{
std::srand(std::time(0));
if( argv != 2 )
{
print_syntax();
return 1;
}
app.system.system_start = time(0);
app.config.config_file = argc[1];
if( !app.config.ReadConfig(true, false) ) /* errors to stdout, stdout in not closed */
return 2;
if( app.stdout_is_closed || app.config.demonize )
app.config.log_stdout = false;
if( !app.config.log_stdout )
CloseDescriptors();
if( app.config.demonize && !app.Demonize() )
return 4;
log.SetTimeZones(&app.system.time_zones);
log.Init(app.config.log_level, app.config.log_save_each_line, app.config.log_file,
app.config.log_stdout, app.config.log_request);
nlog.Init(app.config.log_level, true, app.config.log_notify_file, false, 1);
LogInfo(log3, "booting Winix", true, "");
if( !app.InitFCGI() )
return 5;
if( !app.DropPrivileges() )
return 3;
app.LogUserGroups();
log << log3 << "base_url: " << app.config.base_url << logend;
// load plugins before loading sessions - session_manager.LoadSessions()
// because some of the plugins can init its own sessions dates
plugin.LoadPlugins(app.config.plugins_dir, app.config.plugin_file);
// app.Init() starts other threads as well (they will be waiting on the lock)
if( !app.Init() )
return 1;
app.StartThreads();
// now we have more threads, we should use Lock() and Unlock()
// saving all starting logs
app.Lock();
LogInfo(log1, "Winix", true, "started");
log << logsave;
app.Unlock();
// main loop
app.Start();
app.Lock();
plugin.Call((Session*)0, WINIX_CLOSE);
app.Close();
// now all sessions are cleared
app.Unlock();
app.WaitForThreads();
// now all others threads are terminated
LogInfo(log1, "Winix", true, "stopped");
return 0;
}