171 lines
3.4 KiB
C++
Executable File
171 lines
3.4 KiB
C++
Executable File
/*
|
|
* This file is a part of Winix
|
|
* and is not publicly distributed
|
|
*
|
|
* Copyright (c) 2008-2010, Tomasz Sowa
|
|
* All rights reserved.
|
|
*
|
|
*/
|
|
|
|
#include <cstdlib>
|
|
#include <ctime>
|
|
#include <signal.h>
|
|
#include <iostream>
|
|
#include <sys/param.h>
|
|
#include <cstdio>
|
|
#include <fetch.h>
|
|
|
|
#include "core/log.h"
|
|
#include "core/app.h"
|
|
#include "core/plugin.h"
|
|
|
|
|
|
Log log;
|
|
Log nlog; // notify log (used by a notification thread)
|
|
Plugin plugin;
|
|
|
|
|
|
|
|
/*
|
|
application object
|
|
*/
|
|
App app;
|
|
|
|
|
|
static std::string url_to_fetch_on_exit;
|
|
|
|
|
|
void fetch_page_on_exit()
|
|
{
|
|
// stupid trick to break FCGX_Accept_r() function
|
|
// even with FCGX_InitRequest(..., ..., FCGI_FAIL_ACCEPT_ON_INTR) the FCGX_Accept_r
|
|
// doesn't want to break on a signal
|
|
// so we request one page from the server for exiting from FCGX_Accept_r
|
|
|
|
FILE * f = fetchGetURL(url_to_fetch_on_exit.c_str(), "");
|
|
|
|
if( f )
|
|
fclose(f);
|
|
}
|
|
|
|
|
|
void signal_term(int)
|
|
{
|
|
FCGX_ShutdownPending();
|
|
app.WasStopSignal();
|
|
fetch_page_on_exit();
|
|
}
|
|
|
|
|
|
|
|
void print_syntax()
|
|
{
|
|
std::cout << "Syntax:" << std::endl;
|
|
std::cout << " winix config_file" << std::endl;
|
|
}
|
|
|
|
|
|
|
|
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];
|
|
//app.config.config_file = "/home/tomek/roboczy/slimaczek.pl/slimaczek.conf.loc";
|
|
|
|
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;
|
|
|
|
// closing descriptors only at the beginning
|
|
// !! temporary we do not close standard output for errors
|
|
// client postgresql uses it for reporting warnings (I don't know why)
|
|
//close(2);
|
|
|
|
if( !app.config.log_stdout )
|
|
{
|
|
close(1);
|
|
app.stdout_is_closed = true;
|
|
}
|
|
|
|
log.Init(app.config.log_level, app.config.log_file, app.config.log_stdout, app.config.log_request);
|
|
nlog.Init(app.config.log_level, app.config.log_notify_file, false, 1);
|
|
|
|
log << log3 << "-- preparing to start winix --" << logend << logsavenow;
|
|
|
|
if( !app.InitFCGI() )
|
|
return false; // !! dodac logsave do logow
|
|
|
|
if( !app.DropPrivileges() )
|
|
return 3;
|
|
|
|
app.LogUserGroups();
|
|
|
|
if( app.config.demonize && !app.Demonize() )
|
|
return 4;
|
|
|
|
// app.config.base_server can be changed (stripped from 'http://' or a last slash)
|
|
// it is done when the config is read
|
|
log << log3 << "base_server: " << app.config.base_server << 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);
|
|
|
|
if( !app.Init() )
|
|
return 1;
|
|
|
|
|
|
|
|
|
|
|
|
// ----
|
|
/*
|
|
struct sigaction act, old_act;
|
|
|
|
sigemptyset(&act.sa_mask);
|
|
act.sa_flags = 0;
|
|
act.sa_handler = signal_term;
|
|
sigaction(SIGINT, &act, &old_act);
|
|
|
|
sigemptyset(&act.sa_mask);
|
|
act.sa_flags = 0;
|
|
act.sa_handler = signal_term;
|
|
sigaction(SIGTERM, &act, &old_act);
|
|
*/
|
|
// ----
|
|
|
|
Ezc::WideToUTF8(app.config.base_url, url_to_fetch_on_exit);
|
|
|
|
signal(SIGTERM, signal_term);
|
|
signal(SIGINT, signal_term);
|
|
|
|
log.PutDate(log1);
|
|
log << "winix started" << logend << logsavenow;
|
|
|
|
// !! wywalic to sprawdzanie
|
|
//log << log2 << "checking for table consistency:" << logend;
|
|
// !! zrobic wyjatek dla root
|
|
//app.db.CheckAllUrlSubject();
|
|
|
|
app.Start();
|
|
|
|
plugin.Call(WINIX_CLOSE);
|
|
app.Close();
|
|
|
|
log.PutDate(log1);
|
|
log << "winix stopped" << logend << logsavenow;
|
|
|
|
return 0;
|
|
}
|
|
|