2008-12-10 05:42:49 +01:00
|
|
|
/*
|
2010-02-28 01:08:10 +01:00
|
|
|
* This file is a part of Winix
|
2008-12-10 05:42:49 +01:00
|
|
|
* and is not publicly distributed
|
|
|
|
*
|
2010-02-28 22:33:06 +01:00
|
|
|
* Copyright (c) 2008-2010, Tomasz Sowa
|
2008-12-10 05:42:49 +01:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <ctime>
|
|
|
|
#include <iostream>
|
2010-11-23 22:52:25 +01:00
|
|
|
#include <sys/param.h>
|
2009-04-21 22:50:55 +02:00
|
|
|
|
2010-08-10 18:12:50 +02:00
|
|
|
#include "core/log.h"
|
|
|
|
#include "core/app.h"
|
|
|
|
#include "core/plugin.h"
|
|
|
|
|
|
|
|
|
2008-12-10 05:42:49 +01:00
|
|
|
Log log;
|
2010-09-18 02:51:12 +02:00
|
|
|
Log nlog; // notify log (used by a notification thread)
|
2009-11-13 21:40:11 +01:00
|
|
|
Plugin plugin;
|
2008-12-10 05:42:49 +01:00
|
|
|
|
|
|
|
|
2010-01-28 16:39:01 +01:00
|
|
|
|
2010-08-10 18:12:50 +02:00
|
|
|
/*
|
|
|
|
application object
|
|
|
|
*/
|
|
|
|
App app;
|
|
|
|
|
|
|
|
|
2008-12-10 05:42:49 +01:00
|
|
|
|
|
|
|
|
2008-12-11 03:46:16 +01:00
|
|
|
void print_syntax()
|
|
|
|
{
|
|
|
|
std::cout << "Syntax:" << std::endl;
|
2010-08-18 00:32:47 +02:00
|
|
|
std::cout << " winix config_file" << std::endl;
|
2008-12-11 03:46:16 +01:00
|
|
|
}
|
2008-12-10 05:42:49 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
2011-01-24 00:23:24 +01:00
|
|
|
|
2008-12-11 03:46:16 +01:00
|
|
|
int main(int argv, char ** argc)
|
|
|
|
{
|
|
|
|
std::srand(std::time(0));
|
|
|
|
|
|
|
|
if( argv != 2 )
|
|
|
|
{
|
|
|
|
print_syntax();
|
2008-12-10 05:42:49 +01:00
|
|
|
return 1;
|
2008-12-11 03:46:16 +01:00
|
|
|
}
|
|
|
|
|
2010-08-10 18:12:50 +02:00
|
|
|
app.system.system_start = time(0);
|
2010-11-21 01:19:17 +01:00
|
|
|
app.config.config_file = argc[1];
|
2008-12-11 03:46:16 +01:00
|
|
|
|
2010-08-10 18:12:50 +02:00
|
|
|
if( !app.config.ReadConfig(true, false) ) /* errors to stdout, stdout in not closed */
|
2008-12-11 03:46:16 +01:00
|
|
|
return 2;
|
|
|
|
|
2010-10-24 21:26:54 +02:00
|
|
|
if( app.stdout_is_closed || app.config.demonize )
|
2010-08-10 18:12:50 +02:00
|
|
|
app.config.log_stdout = false;
|
|
|
|
|
2008-12-11 03:46:16 +01:00
|
|
|
// closing descriptors only at the beginning
|
2009-01-26 21:49:28 +01:00
|
|
|
// !! temporary we do not close standard output for errors
|
|
|
|
// client postgresql uses it for reporting warnings (I don't know why)
|
|
|
|
//close(2);
|
2008-12-11 03:46:16 +01:00
|
|
|
|
2010-08-10 18:12:50 +02:00
|
|
|
if( !app.config.log_stdout )
|
2008-12-11 03:46:16 +01:00
|
|
|
{
|
|
|
|
close(1);
|
2010-08-10 18:12:50 +02:00
|
|
|
app.stdout_is_closed = true;
|
2008-12-11 03:46:16 +01:00
|
|
|
}
|
2008-12-10 05:42:49 +01:00
|
|
|
|
2011-01-05 22:24:11 +01:00
|
|
|
log.Init(app.config.log_level, app.config.log_save_each_line, app.config.log_file,
|
|
|
|
app.config.log_stdout, app.config.log_request);
|
2009-11-13 21:40:11 +01:00
|
|
|
|
2011-01-05 22:24:11 +01:00
|
|
|
nlog.Init(app.config.log_level, true, app.config.log_notify_file, false, 1);
|
|
|
|
|
|
|
|
log << log3 << "-- preparing to start winix --" << logend;
|
2010-11-23 22:52:25 +01:00
|
|
|
|
|
|
|
if( !app.InitFCGI() )
|
2011-01-05 22:24:11 +01:00
|
|
|
return false;
|
2010-11-23 22:52:25 +01:00
|
|
|
|
2010-10-24 19:49:38 +02:00
|
|
|
if( !app.DropPrivileges() )
|
|
|
|
return 3;
|
|
|
|
|
|
|
|
app.LogUserGroups();
|
|
|
|
|
2010-10-24 21:26:54 +02:00
|
|
|
if( app.config.demonize && !app.Demonize() )
|
|
|
|
return 4;
|
|
|
|
|
2010-08-10 18:12:50 +02:00
|
|
|
// app.config.base_server can be changed (stripped from 'http://' or a last slash)
|
2009-12-30 21:46:12 +01:00
|
|
|
// it is done when the config is read
|
2010-08-10 18:12:50 +02:00
|
|
|
log << log3 << "base_server: " << app.config.base_server << logend;
|
2009-12-30 21:46:12 +01:00
|
|
|
|
2010-08-10 18:12:50 +02:00
|
|
|
// load plugins before loading sessions - session_manager.LoadSessions()
|
2010-08-14 18:23:18 +02:00
|
|
|
// because some of the plugins can init its own sessions dates
|
2010-08-18 00:32:47 +02:00
|
|
|
plugin.LoadPlugins(app.config.plugins_dir, app.config.plugin_file);
|
2009-11-13 21:40:11 +01:00
|
|
|
|
2010-12-02 02:02:02 +01:00
|
|
|
// app.Init() starts other threads as well (they will be waiting on the lock)
|
2010-08-10 18:12:50 +02:00
|
|
|
if( !app.Init() )
|
2009-10-01 00:31:20 +02:00
|
|
|
return 1;
|
|
|
|
|
2010-10-11 22:42:49 +02:00
|
|
|
log.PutDate(log1);
|
2011-01-05 22:24:11 +01:00
|
|
|
log << "winix started" << logend << logsave;
|
2010-12-02 02:02:02 +01:00
|
|
|
|
|
|
|
app.StartThreads();
|
|
|
|
// now we have more threads, we should use Lock() and Unlock()
|
|
|
|
|
|
|
|
// main loop
|
2010-08-10 18:12:50 +02:00
|
|
|
app.Start();
|
2010-11-23 22:52:25 +01:00
|
|
|
|
2010-12-02 02:02:02 +01:00
|
|
|
app.Lock();
|
2010-08-10 18:12:50 +02:00
|
|
|
plugin.Call(WINIX_CLOSE);
|
|
|
|
app.Close();
|
2010-12-02 02:02:02 +01:00
|
|
|
app.Unlock();
|
|
|
|
|
|
|
|
app.WaitForThreads();
|
|
|
|
// now all others threads are terminated
|
2010-10-11 22:42:49 +02:00
|
|
|
|
|
|
|
log.PutDate(log1);
|
2011-01-05 22:24:11 +01:00
|
|
|
log << "winix stopped" << logend << logsave;
|
2008-12-10 05:42:49 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2010-11-23 22:52:25 +01:00
|
|
|
|