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>pull/26775/head
parent
b1009c7f7d
commit
0d8254e959
5 changed files with 51 additions and 10 deletions
@ -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; |
||||||
|
}; |
Loading…
Reference in new issue