2016-01-20 00:00:00 +00:00
|
|
|
#include "Config.h"
|
2014-04-20 01:54:34 +00:00
|
|
|
#include "Daemon.h"
|
2014-04-22 22:07:21 +00:00
|
|
|
#include "util.h"
|
|
|
|
#include "Log.h"
|
2014-04-20 01:54:34 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
2016-03-10 06:12:46 +00:00
|
|
|
#include "Win32/Win32Service.h"
|
2016-03-09 19:41:14 +00:00
|
|
|
#include "Win32/Win32App.h"
|
2014-04-20 01:54:34 +00:00
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
|
|
|
namespace util
|
|
|
|
{
|
2014-04-22 22:07:21 +00:00
|
|
|
bool DaemonWin32::init(int argc, char* argv[])
|
2014-04-20 01:54:34 +00:00
|
|
|
{
|
|
|
|
setlocale(LC_CTYPE, "");
|
|
|
|
SetConsoleCP(1251);
|
|
|
|
SetConsoleOutputCP(1251);
|
2016-03-10 06:12:46 +00:00
|
|
|
|
|
|
|
if (!Daemon_Singleton::init(argc, argv))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
std::string serviceControl; i2p::config::GetOption("svcctl", serviceControl);
|
|
|
|
if (serviceControl == "install")
|
|
|
|
{
|
|
|
|
LogPrint(eLogInfo, "WinSVC: installing ", SERVICE_NAME, " as service");
|
|
|
|
InstallService(
|
|
|
|
SERVICE_NAME, // Name of service
|
|
|
|
SERVICE_DISPLAY_NAME, // Name to display
|
|
|
|
SERVICE_START_TYPE, // Service start type
|
|
|
|
SERVICE_DEPENDENCIES, // Dependencies
|
|
|
|
SERVICE_ACCOUNT, // Service running account
|
|
|
|
SERVICE_PASSWORD // Password of the account
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else if (serviceControl == "remove")
|
|
|
|
{
|
|
|
|
LogPrint(eLogInfo, "WinSVC: uninstalling ", SERVICE_NAME, " service");
|
|
|
|
UninstallService(SERVICE_NAME);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isDaemon == 1)
|
|
|
|
{
|
|
|
|
LogPrint(eLogDebug, "Daemon: running as service");
|
|
|
|
I2PService service(SERVICE_NAME);
|
|
|
|
if (!I2PService::Run(service))
|
|
|
|
{
|
|
|
|
LogPrint(eLogError, "Daemon: Service failed to run w/err 0x%08lx\n", GetLastError());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
LogPrint(eLogDebug, "Daemon: running as user");
|
|
|
|
|
|
|
|
return true;
|
2016-03-09 19:41:14 +00:00
|
|
|
}
|
2014-04-22 22:07:21 +00:00
|
|
|
|
|
|
|
bool DaemonWin32::start()
|
|
|
|
{
|
|
|
|
setlocale(LC_CTYPE, "");
|
|
|
|
SetConsoleCP(1251);
|
|
|
|
SetConsoleOutputCP(1251);
|
|
|
|
setlocale(LC_ALL, "Russian");
|
2016-03-09 19:41:14 +00:00
|
|
|
if (!i2p::win32::StartWin32App ()) return false;
|
2016-03-10 17:23:17 +00:00
|
|
|
|
|
|
|
// override log
|
|
|
|
i2p::config::SetOption("log", std::string ("file"));
|
2016-03-06 02:46:01 +00:00
|
|
|
bool ret = Daemon_Singleton::start();
|
|
|
|
if (ret && IsLogToFile ())
|
2016-03-09 19:41:14 +00:00
|
|
|
{
|
2016-03-06 02:46:01 +00:00
|
|
|
// TODO: find out where this garbage to console comes from
|
|
|
|
SetStdHandle(STD_OUTPUT_HANDLE, INVALID_HANDLE_VALUE);
|
|
|
|
SetStdHandle(STD_ERROR_HANDLE, INVALID_HANDLE_VALUE);
|
|
|
|
}
|
|
|
|
return ret;
|
2014-04-20 01:54:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool DaemonWin32::stop()
|
2016-03-09 19:41:14 +00:00
|
|
|
{
|
|
|
|
i2p::win32::StopWin32App ();
|
2014-04-20 01:54:34 +00:00
|
|
|
return Daemon_Singleton::stop();
|
2016-03-09 19:41:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DaemonWin32::run ()
|
|
|
|
{
|
|
|
|
i2p::win32::RunWin32App ();
|
|
|
|
}
|
2014-04-20 01:54:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-18 12:27:47 +00:00
|
|
|
#endif
|