Commit 76287053 authored by jan.koester's avatar jan.koester
Browse files

test

parent 3e048496
Loading
Loading
Loading
Loading
+57 −1
Changes for src/main.cpp: 57 added lines, 1 removed line.
Original line number Diff line number Diff line
#include <algorithm>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <map>
@@ -7,8 +8,10 @@
#include <thread>
#include <vector>

#include <fcntl.h>
#include <grp.h>
#include <pwd.h>
#include <sys/stat.h>
#include <unistd.h>

#include "api/JsonApiServer.h"
@@ -147,14 +150,67 @@ void writePidFile(const std::string &path) {
    out << ::getpid() << std::endl;
}

// Looks for "-f"/"--foreground" among the args after the positional config path (argv[1])
// and returns its value string, or "" if absent. Matches authdb's own -f flag (same
// project family) in spirit, but hand-parsed here since the config path stays a plain
// positional argument rather than moving to cmdplus's flag-only model.
std::string foregroundFlagValue(int argc, char **argv) {
    for (int i = 2; i < argc; ++i) {
        std::string arg = argv[i];
        if ((arg == "-f" || arg == "--foreground") && i + 1 < argc) {
            return argv[i + 1];
        }
    }
    return "";
}

#ifndef Windows
// Classic double-step daemonize (fork, detach from the controlling terminal via setsid(),
// redirect stdio to /dev/null): matches authdb's own -f/--foreground daemonizing exactly
// (see authdb/src/authdb.cpp's main()) for consistency across this project family. Must run
// before any threads exist (harmless combined with fork() as it is here, but wrong once any
// exist) and before ConfigStore/AuthDBAdapter/etc. open anything stateful, so the forked
// child -- not a parent that's about to exit -- is what actually owns them.
void daemonize() {
    pid_t pid = ::fork();
    if (pid < 0) {
        std::cerr << "[main] fork failed" << std::endl;
        std::exit(1);
    }
    if (pid > 0) std::exit(0); // parent: detach, let the child carry on in the background

    if (::setsid() < 0) {
        std::cerr << "[main] setsid failed" << std::endl;
        std::exit(1);
    }

    ::umask(0);

    int devnull = ::open("/dev/null", O_RDWR);
    if (devnull >= 0) {
        ::dup2(devnull, STDIN_FILENO);
        ::dup2(devnull, STDOUT_FILENO);
        ::dup2(devnull, STDERR_FILENO);
        if (devnull > STDERR_FILENO) ::close(devnull);
    }
}
#endif

} // namespace

int main(int argc, char **argv) {
    if (argc < 2) {
        std::cerr << "usage: " << argv[0] << " /etc/proxyplus/proxyplus.yaml" << std::endl;
        std::cerr << "usage: " << argv[0] << " /etc/proxyplus/proxyplus.yaml [-f true]" << std::endl;
        return 1;
    }

#ifndef Windows
    // Daemonizes (background, detached) by default -- pass "-f true" to stay in the
    // foreground instead (e.g. under systemd Type=simple, Docker, or for debugging).
    bool foreground = foregroundFlagValue(argc, argv) == "true";
    if (!foreground) daemonize();
#endif

    try {
        proxyplus::config::BootstrapConfig bootstrap(argv[1]);
        auto cfg = bootstrap.get();