From 0d8254e95935f27d7c371ecc4c2d6c9ff8c204d0 Mon Sep 17 00:00:00 2001 From: Dean Lee Date: Tue, 13 Dec 2022 06:47:27 +0800 Subject: [PATCH] common: add new class OpenPilotPrefix (#26753) * new class OpenPilotPrefix * move random_string to util * rename file * style Co-authored-by: Adeeb Shihadeh --- common/prefix.h | 34 ++++++++++++++++++++++++++++++++++ common/util.cc | 13 +++++++++++++ common/util.h | 1 + release/files_common | 1 + tools/cabana/cabana.cc | 12 ++---------- 5 files changed, 51 insertions(+), 10 deletions(-) create mode 100644 common/prefix.h diff --git a/common/prefix.h b/common/prefix.h new file mode 100644 index 0000000000..f5abe14b2b --- /dev/null +++ b/common/prefix.h @@ -0,0 +1,34 @@ +#pragma once + +#include +#include + +#include "common/params.h" +#include "common/util.h" + +class OpenpilotPrefix { +public: + OpenpilotPrefix(std::string prefix = {}) { + if (prefix.empty()) { + prefix = util::random_string(15); + } + msgq_path = "/dev/shm/" + prefix; + bool ret = util::create_directories(msgq_path, 0777); + assert(ret); + setenv("OPENPILOT_PREFIX", prefix.c_str(), 1); + } + + ~OpenpilotPrefix() { + auto param_path = Params().getParamPath(); + if (util::file_exists(param_path)) { + std::string real_path = util::readlink(param_path); + system(util::string_format("rm %s -rf", real_path.c_str()).c_str()); + unlink(param_path.c_str()); + } + system(util::string_format("rm %s -rf", msgq_path.c_str()).c_str()); + unsetenv("OPENPILOT_PREFIX"); + } + +private: + std::string msgq_path; +}; diff --git a/common/util.cc b/common/util.cc index 010fe8a11a..10dff6a9ea 100644 --- a/common/util.cc +++ b/common/util.cc @@ -10,6 +10,7 @@ #include #include #include +#include #include #ifdef __linux__ @@ -228,6 +229,18 @@ std::string hexdump(const uint8_t* in, const size_t size) { return ss.str(); } +std::string random_string(std::string::size_type length) { + const char* chrs = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; + std::mt19937 rg{std::random_device{}()}; + std::uniform_int_distribution pick(0, sizeof(chrs) - 2); + std::string s; + s.reserve(length); + while (length--) { + s += chrs[pick(rg)]; + } + return s; +} + std::string dir_name(std::string const &path) { size_t pos = path.find_last_of("/"); if (pos == std::string::npos) return ""; diff --git a/common/util.h b/common/util.h index b46f7bde4a..028074384e 100644 --- a/common/util.h +++ b/common/util.h @@ -75,6 +75,7 @@ int getenv(const char* key, int default_val); float getenv(const char* key, float default_val); std::string hexdump(const uint8_t* in, const size_t size); +std::string random_string(std::string::size_type length); std::string dir_name(std::string const& path); // **** file fhelpers ***** diff --git a/release/files_common b/release/files_common index 297a7a808e..f438904118 100644 --- a/release/files_common +++ b/release/files_common @@ -146,6 +146,7 @@ selfdrive/debug/vw_mqb_config.py common/SConscript common/version.h +common/prefix.h common/swaglog.h common/swaglog.cc common/statlog.h diff --git a/tools/cabana/cabana.cc b/tools/cabana/cabana.cc index 5e9b255731..51418e293f 100644 --- a/tools/cabana/cabana.cc +++ b/tools/cabana/cabana.cc @@ -1,8 +1,7 @@ #include -#include #include -#include +#include "common/prefix.h" #include "selfdrive/ui/qt/util.h" #include "tools/cabana/mainwin.h" @@ -23,12 +22,6 @@ int main(int argc, char *argv[]) { cmd_parser.showHelp(); } - QString uuid = QUuid::createUuid().toString(QUuid::WithoutBraces); - QString msgq_path = "/dev/shm/" + uuid; - - QDir dir; - dir.mkdir(msgq_path); - setenv("OPENPILOT_PREFIX", qPrintable(uuid), 1); const QString route = args.empty() ? DEMO_ROUTE : args.first(); uint32_t replay_flags = REPLAY_FLAG_NONE; @@ -38,6 +31,7 @@ int main(int argc, char *argv[]) { replay_flags |= REPLAY_FLAG_QCAMERA; } + OpenpilotPrefix op_prefix; CANMessages p(&app); int ret = 0; if (p.loadRoute(route, cmd_parser.value("data_dir"), replay_flags)) { @@ -45,7 +39,5 @@ int main(int argc, char *argv[]) { w.showMaximized(); ret = app.exec(); } - - dir.rmdir(msgq_path); return ret; }