2021-05-09 13:15:17 +08:00
|
|
|
#include "selfdrive/common/params.h"
|
2020-01-18 03:01:02 +08:00
|
|
|
|
|
|
|
#ifndef _GNU_SOURCE
|
|
|
|
#define _GNU_SOURCE
|
|
|
|
#endif // _GNU_SOURCE
|
|
|
|
|
2021-05-09 13:15:17 +08:00
|
|
|
#include <dirent.h>
|
2020-01-18 03:01:02 +08:00
|
|
|
#include <sys/file.h>
|
|
|
|
#include <sys/stat.h>
|
2021-05-09 13:15:17 +08:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2020-10-13 22:23:23 +08:00
|
|
|
#include <csignal>
|
2021-06-09 04:46:22 +08:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
2021-05-09 13:15:17 +08:00
|
|
|
#include <mutex>
|
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-06-11 16:17:52 +08:00
|
|
|
int fsync_dir(const char* path) {
|
2021-04-09 22:35:44 +08:00
|
|
|
int fd = HANDLE_EINTR(open(path, O_RDONLY, 0755));
|
2021-06-11 16:17:52 +08:00
|
|
|
if (fd < 0) {
|
2020-11-09 17:21:48 +08:00
|
|
|
return -1;
|
2020-01-18 03:01:02 +08:00
|
|
|
}
|
|
|
|
|
2020-11-09 17:21:48 +08:00
|
|
|
int result = fsync(fd);
|
|
|
|
int result_close = close(fd);
|
2020-01-18 03:01:02 +08:00
|
|
|
if (result_close < 0) {
|
2020-11-09 17:21:48 +08:00
|
|
|
result = result_close;
|
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-04-09 22:35:44 +08:00
|
|
|
int mkdir_p(std::string path) {
|
2020-10-13 22:23:23 +08:00
|
|
|
char * _path = (char *)path.c_str();
|
|
|
|
|
|
|
|
for (char *p = _path + 1; *p; p++) {
|
|
|
|
if (*p == '/') {
|
|
|
|
*p = '\0'; // Temporarily truncate
|
2021-08-29 09:25:05 +08:00
|
|
|
if (mkdir(_path, 0775) != 0) {
|
2020-10-13 22:23:23 +08:00
|
|
|
if (errno != EEXIST) return -1;
|
|
|
|
}
|
|
|
|
*p = '/';
|
|
|
|
}
|
|
|
|
}
|
2021-08-29 09:25:05 +08:00
|
|
|
if (mkdir(_path, 0775) != 0) {
|
2020-10-13 22:23:23 +08:00
|
|
|
if (errno != EEXIST) return -1;
|
2020-05-09 10:39:18 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
if (!util::file_exists(param_path) && mkdir_p(param_path) != 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// See if the symlink exists, otherwise create it
|
|
|
|
if (!util::file_exists(key_path)) {
|
|
|
|
// 1) Create temp folder
|
|
|
|
// 2) Set permissions
|
|
|
|
// 3) Symlink it to temp link
|
|
|
|
// 4) Move symlink to <params>/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;
|
|
|
|
}
|
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-08-16 22:07:06 +08:00
|
|
|
void ensure_params_path(const std::string ¶ms_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-04-09 22:35:44 +08:00
|
|
|
class FileLock {
|
|
|
|
public:
|
|
|
|
FileLock(const std::string& file_name, int op) : fn_(file_name), op_(op) {}
|
|
|
|
|
|
|
|
void lock() {
|
|
|
|
fd_ = HANDLE_EINTR(open(fn_.c_str(), O_CREAT, 0775));
|
|
|
|
if (fd_ < 0) {
|
|
|
|
LOGE("Failed to open lock file %s, errno=%d", fn_.c_str(), errno);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (HANDLE_EINTR(flock(fd_, op_)) < 0) {
|
|
|
|
LOGE("Failed to lock file %s, errno=%d", fn_.c_str(), errno);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void unlock() { close(fd_); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
int fd_ = -1, op_;
|
|
|
|
std::string fn_;
|
|
|
|
};
|
|
|
|
|
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
|
|
|
{"ApiCache_DriveStats", PERSISTENT},
|
|
|
|
{"ApiCache_Device", PERSISTENT},
|
|
|
|
{"ApiCache_Owner", PERSISTENT},
|
2021-06-22 19:25:22 +08:00
|
|
|
{"ApiCache_NavDestinations", PERSISTENT},
|
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-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-06-22 22:28:11 +08:00
|
|
|
{"DisableRadar", PERSISTENT}, // WARNING: THIS DISABLES AEB
|
2021-05-05 01:49:26 +08:00
|
|
|
{"EndToEndToggle", PERSISTENT},
|
|
|
|
{"CompletedTrainingVersion", PERSISTENT},
|
|
|
|
{"DisablePowerDown", PERSISTENT},
|
|
|
|
{"DisableUpdates", PERSISTENT},
|
2021-07-21 15:58:42 +08:00
|
|
|
{"EnableWideCamera", CLEAR_ON_MANAGER_START},
|
2021-05-05 01:49:26 +08:00
|
|
|
{"DoUninstall", CLEAR_ON_MANAGER_START},
|
|
|
|
{"DongleId", PERSISTENT},
|
|
|
|
{"GitDiff", PERSISTENT},
|
|
|
|
{"GitBranch", PERSISTENT},
|
|
|
|
{"GitCommit", PERSISTENT},
|
|
|
|
{"GitRemote", PERSISTENT},
|
|
|
|
{"GithubSshKeys", PERSISTENT},
|
|
|
|
{"GithubUsername", PERSISTENT},
|
2021-09-02 05:33:06 +08:00
|
|
|
{"GsmRoaming", PERSISTENT},
|
2021-05-05 01:49:26 +08:00
|
|
|
{"HardwareSerial", PERSISTENT},
|
|
|
|
{"HasAcceptedTerms", PERSISTENT},
|
|
|
|
{"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-06-04 06:24:30 +08:00
|
|
|
{"UploadRaw", PERSISTENT},
|
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-08-03 11:16:38 +08:00
|
|
|
{"MapboxToken", PERSISTENT | DONT_LOG},
|
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},
|
|
|
|
{"PandaFirmware", CLEAR_ON_MANAGER_START | CLEAR_ON_PANDA_DISCONNECT},
|
|
|
|
{"PandaFirmwareHex", CLEAR_ON_MANAGER_START | CLEAR_ON_PANDA_DISCONNECT},
|
|
|
|
{"PandaDongleId", CLEAR_ON_MANAGER_START | CLEAR_ON_PANDA_DISCONNECT},
|
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},
|
|
|
|
{"SubscriberInfo", PERSISTENT},
|
|
|
|
{"SshEnabled", PERSISTENT},
|
|
|
|
{"TermsVersion", PERSISTENT},
|
|
|
|
{"Timezone", PERSISTENT},
|
|
|
|
{"TrainingVersion", PERSISTENT},
|
|
|
|
{"UpdateAvailable", CLEAR_ON_MANAGER_START},
|
|
|
|
{"UpdateFailedCount", CLEAR_ON_MANAGER_START},
|
|
|
|
{"Version", PERSISTENT},
|
|
|
|
{"VisionRadarToggle", PERSISTENT},
|
|
|
|
{"Offroad_ChargeDisabled", CLEAR_ON_MANAGER_START | CLEAR_ON_PANDA_DISCONNECT},
|
|
|
|
{"Offroad_ConnectivityNeeded", CLEAR_ON_MANAGER_START},
|
|
|
|
{"Offroad_ConnectivityNeededPrompt", CLEAR_ON_MANAGER_START},
|
|
|
|
{"Offroad_TemperatureTooHigh", CLEAR_ON_MANAGER_START},
|
|
|
|
{"Offroad_PandaFirmwareMismatch", CLEAR_ON_MANAGER_START | CLEAR_ON_PANDA_DISCONNECT},
|
|
|
|
{"Offroad_InvalidTime", CLEAR_ON_MANAGER_START},
|
|
|
|
{"Offroad_IsTakingSnapshot", CLEAR_ON_MANAGER_START},
|
|
|
|
{"Offroad_NeosUpdate", CLEAR_ON_MANAGER_START},
|
|
|
|
{"Offroad_UpdateFailed", CLEAR_ON_MANAGER_START},
|
|
|
|
{"Offroad_HardwareUnsupported", CLEAR_ON_MANAGER_START},
|
2021-05-11 18:18:45 +08:00
|
|
|
{"Offroad_UnofficialHardware", CLEAR_ON_MANAGER_START},
|
2021-05-18 04:35:24 +08:00
|
|
|
{"Offroad_NvmeMissing", CLEAR_ON_MANAGER_START},
|
2021-05-05 01:49:26 +08:00
|
|
|
{"ForcePowerDown", CLEAR_ON_MANAGER_START},
|
2021-06-12 05:33:17 +08:00
|
|
|
{"JoystickDebugMode", CLEAR_ON_MANAGER_START | CLEAR_ON_IGNITION_OFF},
|
2021-05-05 01:49:26 +08:00
|
|
|
};
|
|
|
|
|
2021-04-09 22:35:44 +08:00
|
|
|
} // namespace
|
|
|
|
|
2021-08-21 07:57:45 +08:00
|
|
|
Params::Params() : params_path(Path::params()) {
|
|
|
|
static std::once_flag once_flag;
|
|
|
|
std::call_once(once_flag, ensure_params_path, params_path);
|
|
|
|
}
|
2021-03-31 17:04:34 +08:00
|
|
|
|
|
|
|
Params::Params(const std::string &path) : params_path(path) {
|
2021-08-21 07:57:45 +08:00
|
|
|
ensure_params_path(params_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-04-09 22:35:44 +08:00
|
|
|
FileLock file_lock(params_path + "/.lock", LOCK_EX);
|
|
|
|
std::lock_guard<FileLock> lk(file_lock);
|
2020-01-18 03:01:02 +08:00
|
|
|
|
2021-04-09 22:35:44 +08:00
|
|
|
// Move temp into place.
|
|
|
|
std::string path = params_path + "/d/" + std::string(key);
|
|
|
|
if ((result = rename(tmp_path.c_str(), path.c_str())) < 0) break;
|
2020-01-18 03:01:02 +08:00
|
|
|
|
2021-04-09 22:35:44 +08:00
|
|
|
// fsync parent directory
|
|
|
|
path = params_path + "/d";
|
|
|
|
result = fsync_dir(path.c_str());
|
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-03-30 18:54:59 +08:00
|
|
|
int Params::remove(const char *key) {
|
2021-04-09 22:35:44 +08:00
|
|
|
FileLock file_lock(params_path + "/.lock", LOCK_EX);
|
|
|
|
std::lock_guard<FileLock> lk(file_lock);
|
2020-01-18 03:01:02 +08:00
|
|
|
// Delete value.
|
2021-04-09 22:35:44 +08:00
|
|
|
std::string path = params_path + "/d/" + key;
|
2021-08-31 10:47:47 +08:00
|
|
|
int result = unlink(path.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
|
|
|
}
|
|
|
|
// fsync parent directory
|
2020-10-13 22:23:23 +08:00
|
|
|
path = params_path + "/d";
|
2021-04-09 22:35:44 +08:00
|
|
|
return fsync_dir(path.c_str());
|
2020-01-18 03:01:02 +08:00
|
|
|
}
|
|
|
|
|
2021-03-30 18:54:59 +08:00
|
|
|
std::string Params::get(const char *key, bool block) {
|
|
|
|
std::string path = params_path + "/d/" + key;
|
|
|
|
if (!block) {
|
|
|
|
return util::read_file(path);
|
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) {
|
|
|
|
if (value = util::read_file(path); !value.empty()) {
|
|
|
|
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-04-09 22:35:44 +08:00
|
|
|
FileLock file_lock(params_path + "/.lock", LOCK_SH);
|
|
|
|
std::lock_guard<FileLock> lk(file_lock);
|
2020-01-18 03:01:02 +08:00
|
|
|
|
2020-10-13 22:23:23 +08:00
|
|
|
std::string key_path = params_path + "/d";
|
2021-08-02 16:59:54 +08:00
|
|
|
return util::read_files_in_dir(key_path);
|
2020-01-18 03:01:02 +08:00
|
|
|
}
|
2021-05-05 01:49:26 +08:00
|
|
|
|
|
|
|
void Params::clearAll(ParamKeyType key_type) {
|
2021-08-31 10:47:47 +08:00
|
|
|
FileLock file_lock(params_path + "/.lock", LOCK_EX);
|
|
|
|
std::lock_guard<FileLock> lk(file_lock);
|
|
|
|
|
|
|
|
std::string path;
|
2021-05-05 01:49:26 +08:00
|
|
|
for (auto &[key, type] : keys) {
|
|
|
|
if (type & key_type) {
|
2021-08-31 10:47:47 +08:00
|
|
|
path = params_path + "/d/" + key;
|
|
|
|
unlink(path.c_str());
|
2021-05-05 01:49:26 +08:00
|
|
|
}
|
|
|
|
}
|
2021-08-31 10:47:47 +08:00
|
|
|
|
|
|
|
// fsync parent directory
|
|
|
|
path = params_path + "/d";
|
|
|
|
fsync_dir(path.c_str());
|
2021-05-05 01:49:26 +08:00
|
|
|
}
|