Commit 6d06cf2d authored by jan.koester's avatar jan.koester
Browse files

Remove libcmdplus dependency, add restart limit (max 5)

parent 4d4a501c
Loading
Loading
Loading
Loading
+0 −2
Changes for CMakeLists.txt: 0 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -6,7 +6,6 @@ set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(libconfplus REQUIRED)
find_package(libcmdplus REQUIRED)

add_executable(syslite
    src/main.cpp
@@ -22,7 +21,6 @@ target_include_directories(syslite PRIVATE

target_link_libraries(syslite PRIVATE
    confplus::confplus
    cmdplus::cmdplus
    dl
)

+10 −32
Changes for src/main.cpp: 10 added lines, 32 removed lines.
Original line number Diff line number Diff line
#include "syslite.h"

#include <cmdplus/cmdplus.h>

#include <iostream>
#include <cstring>
#include <csignal>
#include <unistd.h>

@@ -16,31 +15,22 @@ static void sigIntHandler(int) {
    if (g_init) g_init->shutdown();
}

// Ctrl+Alt+Del sends SIGINT to PID 1 on Linux
static void sigRebootHandler(int) {
    if (g_init) g_init->reboot();
}

int main(int argc, char **argv) {
    cmdplus::CmdController &cmd = cmdplus::CmdController::getInstance();
    cmd.registerCmd("help", 'h', false, "", "Show help");
    cmd.registerCmd("config", 'c', true, "", "Config file path (e.g. yaml:/etc/syslite/syslite.yaml)");

    cmd.parseCmd(argc, argv);

    if (cmd.getCmdbyKey("help") && cmd.getCmdbyKey("help")->getFound()) {
        std::cout << "syslite — lightweight Linux init system" << std::endl;
        std::cout << std::endl;
        cmd.printHelp();
    std::string configPath = "yaml:/etc/syslite/syslite.yaml";

    for (int i = 1; i < argc; ++i) {
        if ((std::strcmp(argv[i], "-c") == 0 || std::strcmp(argv[i], "--config") == 0) && i + 1 < argc) {
            configPath = argv[++i];
        } else if (std::strcmp(argv[i], "-h") == 0 || std::strcmp(argv[i], "--help") == 0) {
            std::cout << "syslite — lightweight Linux init system\n"
                      << "  -h, --help     Show help\n"
                      << "  -c, --config   Config file path (default: yaml:/etc/syslite/syslite.yaml)\n";
            return 0;
        }

    if (!cmd.checkRequired()) {
        // If running as PID 1, use default config path
        if (getpid() != 1) {
            std::cerr << "Usage: syslite -c yaml:/etc/syslite/syslite.yaml" << std::endl;
            return 1;
        }
    }

    syslite::Init init;
@@ -56,24 +46,12 @@ int main(int argc, char **argv) {
    sa.sa_handler = sigIntHandler;
    sigaction(SIGINT, &sa, nullptr);

    // SIGUSR1 for reboot (alternative to Ctrl+Alt+Del)
    sa.sa_handler = sigRebootHandler;
    sigaction(SIGUSR1, &sa, nullptr);

    // Ignore SIGPIPE
    signal(SIGPIPE, SIG_IGN);

    // Load config
    std::string configPath;
    auto *configCmd = cmd.getCmdbyKey("config");
    if (configCmd && configCmd->getFound()) {
        configPath = configCmd->getValue();
    } else {
        configPath = "yaml:/etc/syslite/syslite.yaml";
    }

    init.loadConfig(configPath);

    // Run the init main loop
    return init.run();
}
+15 −0
Changes for src/service.cpp: 15 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -474,8 +474,23 @@ namespace syslite {
                shouldRestart = true;
            }

            // Give up after 5 consecutive failures
            if (entry.restartCount >= 5) {
                if (shouldRestart && entry.state != ServiceState::Stopped) {
                    std::cerr << "[syslite] " << name
                              << " failed too many times, giving up" << std::endl;
                    entry.state = ServiceState::Failed;
                }
                continue;
            }

            if (shouldRestart && entry.pid <= 0) {
                ++entry.restartCount;

                // Honor RestartSec — sleep before restarting
                if (entry.unit.restartSec > 0)
                    sleep(entry.unit.restartSec);

                std::cout << "[syslite] Restarting " << name
                          << " (attempt " << entry.restartCount << ")" << std::endl;
                execService(entry);