#include "rpc/platform/instance_lock.hpp" #include #include #include #include #include #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(offsetof(sockaddr_un, sun_path) + 1 + name.size()); if (::bind(fd, reinterpret_cast(&addr), len) != 0) { ::close(fd); return -1; } return fd; } } // namespace velox::daemon::rpc::platform