common: add new class OpenPilotPrefix (#26753)
* new class OpenPilotPrefix * move random_string to util * rename file * style Co-authored-by: Adeeb Shihadeh <adeebshihadeh@gmail.com> old-commit-hash: 0d8254e95935f27d7c371ecc4c2d6c9ff8c204d0
This commit is contained in:
34
common/prefix.h
Normal file
34
common/prefix.h
Normal file
@@ -0,0 +1,34 @@
|
||||
#pragma once
|
||||
|
||||
#include <cassert>
|
||||
#include <string>
|
||||
|
||||
#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;
|
||||
};
|
||||
@@ -10,6 +10,7 @@
|
||||
#include <dirent.h>
|
||||
#include <fstream>
|
||||
#include <iomanip>
|
||||
#include <random>
|
||||
#include <sstream>
|
||||
|
||||
#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<std::string::size_type> 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 "";
|
||||
|
||||
@@ -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 *****
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
#include <QApplication>
|
||||
#include <QDir>
|
||||
#include <QCommandLineParser>
|
||||
#include <QUuid>
|
||||
|
||||
#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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user