SoftwareUpdater.cpp 17 KB

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