~siborgium/prelockdpp

fd38b14b615fe320c7d767efe1cf02486478a98d — Sergey Smirnykh 2 years ago 045d2c9
Add JSON, get_pid_list
2 files changed, 22 insertions(+), 5 deletions(-)

M CMakeLists.txt
M main.cpp
M CMakeLists.txt => CMakeLists.txt +3 -0
@@ 2,7 2,10 @@ cmake_minimum_required(VERSION 3.0)

project(prelockdpp LANGUAGES CXX)

find_package(nlohmann_json 3.10.5 REQUIRED)

add_executable(prelockdpp main.cpp)
target_compile_features(prelockdpp PRIVATE cxx_std_20)
target_link_libraries(prelockdpp PRIVATE nlohmann_json::nlohmann_json)

install(TARGETS prelockdpp RUNTIME DESTINATION bin)

M main.cpp => main.cpp +19 -5
@@ 1,9 1,10 @@
#include <iostream>
#include <fstream>
#include <chrono>
#include <charconv>
#include <string>
#include <string_view>
#include <filesystem>
#include <vector>

#include <cstring>



@@ 133,6 134,20 @@ struct context {
            throw std::runtime_error{ concat("mlockall failed ", errno_) };
        }
    }
    auto get_pid_list() {
        std::vector<int> pids;
        for (auto&& entry: fs::directory_iterator{ "/proc" }) {
            auto&& name = entry.path().filename().native();
            auto& new_pid = pids.emplace_back();
            auto [_, ec ] = std::from_chars(name.data(), name.data() + name.size(), new_pid);
            switch (unsigned(ec != std::errc()) + unsigned(new_pid == self_pid) * 2) {
                case 0: break;
                case 1: /* do nothing */ break;
                case 2: pids.pop_back(); break;
                case 3: throw std::logic_error{ "Unreachable" };
            }
        }
    }
};

struct lock_context {


@@ 150,9 165,8 @@ struct lock_context {
            throw errno_exception("Failed to create epoll fd", errno);
        }

        timer_event.events = EPOLLIN | EPOLLET;
        signal_event.events = EPOLLIN | EPOLLET;
        auto add = [&,this](auto& ev, auto& source) {
            ev.events = EPOLLIN | EPOLLET;
            ev.data.u32 = to_underlying(source.id);
            if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, source.fd, &ev)) {
                throw errno_exception("Failed to add epoll event source", errno);


@@ 196,8 210,8 @@ struct lock_context {
                    auto& event = events[i];
                    if (event.events & EPOLLIN) {
                        switch (static_cast<SourceId>(event.data.u32)) {
                            case SourceId::Timer:  on_timer();  break;
                            case SourceId::Signal: on_signal(); goto run_break;
                            case timer_fd::id:  on_timer();  break;
                            case signal_fd::id: on_signal(); goto run_break;
                            default: throw std::logic_error{ "Event source id is unknown" };
                        }
                    }