2021-05-09 13:15:17 +08:00
|
|
|
#include "selfdrive/common/params.h"
|
2020-01-18 03:01:02 +08:00
|
|
|
|
2021-05-09 13:15:17 +08:00
|
|
|
#include <dirent.h>
|
2020-01-18 03:01:02 +08:00
|
|
|
#include <sys/file.h>
|
2021-05-09 13:15:17 +08:00
|
|
|
|
2020-10-13 22:23:23 +08:00
|
|
|
#include <csignal>
|
2021-05-05 01:49:26 +08:00
|
|
|
#include <unordered_map>
|
2021-05-09 13:15:17 +08:00
|
|
|
|
|
|
|
#include "selfdrive/common/swaglog.h"
|
|
|
|
#include "selfdrive/common/util.h"
|
2021-05-06 20:39:05 +08:00
|
|
|
#include "selfdrive/hardware/hw.h"
|
2021-05-09 13:15:17 +08:00
|
|
|
|
2021-04-09 22:35:44 +08:00
|
|
|
namespace {
|
2020-01-18 03:01:02 +08:00
|
|
|
|
2020-10-13 22:23:23 +08:00
|
|
|
volatile sig_atomic_t params_do_exit = 0;
|
|
|
|
void params_sig_handler(int signal) {
|
|
|
|
params_do_exit = 1;
|
|
|
|
}
|
|
|
|
|
2021-11-01 18:22:34 +08:00
|
|
|
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);
|
2020-01-18 03:01:02 +08:00
|
|
|
}
|
2020-11-09 17:21:48 +08:00
|
|
|
return result;
|
2020-01-18 03:01:02 +08:00
|
|
|
}
|
|
|
|
|
2021-08-16 22:07:06 +08:00
|
|
|
bool create_params_path(const std::string ¶m_path, const std::string &key_path) {
|
2021-03-31 17:04:34 +08:00
|
|
|
// Make sure params path exists
|
2021-10-29 18:27:35 +08:00
|
|
|
if (!util::file_exists(param_path) && !util::create_directories(param_path, 0775)) {
|
2021-03-31 17:04:34 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// See if the symlink exists, otherwise create it
|
|
|
|
if (!util::file_exists(key_path)) {
|
|
|
|
// 1) Create temp folder
|
2021-11-01 18:22:34 +08:00
|
|
|
// 2) Symlink it to temp link
|
|
|
|
// 3) Move symlink to <params>/d
|
2021-03-31 17:04:34 +08:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2020-10-13 22:23:23 +08:00
|
|
|
|
2021-03-31 17:04:34 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-29 09:25:05 +08:00
|
|
|
return true;
|
2020-10-13 22:23:23 +08:00
|
|
|
}
|
|
|
|
|
2021-11-01 18:22:34 +08:00
|
|
|
std::string ensure_params_path(const std::string &path = {}) {
|
|
|
|
std::string params_path = path.empty() ? Path::params() : path;
|
2021-06-15 07:12:27 +08:00
|
|
|
if (!create_params_path(params_path, params_path + "/d")) {
|
|
|
|
throw std::runtime_error(util::string_format("Failed to ensure params path, errno=%d", errno));
|
|
|
|
}
|
2021-11-01 18:22:34 +08:00
|
|
|
return params_path;
|
2021-06-15 07:12:27 +08:00
|
|
|
}
|
|
|
|
|
2021-04-09 22:35:44 +08:00
|
|
|
class FileLock {
|
2021-11-01 18:22:34 +08:00
|
|
|
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);
|
2021-04-09 22:35:44 +08:00
|
|
|
}
|
|
|
|
}
|
2021-11-01 18:22:34 +08:00
|
|
|
~FileLock() { close(fd_); }
|
2021-04-09 22:35:44 +08:00
|
|
|
|
|
|
|
private:
|
2021-11-01 18:22:34 +08:00
|
|
|
int fd_ = -1;
|
2021-04-09 22:35:44 +08:00
|
|
|
};
|
|
|
|
|
2021-05-05 01:49:26 +08:00
|
|
|
std::unordered_map<std::string, uint32_t> keys = {
|
2021-08-03 11:16:38 +08:00
|
|
|
{"AccessToken", CLEAR_ON_MANAGER_START | DONT_LOG},
|
2021-05-05 01:49:26 +08:00
|
|
|
{"AthenadPid", PERSISTENT},
|
2021-09-23 13:25:08 +08:00
|
|
|
{"BootedOnroad", CLEAR_ON_MANAGER_START | CLEAR_ON_IGNITION_OFF},
|
2021-05-05 01:49:26 +08:00
|
|
|
{"CalibrationParams", PERSISTENT},
|
|
|
|
{"CarBatteryCapacity", PERSISTENT},
|
2021-06-04 06:21:53 +08:00
|
|
|
{"CarParams", CLEAR_ON_MANAGER_START | CLEAR_ON_PANDA_DISCONNECT | CLEAR_ON_IGNITION_ON},
|
2021-05-05 01:49:26 +08:00
|
|
|
{"CarParamsCache", CLEAR_ON_MANAGER_START | CLEAR_ON_PANDA_DISCONNECT},
|
2021-06-04 06:21:53 +08:00
|
|
|
{"CarVin", CLEAR_ON_MANAGER_START | CLEAR_ON_PANDA_DISCONNECT | CLEAR_ON_IGNITION_ON},
|
2021-05-05 01:49:26 +08:00
|
|
|
{"CommunityFeaturesToggle", PERSISTENT},
|
2021-09-24 07:46:02 +08:00
|
|
|
{"CompletedTrainingVersion", PERSISTENT},
|
2021-06-04 06:21:53 +08:00
|
|
|
{"ControlsReady", CLEAR_ON_MANAGER_START | CLEAR_ON_PANDA_DISCONNECT | CLEAR_ON_IGNITION_ON},
|
2021-06-29 12:44:40 +08:00
|
|
|
{"CurrentRoute", CLEAR_ON_MANAGER_START | CLEAR_ON_IGNITION_ON},
|
2021-05-05 01:49:26 +08:00
|
|
|
{"DisablePowerDown", PERSISTENT},
|
2021-09-24 08:19:47 +08:00
|
|
|
{"DisableRadar_Allow", PERSISTENT},
|
2021-09-24 07:46:02 +08:00
|
|
|
{"DisableRadar", PERSISTENT}, // WARNING: THIS DISABLES AEB
|
2021-05-05 01:49:26 +08:00
|
|
|
{"DisableUpdates", PERSISTENT},
|
|
|
|
{"DongleId", PERSISTENT},
|
2021-11-13 01:10:34 +08:00
|
|
|
{"DoReboot", CLEAR_ON_MANAGER_START},
|
|
|
|
{"DoShutdown", CLEAR_ON_MANAGER_START},
|
2021-09-24 07:46:02 +08:00
|
|
|
{"DoUninstall", CLEAR_ON_MANAGER_START},
|
|
|
|
{"EnableWideCamera", CLEAR_ON_MANAGER_START},
|
|
|
|
{"EndToEndToggle", PERSISTENT},
|
|
|
|
{"ForcePowerDown", CLEAR_ON_MANAGER_START},
|
2021-05-05 01:49:26 +08:00
|
|
|
{"GitBranch", PERSISTENT},
|
|
|
|
{"GitCommit", PERSISTENT},
|
2021-09-24 07:46:02 +08:00
|
|
|
{"GitDiff", PERSISTENT},
|
2021-05-05 01:49:26 +08:00
|
|
|
{"GithubSshKeys", PERSISTENT},
|
|
|
|
{"GithubUsername", PERSISTENT},
|
2021-09-24 07:46:02 +08:00
|
|
|
{"GitRemote", PERSISTENT},
|
2021-10-08 14:47:23 +08:00
|
|
|
{"GsmApn", PERSISTENT},
|
2021-09-02 05:33:06 +08:00
|
|
|
{"GsmRoaming", PERSISTENT},
|
2021-05-05 01:49:26 +08:00
|
|
|
{"HardwareSerial", PERSISTENT},
|
|
|
|
{"HasAcceptedTerms", PERSISTENT},
|
2021-11-10 23:05:49 +08:00
|
|
|
{"HasPrime", PERSISTENT},
|
2021-05-05 01:49:26 +08:00
|
|
|
{"IMEI", PERSISTENT},
|
2021-09-15 11:24:41 +08:00
|
|
|
{"InstallDate", PERSISTENT},
|
|
|
|
{"IsDriverViewEnabled", CLEAR_ON_MANAGER_START},
|
2021-05-05 01:49:26 +08:00
|
|
|
{"IsLdwEnabled", PERSISTENT},
|
|
|
|
{"IsMetric", PERSISTENT},
|
|
|
|
{"IsOffroad", CLEAR_ON_MANAGER_START},
|
2021-06-05 13:04:26 +08:00
|
|
|
{"IsOnroad", PERSISTENT},
|
2021-05-05 01:49:26 +08:00
|
|
|
{"IsRHD", PERSISTENT},
|
|
|
|
{"IsTakingSnapshot", CLEAR_ON_MANAGER_START},
|
|
|
|
{"IsUpdateAvailable", CLEAR_ON_MANAGER_START},
|
2021-09-24 07:46:02 +08:00
|
|
|
{"JoystickDebugMode", CLEAR_ON_MANAGER_START | CLEAR_ON_IGNITION_OFF},
|
2021-07-27 04:50:58 +08:00
|
|
|
{"LastAthenaPingTime", CLEAR_ON_MANAGER_START},
|
2021-05-05 01:49:26 +08:00
|
|
|
{"LastGPSPosition", PERSISTENT},
|
|
|
|
{"LastUpdateException", PERSISTENT},
|
|
|
|
{"LastUpdateTime", PERSISTENT},
|
|
|
|
{"LiveParameters", PERSISTENT},
|
2021-06-04 19:45:42 +08:00
|
|
|
{"NavDestination", CLEAR_ON_MANAGER_START | CLEAR_ON_IGNITION_OFF},
|
2021-06-10 15:59:34 +08:00
|
|
|
{"NavSettingTime24h", PERSISTENT},
|
2021-05-05 01:49:26 +08:00
|
|
|
{"OpenpilotEnabledToggle", PERSISTENT},
|
2021-08-18 18:48:01 +08:00
|
|
|
{"PandaHeartbeatLost", CLEAR_ON_MANAGER_START | CLEAR_ON_IGNITION_OFF},
|
2021-05-05 01:49:26 +08:00
|
|
|
{"Passive", PERSISTENT},
|
2021-07-27 04:50:58 +08:00
|
|
|
{"PrimeRedirected", PERSISTENT},
|
2021-05-05 01:49:26 +08:00
|
|
|
{"RecordFront", PERSISTENT},
|
|
|
|
{"RecordFrontLock", PERSISTENT}, // for the internal fleet
|
|
|
|
{"ReleaseNotes", PERSISTENT},
|
|
|
|
{"ShouldDoUpdate", CLEAR_ON_MANAGER_START},
|
2021-11-12 07:32:25 +08:00
|
|
|
{"SnoozeUpdate", CLEAR_ON_MANAGER_START | CLEAR_ON_IGNITION_OFF},
|
2021-05-05 01:49:26 +08:00
|
|
|
{"SshEnabled", PERSISTENT},
|
2021-09-24 07:46:02 +08:00
|
|
|
{"SubscriberInfo", PERSISTENT},
|
2021-05-05 01:49:26 +08:00
|
|
|
{"TermsVersion", PERSISTENT},
|
|
|
|
{"Timezone", PERSISTENT},
|
|
|
|
{"TrainingVersion", PERSISTENT},
|
|
|
|
{"UpdateAvailable", CLEAR_ON_MANAGER_START},
|
|
|
|
{"UpdateFailedCount", CLEAR_ON_MANAGER_START},
|
2021-09-24 07:46:02 +08:00
|
|
|
{"UploadRaw", PERSISTENT},
|
2021-05-05 01:49:26 +08:00
|
|
|
{"Version", PERSISTENT},
|
|
|
|
{"VisionRadarToggle", PERSISTENT},
|
2021-09-24 07:47:00 +08:00
|
|
|
{"ApiCache_Device", PERSISTENT},
|
|
|
|
{"ApiCache_DriveStats", PERSISTENT},
|
|
|
|
{"ApiCache_NavDestinations", PERSISTENT},
|
|
|
|
{"ApiCache_Owner", PERSISTENT},
|
2021-11-18 12:29:00 +08:00
|
|
|
{"Offroad_CarUnrecognized", CLEAR_ON_MANAGER_START | CLEAR_ON_IGNITION_ON},
|
2021-05-05 01:49:26 +08:00
|
|
|
{"Offroad_ChargeDisabled", CLEAR_ON_MANAGER_START | CLEAR_ON_PANDA_DISCONNECT},
|
|
|
|
{"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},
|
2021-11-18 12:29:00 +08:00
|
|
|
{"Offroad_NoFirmware", CLEAR_ON_MANAGER_START | CLEAR_ON_IGNITION_ON},
|
2021-10-27 10:34:08 +08:00
|
|
|
{"Offroad_StorageMissing", CLEAR_ON_MANAGER_START},
|
2021-09-24 07:47:00 +08:00
|
|
|
{"Offroad_TemperatureTooHigh", CLEAR_ON_MANAGER_START},
|
|
|
|
{"Offroad_UnofficialHardware", CLEAR_ON_MANAGER_START},
|
|
|
|
{"Offroad_UpdateFailed", CLEAR_ON_MANAGER_START},
|
2021-05-05 01:49:26 +08:00
|
|
|
};
|
|
|
|
|
2021-04-09 22:35:44 +08:00
|
|
|
} // namespace
|
|
|
|
|
2021-11-01 18:22:34 +08:00
|
|
|
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);
|
2020-10-13 22:23:23 +08:00
|
|
|
}
|
|
|
|
|
2021-05-05 01:49:26 +08:00
|
|
|
bool Params::checkKey(const std::string &key) {
|
|
|
|
return keys.find(key) != keys.end();
|
|
|
|
}
|
|
|
|
|
2021-08-03 11:16:38 +08:00
|
|
|
ParamKeyType Params::getKeyType(const std::string &key) {
|
|
|
|
return static_cast<ParamKeyType>(keys[key]);
|
|
|
|
}
|
|
|
|
|
2021-03-30 18:54:59 +08:00
|
|
|
int Params::put(const char* key, const char* value, size_t value_size) {
|
2020-01-18 03:01:02 +08:00
|
|
|
// 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
|
2021-04-09 22:35:44 +08:00
|
|
|
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;
|
|
|
|
}
|
2020-01-18 03:01:02 +08:00
|
|
|
|
2021-04-09 22:35:44 +08:00
|
|
|
// fsync to force persist the changes.
|
|
|
|
if ((result = fsync(tmp_fd)) < 0) break;
|
2020-01-18 03:01:02 +08:00
|
|
|
|
2021-11-01 18:22:34 +08:00
|
|
|
FileLock file_lock(params_path + "/.lock");
|
2020-01-18 03:01:02 +08:00
|
|
|
|
2021-04-09 22:35:44 +08:00
|
|
|
// Move temp into place.
|
2021-11-01 18:22:34 +08:00
|
|
|
if ((result = rename(tmp_path.c_str(), getParamPath(key).c_str())) < 0) break;
|
2020-01-18 03:01:02 +08:00
|
|
|
|
2021-04-09 22:35:44 +08:00
|
|
|
// fsync parent directory
|
2021-11-01 18:22:34 +08:00
|
|
|
result = fsync_dir(getParamPath());
|
2021-06-10 12:09:21 +08:00
|
|
|
} while (false);
|
2020-01-18 03:01:02 +08:00
|
|
|
|
2021-04-09 22:35:44 +08:00
|
|
|
close(tmp_fd);
|
2021-08-31 07:31:27 +08:00
|
|
|
::unlink(tmp_path.c_str());
|
2020-01-18 03:01:02 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-11-01 18:22:34 +08:00
|
|
|
int Params::remove(const std::string &key) {
|
|
|
|
FileLock file_lock(params_path + "/.lock");
|
|
|
|
int result = unlink(getParamPath(key).c_str());
|
2020-01-18 03:01:02 +08:00
|
|
|
if (result != 0) {
|
2021-04-09 22:35:44 +08:00
|
|
|
return result;
|
2020-01-18 03:01:02 +08:00
|
|
|
}
|
2021-11-01 18:22:34 +08:00
|
|
|
return fsync_dir(getParamPath());
|
2020-01-18 03:01:02 +08:00
|
|
|
}
|
|
|
|
|
2021-11-01 18:22:34 +08:00
|
|
|
std::string Params::get(const std::string &key, bool block) {
|
2021-03-30 18:54:59 +08:00
|
|
|
if (!block) {
|
2021-11-01 18:22:34 +08:00
|
|
|
return util::read_file(getParamPath(key));
|
2020-10-13 22:23:23 +08:00
|
|
|
} else {
|
2021-03-30 18:54:59 +08:00
|
|
|
// 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) {
|
2021-11-01 18:22:34 +08:00
|
|
|
if (value = util::read_file(getParamPath(key)); !value.empty()) {
|
2021-03-30 18:54:59 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
util::sleep_for(100); // 0.1 s
|
2020-01-18 03:01:02 +08:00
|
|
|
}
|
2020-10-13 22:23:23 +08:00
|
|
|
|
2021-03-30 18:54:59 +08:00
|
|
|
std::signal(SIGINT, prev_handler_sigint);
|
|
|
|
std::signal(SIGTERM, prev_handler_sigterm);
|
|
|
|
return value;
|
|
|
|
}
|
2020-01-18 03:01:02 +08:00
|
|
|
}
|
|
|
|
|
2021-08-02 16:59:54 +08:00
|
|
|
std::map<std::string, std::string> Params::readAll() {
|
2021-11-01 18:22:34 +08:00
|
|
|
FileLock file_lock(params_path + "/.lock");
|
|
|
|
return util::read_files_in_dir(getParamPath());
|
2020-01-18 03:01:02 +08:00
|
|
|
}
|
2021-05-05 01:49:26 +08:00
|
|
|
|
|
|
|
void Params::clearAll(ParamKeyType key_type) {
|
2021-11-01 18:22:34 +08:00
|
|
|
FileLock file_lock(params_path + "/.lock");
|
2021-08-31 10:47:47 +08:00
|
|
|
|
|
|
|
std::string path;
|
2021-05-05 01:49:26 +08:00
|
|
|
for (auto &[key, type] : keys) {
|
|
|
|
if (type & key_type) {
|
2021-11-01 18:22:34 +08:00
|
|
|
unlink(getParamPath(key).c_str());
|
2021-05-05 01:49:26 +08:00
|
|
|
}
|
|
|
|
}
|
2021-08-31 10:47:47 +08:00
|
|
|
|
2021-11-01 18:22:34 +08:00
|
|
|
fsync_dir(getParamPath());
|
2021-05-05 01:49:26 +08:00
|
|
|
}
|