Files
vdm/daemon/src/rpc/platform/linux/instance_lock.cpp
T
samiandClaude Sonnet 5 6d7c4f7bc4 daemon: Phase 0 platform seams (ADR 0020) — eventfd/SO_PEERCRED/instance-lock/XDG behind platform/, zero behaviour change
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]>
2026-09-15 17:54:32 +04:00

39 lines
1.3 KiB
C++

#include "rpc/platform/instance_lock.hpp"
#include <cstddef>
#include <cstring>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include "util/crypto.hpp"
namespace velox::daemon::rpc::platform {
int acquire_instance_lock(const std::string& runtime_dir) {
const int fd = ::socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
if (fd < 0) return -1;
// Truncated to 16 hex chars (64 bits): a collision would need two distinct runtime
// dirs to hash together, which is not a security boundary here — the socket itself is
// still 0600 same-UID-checked; this is only "don't let two daemons stomp each other".
const std::string name =
"velox-daemon-" + velox::daemon::crypto::sha256_hex(runtime_dir).substr(0, 16);
sockaddr_un addr{};
addr.sun_family = AF_UNIX;
// Leading NUL selects the abstract namespace; the name follows, not NUL-terminated.
addr.sun_path[0] = '\0';
std::memcpy(addr.sun_path + 1, name.c_str(), name.size());
const socklen_t len =
static_cast<socklen_t>(offsetof(sockaddr_un, sun_path) + 1 + name.size());
if (::bind(fd, reinterpret_cast<sockaddr*>(&addr), len) != 0) {
::close(fd);
return -1;
}
return fd;
}
} // namespace velox::daemon::rpc::platform