2
0

SoftwareUpdater.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /*
  2. * Copyright (c)2019 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2026-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <stdint.h>
  17. #include "../node/Constants.hpp"
  18. #include "../version.h"
  19. #ifdef __WINDOWS__
  20. #include <winsock2.h>
  21. #include <windows.h>
  22. #include <shlobj.h>
  23. #include <netioapi.h>
  24. #include <iphlpapi.h>
  25. #else
  26. #include <sys/types.h>
  27. #include <sys/socket.h>
  28. #include <sys/wait.h>
  29. #include <unistd.h>
  30. #include <ifaddrs.h>
  31. #endif
  32. #include "SoftwareUpdater.hpp"
  33. #include "../node/Utils.hpp"
  34. #include "../node/SHA512.hpp"
  35. #include "../node/Buffer.hpp"
  36. #include "../node/Node.hpp"
  37. #include "../osdep/OSUtils.hpp"
  38. namespace ZeroTier {
  39. static int _compareVersion(unsigned int maj1,unsigned int min1,unsigned int rev1,unsigned int b1,unsigned int maj2,unsigned int min2,unsigned int rev2,unsigned int b2)
  40. {
  41. if (maj1 > maj2) {
  42. return 1;
  43. } else if (maj1 < maj2) {
  44. return -1;
  45. } else {
  46. if (min1 > min2) {
  47. return 1;
  48. } else if (min1 < min2) {
  49. return -1;
  50. } else {
  51. if (rev1 > rev2) {
  52. return 1;
  53. } else if (rev1 < rev2) {
  54. return -1;
  55. } else {
  56. if (b1 > b2) {
  57. return 1;
  58. } else if (b1 < b2) {
  59. return -1;
  60. } else {
  61. return 0;
  62. }
  63. }
  64. }
  65. }
  66. }
  67. SoftwareUpdater::SoftwareUpdater(Node &node,const std::string &homePath) :
  68. _node(node),
  69. _lastCheckTime(0),
  70. _homePath(homePath),
  71. _channel(ZT_SOFTWARE_UPDATE_DEFAULT_CHANNEL),
  72. _distLog((FILE *)0),
  73. _latestValid(false),
  74. _downloadLength(0)
  75. {
  76. OSUtils::rm((_homePath + ZT_PATH_SEPARATOR_S ZT_SOFTWARE_UPDATE_BIN_FILENAME).c_str());
  77. }
  78. SoftwareUpdater::~SoftwareUpdater()
  79. {
  80. if (_distLog)
  81. fclose(_distLog);
  82. }
  83. void SoftwareUpdater::setUpdateDistribution(bool distribute)
  84. {
  85. _dist.clear();
  86. if (distribute) {
  87. _distLog = fopen((_homePath + ZT_PATH_SEPARATOR_S "update-dist.log").c_str(),"a");
  88. const std::string udd(_homePath + ZT_PATH_SEPARATOR_S "update-dist.d");
  89. const std::vector<std::string> ud(OSUtils::listDirectory(udd.c_str()));
  90. for(std::vector<std::string>::const_iterator u(ud.begin());u!=ud.end();++u) {
  91. // Each update has a companion .json file describing it. Other files are ignored.
  92. if ((u->length() > 5)&&(u->substr(u->length() - 5,5) == ".json")) {
  93. std::string buf;
  94. if (OSUtils::readFile((udd + ZT_PATH_SEPARATOR_S + *u).c_str(),buf)) {
  95. try {
  96. _D d;
  97. d.meta = OSUtils::jsonParse(buf); // throws on invalid JSON
  98. // If update meta is called e.g. foo.exe.json, then foo.exe is the update itself
  99. const std::string binPath(udd + ZT_PATH_SEPARATOR_S + u->substr(0,u->length() - 5));
  100. const std::string metaHash(OSUtils::jsonBinFromHex(d.meta[ZT_SOFTWARE_UPDATE_JSON_UPDATE_HASH]));
  101. if ((metaHash.length() == 64)&&(OSUtils::readFile(binPath.c_str(),d.bin))) {
  102. std::array<uint8_t,64> sha512;
  103. SHA512(sha512.data(),d.bin.data(),(unsigned int)d.bin.length());
  104. if (!memcmp(sha512.data(),metaHash.data(),64)) { // double check that hash in JSON is correct
  105. d.meta[ZT_SOFTWARE_UPDATE_JSON_UPDATE_SIZE] = d.bin.length(); // override with correct value -- setting this in meta json is optional
  106. std::array<uint8_t,16> shakey;
  107. memcpy(shakey.data(),sha512.data(),16);
  108. _dist[shakey] = d;
  109. if (_distLog) {
  110. fprintf(_distLog,".......... INIT: DISTRIBUTING %s (%u bytes)" ZT_EOL_S,binPath.c_str(),(unsigned int)d.bin.length());
  111. fflush(_distLog);
  112. }
  113. }
  114. }
  115. } catch ( ... ) {} // ignore bad meta JSON, etc.
  116. }
  117. }
  118. }
  119. } else {
  120. if (_distLog) {
  121. fclose(_distLog);
  122. _distLog = (FILE *)0;
  123. }
  124. }
  125. }
  126. void SoftwareUpdater::handleSoftwareUpdateUserMessage(uint64_t origin,const void *data,unsigned int len)
  127. {
  128. if (!len) return;
  129. const MessageVerb v = (MessageVerb)reinterpret_cast<const uint8_t *>(data)[0];
  130. try {
  131. switch(v) {
  132. case VERB_GET_LATEST:
  133. case VERB_LATEST: {
  134. nlohmann::json req = OSUtils::jsonParse(std::string(reinterpret_cast<const char *>(data) + 1,len - 1)); // throws on invalid JSON
  135. if (req.is_object()) {
  136. const unsigned int rvMaj = (unsigned int)OSUtils::jsonInt(req[ZT_SOFTWARE_UPDATE_JSON_VERSION_MAJOR],0);
  137. const unsigned int rvMin = (unsigned int)OSUtils::jsonInt(req[ZT_SOFTWARE_UPDATE_JSON_VERSION_MINOR],0);
  138. const unsigned int rvRev = (unsigned int)OSUtils::jsonInt(req[ZT_SOFTWARE_UPDATE_JSON_VERSION_REVISION],0);
  139. const unsigned int rvBld = (unsigned int)OSUtils::jsonInt(req[ZT_SOFTWARE_UPDATE_JSON_VERSION_BUILD],0);
  140. const unsigned int rvPlatform = (unsigned int)OSUtils::jsonInt(req[ZT_SOFTWARE_UPDATE_JSON_PLATFORM],0);
  141. const unsigned int rvArch = (unsigned int)OSUtils::jsonInt(req[ZT_SOFTWARE_UPDATE_JSON_ARCHITECTURE],0);
  142. const unsigned int rvVendor = (unsigned int)OSUtils::jsonInt(req[ZT_SOFTWARE_UPDATE_JSON_VENDOR],0);
  143. const std::string rvChannel(OSUtils::jsonString(req[ZT_SOFTWARE_UPDATE_JSON_CHANNEL],""));
  144. if (v == VERB_GET_LATEST) {
  145. if (!_dist.empty()) {
  146. const nlohmann::json *latest = (const nlohmann::json *)0;
  147. const std::string expectedSigner = OSUtils::jsonString(req[ZT_SOFTWARE_UPDATE_JSON_EXPECT_SIGNED_BY],"");
  148. unsigned int bestVMaj = rvMaj;
  149. unsigned int bestVMin = rvMin;
  150. unsigned int bestVRev = rvRev;
  151. unsigned int bestVBld = rvBld;
  152. for(std::map< std::array<uint8_t,16>,_D >::const_iterator d(_dist.begin());d!=_dist.end();++d) {
  153. // The arch field in update description .json files can be an array for e.g. multi-arch update files
  154. const nlohmann::json &dvArch2 = d->second.meta[ZT_SOFTWARE_UPDATE_JSON_ARCHITECTURE];
  155. std::vector<unsigned int> dvArch;
  156. if (dvArch2.is_array()) {
  157. for(unsigned long i=0;i<dvArch2.size();++i)
  158. dvArch.push_back((unsigned int)OSUtils::jsonInt(dvArch2[i],0));
  159. } else {
  160. dvArch.push_back((unsigned int)OSUtils::jsonInt(dvArch2,0));
  161. }
  162. if ((OSUtils::jsonInt(d->second.meta[ZT_SOFTWARE_UPDATE_JSON_PLATFORM],0) == rvPlatform)&&
  163. (std::find(dvArch.begin(),dvArch.end(),rvArch) != dvArch.end())&&
  164. (OSUtils::jsonInt(d->second.meta[ZT_SOFTWARE_UPDATE_JSON_VENDOR],0) == rvVendor)&&
  165. (OSUtils::jsonString(d->second.meta[ZT_SOFTWARE_UPDATE_JSON_CHANNEL],"") == rvChannel)&&
  166. (OSUtils::jsonString(d->second.meta[ZT_SOFTWARE_UPDATE_JSON_UPDATE_SIGNED_BY],"") == expectedSigner)) {
  167. const unsigned int dvMaj = (unsigned int)OSUtils::jsonInt(d->second.meta[ZT_SOFTWARE_UPDATE_JSON_VERSION_MAJOR],0);
  168. const unsigned int dvMin = (unsigned int)OSUtils::jsonInt(d->second.meta[ZT_SOFTWARE_UPDATE_JSON_VERSION_MINOR],0);
  169. const unsigned int dvRev = (unsigned int)OSUtils::jsonInt(d->second.meta[ZT_SOFTWARE_UPDATE_JSON_VERSION_REVISION],0);
  170. const unsigned int dvBld = (unsigned int)OSUtils::jsonInt(d->second.meta[ZT_SOFTWARE_UPDATE_JSON_VERSION_BUILD],0);
  171. if (_compareVersion(dvMaj,dvMin,dvRev,dvBld,bestVMaj,bestVMin,bestVRev,bestVBld) > 0) {
  172. latest = &(d->second.meta);
  173. bestVMaj = dvMaj;
  174. bestVMin = dvMin;
  175. bestVRev = dvRev;
  176. bestVBld = dvBld;
  177. }
  178. }
  179. }
  180. if (latest) {
  181. std::string lj;
  182. lj.push_back((char)VERB_LATEST);
  183. lj.append(OSUtils::jsonDump(*latest));
  184. _node.sendUserMessage((void *)0,origin,ZT_SOFTWARE_UPDATE_USER_MESSAGE_TYPE,lj.data(),(unsigned int)lj.length());
  185. if (_distLog) {
  186. fprintf(_distLog,"%.10llx GET_LATEST %u.%u.%u_%u platform %u arch %u vendor %u channel %s -> LATEST %u.%u.%u_%u" ZT_EOL_S,(unsigned long long)origin,rvMaj,rvMin,rvRev,rvBld,rvPlatform,rvArch,rvVendor,rvChannel.c_str(),bestVMaj,bestVMin,bestVRev,bestVBld);
  187. fflush(_distLog);
  188. }
  189. }
  190. } // else no reply, since we have nothing to distribute
  191. } else { // VERB_LATEST
  192. if ((origin == ZT_SOFTWARE_UPDATE_SERVICE)&&
  193. (_compareVersion(rvMaj,rvMin,rvRev,rvBld,ZEROTIER_ONE_VERSION_MAJOR,ZEROTIER_ONE_VERSION_MINOR,ZEROTIER_ONE_VERSION_REVISION,ZEROTIER_ONE_VERSION_BUILD) > 0)&&
  194. (OSUtils::jsonString(req[ZT_SOFTWARE_UPDATE_JSON_UPDATE_SIGNED_BY],"") == ZT_SOFTWARE_UPDATE_SIGNING_AUTHORITY)) {
  195. const unsigned long len = (unsigned long)OSUtils::jsonInt(req[ZT_SOFTWARE_UPDATE_JSON_UPDATE_SIZE],0);
  196. const std::string hash = OSUtils::jsonBinFromHex(req[ZT_SOFTWARE_UPDATE_JSON_UPDATE_HASH]);
  197. if ((len <= ZT_SOFTWARE_UPDATE_MAX_SIZE)&&(hash.length() >= 16)) {
  198. if (_latestMeta != req) {
  199. _latestMeta = req;
  200. _latestValid = false;
  201. OSUtils::rm((_homePath + ZT_PATH_SEPARATOR_S ZT_SOFTWARE_UPDATE_BIN_FILENAME).c_str());
  202. _download = std::string();
  203. memcpy(_downloadHashPrefix.data(),hash.data(),16);
  204. _downloadLength = len;
  205. }
  206. if ((_downloadLength > 0)&&(_download.length() < _downloadLength)) {
  207. Buffer<128> gd;
  208. gd.append((uint8_t)VERB_GET_DATA);
  209. gd.append(_downloadHashPrefix.data(),16);
  210. gd.append((uint32_t)_download.length());
  211. _node.sendUserMessage((void *)0,ZT_SOFTWARE_UPDATE_SERVICE,ZT_SOFTWARE_UPDATE_USER_MESSAGE_TYPE,gd.data(),gd.size());
  212. }
  213. }
  214. }
  215. }
  216. }
  217. } break;
  218. case VERB_GET_DATA:
  219. if ((len >= 21)&&(!_dist.empty())) {
  220. unsigned long idx = (unsigned long)*(reinterpret_cast<const uint8_t *>(data) + 17) << 24;
  221. idx |= (unsigned long)*(reinterpret_cast<const uint8_t *>(data) + 18) << 16;
  222. idx |= (unsigned long)*(reinterpret_cast<const uint8_t *>(data) + 19) << 8;
  223. idx |= (unsigned long)*(reinterpret_cast<const uint8_t *>(data) + 20);
  224. std::array<uint8_t,16> shakey;
  225. memcpy(shakey.data(),reinterpret_cast<const uint8_t *>(data) + 1,16);
  226. std::map< std::array<uint8_t,16>,_D >::iterator d(_dist.find(shakey));
  227. if ((d != _dist.end())&&(idx < (unsigned long)d->second.bin.length())) {
  228. Buffer<ZT_SOFTWARE_UPDATE_CHUNK_SIZE + 128> buf;
  229. buf.append((uint8_t)VERB_DATA);
  230. buf.append(reinterpret_cast<const uint8_t *>(data) + 1,16);
  231. buf.append((uint32_t)idx);
  232. buf.append(d->second.bin.data() + idx,std::min((unsigned long)ZT_SOFTWARE_UPDATE_CHUNK_SIZE,(unsigned long)(d->second.bin.length() - idx)));
  233. _node.sendUserMessage((void *)0,origin,ZT_SOFTWARE_UPDATE_USER_MESSAGE_TYPE,buf.data(),buf.size());
  234. }
  235. }
  236. break;
  237. case VERB_DATA:
  238. if ((len >= 21)&&(_downloadLength > 0)&&(!memcmp(_downloadHashPrefix.data(),reinterpret_cast<const uint8_t *>(data) + 1,16))) {
  239. unsigned long idx = (unsigned long)*(reinterpret_cast<const uint8_t *>(data) + 17) << 24;
  240. idx |= (unsigned long)*(reinterpret_cast<const uint8_t *>(data) + 18) << 16;
  241. idx |= (unsigned long)*(reinterpret_cast<const uint8_t *>(data) + 19) << 8;
  242. idx |= (unsigned long)*(reinterpret_cast<const uint8_t *>(data) + 20);
  243. if (idx == (unsigned long)_download.length()) {
  244. _download.append(reinterpret_cast<const char *>(data) + 21,len - 21);
  245. if (_download.length() < _downloadLength) {
  246. Buffer<128> gd;
  247. gd.append((uint8_t)VERB_GET_DATA);
  248. gd.append(_downloadHashPrefix.data(),16);
  249. gd.append((uint32_t)_download.length());
  250. _node.sendUserMessage((void *)0,ZT_SOFTWARE_UPDATE_SERVICE,ZT_SOFTWARE_UPDATE_USER_MESSAGE_TYPE,gd.data(),gd.size());
  251. }
  252. }
  253. }
  254. break;
  255. default:
  256. if (_distLog) {
  257. fprintf(_distLog,"%.10llx WARNING: bad update message verb==%u length==%u (unrecognized verb)" ZT_EOL_S,(unsigned long long)origin,(unsigned int)v,len);
  258. fflush(_distLog);
  259. }
  260. break;
  261. }
  262. } catch ( ... ) {
  263. if (_distLog) {
  264. fprintf(_distLog,"%.10llx WARNING: bad update message verb==%u length==%u (unexpected exception, likely invalid JSON)" ZT_EOL_S,(unsigned long long)origin,(unsigned int)v,len);
  265. fflush(_distLog);
  266. }
  267. }
  268. }
  269. bool SoftwareUpdater::check(const int64_t now)
  270. {
  271. if ((now - _lastCheckTime) >= ZT_SOFTWARE_UPDATE_CHECK_PERIOD) {
  272. _lastCheckTime = now;
  273. char tmp[512];
  274. const unsigned int len = OSUtils::ztsnprintf(tmp,sizeof(tmp),
  275. "%c{\"" ZT_SOFTWARE_UPDATE_JSON_VERSION_MAJOR "\":%d,"
  276. "\"" ZT_SOFTWARE_UPDATE_JSON_VERSION_MINOR "\":%d,"
  277. "\"" ZT_SOFTWARE_UPDATE_JSON_VERSION_REVISION "\":%d,"
  278. "\"" ZT_SOFTWARE_UPDATE_JSON_VERSION_BUILD "\":%d,"
  279. "\"" ZT_SOFTWARE_UPDATE_JSON_EXPECT_SIGNED_BY "\":\"%s\","
  280. "\"" ZT_SOFTWARE_UPDATE_JSON_PLATFORM "\":%d,"
  281. "\"" ZT_SOFTWARE_UPDATE_JSON_ARCHITECTURE "\":%d,"
  282. "\"" ZT_SOFTWARE_UPDATE_JSON_VENDOR "\":%d,"
  283. "\"" ZT_SOFTWARE_UPDATE_JSON_CHANNEL "\":\"%s\"}",
  284. (char)VERB_GET_LATEST,
  285. ZEROTIER_ONE_VERSION_MAJOR,
  286. ZEROTIER_ONE_VERSION_MINOR,
  287. ZEROTIER_ONE_VERSION_REVISION,
  288. ZEROTIER_ONE_VERSION_BUILD,
  289. ZT_SOFTWARE_UPDATE_SIGNING_AUTHORITY,
  290. ZT_BUILD_PLATFORM,
  291. ZT_BUILD_ARCHITECTURE,
  292. (int)ZT_VENDOR_ZEROTIER,
  293. _channel.c_str());
  294. _node.sendUserMessage((void *)0,ZT_SOFTWARE_UPDATE_SERVICE,ZT_SOFTWARE_UPDATE_USER_MESSAGE_TYPE,tmp,len);
  295. }
  296. if (_latestValid)
  297. return true;
  298. if (_downloadLength > 0) {
  299. if (_download.length() >= _downloadLength) {
  300. // This is the very important security validation part that makes sure
  301. // this software update doesn't have cooties.
  302. const std::string binPath(_homePath + ZT_PATH_SEPARATOR_S ZT_SOFTWARE_UPDATE_BIN_FILENAME);
  303. try {
  304. // (1) Check the hash itself to make sure the image is basically okay
  305. uint8_t sha512[64];
  306. SHA512(sha512,_download.data(),(unsigned int)_download.length());
  307. char hexbuf[(64 * 2) + 2];
  308. if (OSUtils::jsonString(_latestMeta[ZT_SOFTWARE_UPDATE_JSON_UPDATE_HASH],"") == Utils::hex(sha512,64,hexbuf)) {
  309. // (2) Check signature by signing authority
  310. const std::string sig(OSUtils::jsonBinFromHex(_latestMeta[ZT_SOFTWARE_UPDATE_JSON_UPDATE_SIGNATURE]));
  311. if (Identity(ZT_SOFTWARE_UPDATE_SIGNING_AUTHORITY).verify(_download.data(),(unsigned int)_download.length(),sig.data(),(unsigned int)sig.length())) {
  312. // (3) Try to save file, and if so we are good.
  313. OSUtils::rm(binPath.c_str());
  314. if (OSUtils::writeFile(binPath.c_str(),_download)) {
  315. OSUtils::lockDownFile(binPath.c_str(),false);
  316. _latestValid = true;
  317. _download = std::string();
  318. _downloadLength = 0;
  319. return true;
  320. }
  321. }
  322. }
  323. } catch ( ... ) {} // any exception equals verification failure
  324. // If we get here, checks failed.
  325. OSUtils::rm(binPath.c_str());
  326. _latestMeta = nlohmann::json();
  327. _latestValid = false;
  328. _download = std::string();
  329. _downloadLength = 0;
  330. } else {
  331. Buffer<128> gd;
  332. gd.append((uint8_t)VERB_GET_DATA);
  333. gd.append(_downloadHashPrefix.data(),16);
  334. gd.append((uint32_t)_download.length());
  335. _node.sendUserMessage((void *)0,ZT_SOFTWARE_UPDATE_SERVICE,ZT_SOFTWARE_UPDATE_USER_MESSAGE_TYPE,gd.data(),gd.size());
  336. }
  337. }
  338. return false;
  339. }
  340. void SoftwareUpdater::apply()
  341. {
  342. std::string updatePath(_homePath + ZT_PATH_SEPARATOR_S ZT_SOFTWARE_UPDATE_BIN_FILENAME);
  343. if ((_latestMeta.is_object())&&(_latestValid)&&(OSUtils::fileExists(updatePath.c_str(),false))) {
  344. #ifdef __WINDOWS__
  345. std::string cmdArgs(OSUtils::jsonString(_latestMeta[ZT_SOFTWARE_UPDATE_JSON_UPDATE_EXEC_ARGS],""));
  346. if (cmdArgs.length() > 0) {
  347. updatePath.push_back(' ');
  348. updatePath.append(cmdArgs);
  349. }
  350. STARTUPINFOA si;
  351. PROCESS_INFORMATION pi;
  352. memset(&si,0,sizeof(si));
  353. memset(&pi,0,sizeof(pi));
  354. CreateProcessA(NULL,const_cast<LPSTR>(updatePath.c_str()),NULL,NULL,FALSE,CREATE_NO_WINDOW|CREATE_NEW_PROCESS_GROUP,NULL,NULL,&si,&pi);
  355. // Windows doesn't exit here -- updater will stop the service during update, etc. -- but we do want to stop multiple runs from happening
  356. _latestMeta = nlohmann::json();
  357. _latestValid = false;
  358. #else
  359. char *argv[256];
  360. unsigned long ac = 0;
  361. argv[ac++] = const_cast<char *>(updatePath.c_str());
  362. const std::vector<std::string> argsSplit(OSUtils::split(OSUtils::jsonString(_latestMeta[ZT_SOFTWARE_UPDATE_JSON_UPDATE_EXEC_ARGS],"").c_str()," ","\\","\""));
  363. for(std::vector<std::string>::const_iterator a(argsSplit.begin());a!=argsSplit.end();++a) {
  364. argv[ac] = const_cast<char *>(a->c_str());
  365. if (++ac == 255) break;
  366. }
  367. argv[ac] = (char *)0;
  368. chmod(updatePath.c_str(),0700);
  369. // Close all open file descriptors except stdout/stderr/etc.
  370. int minMyFd = STDIN_FILENO;
  371. if (STDOUT_FILENO > minMyFd) minMyFd = STDOUT_FILENO;
  372. if (STDERR_FILENO > minMyFd) minMyFd = STDERR_FILENO;
  373. ++minMyFd;
  374. #ifdef _SC_OPEN_MAX
  375. int maxMyFd = (int)sysconf(_SC_OPEN_MAX);
  376. if (maxMyFd <= minMyFd)
  377. maxMyFd = 65536;
  378. #else
  379. int maxMyFd = 65536;
  380. #endif
  381. while (minMyFd < maxMyFd)
  382. close(minMyFd++);
  383. execv(updatePath.c_str(),argv);
  384. fprintf(stderr,"FATAL: unable to execute software update binary at %s\n",updatePath.c_str());
  385. exit(1);
  386. #endif
  387. }
  388. }
  389. } // namespace ZeroTier