Bring Windows service code up to date and into sync.

This commit is contained in:
Adam Ierymenko 2015-04-24 15:44:39 -07:00
parent 0e31551349
commit e2c65bf16a
5 changed files with 109 additions and 28 deletions

11
one.cpp
View File

@ -32,6 +32,8 @@
#include <time.h> #include <time.h>
#include <errno.h> #include <errno.h>
#include "node/Constants.hpp"
#ifdef __WINDOWS__ #ifdef __WINDOWS__
#include <WinSock2.h> #include <WinSock2.h>
#include <Windows.h> #include <Windows.h>
@ -60,7 +62,6 @@
#include "ext/json-parser/json.h" #include "ext/json-parser/json.h"
#include "node/Constants.hpp"
#include "node/Identity.hpp" #include "node/Identity.hpp"
#include "node/CertificateOfMembership.hpp" #include "node/CertificateOfMembership.hpp"
#include "node/Utils.hpp" #include "node/Utils.hpp"
@ -499,7 +500,7 @@ static int cli(int argc,char **argv)
cliPrintHelp(argv[0],stderr); cliPrintHelp(argv[0],stderr);
return 2; return 2;
} }
unsigned int scode = Http::DELETE( unsigned int scode = Http::DEL(
1024 * 1024 * 16, 1024 * 1024 * 16,
60000, 60000,
(const struct sockaddr *)&addr, (const struct sockaddr *)&addr,
@ -731,9 +732,9 @@ static BOOL WINAPI _winConsoleCtrlHandler(DWORD dwCtrlType)
case CTRL_BREAK_EVENT: case CTRL_BREAK_EVENT:
case CTRL_CLOSE_EVENT: case CTRL_CLOSE_EVENT:
case CTRL_SHUTDOWN_EVENT: case CTRL_SHUTDOWN_EVENT:
Node *n = node; OneService *s = zt1Service;
if (n) if (s)
n->terminate(Node::NODE_NORMAL_TERMINATION,"terminated by signal"); s->terminate();
return TRUE; return TRUE;
} }
return FALSE; return FALSE;

View File

@ -43,12 +43,21 @@
<ClCompile Include="..\..\node\Switch.cpp" /> <ClCompile Include="..\..\node\Switch.cpp" />
<ClCompile Include="..\..\node\Topology.cpp" /> <ClCompile Include="..\..\node\Topology.cpp" />
<ClCompile Include="..\..\node\Utils.cpp" /> <ClCompile Include="..\..\node\Utils.cpp" />
<ClCompile Include="..\..\one.cpp" />
<ClCompile Include="..\..\osdep\Http.cpp" /> <ClCompile Include="..\..\osdep\Http.cpp" />
<ClCompile Include="..\..\osdep\OSUtils.cpp" /> <ClCompile Include="..\..\osdep\OSUtils.cpp" />
<ClCompile Include="..\..\osdep\WindowsEthernetTap.cpp" /> <ClCompile Include="..\..\osdep\WindowsEthernetTap.cpp" />
<ClCompile Include="..\..\selftest.cpp" /> <ClCompile Include="..\..\selftest.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="..\..\service\ControlPlane.cpp" /> <ClCompile Include="..\..\service\ControlPlane.cpp" />
<ClCompile Include="..\..\service\OneService.cpp" /> <ClCompile Include="..\..\service\OneService.cpp" />
<ClCompile Include="ServiceBase.cpp" />
<ClCompile Include="ServiceInstaller.cpp" />
<ClCompile Include="ZeroTierOneService.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="..\..\ext\http-parser\http_parser.h" /> <ClInclude Include="..\..\ext\http-parser\http_parser.h" />
@ -102,6 +111,9 @@
<ClInclude Include="..\..\service\OneService.hpp" /> <ClInclude Include="..\..\service\OneService.hpp" />
<ClInclude Include="..\..\version.h" /> <ClInclude Include="..\..\version.h" />
<ClInclude Include="resource.h" /> <ClInclude Include="resource.h" />
<ClInclude Include="ServiceBase.h" />
<ClInclude Include="ServiceInstaller.h" />
<ClInclude Include="ZeroTierOneService.h" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ResourceCompile Include="ZeroTierOne.rc" /> <ResourceCompile Include="ZeroTierOne.rc" />

View File

