Squash commits for public release
This commit is contained in:
15
userland/servers/launch_server/BUILD.gn
Normal file
15
userland/servers/launch_server/BUILD.gn
Normal file
@@ -0,0 +1,15 @@
|
||||
import("//build/userland/TEMPLATE.gni")
|
||||
|
||||
xOS_executable("launch_server") {
|
||||
signexec = true
|
||||
install_path = "System/"
|
||||
sources = [
|
||||
"LaunchWatchdog.cpp",
|
||||
"main.cpp",
|
||||
]
|
||||
configs = [ "//build/userland:userland_flags" ]
|
||||
deplibs = [
|
||||
"libcxx",
|
||||
"libfoundation",
|
||||
]
|
||||
}
|
||||
41
userland/servers/launch_server/Exec.h
Normal file
41
userland/servers/launch_server/Exec.h
Normal file
@@ -0,0 +1,41 @@
|
||||
#pragma once
|
||||
|
||||
#include <csignal>
|
||||
#include <string>
|
||||
#include <unistd.h>
|
||||
#include <vector>
|
||||
|
||||
namespace LaunchServer {
|
||||
|
||||
class Exec {
|
||||
public:
|
||||
enum Flags {
|
||||
None = 0,
|
||||
RestartOnFail = (1 << 0),
|
||||
};
|
||||
|
||||
Exec(const std::string& path, Flags flag = Flags::None)
|
||||
: m_path(path)
|
||||
, m_flags(flag)
|
||||
{
|
||||
}
|
||||
~Exec() = default;
|
||||
|
||||
void launch()
|
||||
{
|
||||
m_pid = fork();
|
||||
if (m_pid == 0) {
|
||||
execlp(m_path.c_str(), m_path.c_str(), NULL);
|
||||
std::abort();
|
||||
}
|
||||
}
|
||||
|
||||
bool is_alive() const { return m_pid > 0 && kill(m_pid, 0) == 0; }
|
||||
|
||||
private:
|
||||
std::string m_path;
|
||||
Flags m_flags;
|
||||
int m_pid { -1 };
|
||||
};
|
||||
|
||||
}
|
||||
13
userland/servers/launch_server/LaunchWatchdog.cpp
Normal file
13
userland/servers/launch_server/LaunchWatchdog.cpp
Normal file
@@ -0,0 +1,13 @@
|
||||
#include "LaunchWatchdog.h"
|
||||
|
||||
namespace LaunchServer {
|
||||
|
||||
LaunchWatchdog* s_LaunchServer_LaunchWatchdog_the = nullptr;
|
||||
|
||||
LaunchWatchdog::LaunchWatchdog()
|
||||
: LFoundation::EventReceiver()
|
||||
{
|
||||
s_LaunchServer_LaunchWatchdog_the = this;
|
||||
}
|
||||
|
||||
}
|
||||
37
userland/servers/launch_server/LaunchWatchdog.h
Normal file
37
userland/servers/launch_server/LaunchWatchdog.h
Normal file
@@ -0,0 +1,37 @@
|
||||
#pragma once
|
||||
|
||||
#include "Exec.h"
|
||||
#include <iostream>
|
||||
#include <libfoundation/EventReceiver.h>
|
||||
#include <vector>
|
||||
|
||||
namespace LaunchServer {
|
||||
|
||||
class LaunchWatchdog : public LFoundation::EventReceiver {
|
||||
public:
|
||||
inline static LaunchWatchdog& the()
|
||||
{
|
||||
extern LaunchWatchdog* s_LaunchServer_LaunchWatchdog_the;
|
||||
return *s_LaunchServer_LaunchWatchdog_the;
|
||||
}
|
||||
|
||||
LaunchWatchdog();
|
||||
~LaunchWatchdog() = default;
|
||||
|
||||
void tick()
|
||||
{
|
||||
for (auto& exec : m_execs) {
|
||||
if (!exec.is_alive()) {
|
||||
exec.launch();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void add(const Exec& exec) { m_execs.push_back(exec); }
|
||||
void add(Exec&& exec) { m_execs.push_back(std::move(exec)); }
|
||||
|
||||
private:
|
||||
std::vector<Exec> m_execs;
|
||||
};
|
||||
|
||||
}
|
||||
34
userland/servers/launch_server/main.cpp
Normal file
34
userland/servers/launch_server/main.cpp
Normal file
@@ -0,0 +1,34 @@
|
||||
#include "LaunchWatchdog.h"
|
||||
#include <libfoundation/EventLoop.h>
|
||||
#include <libfoundation/json/Parser.h>
|
||||
#include <new>
|
||||
|
||||
void load_from_file(LaunchServer::LaunchWatchdog& launch_watchdog)
|
||||
{
|
||||
auto json_parser = LFoundation::Json::Parser("/System/launch_server_config.json");
|
||||
LFoundation::Json::Object* jobj_root = json_parser.object();
|
||||
if (jobj_root->invalid()) {
|
||||
std::abort();
|
||||
}
|
||||
|
||||
auto* jdict_root = jobj_root->cast_to<LFoundation::Json::DictObject>();
|
||||
auto* jlaunch_list = jdict_root->data()["launch"]->cast_to<LFoundation::Json::ListObject>();
|
||||
for (auto* jobj : jlaunch_list->data()) {
|
||||
const std::string& strdata = jobj->cast_to<LFoundation::Json::StringObject>()->data();
|
||||
launch_watchdog.add(LaunchServer::Exec(strdata, LaunchServer::Exec::Flags::RestartOnFail));
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
auto* event_loop = new LFoundation::EventLoop();
|
||||
auto* launch_watchdog = new LaunchServer::LaunchWatchdog();
|
||||
|
||||
load_from_file(*launch_watchdog);
|
||||
launch_watchdog->tick();
|
||||
event_loop->add(LFoundation::Timer([launch_watchdog] {
|
||||
launch_watchdog->tick();
|
||||
},
|
||||
5000, LFoundation::Timer::Repeat));
|
||||
return event_loop->run();
|
||||
}
|
||||
Reference in New Issue
Block a user