@ -5,7 +5,7 @@
*/
/*
* Copyright ( c ) 2008 - 2018 , Tomasz Sowa
* Copyright ( c ) 2008 - 2022 , Tomasz Sowa
* All rights reserved .
*
* Redistribution and use in source and binary forms , with or without
@ -43,7 +43,7 @@
# include "core/app.h"
# include "core/version.h"
# include "utf8/utf8.h"
# include "mainoptions/mainoptionsparser.h"
@ -59,11 +59,19 @@ App app;
void print_syntax ( )
{
std : : cout < < " Syntax: " < < std : : endl ;
std : : cout < < " winix config_file " < < std : : endl ;
std : : cout < < " Winix version " < < WINIX_VER_MAJOR < < ' . ' < < WINIX_VER_MINOR < < ' . ' < < WINIX_VER_REVISION < < std : : endl ;
std : : cout < < " Copyright (c) 2008-2022, Tomasz Sowa " < < std : : endl ;
std : : cout < < " Usage: winix [options] " < < std : : endl ;
std : : cout < < " -c : a path to the config file " < < std : : endl ;
std : : cout < < " --config : a path to the config file " < < std : : endl ;
std : : cout < < " -h : print usage information " < < std : : endl ;
std : : cout < < " --help : print usage information " < < std : : endl ;
std : : cout < < " -? : print usage information " < < std : : endl ;
std : : cout < < " At least one -c or --config parameter is required, use -c or --config option " < < std : : endl ;
std : : cout < < " multiple times to load more than one config. " < < std : : endl ;
}
@ -148,34 +156,107 @@ void RemovePidFile()
}
bool ReadConfigs ( const pt : : Space : : TableType & table , size_t & config_read )
{
bool status = true ;
for ( const pt : : Space * config : table )
{
if ( config - > is_table ( ) & & config - > table_size ( ) = = 1 )
{
std : : wstring config_file = config - > value . value_table [ 0 ] - > to_wstr ( ) ;
config_read + = 1 ;
if ( ! app . config . ReadConfig ( config_file ) )
{
status = false ;
break ;
}
}
}
return status ;
}
bool ReadConfigs ( const pt : : Space & options )
{
size_t config_read = 0 ;
bool status = true ;
const pt : : Space : : TableType * table = options . get_table ( L " c " ) ;
app . config . space . clear ( ) ;
if ( table )
{
status = status & & ReadConfigs ( * table , config_read ) ;
}
table = options . get_table ( L " config " ) ;
if ( table )
{
status = status & & ReadConfigs ( * table , config_read ) ;
}
if ( config_read = = 0 )
{
std : : cout < < " You have to provide a config file with c parameter " < < std : : endl ;
Winix : : print_syntax ( ) ;
return false ;
}
return status ;
}
} // namespace Winix
int main ( int argv , char * * argc )
int main ( int argc , const char * * argv )
{
using Winix : : app ;
std : : srand ( std : : time ( 0 ) ) ;
app . system . system_start = time ( 0 ) ;
if ( argv ! = 2 )
pt : : Space options ;
pt : : Space arguments ;
pt : : MainOptionsParser options_parser ;
arguments . add ( L " c " , 1 ) ;
arguments . add ( L " config " , 1 ) ;
pt : : MainOptionsParser : : Status status = options_parser . parse ( argc , argv , options , arguments ) ;
if ( status ! = pt : : MainOptionsParser : : status_ok )
{
Winix : : print_syntax ( ) ;
return 1 ;
return 6 ;
}
app . system . system_start = time ( 0 ) ;
if ( ! pt : : utf8_to_wide ( argc [ 1 ] , app . config . config_file ) )
if ( options . has_key ( L " h " ) | | options . has_key ( L " help " ) | | options . has_key ( L " ? " ) )
{
std : : wcout < < " An incorrect UTF-8 path of the config file " < < std : : endl ;
return 6 ;
Winix : : print_syntax ( ) ;
return 0 ;
}
if ( ! app . config . ReadConfig ( true , false ) ) /* errors to stdout, stdout in not closed */
Winix : : Log & log = app . GetMainLog ( ) ;
Winix : : LogInfo ( log , Winix : : log3 , " UTC booting Winix " , true , " " ) ; // date will be printed as UTC because the time zones are not loaded yet
if ( ! Winix : : ReadConfigs ( options ) )
{
// we need to print the buffer by hand because the logger
// is not fully initialized yet
pt : : WTextStream * buffer = log . get_log_buffer ( ) ;
if ( buffer & & ! buffer - > empty ( ) )
{
pt : : wide_stream_to_utf8 ( * buffer , std : : cout ) ;
}
return 2 ;
}
Winix : : Log & log = app . GetMainLog ( ) ;
app . InitLoggers ( ) ;
if ( app . stdout_is_closed | | app . config . demonize )
@ -185,15 +266,22 @@ using Winix::app;
Winix : : CloseDescriptors ( ) ;
if ( app . config . demonize & & ! app . Demonize ( ) )
{
log < < Winix : : logsave ;
return 4 ;
Winix : : LogInfo ( log , Winix : : log3 , " booting Winix " , true , " " ) ;
}
if ( ! app . InitFCGI ( ) )
{
log < < Winix : : logsave ;
return 5 ;
}
if ( ! app . DropPrivileges ( ) )
{
log < < Winix : : logsave ;
return 3 ;
}
app . LogUserGroups ( ) ;
Winix : : SavePidFile ( log ) ;