winix/main/main.cpp

155 lines
2.7 KiB
C++
Executable File

/*
* This file is a part of Winix
* and is not publicly distributed
*
* Copyright (c) 2008-2011, 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"
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);
}
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();
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);
log << log3 << "-- preparing to start winix --" << logend;
if( !app.InitFCGI() )
return false;
if( !app.DropPrivileges() )
return 3;
app.LogUserGroups();
if( app.config.demonize && !app.Demonize() )
return 4;
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;
log.PutDate(log1);
log << "winix started" << logend;
app.StartThreads();
// now we have more threads, we should use Lock() and Unlock()
// saving all starting logs
app.Lock();
log << logsave;
app.Unlock();
// main loop
app.Start();
app.Lock();
plugin.Call(WINIX_CLOSE);
app.Close();
app.Unlock();
app.WaitForThreads();
// now all others threads are terminated
log.PutDate(log1);
log << "winix stopped" << logend << logsave;
return 0;
}