params.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. #include "common/params.h"
  2. #include <dirent.h>
  3. #include <sys/file.h>
  4. #include <algorithm>
  5. #include <cassert>
  6. #include <csignal>
  7. #include <unordered_map>
  8. #include "common/queue.h"
  9. #include "common/swaglog.h"
  10. #include "common/util.h"
  11. #include "system/hardware/hw.h"
  12. namespace {
  13. volatile sig_atomic_t params_do_exit = 0;
  14. void params_sig_handler(int signal) {
  15. params_do_exit = 1;
  16. }
  17. int fsync_dir(const std::string &path) {
  18. int result = -1;
  19. int fd = HANDLE_EINTR(open(path.c_str(), O_RDONLY, 0755));
  20. if (fd >= 0) {
  21. result = HANDLE_EINTR(fsync(fd));
  22. HANDLE_EINTR(close(fd));
  23. }
  24. return result;
  25. }
  26. bool create_params_path(const std::string &param_path, const std::string &key_path) {
  27. // Make sure params path exists
  28. if (!util::file_exists(param_path) && !util::create_directories(param_path, 0775)) {
  29. return false;
  30. }
  31. // See if the symlink exists, otherwise create it
  32. if (!util::file_exists(key_path)) {
  33. // 1) Create temp folder
  34. // 2) Symlink it to temp link
  35. // 3) Move symlink to <params>/d
  36. std::string tmp_path = param_path + "/.tmp_XXXXXX";
  37. // this should be OK since mkdtemp just replaces characters in place
  38. char *tmp_dir = mkdtemp((char *)tmp_path.c_str());
  39. if (tmp_dir == NULL) {
  40. return false;
  41. }
  42. std::string link_path = std::string(tmp_dir) + ".link";
  43. if (symlink(tmp_dir, link_path.c_str()) != 0) {
  44. return false;
  45. }
  46. // don't return false if it has been created by other
  47. if (rename(link_path.c_str(), key_path.c_str()) != 0 && errno != EEXIST) {
  48. return false;
  49. }
  50. }
  51. return true;
  52. }
  53. std::string ensure_params_path(const std::string &prefix, const std::string &path = {}) {
  54. std::string params_path = path.empty() ? Path::params() : path;
  55. if (!create_params_path(params_path, params_path + prefix)) {
  56. throw std::runtime_error(util::string_format(
  57. "Failed to ensure params path, errno=%d, path=%s, param_prefix=%s",
  58. errno, params_path.c_str(), prefix.c_str()));
  59. }
  60. return params_path;
  61. }
  62. class FileLock {
  63. public:
  64. FileLock(const std::string &fn) {
  65. fd_ = HANDLE_EINTR(open(fn.c_str(), O_CREAT, 0775));
  66. if (fd_ < 0 || HANDLE_EINTR(flock(fd_, LOCK_EX)) < 0) {
  67. LOGE("Failed to lock file %s, errno=%d", fn.c_str(), errno);
  68. }
  69. }
  70. ~FileLock() { close(fd_); }
  71. private:
  72. int fd_ = -1;
  73. };
  74. std::unordered_map<std::string, uint32_t> keys = {
  75. {"AccessToken", CLEAR_ON_MANAGER_START | DONT_LOG},
  76. {"AlwaysOnDM", PERSISTENT},
  77. {"ApiCache_Device", PERSISTENT},
  78. {"AssistNowToken", PERSISTENT},
  79. {"AthenadPid", PERSISTENT},
  80. {"AthenadUploadQueue", PERSISTENT},
  81. {"AthenadRecentlyViewedRoutes", PERSISTENT},
  82. {"BootCount", PERSISTENT},
  83. {"CalibrationParams", PERSISTENT},
  84. {"CameraDebugExpGain", CLEAR_ON_MANAGER_START},
  85. {"CameraDebugExpTime", CLEAR_ON_MANAGER_START},
  86. {"CarBatteryCapacity", PERSISTENT},
  87. {"CarParams", CLEAR_ON_MANAGER_START | CLEAR_ON_ONROAD_TRANSITION},
  88. {"CarParamsCache", CLEAR_ON_MANAGER_START},
  89. {"CarParamsPersistent", PERSISTENT},
  90. {"CarParamsPrevRoute", PERSISTENT},
  91. {"CompletedTrainingVersion", PERSISTENT},
  92. {"ControlsReady", CLEAR_ON_MANAGER_START | CLEAR_ON_ONROAD_TRANSITION},
  93. {"CurrentBootlog", PERSISTENT},
  94. {"CurrentRoute", CLEAR_ON_MANAGER_START | CLEAR_ON_ONROAD_TRANSITION},
  95. {"DisableLogging", CLEAR_ON_MANAGER_START | CLEAR_ON_ONROAD_TRANSITION},
  96. {"DisablePowerDown", PERSISTENT},
  97. {"DisableUpdates", PERSISTENT},
  98. {"DisengageOnAccelerator", PERSISTENT},
  99. {"DmModelInitialized", CLEAR_ON_ONROAD_TRANSITION},
  100. {"DongleId", PERSISTENT},
  101. {"DoReboot", CLEAR_ON_MANAGER_START},
  102. {"DoShutdown", CLEAR_ON_MANAGER_START},
  103. {"DoUninstall", CLEAR_ON_MANAGER_START},
  104. {"ExperimentalLongitudinalEnabled", PERSISTENT | DEVELOPMENT_ONLY},
  105. {"ExperimentalMode", PERSISTENT},
  106. {"ExperimentalModeConfirmed", PERSISTENT},
  107. {"FirmwareQueryDone", CLEAR_ON_MANAGER_START | CLEAR_ON_ONROAD_TRANSITION},
  108. {"ForcePowerDown", PERSISTENT},
  109. {"GitBranch", PERSISTENT},
  110. {"GitCommit", PERSISTENT},
  111. {"GitCommitDate", PERSISTENT},
  112. {"GitDiff", PERSISTENT},
  113. {"GithubSshKeys", PERSISTENT},
  114. {"GithubUsername", PERSISTENT},
  115. {"GitRemote", PERSISTENT},
  116. {"GsmApn", PERSISTENT},
  117. {"GsmMetered", PERSISTENT},
  118. {"GsmRoaming", PERSISTENT},
  119. {"HardwareSerial", PERSISTENT},
  120. {"HasAcceptedTerms", PERSISTENT},
  121. {"IMEI", PERSISTENT},
  122. {"InstallDate", PERSISTENT},
  123. {"IsDriverViewEnabled", CLEAR_ON_MANAGER_START},
  124. {"IsEngaged", PERSISTENT},
  125. {"IsLdwEnabled", PERSISTENT},
  126. {"IsMetric", PERSISTENT},
  127. {"IsOffroad", CLEAR_ON_MANAGER_START},
  128. {"IsOnroad", PERSISTENT},
  129. {"IsRhdDetected", PERSISTENT},
  130. {"IsReleaseBranch", CLEAR_ON_MANAGER_START},
  131. {"IsTakingSnapshot", CLEAR_ON_MANAGER_START},
  132. {"IsTestedBranch", CLEAR_ON_MANAGER_START},
  133. {"JoystickDebugMode", CLEAR_ON_MANAGER_START | CLEAR_ON_OFFROAD_TRANSITION},
  134. {"LanguageSetting", PERSISTENT},
  135. {"LastAthenaPingTime", CLEAR_ON_MANAGER_START},
  136. {"LastGPSPosition", PERSISTENT},
  137. {"LastManagerExitReason", CLEAR_ON_MANAGER_START},
  138. {"LastOffroadStatusPacket", CLEAR_ON_MANAGER_START | CLEAR_ON_OFFROAD_TRANSITION},
  139. {"LastPowerDropDetected", CLEAR_ON_MANAGER_START},
  140. {"LastUpdateException", CLEAR_ON_MANAGER_START},
  141. {"LastUpdateTime", PERSISTENT},
  142. {"LiveParameters", PERSISTENT},
  143. {"LiveTorqueParameters", PERSISTENT | DONT_LOG},
  144. {"LocationFilterInitialState", PERSISTENT},
  145. {"LongitudinalManeuverMode", CLEAR_ON_MANAGER_START | CLEAR_ON_OFFROAD_TRANSITION},
  146. {"LongitudinalPersonality", PERSISTENT},
  147. {"NetworkMetered", PERSISTENT},
  148. {"ObdMultiplexingChanged", CLEAR_ON_MANAGER_START | CLEAR_ON_ONROAD_TRANSITION},
  149. {"ObdMultiplexingEnabled", CLEAR_ON_MANAGER_START | CLEAR_ON_ONROAD_TRANSITION},
  150. {"Offroad_BadNvme", CLEAR_ON_MANAGER_START},
  151. {"Offroad_CarUnrecognized", CLEAR_ON_MANAGER_START | CLEAR_ON_ONROAD_TRANSITION},
  152. {"Offroad_ConnectivityNeeded", CLEAR_ON_MANAGER_START},
  153. {"Offroad_ConnectivityNeededPrompt", CLEAR_ON_MANAGER_START},
  154. {"Offroad_IsTakingSnapshot", CLEAR_ON_MANAGER_START},
  155. {"Offroad_NeosUpdate", CLEAR_ON_MANAGER_START},
  156. {"Offroad_NoFirmware", CLEAR_ON_MANAGER_START | CLEAR_ON_ONROAD_TRANSITION},
  157. {"Offroad_Recalibration", CLEAR_ON_MANAGER_START | CLEAR_ON_ONROAD_TRANSITION},
  158. {"Offroad_StorageMissing", CLEAR_ON_MANAGER_START},
  159. {"Offroad_TemperatureTooHigh", CLEAR_ON_MANAGER_START},
  160. {"Offroad_UnofficialHardware", CLEAR_ON_MANAGER_START},
  161. {"Offroad_UpdateFailed", CLEAR_ON_MANAGER_START},
  162. {"OpenpilotEnabledToggle", PERSISTENT},
  163. {"PandaHeartbeatLost", CLEAR_ON_MANAGER_START | CLEAR_ON_OFFROAD_TRANSITION},
  164. {"PandaSomResetTriggered", CLEAR_ON_MANAGER_START | CLEAR_ON_OFFROAD_TRANSITION},
  165. {"PandaSignatures", CLEAR_ON_MANAGER_START},
  166. {"PrimeType", PERSISTENT},
  167. {"RecordFront", PERSISTENT},
  168. {"RecordFrontLock", PERSISTENT}, // for the internal fleet
  169. {"SecOCKey", PERSISTENT | DONT_LOG},
  170. {"RouteCount", PERSISTENT},
  171. {"SnoozeUpdate", CLEAR_ON_MANAGER_START | CLEAR_ON_OFFROAD_TRANSITION},
  172. {"SshEnabled", PERSISTENT},
  173. {"TermsVersion", PERSISTENT},
  174. {"TrainingVersion", PERSISTENT},
  175. {"UbloxAvailable", PERSISTENT},
  176. {"UpdateAvailable", CLEAR_ON_MANAGER_START | CLEAR_ON_ONROAD_TRANSITION},
  177. {"UpdateFailedCount", CLEAR_ON_MANAGER_START},
  178. {"UpdaterAvailableBranches", PERSISTENT},
  179. {"UpdaterCurrentDescription", CLEAR_ON_MANAGER_START},
  180. {"UpdaterCurrentReleaseNotes", CLEAR_ON_MANAGER_START},
  181. {"UpdaterFetchAvailable", CLEAR_ON_MANAGER_START},
  182. {"UpdaterNewDescription", CLEAR_ON_MANAGER_START},
  183. {"UpdaterNewReleaseNotes", CLEAR_ON_MANAGER_START},
  184. {"UpdaterState", CLEAR_ON_MANAGER_START},
  185. {"UpdaterTargetBranch", CLEAR_ON_MANAGER_START},
  186. {"UpdaterLastFetchTime", PERSISTENT},
  187. {"Version", PERSISTENT},
  188. };
  189. } // namespace
  190. Params::Params(const std::string &path) {
  191. params_prefix = "/" + util::getenv("OPENPILOT_PREFIX", "d");
  192. params_path = ensure_params_path(params_prefix, path);
  193. }
  194. Params::~Params() {
  195. if (future.valid()) {
  196. future.wait();
  197. }
  198. assert(queue.empty());
  199. }
  200. std::vector<std::string> Params::allKeys() const {
  201. std::vector<std::string> ret;
  202. for (auto &p : keys) {
  203. ret.push_back(p.first);
  204. }
  205. return ret;
  206. }
  207. bool Params::checkKey(const std::string &key) {
  208. return keys.find(key) != keys.end();
  209. }
  210. ParamKeyType Params::getKeyType(const std::string &key) {
  211. return static_cast<ParamKeyType>(keys[key]);
  212. }
  213. int Params::put(const char* key, const char* value, size_t value_size) {
  214. // Information about safely and atomically writing a file: https://lwn.net/Articles/457667/
  215. // 1) Create temp file
  216. // 2) Write data to temp file
  217. // 3) fsync() the temp file
  218. // 4) rename the temp file to the real name
  219. // 5) fsync() the containing directory
  220. std::string tmp_path = params_path + "/.tmp_value_XXXXXX";
  221. int tmp_fd = mkstemp((char*)tmp_path.c_str());
  222. if (tmp_fd < 0) return -1;
  223. int result = -1;
  224. do {
  225. // Write value to temp.
  226. ssize_t bytes_written = HANDLE_EINTR(write(tmp_fd, value, value_size));
  227. if (bytes_written < 0 || (size_t)bytes_written != value_size) {
  228. result = -20;
  229. break;
  230. }
  231. // fsync to force persist the changes.
  232. if ((result = fsync(tmp_fd)) < 0) break;
  233. FileLock file_lock(params_path + "/.lock");
  234. // Move temp into place.
  235. if ((result = rename(tmp_path.c_str(), getParamPath(key).c_str())) < 0) break;
  236. // fsync parent directory
  237. result = fsync_dir(getParamPath());
  238. } while (false);
  239. close(tmp_fd);
  240. if (result != 0) {
  241. ::unlink(tmp_path.c_str());
  242. }
  243. return result;
  244. }
  245. int Params::remove(const std::string &key) {
  246. FileLock file_lock(params_path + "/.lock");
  247. int result = unlink(getParamPath(key).c_str());
  248. if (result != 0) {
  249. return result;
  250. }
  251. return fsync_dir(getParamPath());
  252. }
  253. std::string Params::get(const std::string &key, bool block) {
  254. if (!block) {
  255. return util::read_file(getParamPath(key));
  256. } else {
  257. // blocking read until successful
  258. params_do_exit = 0;
  259. void (*prev_handler_sigint)(int) = std::signal(SIGINT, params_sig_handler);
  260. void (*prev_handler_sigterm)(int) = std::signal(SIGTERM, params_sig_handler);
  261. std::string value;
  262. while (!params_do_exit) {
  263. if (value = util::read_file(getParamPath(key)); !value.empty()) {
  264. break;
  265. }
  266. util::sleep_for(100); // 0.1 s
  267. }
  268. std::signal(SIGINT, prev_handler_sigint);
  269. std::signal(SIGTERM, prev_handler_sigterm);
  270. return value;
  271. }
  272. }
  273. std::map<std::string, std::string> Params::readAll() {
  274. FileLock file_lock(params_path + "/.lock");
  275. return util::read_files_in_dir(getParamPath());
  276. }
  277. void Params::clearAll(ParamKeyType key_type) {
  278. FileLock file_lock(params_path + "/.lock");
  279. // 1) delete params of key_type
  280. // 2) delete files that are not defined in the keys.
  281. if (DIR *d = opendir(getParamPath().c_str())) {
  282. struct dirent *de = NULL;
  283. while ((de = readdir(d))) {
  284. if (de->d_type != DT_DIR) {
  285. auto it = keys.find(de->d_name);
  286. if (it == keys.end() || (it->second & key_type)) {
  287. unlink(getParamPath(de->d_name).c_str());
  288. }
  289. }
  290. }
  291. closedir(d);
  292. }
  293. fsync_dir(getParamPath());
  294. }
  295. void Params::putNonBlocking(const std::string &key, const std::string &val) {
  296. queue.push(std::make_pair(key, val));
  297. // start thread on demand
  298. if (!future.valid() || future.wait_for(std::chrono::milliseconds(0)) == std::future_status::ready) {
  299. future = std::async(std::launch::async, &Params::asyncWriteThread, this);
  300. }
  301. }
  302. void Params::asyncWriteThread() {
  303. // TODO: write the latest one if a key has multiple values in the queue.
  304. std::pair<std::string, std::string> p;
  305. while (queue.try_pop(p, 0)) {
  306. // Params::put is Thread-Safe
  307. put(p.first, p.second);
  308. }
  309. }