@ -58,6 +58,18 @@
<Filter Include="Source Files\ext\json-parser"> <Filter Include="Source Files\ext\json-parser">
<UniqueIdentifier>{da28e961-1761-41d8-9a59-65b00dfb1302}</UniqueIdentifier> <UniqueIdentifier>{da28e961-1761-41d8-9a59-65b00dfb1302}</UniqueIdentifier>
</Filter> </Filter>
<Filter Include="Source Files\windows">
<UniqueIdentifier>{43f75f84-c70d-4d44-a0ef-28a7a399abd4}</UniqueIdentifier>
</Filter>
<Filter Include="Source Files\windows\ZeroTierOne">
<UniqueIdentifier>{0da07a2f-8922-4827-ac51-29ca3f30f881}</UniqueIdentifier>
</Filter>
<Filter Include="Header Files\windows">
<UniqueIdentifier>{b74916eb-bb6c-4449-a2a2-fa0b17f60121}</UniqueIdentifier>
</Filter>
<Filter Include="Header Files\windows\ZeroTierOne">
<UniqueIdentifier>{bf604491-14c4-4a74-81a6-6105d07c5c7c}</UniqueIdentifier>
</Filter>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="..\..\service\ControlPlane.cpp"> <ClCompile Include="..\..\service\ControlPlane.cpp">
@ -150,6 +162,18 @@
<ClCompile Include="..\..\ext\json-parser\json.c"> <ClCompile Include="..\..\ext\json-parser\json.c">
<Filter>Source Files\ext\json-parser</Filter> <Filter>Source Files\ext\json-parser</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\..\one.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="ServiceBase.cpp">
<Filter>Source Files\windows\ZeroTierOne</Filter>
</ClCompile>
<ClCompile Include="ServiceInstaller.cpp">
<Filter>Source Files\windows\ZeroTierOne</Filter>
</ClCompile>
<ClCompile Include="ZeroTierOneService.cpp">
<Filter>Source Files\windows\ZeroTierOne</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="resource.h"> <ClInclude Include="resource.h">
@ -305,6 +329,15 @@
<ClInclude Include="..\..\ext\http-parser\http_parser.h"> <ClInclude Include="..\..\ext\http-parser\http_parser.h">
<Filter>Header Files\ext\http-parser</Filter> <Filter>Header Files\ext\http-parser</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="ServiceBase.h">
<Filter>Header Files\windows\ZeroTierOne</Filter>
</ClInclude>
<ClInclude Include="ServiceInstaller.h">
<Filter>Header Files\windows\ZeroTierOne</Filter>
</ClInclude>
<ClInclude Include="ZeroTierOneService.h">
<Filter>Header Files\windows\ZeroTierOne</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ResourceCompile Include="ZeroTierOne.rc"> <ResourceCompile Include="ZeroTierOne.rc">

View File

