2022-05-19 05:11:57 +08:00
|
|
|
#include "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
|
|
|
|
2022-09-16 06:43:27 +08:00
|
|
|
#include <algorithm>
|
2023-12-14 12:36:01 +08:00
|
|
|
#include <cassert>
|
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
|
|
|
|
2023-12-14 12:36:01 +08:00
|
|
|
#include "common/queue.h"
|
2022-05-19 05:11:57 +08:00
|
|
|
#include "common/swaglog.h"
|
|
|
|
#include "common/util.h"
|
2022-06-12 07:38:24 +08:00
|
|
|
#include "system/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
|
|
|
}
|
|
|
|
|
2022-05-17 06:58:13 +08:00
|
|
|
std::string ensure_params_path(const std::string &prefix, const std::string &path = {}) {
|
2021-11-01 18:22:34 +08:00
|
|
|
std::string params_path = path.empty() ? Path::params() : path;
|
2022-05-17 06:58:13 +08:00
|
|
|
if (!create_params_path(params_path, params_path + prefix)) {
|
2023-09-20 07:22:33 +08:00
|
|
|
throw std::runtime_error(util::string_format(
|
|
|
|
"Failed to ensure params path, errno=%d, path=%s, param_prefix=%s",
|
|
|
|
errno, params_path.c_str(), prefix.c_str()));
|
2021-06-15 07:12:27 +08:00
|
|
|
}
|
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},
|
2023-06-20 06:26:52 +08:00
|
|
|
{"ApiCache_Device", PERSISTENT},
|
|
|
|
{"ApiCache_NavDestinations", PERSISTENT},
|
2022-08-27 14:11:46 +08:00
|
|
|
{"AssistNowToken", PERSISTENT},
|
2021-05-05 01:49:26 +08:00
|
|
|
{"AthenadPid", PERSISTENT},
|
2021-12-14 20:29:17 +08:00
|
|
|
{"AthenadUploadQueue", PERSISTENT},
|
2021-05-05 01:49:26 +08:00
|
|
|
{"CalibrationParams", PERSISTENT},
|
2022-09-01 07:40:25 +08:00
|
|
|
{"CameraDebugExpGain", CLEAR_ON_MANAGER_START},
|
|
|
|
{"CameraDebugExpTime", CLEAR_ON_MANAGER_START},
|
2021-05-05 01:49:26 +08:00
|
|
|
{"CarBatteryCapacity", PERSISTENT},
|
2023-05-10 12:25:23 +08:00
|
|
|
{"CarParams", CLEAR_ON_MANAGER_START | CLEAR_ON_ONROAD_TRANSITION},
|
2022-01-14 21:13:18 +08:00
|
|
|
{"CarParamsCache", CLEAR_ON_MANAGER_START},
|
2022-09-08 08:12:49 +08:00
|
|
|
{"CarParamsPersistent", PERSISTENT},
|
2023-11-09 07:25:41 +08:00
|
|
|
{"CarParamsPrevRoute", PERSISTENT},
|
2023-05-10 12:25:23 +08:00
|
|
|
{"CarVin", CLEAR_ON_MANAGER_START | CLEAR_ON_ONROAD_TRANSITION},
|
2021-09-24 07:46:02 +08:00
|
|
|
{"CompletedTrainingVersion", PERSISTENT},
|
2023-05-10 12:25:23 +08:00
|
|
|
{"ControlsReady", CLEAR_ON_MANAGER_START | CLEAR_ON_ONROAD_TRANSITION},
|
2023-03-22 08:28:38 +08:00
|
|
|
{"CurrentBootlog", PERSISTENT},
|
2023-05-10 12:25:23 +08:00
|
|
|
{"CurrentRoute", CLEAR_ON_MANAGER_START | CLEAR_ON_ONROAD_TRANSITION},
|
|
|
|
{"DisableLogging", CLEAR_ON_MANAGER_START | CLEAR_ON_ONROAD_TRANSITION},
|
2021-05-05 01:49:26 +08:00
|
|
|
{"DisablePowerDown", PERSISTENT},
|
|
|
|
{"DisableUpdates", PERSISTENT},
|
2022-04-05 12:59:52 +08:00
|
|
|
{"DisengageOnAccelerator", PERSISTENT},
|
2023-07-24 00:28:22 +08:00
|
|
|
{"DmModelInitialized", CLEAR_ON_ONROAD_TRANSITION},
|
2021-05-05 01:49:26 +08:00
|
|
|
{"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},
|
2023-12-02 15:57:54 +08:00
|
|
|
{"ExperimentalLongitudinalEnabled", PERSISTENT | DEVELOPMENT_ONLY},
|
2023-06-20 06:26:52 +08:00
|
|
|
{"ExperimentalMode", PERSISTENT},
|
|
|
|
{"ExperimentalModeConfirmed", PERSISTENT},
|
2023-05-10 12:25:23 +08:00
|
|
|
{"FirmwareQueryDone", CLEAR_ON_MANAGER_START | CLEAR_ON_ONROAD_TRANSITION},
|
2023-06-09 10:36:23 +08:00
|
|
|
{"ForcePowerDown", PERSISTENT},
|
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},
|
2022-10-02 05:47:06 +08:00
|
|
|
{"GsmMetered", 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},
|
2022-01-11 02:54:07 +08:00
|
|
|
{"IsEngaged", PERSISTENT},
|
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},
|
2022-07-30 04:52:24 +08:00
|
|
|
{"IsRhdDetected", PERSISTENT},
|
2023-06-20 06:26:52 +08:00
|
|
|
{"IsReleaseBranch", CLEAR_ON_MANAGER_START},
|
2021-05-05 01:49:26 +08:00
|
|
|
{"IsTakingSnapshot", CLEAR_ON_MANAGER_START},
|
2022-07-19 12:18:10 +08:00
|
|
|
{"IsTestedBranch", CLEAR_ON_MANAGER_START},
|
2023-05-10 12:25:23 +08:00
|
|
|
{"JoystickDebugMode", CLEAR_ON_MANAGER_START | CLEAR_ON_OFFROAD_TRANSITION},
|
2022-07-09 11:25:54 +08:00
|
|
|
{"LanguageSetting", PERSISTENT},
|
2021-07-27 04:50:58 +08:00
|
|
|
{"LastAthenaPingTime", CLEAR_ON_MANAGER_START},
|
2021-05-05 01:49:26 +08:00
|
|
|
{"LastGPSPosition", PERSISTENT},
|
2022-01-15 00:55:51 +08:00
|
|
|
{"LastManagerExitReason", CLEAR_ON_MANAGER_START},
|
2023-06-20 06:26:52 +08:00
|
|
|
{"LastOffroadStatusPacket", CLEAR_ON_MANAGER_START | CLEAR_ON_OFFROAD_TRANSITION},
|
2021-11-29 22:51:27 +08:00
|
|
|
{"LastPowerDropDetected", CLEAR_ON_MANAGER_START},
|
2022-04-23 05:43:44 +08:00
|
|
|
{"LastUpdateException", CLEAR_ON_MANAGER_START},
|
2021-05-05 01:49:26 +08:00
|
|
|
{"LastUpdateTime", PERSISTENT},
|
|
|
|
{"LiveParameters", PERSISTENT},
|
Live torque (#25456)
* wip torqued
* add basic logic
* setup in manager
* check sanity and publish msg
* add first order filter to outputs
* wire up controlsd, and update gains
* rename intercept to offset
* add cloudlog, live values are not updated
* fix bugs, do not reset points for now
* fix crashes
* rename to main
* fix bugs, works offline
* fix float in cereal bug
* add latacc filter
* randomly choose points, approx for iid
* add variable decay
* local param to capnp instead of dict
* verify works in replay
* use torqued output in controlsd
* use in controlsd; use points from past routes
* controlsd bugfix
* filter before updating gains, needs to be replaced
* save all points to ensure smooth transition across routes, revert friction factor to 1.5
* add filters to prevent noisy low-speed data points; improve fit sanity
* add engaged buffer
* revert lat_acc thresh
* use paramsd realtime process config
* make latacc-to-torque generic, and overrideable
* move freq to 4Hz, avoid storing in np.array, don't publish points in the message
* float instead of np
* remove constant while storing pts
* rename slope, offset to lat_accet_factor, offset
* resolve issues
* use camelcase in all capnp params
* use camelcase everywhere
* reduce latacc threshold or sanity, add car_sane todo, save points properly
* add and check tag
* write param to disk at end of route
* remove args
* rebase op, cereal
* save on exit
* restore default handler
* cpu usage check
* add to process replay
* handle reset better, reduce unnecessary computation
* always publish raw values - useful for debug
* regen routes
* update refs
* checks on cache restore
* check tuning vals too
* clean that up
* reduce cpu usage
* reduce cpu usage by 75%
* cleanup
* optimize further
* handle reset condition better, don't put points in init, use only in corolla
* bump cereal after rebasing
* update refs
* Update common/params.cc
Co-authored-by: Adeeb Shihadeh <adeebshihadeh@gmail.com>
* remove unnecessary checks
* Update RELEASES.md
Co-authored-by: Adeeb Shihadeh <adeebshihadeh@gmail.com>
old-commit-hash: 4fa62f146426f76c9c1c2867d9729b33ec612b59
2022-09-20 06:19:26 +08:00
|
|
|
{"LiveTorqueParameters", PERSISTENT | DONT_LOG},
|
2023-06-20 06:26:52 +08:00
|
|
|
{"LongitudinalPersonality", PERSISTENT},
|
2023-05-10 12:25:23 +08:00
|
|
|
{"NavDestination", CLEAR_ON_MANAGER_START | CLEAR_ON_OFFROAD_TRANSITION},
|
|
|
|
{"NavDestinationWaypoints", CLEAR_ON_MANAGER_START | CLEAR_ON_OFFROAD_TRANSITION},
|
2023-08-27 22:13:43 +08:00
|
|
|
{"NavPastDestinations", PERSISTENT},
|
2022-07-18 19:57:21 +08:00
|
|
|
{"NavSettingLeftSide", PERSISTENT},
|
2023-06-20 06:26:52 +08:00
|
|
|
{"NavSettingTime24h", PERSISTENT},
|
2024-01-18 08:30:08 +08:00
|
|
|
{"NetworkMetered", PERSISTENT},
|
2023-05-10 12:25:23 +08:00
|
|
|
{"ObdMultiplexingChanged", CLEAR_ON_MANAGER_START | CLEAR_ON_ONROAD_TRANSITION},
|
|
|
|
{"ObdMultiplexingEnabled", CLEAR_ON_MANAGER_START | CLEAR_ON_ONROAD_TRANSITION},
|
2023-06-20 06:26:52 +08:00
|
|
|
{"Offroad_BadNvme", CLEAR_ON_MANAGER_START},
|
|
|
|
{"Offroad_CarUnrecognized", CLEAR_ON_MANAGER_START | CLEAR_ON_ONROAD_TRANSITION},
|
|
|
|
{"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_ONROAD_TRANSITION},
|
|
|
|
{"Offroad_Recalibration", CLEAR_ON_MANAGER_START | CLEAR_ON_ONROAD_TRANSITION},
|
|
|
|
{"Offroad_StorageMissing", CLEAR_ON_MANAGER_START},
|
|
|
|
{"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
|
|
|
{"OpenpilotEnabledToggle", PERSISTENT},
|
2023-05-10 12:25:23 +08:00
|
|
|
{"PandaHeartbeatLost", CLEAR_ON_MANAGER_START | CLEAR_ON_OFFROAD_TRANSITION},
|
2023-11-10 10:09:18 +08:00
|
|
|
{"PandaSomResetTriggered", CLEAR_ON_MANAGER_START | CLEAR_ON_OFFROAD_TRANSITION},
|
2022-01-31 05:07:05 +08:00
|
|
|
{"PandaSignatures", CLEAR_ON_MANAGER_START},
|
2021-05-05 01:49:26 +08:00
|
|
|
{"Passive", PERSISTENT},
|
2022-02-10 03:27:58 +08:00
|
|
|
{"PrimeType", PERSISTENT},
|
2021-05-05 01:49:26 +08:00
|
|
|
{"RecordFront", PERSISTENT},
|
|
|
|
{"RecordFrontLock", PERSISTENT}, // for the internal fleet
|
2023-05-10 12:25:23 +08:00
|
|
|
{"ReplayControlsState", CLEAR_ON_MANAGER_START | CLEAR_ON_ONROAD_TRANSITION},
|
|
|
|
{"SnoozeUpdate", CLEAR_ON_MANAGER_START | CLEAR_ON_OFFROAD_TRANSITION},
|
2021-05-05 01:49:26 +08:00
|
|
|
{"SshEnabled", PERSISTENT},
|
|
|
|
{"TermsVersion", PERSISTENT},
|
|
|
|
{"Timezone", PERSISTENT},
|
|
|
|
{"TrainingVersion", PERSISTENT},
|
2022-10-07 07:46:15 +08:00
|
|
|
{"UbloxAvailable", PERSISTENT},
|
2023-05-10 12:25:23 +08:00
|
|
|
{"UpdateAvailable", CLEAR_ON_MANAGER_START | CLEAR_ON_ONROAD_TRANSITION},
|
2021-05-05 01:49:26 +08:00
|
|
|
{"UpdateFailedCount", CLEAR_ON_MANAGER_START},
|
2024-01-11 15:23:13 +08:00
|
|
|
{"UpdaterAvailableBranches", PERSISTENT},
|
2022-09-16 06:12:36 +08:00
|
|
|
{"UpdaterCurrentDescription", CLEAR_ON_MANAGER_START},
|
|
|
|
{"UpdaterCurrentReleaseNotes", CLEAR_ON_MANAGER_START},
|
2023-06-20 06:26:52 +08:00
|
|
|
{"UpdaterFetchAvailable", CLEAR_ON_MANAGER_START},
|
2022-09-16 06:12:36 +08:00
|
|
|
{"UpdaterNewDescription", CLEAR_ON_MANAGER_START},
|
|
|
|
{"UpdaterNewReleaseNotes", CLEAR_ON_MANAGER_START},
|
2023-06-20 06:26:52 +08:00
|
|
|
{"UpdaterState", CLEAR_ON_MANAGER_START},
|
|
|
|
{"UpdaterTargetBranch", CLEAR_ON_MANAGER_START},
|
2024-01-18 08:30:08 +08:00
|
|
|
{"UpdaterLastFetchTime", PERSISTENT},
|
2021-05-05 01:49:26 +08:00
|
|
|
{"Version", PERSISTENT},
|
|
|
|
{"VisionRadarToggle", PERSISTENT},
|
2023-07-25 10:49:51 +08:00
|
|
|
{"WheeledBody", PERSISTENT},
|
2021-05-05 01:49:26 +08:00
|
|
|
};
|
|
|
|
|
2021-04-09 22:35:44 +08:00
|
|
|
} // namespace
|
|
|
|
|
2022-05-17 06:58:13 +08:00
|
|
|
|
2021-11-01 18:22:34 +08:00
|
|
|
Params::Params(const std::string &path) {
|
2023-12-14 12:36:01 +08:00
|
|
|
params_prefix = "/" + util::getenv("OPENPILOT_PREFIX", "d");
|
|
|
|
params_path = ensure_params_path(params_prefix, path);
|
|
|
|
}
|
|
|
|
|
|
|
|
Params::~Params() {
|
|
|
|
if (future.valid()) {
|
|
|
|
future.wait();
|
|
|
|
}
|
|
|
|
assert(queue.empty());
|
2020-10-13 22:23:23 +08:00
|
|
|
}
|
|
|
|
|
2022-09-16 06:43:27 +08:00
|
|
|
std::vector<std::string> Params::allKeys() const {
|
|
|
|
std::vector<std::string> ret;
|
|
|
|
for (auto &p : keys) {
|
|
|
|
ret.push_back(p.first);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2023-05-05 02:21:41 +08:00
|
|
|
// 1) delete params of key_type
|
|
|
|
// 2) delete files that are not defined in the keys.
|
|
|
|
if (DIR *d = opendir(getParamPath().c_str())) {
|
|
|
|
struct dirent *de = NULL;
|
|
|
|
while ((de = readdir(d))) {
|
|
|
|
if (de->d_type != DT_DIR) {
|
|
|
|
auto it = keys.find(de->d_name);
|
|
|
|
if (it == keys.end() || (it->second & key_type)) {
|
|
|
|
unlink(getParamPath(de->d_name).c_str());
|
|
|
|
}
|
2022-10-08 02:28:09 +08:00
|
|
|
}
|
2021-05-05 01:49:26 +08:00
|
|
|
}
|
2023-05-05 02:21:41 +08:00
|
|
|
closedir(d);
|
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
|
|
|
}
|
2023-12-14 12:36:01 +08:00
|
|
|
|
|
|
|
void Params::putNonBlocking(const std::string &key, const std::string &val) {
|
|
|
|
queue.push(std::make_pair(key, val));
|
|
|
|
// start thread on demand
|
|
|
|
if (!future.valid() || future.wait_for(std::chrono::milliseconds(0)) == std::future_status::ready) {
|
|
|
|
future = std::async(std::launch::async, &Params::asyncWriteThread, this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Params::asyncWriteThread() {
|
|
|
|
// TODO: write the latest one if a key has multiple values in the queue.
|
|
|
|
std::pair<std::string, std::string> p;
|
|
|
|
while (queue.try_pop(p, 0)) {
|
|
|
|
// Params::put is Thread-Safe
|
|
|
|
put(p.first, p.second);
|
|
|
|
}
|
|
|
|
}
|