/*
 * Copyright (c) 2025-2026 IB-Robot Group & openEuler Embedded SIG & openharmony-robot sig_RoboFrame.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <dirent.h>
#include <errno.h>
#include <pthread.h>
#include <sys/epoll.h>
#include <sys/eventfd.h>
#include <sys/inotify.h>
#include <sys/stat.h>
#include <unistd.h>

#include <algorithm>
#include <chrono>
#include <cinttypes>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <exception>
#include <future>
#include <memory>
#include <new>

#include "impl.h"
#include "runtime_impl.h"

namespace ibmw::selfdev::shm {

const char* ErrorName(Error e) noexcept
{
    switch (e) {
        case Error::kOk:
            return "ok";
        case Error::kNoMemory:
            return "no_memory";
        case Error::kTooManyLoans:
            return "too_many_loans";
        case Error::kTooManyHeld:
            return "too_many_held";
        case Error::kNoData:
            return "no_data";
        case Error::kInvalidArg:
            return "invalid_arg";
        case Error::kNotConnected:
            return "not_connected";
        case Error::kShuttingDown:
            return "shutting_down";
        case Error::kAbiMismatch:
            return "abi_mismatch";
        case Error::kPermission:
            return "permission";
        case Error::kTimeout:
            return "timeout";
        case Error::kCorrupted:
            return "corrupted";
        case Error::kSystem:
            return "system";
        case Error::kAlreadyInitialized:
            return "already_initialized";
    }
    return "unknown";
}

Result<void> Runtime::Init(const RuntimeOptions& options)
{
    return detail::RuntimeImpl::Init(options);
}
bool Runtime::IsInitialized() noexcept
{
    return detail::RuntimeImpl::IsInitialized();
}
std::string Runtime::ServiceDirectory()
{
    auto* rt = detail::RuntimeImpl::Instance();
    return rt != nullptr ? rt->ServiceDir() : std::string();
}
void Runtime::Shutdown() noexcept
{
    detail::RuntimeImpl::Shutdown();
}

namespace detail {

namespace {

std::mutex g_instance_mu;
RuntimeImpl* g_instance = nullptr;    // intentionally leaked unless Shutdown()
Error g_last_init_error = Error::kOk; // guarded by g_instance_mu

constexpr uint32_t kEpollMaxEvents = 64;
// epoll user data: generation in the high half, fd in the low half, so an
// event for a closed fd whose number was reused is ignored.
constexpr uint64_t kWakeTag = 0xffffffff00000000ull;
constexpr uint64_t kInotifyTag = 0xfffffffe00000000ull;
constexpr uint32_t kWatchMask = IN_MOVED_TO | IN_DELETE_SELF | IN_MOVE_SELF;

void LogError(const std::string& what)
{
    // Best effort: a failed write to stderr is not reported further.
    static_cast<void>(std::fprintf(stderr, "[ibmw_shm] %s\n", what.c_str()));
}

// 10 ms doubling to 1 s.
uint64_t BackoffNs(uint32_t attempt) noexcept
{
    return static_cast<uint64_t>(std::min<uint32_t>(10u << std::min<uint32_t>(attempt, 7), 1000)) * 1000000ull;
}

// One failing command or timer must not take the control thread (and with
// it every publisher and subscriber of the process) down.
void RunGuarded(const std::function<void()>& fn, const char* what) noexcept
{
    try {
        fn();
    } catch (const std::exception& e) {
        // Best effort: a failed write to stderr is not reported further.
        static_cast<void>(std::fprintf(stderr, "[ibmw_shm] %s failed: %s\n", what, e.what()));
    } catch (...) {
        // Best effort: a failed write to stderr is not reported further.
        static_cast<void>(std::fprintf(stderr, "[ibmw_shm] %s failed\n", what));
    }
}

// Root used when neither RuntimeOptions::root_dir nor $IBMW_SHM_ROOT is set;
// "" when there is none. A fixed path, never derived from per-process
// environment (XDG_RUNTIME_DIR, TMPDIR), so every process of a user meets.
std::string DefaultRoot()
{
#if defined(__OHOS__)
    // OHOS (HongMeng or Linux kernel): /tmp is not writable on devices and
    // what an app sandbox can see is untested, so the root must be explicit.
    return {};
#else
    // /dev/shm is tmpfs and not aged by systemd-tmpfiles like /tmp.
    struct stat st {};
    if (::stat("/dev/shm", &st) == 0 && S_ISDIR(st.st_mode) && ::access("/dev/shm", W_OK | X_OK) == 0) {
        return "/dev/shm";
    }
    return "/tmp";
#endif
}

std::string EnvOrEmpty(const char* name)
{
    const char* v = std::getenv(name);
    return v != nullptr ? v : "";
}

// Reads IBMW_SHM_GROUP into `group` when it is set; false (logged) when it is
// not a numeric gid.
bool GroupFromEnvironment(std::optional<gid_t>* group)
{
    std::string g = EnvOrEmpty("IBMW_SHM_GROUP");
    if (g.empty()) {
        return true;
    }
    // strtoul() alone accepts leading blanks, '+' and '-'; only digits are a gid.
    const bool digits = g.size() <= 10 && g.find_first_not_of("0123456789") == std::string::npos;
    char* end = nullptr;
    unsigned long v = digits ? std::strtoul(g.c_str(), &end, 10) : 0;
    if (!digits || end == g.c_str() || *end != '\0' || v > 0xfffffffful) {
        LogError("invalid IBMW_SHM_GROUP '" + g + "': expected a numeric gid");
        return false;
    }
    *group = static_cast<gid_t>(v);
    return true;
}

// The publisher hash of a "<16 hex digits>.<...>.sock" socket name.
bool ParsePublisherName(std::string_view name, uint64_t* hash)
{
    if (name.size() < 22 || name.substr(name.size() - 5) != ".sock" || name[16] != '.') {
        return false;
    }
    char* end = nullptr;
    std::string hex(name.substr(0, 16));
    *hash = std::strtoull(hex.c_str(), &end, 16);
    return end == hex.c_str() + 16;
}

} // namespace

namespace {

std::atomic<uint64_t> g_log_dropped{0};

void WriteLog(const char* side, const char* what, const std::string& url, const char* reason) noexcept
{
    const uint64_t n = g_log_dropped.exchange(0, std::memory_order_relaxed);
    const char* sep = reason != nullptr ? ": " : "";
    if (reason == nullptr) {
        reason = "";
    }
    if (n != 0) {
        // Best effort: a failed write to stderr is not reported further.
        static_cast<void>(std::fprintf(stderr, "[ibmw_shm] %s %s: %s%s%s (%" PRIu64 " earlier lines dropped)\n", side,
                                       url.c_str(), what, sep, reason, n));
    } else {
        // Best effort: a failed write to stderr is not reported further.
        static_cast<void>(std::fprintf(stderr, "[ibmw_shm] %s %s: %s%s%s\n", side, url.c_str(), what, sep, reason));
    }
}

} // namespace

void Log(const char* side, const char* what, const std::string& url, const char* reason) noexcept
{
    constexpr uint64_t kLogLinesPerSecond = 10;
    constexpr int kCountBits = 16;
    // Second of the current window (high bits) and lines written in it: one
    // CAS, so the window only moves forward and no count is lost.
    static std::atomic<uint64_t> window{0};
    const uint64_t nowS = NowNs() / 1'000'000'000;
    uint64_t cur = window.load(std::memory_order_relaxed);
    uint64_t next;
    do {
        const uint64_t startS = cur >> kCountBits;
        const uint64_t lines = cur & ((uint64_t{1} << kCountBits) - 1);
        if (nowS > startS) {
            next = (nowS << kCountBits) | 1;
        } else if (lines < kLogLinesPerSecond) {
            next = cur + 1;
        } else {
            g_log_dropped.fetch_add(1, std::memory_order_relaxed);
            return;
        }
    } while (!window.compare_exchange_weak(cur, next, std::memory_order_relaxed));
    WriteLog(side, what, url, reason);
}

void LogAlways(const char* side, const char* what, const std::string& url) noexcept
{
    WriteLog(side, what, url, nullptr);
}

std::vector<std::string> ForeignServiceDirs(const std::string& root, const std::string& owner,
                                            const std::string& domain)
{
    std::vector<std::string> out;
    DIR* d = ::opendir(root.c_str());
    if (d == nullptr) {
        return out;
    }
    const std::string prefix = "ibmw_shm.";
    const std::string suffix = "." + domain;
    while (struct dirent* de = ::readdir(d)) {
        std::string name(de->d_name);
        if (name.size() <= prefix.size() + suffix.size() || name.compare(0, prefix.size(), prefix) != 0 ||
            name.compare(name.size() - suffix.size(), suffix.size(), suffix) != 0) {
            continue;
        }
        std::string who = name.substr(prefix.size(), name.size() - prefix.size() - suffix.size());
        if (who != owner && who.find('.') == std::string::npos) {
            out.push_back(root + "/" + name);
        }
    }
    ::closedir(d);
    return out;
}

RuntimeImpl* RuntimeImpl::Instance() noexcept
{
    {
        std::lock_guard<std::mutex> lock(g_instance_mu);
        if (g_instance != nullptr) {
            return g_instance;
        }
    }
    (void)Init(RuntimeOptions{});
    std::lock_guard<std::mutex> lock(g_instance_mu);
    return g_instance;
}

Error RuntimeImpl::LastInitError() noexcept
{
    std::lock_guard<std::mutex> lock(g_instance_mu);
    // Never kOk: callers return it as the error of a failed creation.
    return g_last_init_error != Error::kOk ? g_last_init_error : Error::kSystem;
}

Result<void> RuntimeImpl::Init(const RuntimeOptions& options)
{
    std::lock_guard<std::mutex> lock(g_instance_mu);
    if (g_instance != nullptr) {
        std::optional<gid_t> group;
        auto dir = ResolveServiceDir(options, &group);
        if (!dir) {
            return dir.error();
        }
        if (*dir == g_instance->ServiceDir()) {
            return {};
        }
        return Error::kAlreadyInitialized;
    }
    RuntimeImpl* rt = nullptr;
    try {
        rt = new RuntimeImpl();
        auto r = rt->Start(options);
        if (!r) {
            delete rt;
            g_last_init_error = r.error();
            return r;
        }
    } catch (...) { // std::bad_alloc, std::system_error from std::thread
        delete rt;
        g_last_init_error = Error::kNoMemory;
        return Error::kNoMemory;
    }
    g_instance = rt;
    return {};
}

bool RuntimeImpl::IsInitialized() noexcept
{
    std::lock_guard<std::mutex> lock(g_instance_mu);
    return g_instance != nullptr;
}

void RuntimeImpl::Shutdown() noexcept
{
    RuntimeImpl* rt;
    {
        std::lock_guard<std::mutex> lock(g_instance_mu);
        rt = g_instance;
        g_instance = nullptr;
    }
    delete rt;
}

std::string RuntimeImpl::HashPrefix(uint64_t hash)
{
    return HexDigits(hash, kHexDigitsU64);
}

Result<std::string> RuntimeImpl::ResolveServiceDir(const RuntimeOptions& options, std::optional<gid_t>* group,
                                                   std::string* rootOut, std::string* domainOut)
{
    std::string root = !options.root_dir.empty() ? options.root_dir : EnvOrEmpty("IBMW_SHM_ROOT");
    if (root.empty()) {
        root = DefaultRoot();
    }
    if (root.empty()) {
        LogError("no shm root directory: set RuntimeOptions::root_dir or IBMW_SHM_ROOT (required on OHOS)");
        return Error::kInvalidArg;
    }
    std::string domain = !options.domain.empty() ? options.domain : EnvOrEmpty("IBMW_SHM_DOMAIN");
    if (domain.empty()) {
        domain = "default";
    }
    if (domain.find('/') != std::string::npos || domain.size() > 32) {
        LogError("invalid shm domain '" + domain + "': at most 32 characters, no '/'");
        return Error::kInvalidArg;
    }
    *group = options.group;
    if (!group->has_value() && !GroupFromEnvironment(group)) {
        return Error::kInvalidArg;
    }
    // (gid_t)-1 means "leave the group unchanged" to chown(): the directory and
    // sockets would silently stay in the creator's primary group.
    if (group->has_value() && **group == static_cast<gid_t>(-1)) {
        LogError("invalid shm group " + std::to_string(**group) + ": (gid_t)-1 is not a group");
        return Error::kInvalidArg;
    }
    std::string owner = group->has_value() ? "g" + std::to_string(**group) : std::to_string(::geteuid());
    std::string dir = root + "/ibmw_shm." + owner + "." + domain;
    if (dir.size() > kMaxServiceDirLength) {
        LogError("service directory '" + dir + "' is " + std::to_string(dir.size()) + " bytes, the limit is " +
                 std::to_string(kMaxServiceDirLength) + " (socket paths must fit sun_path): use a root of at most " +
                 std::to_string(kMaxServiceDirLength - (dir.size() - root.size())) + " bytes");
        return Error::kInvalidArg;
    }
    if (rootOut != nullptr) {
        *rootOut = root;
    }
    if (domainOut != nullptr) {
        *domainOut = domain;
    }
    return dir;
}

Result<void> RuntimeImpl::Start(const RuntimeOptions& options)
{
    options_ = options;
    euid_ = ::geteuid();
    std::string root;
    std::string domain;
    auto dir = ResolveServiceDir(options, &group_, &root, &domain);
    if (!dir) {
        return dir.error();
    }
    service_dir_ = std::move(dir.value());
    if (!MakeDirectory(service_dir_, group_)) {
        LogError("cannot use service directory '" + service_dir_ + "': " + std::strerror(errno) +
                 " (it must be a directory owned by this user or, in group mode, by RuntimeOptions::group with g+rwx,"
                 " no access for others, and this process must be in that group)");
        return Error::kPermission;
    }
    const std::string owner = group_ ? "g" + std::to_string(*group_) : std::to_string(euid_);
    for (const auto& other : ForeignServiceDirs(root, owner, domain)) {
        LogError("note: '" + other + "' serves domain '" + domain +
                 "' for another user or group; processes only meet when they run as the same user or all set the "
                 "same RuntimeOptions::group (or IBMW_SHM_GROUP)");
    }

    const Error notifyError = CreateNotifySegment();
    if (notifyError != Error::kOk) {
        return notifyError;
    }
    if (!OpenEventFds()) {
        return Error::kSystem;
    }

    std::promise<std::thread::id> started;
    auto startedFuture = started.get_future();
    thread_ = std::thread([this, &started] {
        ::pthread_setname_np(::pthread_self(), "ibmw_shm_ctl");
        started.set_value(std::this_thread::get_id());
        Loop();
    });
    thread_id_ = startedFuture.get();
    return {};
}

Error RuntimeImpl::CreateNotifySegment()
{
    auto notify = CreateSegment("ibmw_shm_notify", kNotifySegmentSize, false, true);
    if (!notify) {
        return notify.error();
    }
    notify_ = std::move(notify.value());
    auto* ns = Notify();
    ns->magic = kNotifyMagic;
    ns->slots = kMaxListenersPerProcess;
    for (auto& s : ns->slot) {
        s.epoch.store(0, std::memory_order_relaxed);
        s.sleeping.store(0, std::memory_order_relaxed);
    }
    return Error::kOk;
}

// The control thread's epoll, wake eventfd and service directory watch.
bool RuntimeImpl::OpenEventFds()
{
    epoll_ = UniqueFd(::epoll_create1(EPOLL_CLOEXEC));
    wake_ = UniqueFd(::eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK));
    inotify_ = UniqueFd(::inotify_init1(IN_CLOEXEC | IN_NONBLOCK));
    if (!epoll_.Valid() || !wake_.Valid() || !inotify_.Valid()) {
        return false;
    }
    watch_ = ::inotify_add_watch(inotify_.Get(), service_dir_.c_str(), kWatchMask);
    if (watch_ < 0) {
        return false;
    }

    struct epoll_event ev {};
    ev.events = EPOLLIN;
    ev.data.u64 = kWakeTag;
    if (::epoll_ctl(epoll_.Get(), EPOLL_CTL_ADD, wake_.Get(), &ev) != 0) {
        return false;
    }
    ev.data.u64 = kInotifyTag;
    return ::epoll_ctl(epoll_.Get(), EPOLL_CTL_ADD, inotify_.Get(), &ev) == 0;
}

RuntimeImpl::~RuntimeImpl()
{
    if (thread_.joinable()) {
        stop_.store(true, std::memory_order_release);
        uint64_t one = 1;
        ssize_t n = ::write(wake_.Get(), &one, sizeof(one));
        (void)n; // EAGAIN only when the counter is saturated: already readable
        thread_.join();
    }
    for (auto& [fd, reg] : handlers_) {
        ::epoll_ctl(epoll_.Get(), EPOLL_CTL_DEL, fd, nullptr);
    }
    handlers_.clear();
}

bool RuntimeImpl::OnControlThread() const noexcept
{
    return std::this_thread::get_id() == thread_id_;
}

void RuntimeImpl::Post(std::function<void()> fn)
{
    {
        std::lock_guard<std::mutex> lock(cmd_mu_);
        commands_.push_back(std::move(fn));
    }
    uint64_t one = 1;
    ssize_t n = ::write(wake_.Get(), &one, sizeof(one));
    (void)n; // EAGAIN only when the counter is saturated: already readable
}

void RuntimeImpl::Call(const std::function<void()>& fn)
{
    if (OnControlThread()) {
        fn();
        return;
    }
    // Shutdown paths call this from noexcept functions: an allocation that
    // fails before the command is queued is retried, not thrown. The promise
    // is shared with the command, and what fn throws goes through it: the
    // waiter must wake up either way.
    std::future<void> done;
    for (;;) {
        try {
            auto promise = std::make_shared<std::promise<void>>();
            done = promise->get_future();
            Post([&fn, promise] {
                try {
                    fn();
                    promise->set_value();
                } catch (...) {
                    promise->set_exception(std::current_exception());
                }
            });
            break;
        } catch (const std::bad_alloc&) {
            std::this_thread::sleep_for(std::chrono::milliseconds(1));
        }
    }
    done.get();
}

bool RuntimeImpl::AddFd(int fd, uint32_t events, std::shared_ptr<EventHandler> handler, uint64_t* tag)
{
    uint32_t gen = next_generation_++;
    if (next_generation_ >= 0xfffffff0u) {
        next_generation_ = 1;
    }
    struct epoll_event ev {};
    ev.events = events;
    ev.data.u64 = (static_cast<uint64_t>(gen) << 32) | static_cast<uint32_t>(fd);
    try {
        handlers_[fd] = Registered{gen, std::move(handler)};
    } catch (...) { // std::bad_alloc
        return false;
    }
    if (::epoll_ctl(epoll_.Get(), EPOLL_CTL_ADD, fd, &ev) != 0) {
        handlers_.erase(fd);
        return false;
    }
    if (tag != nullptr) {
        *tag = ev.data.u64;
    }
    return true;
}

void RuntimeImpl::ModFd(int fd, uint64_t tag, uint32_t events) noexcept
{
    struct epoll_event ev {};
    ev.events = events;
    ev.data.u64 = tag;
    ::epoll_ctl(epoll_.Get(), EPOLL_CTL_MOD, fd, &ev);
}

bool RuntimeImpl::RunAfter(uint32_t delayMs, std::function<void()> fn) noexcept
{
    try {
        timers_.emplace(NowNs() + static_cast<uint64_t>(delayMs) * 1000000ull, std::move(fn));
        return true;
    } catch (...) { // std::bad_alloc
        return false;
    }
}

void RuntimeImpl::RunTimers() noexcept
{
    const uint64_t now = NowNs();
    while (!timers_.empty() && timers_.begin()->first <= now) {
        auto fn = std::move(timers_.begin()->second);
        timers_.erase(timers_.begin());
        RunGuarded(fn, "control thread timer");
    }
}

void RuntimeImpl::ScheduleRescan() noexcept
{
    if (rescan_at_ != 0) {
        return;
    }
    rescan_at_ = NowNs() + BackoffNs(rescan_attempt_);
    if (rescan_attempt_ < 7) {
        ++rescan_attempt_;
    }
}

void RuntimeImpl::RunDeferred() noexcept
{
    const uint64_t now = NowNs();
    if (rewatch_at_ != 0 && rewatch_at_ <= now) {
        rewatch_at_ = 0;
        Rewatch();
    }
    if (rescan_at_ != 0 && rescan_at_ <= now) {
        rescan_at_ = 0;
        ScanAll();
        if (rescan_at_ == 0) {
            rescan_attempt_ = 0; // a clean pass ends the backoff
        }
    }
}

void RuntimeImpl::RemoveFd(int fd)
{
    auto it = handlers_.find(fd);
    if (it == handlers_.end()) {
        return;
    }
    ::epoll_ctl(epoll_.Get(), EPOLL_CTL_DEL, fd, nullptr);
    // The handler may be the caller; keep it alive until the call returns.
    // (Loop() holds its own reference while dispatching, so if queuing fails
    // dropping it here is still safe.)
    auto keep = std::move(it->second.handler);
    handlers_.erase(it);
    try {
        Post([keep] {});
    } catch (...) { // std::bad_alloc
    }
}

std::optional<uint32_t> RuntimeImpl::AllocListenerSlot()
{
    std::lock_guard<std::mutex> lock(reg_mu_);
    for (uint32_t i = 0; i < kMaxListenersPerProcess; ++i) {
        if ((listener_slots_used_ & (1ull << i)) == 0) {
            listener_slots_used_ |= 1ull << i;
            return i;
        }
    }
    return std::nullopt;
}

void RuntimeImpl::FreeListenerSlot(uint32_t slot)
{
    std::lock_guard<std::mutex> lock(reg_mu_);
    if (slot < kMaxListenersPerProcess) {
        listener_slots_used_ &= ~(1ull << slot);
    }
}

void RuntimeImpl::RegisterSubscriber(uint64_t hash, const std::shared_ptr<SubscriberImpl>& sub)
{
    std::lock_guard<std::mutex> lock(reg_mu_);
    subscribers_.emplace(hash, sub);
}

void RuntimeImpl::UnregisterSubscriber(uint64_t hash, const SubscriberImpl* sub)
{
    std::lock_guard<std::mutex> lock(reg_mu_);
    auto [b, e] = subscribers_.equal_range(hash);
    for (auto it = b; it != e;) {
        auto sp = it->second.lock();
        if (!sp || sp.get() == sub) {
            it = subscribers_.erase(it);
        } else {
            ++it;
        }
    }
}

void RuntimeImpl::OfferPublisher(uint64_t hash, const std::string& path) noexcept
{
    try {
        if (fault::Hit(fault::offer_bad_alloc)) {
            throw std::bad_alloc();
        }
        std::vector<std::shared_ptr<SubscriberImpl>> subs;
        {
            std::lock_guard<std::mutex> lock(reg_mu_);
            auto [b, e] = subscribers_.equal_range(hash);
            for (auto it = b; it != e; ++it) {
                if (auto sp = it->second.lock()) {
                    subs.push_back(std::move(sp));
                }
            }
        }
        for (auto& s : subs) {
            s->ConnectTo(path);
        }
    } catch (...) { // std::bad_alloc: a publisher must not stay undiscovered
        ScheduleRescan();
    }
}

void RuntimeImpl::ScanAll() noexcept
{
    try {
        std::vector<uint64_t> hashes;
        {
            std::lock_guard<std::mutex> lock(reg_mu_);
            for (auto it = subscribers_.begin(); it != subscribers_.end(); it = subscribers_.upper_bound(it->first)) {
                hashes.push_back(it->first);
            }
        }
        for (uint64_t h : hashes) {
            ScanFor(h);
        }
    } catch (...) { // std::bad_alloc
        ScheduleRescan();
    }
}

void RuntimeImpl::ScanFor(uint64_t hash) noexcept
{
    try {
        std::string prefix = HashPrefix(hash) + ".";
        std::unique_ptr<DIR, int (*)(DIR*)> d(::opendir(service_dir_.c_str()), ::closedir);
        if (!d) {
            ScheduleRescan();
            return;
        }
        std::vector<std::string> paths;
        errno = 0;
        while (struct dirent* de = ::readdir(d.get())) {
            std::string_view name(de->d_name);
            if (name.size() > 5 && name.substr(0, prefix.size()) == prefix && name.substr(name.size() - 5) == ".sock") {
                paths.push_back(service_dir_ + "/" + std::string(name));
            }
        }
        if (errno != 0) {
            ScheduleRescan(); // what was read is still offered
        }
        d.reset();
        for (auto& p : paths) {
            OfferPublisher(hash, p);
        }
    } catch (...) { // std::bad_alloc
        ScheduleRescan();
    }
}

bool RuntimeImpl::PeerAllowed(const PeerCredentials& cred, int sock) const noexcept
{
    if (cred.uid == euid_ || cred.uid == 0) {
        return true;
    }
    if (!group_.has_value()) {
        return false;
    }
    return cred.gid == *group_ || PeerInSupplementaryGroup(sock, *group_);
}

bool RuntimeImpl::SetSocketAccess(const char* path) const noexcept
{
    if (group_.has_value() && ::chown(path, static_cast<uid_t>(-1), *group_) != 0) {
        return false;
    }
    return ::chmod(path, group_.has_value() ? 0660 : 0600) == 0;
}

void RuntimeImpl::HandleInotify() noexcept
{
    alignas(struct inotify_event) char buf[4096];
    bool rescan = false;
    bool rewatch = false;
    for (;;) {
        ssize_t n = ::read(inotify_.Get(), buf, sizeof(buf));
        if (n < 0 && errno == EINTR) {
            continue;
        }
        if (n <= 0) {
            if (n < 0 && errno != EAGAIN) {
                ScheduleRescan(); // events may be lost
            }
            break;
        }
        for (ssize_t off = 0; off < n;) {
            auto* ev = reinterpret_cast<struct inotify_event*>(buf + off);
            off += static_cast<ssize_t>(sizeof(struct inotify_event) + ev->len);
            HandleInotifyEvent(*ev, &rescan, &rewatch);
        }
    }
    if (rewatch) {
        // Best effort: a failed write to stderr is not reported further.
        static_cast<void>(std::fprintf(
            stderr, "[ibmw_shm] service directory '%s' was removed or moved; re-creating it\n", service_dir_.c_str()));
        Rewatch();
    } else if (rescan) {
        // Best effort: a failed write to stderr is not reported further.
        static_cast<void>(
            std::fprintf(stderr, "[ibmw_shm] inotify queue overflow; rescanning '%s'\n", service_dir_.c_str()));
        ScanAll();
    }
}

void RuntimeImpl::HandleInotifyEvent(const struct inotify_event& ev, bool* rescan, bool* rewatch) noexcept
{
    // Events were dropped: some publishers may never have been offered.
    if ((ev.mask & IN_Q_OVERFLOW) != 0) {
        *rescan = true;
    }
    // The directory itself went away (rm -r, systemd RemoveIPC, tmp
    // cleaners) or was moved: nothing would ever be reported again.
    if (ev.wd == watch_ && (ev.mask & (IN_DELETE_SELF | IN_MOVE_SELF | IN_IGNORED)) != 0) {
        *rewatch = true;
    }
    if (ev.len == 0) {
        return;
    }
    try {
        std::string_view name(ev.name);
        uint64_t hash = 0;
        if (ParsePublisherName(name, &hash)) {
            OfferPublisher(hash, service_dir_ + "/" + std::string(name));
        }
    } catch (...) { // std::bad_alloc: this event is consumed, find it later
        ScheduleRescan();
    }
}

void RuntimeImpl::Rewatch() noexcept
{
    if (watch_ >= 0) {
        ::inotify_rm_watch(inotify_.Get(), watch_); // still set after IN_MOVE_SELF
    }
    watch_ = -1;
    if (MakeDirectory(service_dir_, group_)) {
        watch_ = ::inotify_add_watch(inotify_.Get(), service_dir_.c_str(), kWatchMask);
    }
    if (watch_ < 0) {
        // Retried with backoff (10 ms .. 1 s) for as long as the runtime lives.
        if (rewatch_attempt_ == 0) {
            // Best effort: a failed write to stderr is not reported further.
            static_cast<void>(std::fprintf(stderr, "[ibmw_shm] cannot re-create '%s': %s; retrying\n",
                                           service_dir_.c_str(), std::strerror(errno)));
        }
        rewatch_at_ = NowNs() + BackoffNs(rewatch_attempt_);
        if (rewatch_attempt_ < 7) {
            ++rewatch_attempt_;
        }
        return;
    }
    rewatch_attempt_ = 0;
    // Sockets created in between had no watch; publishers of this process
    // whose socket file was deleted with the directory cannot be found again
    // until they are re-created.
    ScanAll();
}

void RuntimeImpl::RunCommands() noexcept
{
    uint64_t v;
    while (::read(wake_.Get(), &v, sizeof(v)) > 0) {
    }
    for (;;) {
        std::function<void()> fn;
        {
            std::lock_guard<std::mutex> lock(cmd_mu_);
            if (commands_.empty()) {
                if (stop_.load(std::memory_order_acquire)) {
                    running_ = false;
                }
                return;
            }
            fn = std::move(commands_.front());
            commands_.pop_front();
        }
        RunGuarded(fn, "control thread command");
    }
}

// epoll_wait timeout in ms: -1 unless something is pending (retries,
// quarantine polling), so an idle process costs zero wake-ups.
int RuntimeImpl::NextTimeoutMs() const noexcept
{
    uint64_t at = timers_.empty() ? 0 : timers_.begin()->first;
    for (uint64_t deferred : {rescan_at_, rewatch_at_}) {
        if (deferred != 0 && (at == 0 || deferred < at)) {
            at = deferred;
        }
    }
    if (at == 0) {
        return -1;
    }
    uint64_t now = NowNs();
    return at <= now ? 0 : static_cast<int>(std::min<uint64_t>((at - now + 999999) / 1000000, 60000));
}

void RuntimeImpl::DispatchEvent(const struct epoll_event& event) noexcept
{
    uint64_t tag = event.data.u64;
    if (tag == kWakeTag) {
        RunCommands();
        return;
    }
    if (tag == kInotifyTag) {
        HandleInotify();
        return;
    }
    int fd = static_cast<int>(static_cast<uint32_t>(tag));
    uint32_t gen = static_cast<uint32_t>(tag >> 32);
    auto it = handlers_.find(fd);
    if (it == handlers_.end() || it->second.generation != gen) {
        return;
    }
    auto handler = it->second.handler;
    handler->OnEvent(event.events);
}

void RuntimeImpl::Loop() noexcept
{
    struct epoll_event events[kEpollMaxEvents];
    while (running_) {
        // The control thread only wakes for socket, inotify or command events
        // and for pending timers.
        int n = ::epoll_wait(epoll_.Get(), events, kEpollMaxEvents, NextTimeoutMs());
        if (n < 0) {
            if (errno == EINTR) {
                continue;
            }
            // Best effort: a failed write to stderr is not reported further.
            static_cast<void>(std::fprintf(stderr, "[ibmw_shm] epoll_wait failed: %s\n", std::strerror(errno)));
            return;
        }
        for (int i = 0; i < n && running_; ++i) {
            DispatchEvent(events[i]);
        }
        if (running_ && !timers_.empty()) {
            RunTimers();
        }
        if (running_ && (rescan_at_ != 0 || rewatch_at_ != 0)) {
            RunDeferred();
        }
    }
}

} // namespace detail
} // namespace ibmw::selfdev::shm