Introduces the four rpc-layer seams docs/08-porting.md calls for this lane: platform::Wakeup (eventfd), platform::peer_of (SO_PEERCRED/struct ucred, same-UID check preserved unchanged), platform::acquire_instance_lock (abstract-namespace socket), platform::runtime_base_dir/data_base_dir (XDG lookups). Today's Linux code moves unchanged into daemon/src/rpc/platform/linux/; the seam headers carry no OS types and no #ifdef. No fifth interface for timerfd: EventLoop gains a portable add_timer() that folds the next deadline into poll()'s own timeout, replacing both timerfd instances in main.cpp — the loop already computes a deadline, so this needs no per-OS backend at all. Full suite 59/59 green; no #ifdef outside platform/linux/, no behaviour change. Co-Authored-By: Claude Sonnet 5 <[email protected]>
62 lines
1.8 KiB
C++
62 lines
1.8 KiB
C++
#include "rpc/runtime_dir.hpp"
|
|
|
|
#include <sys/stat.h>
|
|
#include <sys/types.h>
|
|
#include <unistd.h>
|
|
|
|
#include <cerrno>
|
|
#include <string>
|
|
|
|
#include "rpc/platform/runtime_paths.hpp"
|
|
|
|
namespace velox::daemon::rpc {
|
|
|
|
namespace {
|
|
|
|
std::error_code errc(int e) { return std::error_code(e, std::generic_category()); }
|
|
|
|
// Ensure `dir` exists as a directory we own with mode 0700. Creates it if absent.
|
|
std::error_code ensure_private_dir(const std::string& dir) {
|
|
if (::mkdir(dir.c_str(), 0700) != 0 && errno != EEXIST) return errc(errno);
|
|
|
|
struct stat st{};
|
|
if (::lstat(dir.c_str(), &st) != 0) return errc(errno);
|
|
if (!S_ISDIR(st.st_mode)) return errc(ENOTDIR);
|
|
if (st.st_uid != ::geteuid()) return errc(EPERM);
|
|
|
|
// Tighten if a prior run (or umask) left it looser. Group/other bits must be clear:
|
|
// the socket is 0600 but a traversable parent still lets another user stat it.
|
|
if ((st.st_mode & 077) != 0 && ::chmod(dir.c_str(), 0700) != 0) return errc(errno);
|
|
return {};
|
|
}
|
|
|
|
} // namespace
|
|
|
|
std::error_code resolve_runtime_dir(RuntimeDir& out) {
|
|
std::string base;
|
|
if (auto ec = platform::runtime_base_dir(base)) return ec;
|
|
if (!base.empty() && base.back() == '/') base.pop_back();
|
|
|
|
const std::string dir = base + "/velox";
|
|
if (auto ec = ensure_private_dir(dir)) return ec;
|
|
|
|
out.path = dir;
|
|
return {};
|
|
}
|
|
|
|
std::error_code resolve_data_dir(std::string& out) {
|
|
std::string base;
|
|
if (auto ec = platform::data_base_dir(base)) return ec;
|
|
if (!base.empty() && base.back() == '/') base.pop_back();
|
|
|
|
// Create the XDG base components leniently, then the velox dir with a strict check.
|
|
::mkdir(base.c_str(), 0700);
|
|
const std::string dir = base + "/velox";
|
|
if (auto ec = ensure_private_dir(dir)) return ec;
|
|
|
|
out = dir;
|
|
return {};
|
|
}
|
|
|
|
} // namespace velox::daemon::rpc
|