Browse Source

Windows auto-updater invocation works... time to try an installer!

Adam Ierymenko 11 years ago
parent
commit
2203958798
3 changed files with 51 additions and 24 deletions
  1. 14 1
      main.cpp
  2. 32 23
      windows/ZeroTierOne/ZeroTierOneService.cpp
  3. 5 0
      windows/ZeroTierOne/ZeroTierOneService.h

+ 14 - 1
main.cpp

@@ -641,7 +641,20 @@ int main(int argc,char **argv)
 		try {
 			node = new Node(homeDir,port,controlPort);
 			switch(node->run()) {
-#ifndef __WINDOWS__
+#ifdef __WINDOWS__
+				case Node::NODE_RESTART_FOR_UPGRADE: {
+					const char *upgPath = node->reasonForTermination();
+					if (upgPath) {
+						if (!ZeroTierOneService::doStartUpgrade(std::string(upgPath))) {
+							exitCode = 3;
+							fprintf(stderr,"%s: abnormal termination: unable to execute update at %s (doStartUpgrade failed)\n",argv[0],(upgPath) ? upgPath : "(unknown path)");
+						}
+					} else {
+						exitCode = 3;
+						fprintf(stderr,"%s: abnormal termination: unable to execute update at %s (no upgrade path provided)\n",argv[0],(upgPath) ? upgPath : "(unknown path)");
+					}
+				}	break;
+#else // __UNIX_LIKE__
 				case Node::NODE_RESTART_FOR_UPGRADE: {
 					const char *upgPath = node->reasonForTermination();
 					// On Unix-type OSes we exec() right into the upgrade. This in turn will

+ 32 - 23
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 <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();
@@ -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 <LPSTR>((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)

+ 5 - 0
windows/ZeroTierOne/ZeroTierOneService.h

@@ -29,6 +29,8 @@
 
 #include "ServiceBase.h"
 
+#include <string>
+
 #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();