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>
|
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
|
|
|
|
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)) {
|
2021-06-15 07:12:27 +08:00
|
|
|
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},
|
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},
|
2022-01-14 21:13:18 +08:00
|
|
|
{"CarParams", CLEAR_ON_MANAGER_START | CLEAR_ON_IGNITION_ON},
|
|
|
|
{"CarParamsCache", CLEAR_ON_MANAGER_START},
|
2022-09-08 08:12:49 +08:00
|
|
|
{"CarParamsPersistent", PERSISTENT},
|
2022-01-14 21:13:18 +08:00
|
|
|
{"CarVin", CLEAR_ON_MANAGER_START | CLEAR_ON_IGNITION_ON},
|
2021-09-24 07:46:02 +08:00
|
|
|
{"CompletedTrainingVersion", PERSISTENT},
|
2022-01-14 21:13:18 +08:00
|
|
|
{"ControlsReady", CLEAR_ON_MANAGER_START | CLEAR_ON_IGNITION_ON},
|
2023-03-22 08:28:38 +08:00
|
|
|
{"CurrentBootlog", PERSISTENT},
|
2021-06-29 12:44:40 +08:00
|
|
|
{"CurrentRoute", CLEAR_ON_MANAGER_START | CLEAR_ON_IGNITION_ON},
|
2022-05-05 05:57:39 +08:00
|
|
|
{"DisableLogging", CLEAR_ON_MANAGER_START | CLEAR_ON_IGNITION_ON},
|
2021-05-05 01:49:26 +08:00
|
|
|
{"DisablePowerDown", PERSISTENT},
|
2022-11-09 06:45:01 +08:00
|
|
|
{"ExperimentalMode", PERSISTENT},
|
2022-11-16 08:44:03 +08:00
|
|
|
{"ExperimentalModeConfirmed", PERSISTENT},
|
2023-01-20 15:06:18 +08:00
|
|
|
{"ExperimentalLongitudinalEnabled", PERSISTENT},
|
2021-05-05 01:49:26 +08:00
|
|
|
{"DisableUpdates", PERSISTENT},
|
2022-04-05 12:59:52 +08:00
|
|
|
{"DisengageOnAccelerator", PERSISTENT},
|
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-03-23 15:14:31 +08:00
|
|
|
{"FirmwareQueryDone", CLEAR_ON_MANAGER_START | CLEAR_ON_IGNITION_ON},
|
2021-09-24 07:46:02 +08:00
|
|
|
{"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},
|
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},
|
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-01-20 15:06:18 +08:00
|
|
|
{"IsReleaseBranch", CLEAR_ON_MANAGER_START},
|
2021-05-05 01:49:26 +08:00
|
|
|
{"IsUpdateAvailable", CLEAR_ON_MANAGER_START},
|
2021-09-24 07:46:02 +08:00
|
|
|
{"JoystickDebugMode", CLEAR_ON_MANAGER_START | CLEAR_ON_IGNITION_OFF},
|
2023-03-16 11:26:19 +08:00
|
|
|
{"LaikadEphemerisV3", PERSISTENT | DONT_LOG},
|
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},
|
2021-11-29 22:51:27 +08:00
|
|
|
{"LastPowerDropDetected", CLEAR_ON_MANAGER_START},
|
2022-01-14 14:40:35 +08:00
|
|
|
{"LastSystemShutdown", 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
|
|
|
{"LiveTorqueCarParams", PERSISTENT},
|
|
|
|
{"LiveTorqueParameters", PERSISTENT | DONT_LOG},
|
2021-06-04 19:45:42 +08:00
|
|
|
{"NavDestination", CLEAR_ON_MANAGER_START | CLEAR_ON_IGNITION_OFF},
|
2022-12-15 10:48:39 +08:00
|
|
|
{"NavDestinationWaypoints", CLEAR_ON_MANAGER_START | CLEAR_ON_IGNITION_OFF},
|
2021-06-10 15:59:34 +08:00
|
|
|
{"NavSettingTime24h", PERSISTENT},
|
2022-07-18 19:57:21 +08:00
|
|
|
{"NavSettingLeftSide", PERSISTENT},
|
2021-11-26 21:38:02 +08:00
|
|
|
{"NavdRender", PERSISTENT},
|
2023-03-23 15:14:31 +08:00
|
|
|
{"ObdMultiplexingChanged", CLEAR_ON_MANAGER_START | CLEAR_ON_IGNITION_ON},
|
|
|
|
{"ObdMultiplexingEnabled", CLEAR_ON_MANAGER_START | CLEAR_ON_IGNITION_ON},
|
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},
|
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
|
2022-08-17 13:02:03 +08:00
|
|
|
{"ReplayControlsState", CLEAR_ON_MANAGER_START | CLEAR_ON_IGNITION_ON},
|
2021-05-05 01:49:26 +08:00
|
|
|
{"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},
|
2022-10-07 07:46:15 +08:00
|
|
|
{"UbloxAvailable", PERSISTENT},
|
2023-02-03 02:25:20 +08:00
|
|
|
{"UpdateAvailable", CLEAR_ON_MANAGER_START | CLEAR_ON_IGNITION_ON},
|
2021-05-05 01:49:26 +08:00
|
|
|
{"UpdateFailedCount", CLEAR_ON_MANAGER_START},
|
2022-09-16 06:12:36 +08:00
|
|
|
{"UpdaterState", CLEAR_ON_MANAGER_START},
|
|
|
|
{"UpdaterFetchAvailable", CLEAR_ON_MANAGER_START},
|
|
|
|
{"UpdaterTargetBranch", CLEAR_ON_MANAGER_START},
|
|
|
|
{"UpdaterAvailableBranches", CLEAR_ON_MANAGER_START},
|
|
|
|
{"UpdaterCurrentDescription", CLEAR_ON_MANAGER_START},
|
|
|
|
{"UpdaterCurrentReleaseNotes", CLEAR_ON_MANAGER_START},
|
|
|
|
{"UpdaterNewDescription", CLEAR_ON_MANAGER_START},
|
|
|
|
{"UpdaterNewReleaseNotes", CLEAR_ON_MANAGER_START},
|
2021-05-05 01:49:26 +08:00
|
|
|
{"Version", PERSISTENT},
|
|
|
|
{"VisionRadarToggle", PERSISTENT},
|
2022-05-30 21:25:07 +08:00
|
|
|
{"WideCameraOnly", PERSISTENT},
|
2021-09-24 07:47:00 +08:00
|
|
|
{"ApiCache_Device", PERSISTENT},
|
|
|
|
{"ApiCache_DriveStats", PERSISTENT},
|
|
|
|
{"ApiCache_NavDestinations", PERSISTENT},
|
|
|
|
{"ApiCache_Owner", PERSISTENT},
|
2022-03-21 08:28:09 +08:00
|
|
|
{"Offroad_BadNvme", CLEAR_ON_MANAGER_START},
|
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_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
|
|
|
|
|
2022-05-17 06:58:13 +08:00
|
|
|
|
2021-11-01 18:22:34 +08:00
|
|
|
Params::Params(const std::string &path) {
|
2022-09-19 05:01:33 +08:00
|
|
|
prefix = "/" + util::getenv("OPENPILOT_PREFIX", "d");
|
|
|
|
params_path = ensure_params_path(prefix, path);
|
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
|
|
|
|
2022-10-08 02:28:09 +08:00
|
|
|
if (key_type == ALL) {
|
|
|
|
util::remove_files_in_dir(getParamPath());
|
|
|
|
} else {
|
|
|
|
for (auto &[key, type] : keys) {
|
|
|
|
if (type & key_type) {
|
|
|
|
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
|
|
|
}
|