* 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.
*/
#pragma once
#include <sys/epoll.h>
#include <sys/inotify.h>
#include <sys/types.h>
#include <atomic>
#include <condition_variable>
#include <cstdint>
#include <deque>
#include <functional>
#include <map>
#include <memory>
#include <mutex>
#include <optional>
#include <string>
#include <thread>
#include <unordered_map>
#include <vector>
#include "ibmw_selfdev/shm/runtime.h"
#include "platform.h"
#include "protocol.h"
namespace ibmw::selfdev::shm::detail {
class SubscriberImpl;
inline constexpr size_t kMaxServiceDirLength = 107 - (2 + 16 + 1 + 7 + 1 + 8 + 5 + 4);
std::vector<std::string> ForeignServiceDirs(const std::string& root, const std::string& owner,
const std::string& domain);
class EventHandler {
public:
virtual ~EventHandler() = default;
virtual void OnEvent(uint32_t events) noexcept = 0;
};
class RuntimeImpl {
public:
static RuntimeImpl* Instance() noexcept;
static Error LastInitError() noexcept;
static Result<void> Init(const RuntimeOptions& options);
static bool IsInitialized() noexcept;
static void Shutdown() noexcept;
~RuntimeImpl();
const std::string& ServiceDir() const noexcept { return service_dir_; }
bool OnControlThread() const noexcept;
void Call(const std::function<void()>& fn);
void Post(std::function<void()> fn);
bool AddFd(int fd, uint32_t events, std::shared_ptr<EventHandler> handler, uint64_t* tag = nullptr);
void RemoveFd(int fd);
void ModFd(int fd, uint64_t tag, uint32_t events) noexcept;
bool RunAfter(uint32_t delayMs, std::function<void()> fn) noexcept;
void ScheduleRescan() noexcept;
NotifySegment* Notify() const noexcept { return reinterpret_cast<NotifySegment*>(notify_.map.Data()); }
int NotifyFd() const noexcept { return notify_.fd.Get(); }
std::optional<uint32_t> AllocListenerSlot();
void FreeListenerSlot(uint32_t slot);
void RegisterSubscriber(uint64_t hash, const std::shared_ptr<SubscriberImpl>& sub);
void UnregisterSubscriber(uint64_t hash, const SubscriberImpl* sub);
void OfferPublisher(uint64_t hash, const std::string& path) noexcept;
void ScanFor(uint64_t hash) noexcept;
void ScanAll() noexcept;
bool PeerAllowed(const PeerCredentials& cred, int sock) const noexcept;
bool SetSocketAccess(const char* path) const noexcept;
static std::string HashPrefix(uint64_t hash);
private:
RuntimeImpl() = default;
Result<void> Start(const RuntimeOptions& options);
static Result<std::string> ResolveServiceDir(const RuntimeOptions& options, std::optional<gid_t>* group,
std::string* root = nullptr, std::string* domain = nullptr);
Error CreateNotifySegment();
bool OpenEventFds();
void Loop() noexcept;
int NextTimeoutMs() const noexcept;
void DispatchEvent(const struct epoll_event& event) noexcept;
void HandleInotify() noexcept;
void HandleInotifyEvent(const struct inotify_event& ev, bool* rescan, bool* rewatch) noexcept;
void Rewatch() noexcept;
void RunCommands() noexcept;
void RunTimers() noexcept;
void RunDeferred() noexcept;
RuntimeOptions options_;
std::string service_dir_;
std::optional<gid_t> group_;
uid_t euid_ = 0;
UniqueFd epoll_;
UniqueFd wake_;
UniqueFd inotify_;
int watch_ = -1;
Segment notify_;
std::thread thread_;
std::thread::id thread_id_;
bool running_ = true;
std::atomic<bool> stop_{false};
uint64_t rescan_at_ = 0;
uint32_t rescan_attempt_ = 0;
uint64_t rewatch_at_ = 0;
uint32_t rewatch_attempt_ = 0;
struct Registered {
uint32_t generation;
std::shared_ptr<EventHandler> handler;
};
std::unordered_map<int, Registered> handlers_;
uint32_t next_generation_ = 1;
std::multimap<uint64_t, std::function<void()>> timers_;
std::mutex cmd_mu_;
std::deque<std::function<void()>> commands_;
mutable std::mutex reg_mu_;
std::multimap<uint64_t, std::weak_ptr<SubscriberImpl>> subscribers_;
uint64_t listener_slots_used_ = 0;
};
}