#include "selfdrive/common/params.h" #include #include #include #include #include "selfdrive/common/swaglog.h" #include "selfdrive/common/util.h" #include "selfdrive/hardware/hw.h" namespace { volatile sig_atomic_t params_do_exit = 0; void params_sig_handler(int signal) { params_do_exit = 1; } int fsync_dir(const std::string &path) { int result = -1; int fd = HANDLE_EINTR(open(path.c_str(), O_RDONLY, 0755)); if (fd >= 0) { result = fsync(fd); close(fd); } return result; } bool create_params_path(const std::string ¶m_path, const std::string &key_path) { // Make sure params path exists if (!util::file_exists(param_path) && !util::create_directories(param_path, 0775)) { return false; } // See if the symlink exists, otherwise create it if (!util::file_exists(key_path)) { // 1) Create temp folder // 2) Symlink it to temp link // 3) Move symlink to /d std::string tmp_path = param_path + "/.tmp_XXXXXX"; // this should be OK since mkdtemp just replaces characters in place char *tmp_dir = mkdtemp((char *)tmp_path.c_str()); if (tmp_dir == NULL) { return false; } std::string link_path = std::string(tmp_dir) + ".link"; if (symlink(tmp_dir, link_path.c_str()) != 0) { return false; } // don't return false if it has been created by other if (rename(link_path.c_str(), key_path.c_str()) != 0 && errno != EEXIST) { return false; } } return true; } std::string ensure_params_path(const std::string &path = {}) { std::string params_path = path.empty() ? Path::params() : path; if (!create_params_path(params_path, params_path + "/d")) { throw std::runtime_error(util::string_format("Failed to ensure params path, errno=%d", errno)); } return params_path; } class FileLock { public: FileLock(const std::string &fn) { fd_ = HANDLE_EINTR(open(fn.c_str(), O_CREAT, 0775)); if (fd_ < 0 || HANDLE_EINTR(flock(fd_, LOCK_EX)) < 0) { LOGE("Failed to lock file %s, errno=%d", fn.c_str(), errno); } } ~FileLock() { close(fd_); } private: int fd_ = -1; }; std::unordered_map keys = { {"AccessToken", CLEAR_ON_MANAGER_START | DONT_LOG}, {"AthenadPid", PERSISTENT}, {"AthenadUploadQueue", PERSISTENT}, {"CalibrationParams", PERSISTENT}, {"CarBatteryCapacity", PERSISTENT}, {"CarParams", CLEAR_ON_MANAGER_START | CLEAR_ON_IGNITION_ON}, {"CarParamsCache", CLEAR_ON_MANAGER_START}, {"CarVin", CLEAR_ON_MANAGER_START | CLEAR_ON_IGNITION_ON}, {"CompletedTrainingVersion", PERSISTENT}, {"ControlsReady", CLEAR_ON_MANAGER_START | CLEAR_ON_IGNITION_ON}, {"CurrentRoute", CLEAR_ON_MANAGER_START | CLEAR_ON_IGNITION_ON}, {"DisablePowerDown", PERSISTENT}, {"DisableRadar_Allow", PERSISTENT}, {"DisableRadar", PERSISTENT}, // WARNING: THIS DISABLES AEB {"DisableUpdates", PERSISTENT}, {"DongleId", PERSISTENT}, {"DoReboot", CLEAR_ON_MANAGER_START}, {"DoShutdown", CLEAR_ON_MANAGER_START}, {"DoUninstall", CLEAR_ON_MANAGER_START}, {"EnableWideCamera", CLEAR_ON_MANAGER_START}, {"EndToEndToggle", PERSISTENT}, {"ForcePowerDown", CLEAR_ON_MANAGER_START}, {"GitBranch", PERSISTENT}, {"GitCommit", PERSISTENT}, {"GitDiff", PERSISTENT}, {"GithubSshKeys", PERSISTENT}, {"GithubUsername", PERSISTENT}, {"GitRemote", PERSISTENT}, {"GsmApn", PERSISTENT}, {"GsmRoaming", PERSISTENT}, {"HardwareSerial", PERSISTENT}, {"HasAcceptedTerms", PERSISTENT}, {"IMEI", PERSISTENT}, {"InstallDate", PERSISTENT}, {"IsDriverViewEnabled", CLEAR_ON_MANAGER_START}, {"IsEngaged", PERSISTENT}, {"IsLdwEnabled", PERSISTENT}, {"IsMetric", PERSISTENT}, {"IsOffroad", CLEAR_ON_MANAGER_START}, {"IsOnroad", PERSISTENT}, {"IsRHD", PERSISTENT}, {"IsTakingSnapshot", CLEAR_ON_MANAGER_START}, {"IsUpdateAvailable", CLEAR_ON_MANAGER_START}, {"JoystickDebugMode", CLEAR_ON_MANAGER_START | CLEAR_ON_IGNITION_OFF}, {"LastAthenaPingTime", CLEAR_ON_MANAGER_START}, {"LastGPSPosition", PERSISTENT}, {"LastManagerExitReason", CLEAR_ON_MANAGER_START}, {"LastPeripheralPandaType", PERSISTENT}, {"LastPowerDropDetected", CLEAR_ON_MANAGER_START}, {"LastSystemShutdown", CLEAR_ON_MANAGER_START}, {"LastUpdateException", PERSISTENT}, {"LastUpdateTime", PERSISTENT}, {"LiveParameters", PERSISTENT}, {"NavDestination", CLEAR_ON_MANAGER_START | CLEAR_ON_IGNITION_OFF}, {"NavSettingTime24h", PERSISTENT}, {"NavdRender", PERSISTENT}, {"OpenpilotEnabledToggle", PERSISTENT}, {"PandaHeartbeatLost", CLEAR_ON_MANAGER_START | CLEAR_ON_IGNITION_OFF}, {"PandaSignatures", CLEAR_ON_MANAGER_START}, {"Passive", PERSISTENT}, {"PrimeRedirected", PERSISTENT}, {"PrimeType", PERSISTENT}, {"RecordFront", PERSISTENT}, {"RecordFrontLock", PERSISTENT}, // for the internal fleet {"ReleaseNotes", PERSISTENT}, {"ShouldDoUpdate", CLEAR_ON_MANAGER_START}, {"ShowDebugUI", PERSISTENT}, {"SnoozeUpdate", CLEAR_ON_MANAGER_START | CLEAR_ON_IGNITION_OFF}, {"SpeedLimitControl", PERSISTENT}, {"SpeedLimitPercOffset", PERSISTENT}, {"SshEnabled", PERSISTENT}, {"SubscriberInfo", PERSISTENT}, {"TermsVersion", PERSISTENT}, {"Timezone", PERSISTENT}, {"TrainingVersion", PERSISTENT}, {"TurnSpeedControl", PERSISTENT}, {"TurnVisionControl", PERSISTENT}, {"UpdateAvailable", CLEAR_ON_MANAGER_START}, {"UpdateFailedCount", CLEAR_ON_MANAGER_START}, {"Version", PERSISTENT}, {"VisionRadarToggle", PERSISTENT}, {"ApiCache_Device", PERSISTENT}, {"ApiCache_DriveStats", PERSISTENT}, {"ApiCache_NavDestinations", PERSISTENT}, {"ApiCache_Owner", PERSISTENT}, {"Offroad_CarUnrecognized", CLEAR_ON_MANAGER_START | CLEAR_ON_IGNITION_ON}, {"Offroad_ChargeDisabled", CLEAR_ON_MANAGER_START }, {"Offroad_ConnectivityNeeded", CLEAR_ON_MANAGER_START}, {"Offroad_ConnectivityNeededPrompt", CLEAR_ON_MANAGER_START}, {"Offroad_InvalidTime", CLEAR_ON_MANAGER_START}, {"Offroad_IsTakingSnapshot", CLEAR_ON_MANAGER_START}, {"Offroad_NeosUpdate", CLEAR_ON_MANAGER_START}, {"Offroad_NoFirmware", CLEAR_ON_MANAGER_START | CLEAR_ON_IGNITION_ON}, {"Offroad_StorageMissing", CLEAR_ON_MANAGER_START}, {"Offroad_TemperatureTooHigh", CLEAR_ON_MANAGER_START}, {"Offroad_UnofficialHardware", CLEAR_ON_MANAGER_START}, {"Offroad_UpdateFailed", CLEAR_ON_MANAGER_START}, // dp {"dp_api_custom", PERSISTENT}, {"dp_api_custom_url", PERSISTENT}, {"dp_atl", PERSISTENT}, {"dp_atl_op_long", PERSISTENT}, {"dp_dashcamd", PERSISTENT}, {"dp_auto_shutdown", PERSISTENT}, {"dp_auto_shutdown_in", PERSISTENT}, {"dp_updated", PERSISTENT}, {"dp_logger", PERSISTENT}, {"dp_athenad", PERSISTENT}, {"dp_uploader", PERSISTENT}, {"dp_hotspot_on_boot", PERSISTENT}, {"dp_lateral_mode", PERSISTENT}, {"dp_signal_off_delay", PERSISTENT}, {"dp_lc_min_mph", PERSISTENT}, {"dp_lc_auto_min_mph", PERSISTENT}, {"dp_lc_auto_delay", PERSISTENT}, {"dp_lane_less_mode_ctrl", PERSISTENT}, {"dp_lane_less_mode", PERSISTENT}, {"dp_allow_gas", PERSISTENT}, //{"dp_following_profile_ctrl", PERSISTENT}, //{"dp_following_profile", PERSISTENT}, {"dp_accel_profile_ctrl", PERSISTENT}, {"dp_accel_profile", PERSISTENT}, {"dp_gear_check", PERSISTENT}, {"dp_speed_check", PERSISTENT}, {"dp_temp_monitor", PERSISTENT}, {"dp_ui_display_mode", PERSISTENT}, {"dp_ui_speed", PERSISTENT}, {"dp_ui_event", PERSISTENT}, {"dp_ui_max_speed", PERSISTENT}, {"dp_ui_face", PERSISTENT}, {"dp_ui_lane", PERSISTENT}, {"dp_ui_lead", PERSISTENT}, {"dp_ui_side", PERSISTENT}, {"dp_ui_top", PERSISTENT}, {"dp_ui_blinker", PERSISTENT}, {"dp_ui_brightness", PERSISTENT}, {"dp_ui_volume", PERSISTENT}, {"dp_toyota_no_min_acc_limit", PERSISTENT}, {"dp_toyota_ldw", PERSISTENT}, {"dp_toyota_sng", PERSISTENT}, {"dp_toyota_zss", PERSISTENT}, {"dp_toyota_fp_btn_link", PERSISTENT}, {"dp_toyota_ap_btn_link", PERSISTENT}, {"dp_toyota_lkas_btn_link", PERSISTENT}, {"dp_toyota_disable_relay", PERSISTENT}, {"dp_toyota_cruise_override", PERSISTENT}, {"dp_toyota_cruise_override_vego", PERSISTENT}, {"dp_toyota_cruise_override_at", PERSISTENT}, {"dp_toyota_cruise_override_speed", PERSISTENT}, {"dp_hkg_smart_mdps", PERSISTENT}, {"dp_honda_eps_mod", PERSISTENT}, {"dp_honda_kmh_display", PERSISTENT}, {"dp_fan_mode", PERSISTENT}, {"dp_last_modified", PERSISTENT}, {"dp_camera_offset", PERSISTENT}, {"dp_path_offset", PERSISTENT}, {"dp_locale", PERSISTENT}, {"dp_reg", PERSISTENT}, {"dp_sr_learner", PERSISTENT}, {"dp_sr_custom", PERSISTENT}, {"dp_sr_stock", PERSISTENT}, {"dp_lqr", PERSISTENT}, {"dp_reset_live_param_on_start", PERSISTENT}, {"dp_jetson", PERSISTENT}, {"dp_car_assigned", PERSISTENT}, {"dp_car_list", PERSISTENT}, {"dp_no_batt", PERSISTENT}, {"dp_last_candidate", PERSISTENT}, {"dp_prebuilt", PERSISTENT}, {"dp_gpxd", PERSISTENT}, {"dp_mapd", PERSISTENT}, {"dp_otisserv", PERSISTENT}, {"dp_panda_no_gps", PERSISTENT}, {"dp_nav_mapbox_token_pk", PERSISTENT}, {"dp_nav_mapbox_token_sk", PERSISTENT}, {"dp_nav_full_screen", PERSISTENT}, {"dp_nav_gmap_enable", PERSISTENT}, {"dp_nav_gmap_key", PERSISTENT}, {"dp_nav_amap_enable", PERSISTENT}, {"dp_nav_amap_key", PERSISTENT}, {"dp_nav_amap_key_2", PERSISTENT}, {"dp_nav_style_day", PERSISTENT}, {"dp_nav_style_night", PERSISTENT}, {"dp_no_offroad_fix", PERSISTENT}, {"dp_ftpd", PERSISTENT}, {"dp_quiet_drive", PERSISTENT} }; } // namespace Params::Params(const std::string &path) { static std::string default_param_path = ensure_params_path(); params_path = path.empty() ? default_param_path : ensure_params_path(path); } bool Params::checkKey(const std::string &key) { return keys.find(key) != keys.end(); } ParamKeyType Params::getKeyType(const std::string &key) { return static_cast(keys[key]); } int Params::put(const char* key, const char* value, size_t value_size) { // Information about safely and atomically writing a file: https://lwn.net/Articles/457667/ // 1) Create temp file // 2) Write data to temp file // 3) fsync() the temp file // 4) rename the temp file to the real name // 5) fsync() the containing directory std::string tmp_path = params_path + "/.tmp_value_XXXXXX"; int tmp_fd = mkstemp((char*)tmp_path.c_str()); if (tmp_fd < 0) return -1; int result = -1; do { // Write value to temp. ssize_t bytes_written = HANDLE_EINTR(write(tmp_fd, value, value_size)); if (bytes_written < 0 || (size_t)bytes_written != value_size) { result = -20; break; } // fsync to force persist the changes. if ((result = fsync(tmp_fd)) < 0) break; FileLock file_lock(params_path + "/.lock"); // Move temp into place. if ((result = rename(tmp_path.c_str(), getParamPath(key).c_str())) < 0) break; // fsync parent directory result = fsync_dir(getParamPath()); } while (false); close(tmp_fd); ::unlink(tmp_path.c_str()); return result; } int Params::remove(const std::string &key) { FileLock file_lock(params_path + "/.lock"); int result = unlink(getParamPath(key).c_str()); if (result != 0) { return result; } return fsync_dir(getParamPath()); } std::string Params::get(const std::string &key, bool block) { if (!block) { return util::read_file(getParamPath(key)); } else { // blocking read until successful params_do_exit = 0; void (*prev_handler_sigint)(int) = std::signal(SIGINT, params_sig_handler); void (*prev_handler_sigterm)(int) = std::signal(SIGTERM, params_sig_handler); std::string value; while (!params_do_exit) { if (value = util::read_file(getParamPath(key)); !value.empty()) { break; } util::sleep_for(100); // 0.1 s } std::signal(SIGINT, prev_handler_sigint); std::signal(SIGTERM, prev_handler_sigterm); return value; } } std::map Params::readAll() { FileLock file_lock(params_path + "/.lock"); return util::read_files_in_dir(getParamPath()); } void Params::clearAll(ParamKeyType key_type) { FileLock file_lock(params_path + "/.lock"); std::string path; for (auto &[key, type] : keys) { if (type & key_type) { unlink(getParamPath(key).c_str()); } } fsync_dir(getParamPath()); } std::string Params::get_params_path() { return params_path; }