浏览代码

Fix for GitHub issue #97

Adam Ierymenko 11 年之前
父节点
当前提交
80fc584923
共有 3 个文件被更改,包括 43 次插入1 次删除
  1. 5 1
      main.cpp
  2. 23 0
      node/Utils.cpp
  3. 15 0
      node/Utils.hpp

+ 5 - 1
main.cpp

@@ -729,12 +729,16 @@ int main(int argc,char **argv)
 				// like OSX's launchd.
 				// like OSX's launchd.
 				if (upgPath) {
 				if (upgPath) {
 					Utils::rm((std::string(homeDir)+"/zerotier-one.pid").c_str());
 					Utils::rm((std::string(homeDir)+"/zerotier-one.pid").c_str());
+					std::string updateLogPath(homeDir);
+					updateLogPath.append("/autoupdate.log");
+					Utils::rm(updateLogPath.c_str());
+					Utils::redirectUnixOutputs(updateLogPath.c_str(),(const char *)0);
 					::execl(upgPath,upgPath,(char *)0);
 					::execl(upgPath,upgPath,(char *)0);
 				}
 				}
 				exitCode = 3;
 				exitCode = 3;
 				fprintf(stderr,"%s: abnormal termination: unable to execute update at %s\n",argv[0],(upgPath) ? upgPath : "(unknown path)");
 				fprintf(stderr,"%s: abnormal termination: unable to execute update at %s\n",argv[0],(upgPath) ? upgPath : "(unknown path)");
 			}	break;
 			}	break;
-#endif
+#endif // __WINDOWS__ / __UNIX_LIKE__
 			case Node::NODE_UNRECOVERABLE_ERROR: {
 			case Node::NODE_UNRECOVERABLE_ERROR: {
 				exitCode = 3;
 				exitCode = 3;
 				const char *termReason = node->reasonForTermination();
 				const char *termReason = node->reasonForTermination();

+ 23 - 0
node/Utils.cpp

@@ -38,6 +38,7 @@
 #include <errno.h>
 #include <errno.h>
 #include <fcntl.h>
 #include <fcntl.h>
 #include <sys/types.h>
 #include <sys/types.h>
+#include <sys/stat.h>
 #include <sys/uio.h>
 #include <sys/uio.h>
 #include <dirent.h>
 #include <dirent.h>
 #endif
 #endif
@@ -50,6 +51,28 @@ namespace ZeroTier {
 
 
 const char Utils::HEXCHARS[16] = { '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f' };
 const char Utils::HEXCHARS[16] = { '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f' };
 
 
+bool Utils::redirectUnixOutputs(const char *stdoutPath,const char *stderrPath)
+	throw()
+{
+	int fdout = ::open(stdoutPath,O_WRONLY|O_CREAT);
+	if (fdout > 0) {
+		int fderr;
+		if (stderrPath) {
+			fderr = ::open(stderrPath,O_WRONLY|O_CREAT);
+			if (fderr <= 0) {
+				::close(fdout);
+				return false;
+			}
+		} else fderr = fdout;
+		::close(STDOUT_FILENO);
+		::close(STDERR_FILENO);
+		::dup2(fdout,STDOUT_FILENO);
+		::dup2(fderr,STDERR_FILENO);
+		return true;
+	}
+	return false;
+}
+
 std::map<std::string,bool> Utils::listDirectory(const char *path)
 std::map<std::string,bool> Utils::listDirectory(const char *path)
 {
 {
 	std::map<std::string,bool> r;
 	std::map<std::string,bool> r;

+ 15 - 0
node/Utils.hpp

@@ -58,6 +58,21 @@ namespace ZeroTier {
 class Utils
 class Utils
 {
 {
 public:
 public:
+#ifdef __UNIX_LIKE__
+	/**
+	 * Close STDOUT_FILENO and STDERR_FILENO and replace them with output to given path
+	 *
+	 * This can be called after fork() and prior to exec() to suppress output
+	 * from a subprocess, such as auto-update.
+	 *
+	 * @param stdoutPath Path to file to use for stdout
+	 * @param stderrPath Path to file to use for stderr, or NULL for same as stdout (default)
+	 * @return True on success
+	 */
+	static bool redirectUnixOutputs(const char *stdoutPath,const char *stderrPath = (const char *)0)
+		throw();
+#endif
+
 	/**
 	/**
 	 * Perform a time-invariant binary comparison
 	 * Perform a time-invariant binary comparison
 	 *
 	 *