@ -34,15 +34,13 @@
#include "ZeroTierOneService.h" #include "ZeroTierOneService.h"
#include "../../node/Defaults.hpp" #include "../../version.h"
#include "../../include/ZeroTierOne.h"
#include "../../node/Constants.hpp"
#include "../../node/Utils.hpp" #include "../../node/Utils.hpp"
#include "../../osdep/OSUtils.hpp"
#include "../../control/NodeControlClient.hpp" #include "../../service/OneService.hpp"
#include "../../control/NodeControlService.hpp"
#include "../../osdep/WindowsEthernetTapFactory.hpp"
#include "../../osdep/WindowsRoutingTable.hpp"
#include "../../osdep/NativeSocketManager.hpp"
#pragma endregion // Includes #pragma endregion // Includes
@ -53,7 +51,7 @@ ZeroTier::Mutex SVCDBGfile_m;
ZeroTierOneService::ZeroTierOneService() : ZeroTierOneService::ZeroTierOneService() :
CServiceBase(ZT_SERVICE_NAME,TRUE,TRUE,FALSE), CServiceBase(ZT_SERVICE_NAME,TRUE,TRUE,FALSE),
_node((ZeroTier::Node *)0) _service((ZeroTier::OneService *)0)
{ {
#ifdef ZT_DEBUG_SERVICE #ifdef ZT_DEBUG_SERVICE
SVCDBGfile_m.lock(); SVCDBGfile_m.lock();
@ -86,6 +84,41 @@ void ZeroTierOneService::threadMain()
restart_node: restart_node:
try { try {
{
ZeroTier::Mutex::Lock _l(_lock);
delete _service;
_service = (ZeroTier::OneService *)0; // in case newInstance() fails
_service = ZeroTier::OneService::newInstance(
ZeroTier::OneService::platformDefaultHomePath().c_str(),
ZT1_DEFAULT_PORT);
}
switch(_service->run()) {
case ZeroTier::OneService::ONE_UNRECOVERABLE_ERROR: {
std::string err("ZeroTier One encountered an unrecoverable error: ");
err.append(_service->fatalErrorMessage());
err.append(" (restarting in 5 seconds)");
WriteEventLogEntry(const_cast <PSTR>(err.c_str()),EVENTLOG_ERROR_TYPE);
Sleep(5000);
} goto restart_node;
case ZeroTier::OneService::ONE_IDENTITY_COLLISION: {
std::string homeDir(ZeroTier::OneService::platformDefaultHomePath());
delete _service;
_service = (ZeroTier::OneService *)0;
std::string oldid;
ZeroTier::OSUtils::readFile((homeDir + ZT_PATH_SEPARATOR_S + "identity.secret").c_str(),oldid);
if (oldid.length()) {
ZeroTier::OSUtils::writeFile((homeDir + ZT_PATH_SEPARATOR_S + "identity.secret.saved_after_collision").c_str(),oldid);
ZeroTier::OSUtils::rm((homeDir + ZT_PATH_SEPARATOR_S + "identity.secret").c_str());
ZeroTier::OSUtils::rm((homeDir + ZT_PATH_SEPARATOR_S + "identity.public").c_str());
}
} goto restart_node;
default: // normal termination
break;
}
#if 0
std::string authToken(ZeroTier::NodeControlClient::getAuthToken((ZeroTier::ZT_DEFAULTS.defaultHomePath + ZT_PATH_SEPARATOR_S + "authtoken.secret").c_str(),true)); std::string authToken(ZeroTier::NodeControlClient::getAuthToken((ZeroTier::ZT_DEFAULTS.defaultHomePath + ZT_PATH_SEPARATOR_S + "authtoken.secret").c_str(),true));
ZeroTier::WindowsEthernetTapFactory tapFactory(ZeroTier::ZT_DEFAULTS.defaultHomePath.c_str()); ZeroTier::WindowsEthernetTapFactory tapFactory(ZeroTier::ZT_DEFAULTS.defaultHomePath.c_str());
@ -154,6 +187,7 @@ restart_node:
break; break;
} }
#endif
} catch ( ... ) { } catch ( ... ) {
// sanity check, shouldn't happen since Node::run() should catch all its own errors // sanity check, shouldn't happen since Node::run() should catch all its own errors
// could also happen if we're out of memory though! // could also happen if we're out of memory though!
@ -164,17 +198,19 @@ restart_node:
{ {
ZeroTier::Mutex::Lock _l(_lock); ZeroTier::Mutex::Lock _l(_lock);
delete _node; delete _service;
_node = (ZeroTier::Node *)0; _service = (ZeroTier::OneService *)0;
} }
} }
bool ZeroTierOneService::doStartUpgrade(const std::string &msiPath) bool ZeroTierOneService::doStartUpgrade(const std::string &msiPath)
{ {
std::string msiLog(ZeroTier::ZT_DEFAULTS.defaultHomePath + "\\LastUpdateLog.txt"); std::string homePath(ZeroTier::OneService::platformDefaultHomePath());
ZeroTier::Utils::rm(msiLog);
std::string bat(ZeroTier::ZT_DEFAULTS.defaultHomePath + "\\InstallAndRestartService.bat"); std::string msiLog(homePath + "\\LastUpdateLog.txt");
ZeroTier::OSUtils::rm(msiLog);
std::string bat(homePath + "\\InstallAndRestartService.bat");
FILE *batf = fopen(bat.c_str(),"wb"); FILE *batf = fopen(bat.c_str(),"wb");
if (!batf) if (!batf)
return false; return false;
@ -210,10 +246,11 @@ void ZeroTierOneService::OnStop()
ZT_SVCDBG("ZeroTierOneService::OnStop()\r\n"); ZT_SVCDBG("ZeroTierOneService::OnStop()\r\n");
_lock.lock(); _lock.lock();
ZeroTier::Node *n = _node; ZeroTier::OneService *s = _service;
_lock.unlock(); _lock.unlock();
if (n) {
n->terminate(ZeroTier::Node::NODE_NORMAL_TERMINATION,"Windows service stopped"); if (s) {
s->terminate();
ZeroTier::Thread::join(_thread); ZeroTier::Thread::join(_thread);
} }
} }

View File

@ -33,11 +33,9 @@
#include <string> #include <string>
#include "../../node/Node.hpp"
#include "../../node/Defaults.hpp"
#include "../../node/Thread.hpp"
#include "../../node/Mutex.hpp" #include "../../node/Mutex.hpp"
#include "../../node/Utils.hpp" #include "../../osdep/Thread.hpp"
#include "../../service/OneService.hpp"
// Uncomment to make debugging Windows services suck slightly less hard. // Uncomment to make debugging Windows services suck slightly less hard.
//#define ZT_DEBUG_SERVICE "C:\\ZeroTierOneServiceDebugLog.txt" //#define ZT_DEBUG_SERVICE "C:\\ZeroTierOneServiceDebugLog.txt"
@ -79,7 +77,7 @@ protected:
virtual void OnShutdown(); virtual void OnShutdown();
private: private:
ZeroTier::Node *volatile _node; ZeroTier::OneService *volatile _service;
ZeroTier::Mutex _lock; ZeroTier::Mutex _lock;
ZeroTier::Thread _thread; ZeroTier::Thread _thread;
}; };