From 2203958798ca151cfde348254d8d18a5ca471e31 Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Wed, 26 Feb 2014 14:37:21 -0800 Subject: Windows auto-updater invocation works... time to try an installer! --- windows/ZeroTierOne/ZeroTierOneService.cpp | 55 +++++++++++++++++------------- windows/ZeroTierOne/ZeroTierOneService.h | 5 +++ 2 files changed, 37 insertions(+), 23 deletions(-) (limited to 'windows') diff --git a/windows/ZeroTierOne/ZeroTierOneService.cpp b/windows/ZeroTierOne/ZeroTierOneService.cpp index f2a83cbd..daff0bb0 100644 --- a/windows/ZeroTierOne/ZeroTierOneService.cpp +++ b/windows/ZeroTierOne/ZeroTierOneService.cpp @@ -67,40 +67,24 @@ restart_node: _node = (ZeroTier::Node *)0; } std::string msiPath; - const char *msiPathTmp = n->reasonForTermination(); - if (msiPathTmp) - msiPath = msiPathTmp; + if (n) { + 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) { + + if (!doStartUpgrade(msiPath)) { 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 ((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(); @@ -133,6 +117,31 @@ restart_node: _lock.unlock(); } +bool ZeroTierOneService::doStartUpgrade(const std::string &msiPath) +{ + std::string msiLog(ZeroTier::ZT_DEFAULTS.defaultHomePath + "\\LastUpdateLog.txt"); + ZeroTier::Utils::rm(msiLog); + + std::string bat(ZeroTier::ZT_DEFAULTS.defaultHomePath + "\\InstallAndRestartService.bat"); + FILE *batf = fopen(bat.c_str(),"wb"); + if (!batf) + return false; + fprintf(batf,"TIMEOUT.EXE /T 1 /NOBREAK\r\n"); + fprintf(batf,"NET.EXE STOP \"ZeroTier One\"\r\n"); + fprintf(batf,"MSIEXEC.EXE /i \"%s\" /l* \"%s\" /qn\r\n",msiPath.c_str(),msiLog.c_str()); + fprintf(batf,"NET.EXE START \"ZeroTier One\"\r\n"); + fclose(batf); + + STARTUPINFOA si; + PROCESS_INFORMATION pi; + memset(&si,0,sizeof(si)); + memset(&pi,0,sizeof(pi)); + if (!CreateProcessA(NULL,const_cast ((std::string("CMD.EXE /c \"") + bat + "\"").c_str()),NULL,NULL,FALSE,CREATE_NO_WINDOW|CREATE_NEW_PROCESS_GROUP,NULL,NULL,&si,&pi)) + return false; + + return true; +} + void ZeroTierOneService::OnStart(DWORD dwArgc, LPSTR *lpszArgv) { if (_node) diff --git a/windows/ZeroTierOne/ZeroTierOneService.h b/windows/ZeroTierOne/ZeroTierOneService.h index 5e0180ba..042a398a 100644 --- a/windows/ZeroTierOne/ZeroTierOneService.h +++ b/windows/ZeroTierOne/ZeroTierOneService.h @@ -29,6 +29,8 @@ #include "ServiceBase.h" +#include + #include "../../node/Node.hpp" #include "../../node/Defaults.hpp" #include "../../node/Thread.hpp" @@ -54,6 +56,9 @@ public: void threadMain() throw(); + // Returns false on failure. + static bool doStartUpgrade(const std::string &msiPath); + protected: virtual void OnStart(DWORD dwArgc, PSTR *pszArgv); virtual void OnStop(); -- cgit v1.2.3