diff options
author | Adam Ierymenko <adam.ierymenko@zerotier.com> | 2014-02-21 23:49:24 -0800 |
---|---|---|
committer | Adam Ierymenko <adam.ierymenko@zerotier.com> | 2014-02-21 23:49:24 -0800 |
commit | 833a0621faeae6df4178b46346a8f8e0f1537ba1 (patch) | |
tree | 08e4aaaeb1978d5517f83a9d1d4a9921d6fa8a05 /windows/ZeroTierOne/ZeroTierOneService.cpp | |
parent | b5c3a92be230f32a80899907f2c62cb2c09f8f69 (diff) | |
download | infinitytier-833a0621faeae6df4178b46346a8f8e0f1537ba1.tar.gz infinitytier-833a0621faeae6df4178b46346a8f8e0f1537ba1.zip |
Windows service auto-update work.
Diffstat (limited to 'windows/ZeroTierOne/ZeroTierOneService.cpp')
-rw-r--r-- | windows/ZeroTierOne/ZeroTierOneService.cpp | 54 |
1 files changed, 53 insertions, 1 deletions
diff --git a/windows/ZeroTierOne/ZeroTierOneService.cpp b/windows/ZeroTierOne/ZeroTierOneService.cpp index 242bfb82..f2a83cbd 100644 --- a/windows/ZeroTierOne/ZeroTierOneService.cpp +++ b/windows/ZeroTierOne/ZeroTierOneService.cpp @@ -26,7 +26,14 @@ */ #pragma region Includes +#include <WinSock2.h> +#include <Windows.h> +#include <stdio.h> +#include <stdlib.h> + #include "ZeroTierOneService.h" +#include "../../node/Defaults.hpp" +#include "../../node/Utils.hpp" #pragma endregion ZeroTierOneService::ZeroTierOneService() : @@ -52,7 +59,52 @@ restart_node: } switch(_node->run()) { case ZeroTier::Node::NODE_RESTART_FOR_UPGRADE: { - } break; + // Shut down node + ZeroTier::Node *n; + { + ZeroTier::Mutex::Lock _l(_lock); + n = _node; + _node = (ZeroTier::Node *)0; + } + std::string msiPath; + const char *msiPathTmp = n->reasonForTermination(); + if (msiPathTmp) + msiPath = msiPathTmp; + delete n; + + // Write a batch file to ensure the service is stopped + if ((!msiPath.length())||(!ZeroTier::Utils::fileExists(msiPath.c_str()))) { + WriteEventLogEntry("auto-update failed: no msi path provided by Node",EVENTLOG_ERROR_TYPE); + Sleep(5000); + goto restart_node; + } + std::string bat(ZeroTier::ZT_DEFAULTS.defaultHomePath + "\\InstallAndRestartService.bat"); + FILE *batf = fopen(bat.c_str(),"wb"); + if (!batf) { + WriteEventLogEntry("auto-update failed: unable to create InstallAndRestartService.bat",EVENTLOG_ERROR_TYPE); + Sleep(5000); + goto restart_node; + } + fprintf(batf,"TIMEOUT.EXE /T 1 /NOBREAK\r\n"); + fprintf(batf,"NET.EXE STOP \"ZeroTier One\"\r\n"); + fprintf(batf,"MSIEXEC.EXE /i \"%s\" /qn\r\n",msiPath.c_str()); + fprintf(batf,"NET.EXE START \"ZeroTier One\"\r\n"); + fclose(batf); + + // Execute updater, which will update and restart service + STARTUPINFOA si; + PROCESS_INFORMATION pi; + memset(&si,0,sizeof(si)); + memset(&pi,0,sizeof(pi)); + if (!CreateProcessA(NULL,const_cast <LPSTR>((std::string("CMD.EXE /c \"") + bat + "\"").c_str()),NULL,NULL,FALSE,CREATE_NO_WINDOW|CREATE_NEW_PROCESS_GROUP,NULL,NULL,&si,&pi)) { + WriteEventLogEntry("auto-update failed: unable to execute InstallAndRestartService.bat",EVENTLOG_ERROR_TYPE); + Sleep(5000); + goto restart_node; + } + + // Terminate service to allow updater to update + Stop(); + } return; case ZeroTier::Node::NODE_UNRECOVERABLE_ERROR: { std::string err("ZeroTier node encountered an unrecoverable error: "); const char *r = _node->reasonForTermination(); |