SoftwareUpdater.cpp 17 KB

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