Begin implementing nanosm main logic

This commit is contained in:
Lily Tsuru 2024-02-02 05:50:08 -05:00
parent 753164b393
commit 44160204e2
5 changed files with 67 additions and 27 deletions

View File

@ -14,7 +14,11 @@ A more robust solution that's still small and easy to setup (read: Not written i
# Installation # Installation
TODO ```bash
$ cmake -Wno-dev -GNinja -Bbuild -DCMAKE_INSTALL_PREFIX=/usr
$ ninja -C build
# ninja -C build install
```
# Configuration # Configuration

View File

@ -2,17 +2,13 @@
[nanosm] [nanosm]
# Controls if nanosm should be more verbose. Defaults to false. # The time in seconds nanosm will wait before restarting any app which crashes.
verbose = false
# The time in seconds nanosm will wait before restarting any app which exits.
restart-time = 1 restart-time = 1
# Any applications you want to run at startup. # Any applications you want to run at startup.
# Note that applications are executed in the order they are declared, but # Note that applications are executed in the order they are declared
# this will not hold true if any (or all apps) crash (they will be restarted # only once they are first started, and will from then on start in the
# effectively in a psuedorandom order). # order that they happen to crash. This may not be great but is how it be for now.
[nanosm.apps] [nanosm.apps]
# The window manager you want to use. # The window manager you want to use.
window-manager = { command = "/path/to/wm/binary --any-additional-args-here" } window-manager = { command = "/path/to/wm/binary --any-additional-args-here" }

View File

@ -51,7 +51,7 @@ namespace nanosm {
execvp(exp.words[0].data(), argv.data()); execvp(exp.words[0].data(), argv.data());
} else { } else {
// Parent: monitor FD // Parent: monitor the pidfd by adding ourselves to the event loop now
eventLoop.AddObject(IoObject::Ptr(shared_from_this())); eventLoop.AddObject(IoObject::Ptr(shared_from_this()));
if(onProcessSpawn) if(onProcessSpawn)
eventLoop.Post(onProcessSpawn); eventLoop.Post(onProcessSpawn);

View File

@ -3,6 +3,7 @@
namespace nanosm { namespace nanosm {
/// A timerfd backed timer. Is always oneshot.
struct Timer : nanosm::EventLoop::IoObject, std::enable_shared_from_this<Timer> { struct Timer : nanosm::EventLoop::IoObject, std::enable_shared_from_this<Timer> {
Timer(nanosm::EventLoop& ev); Timer(nanosm::EventLoop& ev);
@ -15,8 +16,12 @@ namespace nanosm {
void OnReady(int eventMask) override; void OnReady(int eventMask) override;
/// Set the callback used to notify when the timer expires.
void SetExpiryCallback(std::function<void()> expire); void SetExpiryCallback(std::function<void()> expire);
/// Arm and start the timer.
void Arm(u32 durationSeconds); void Arm(u32 durationSeconds);
private: private:
int timerFd { -1 }; int timerFd { -1 };
std::function<void()> cb; std::function<void()> cb;

View File

@ -1,33 +1,68 @@
#include <ctime> #include <ctime>
#include <map>
#include <memory> #include <memory>
#include "EventLoop.hpp" #include "EventLoop.hpp"
#include "Process.hpp"
#include "RestartingProcess.hpp" #include "RestartingProcess.hpp"
#include "Timer.hpp"
nanosm::EventLoop ev; struct NanoSm {
struct App : std::enable_shared_from_this<App> {
auto process = std::make_shared<nanosm::RestartingProcess>(ev); App(nanosm::EventLoop& ev, const std::string& tomlId, const std::string& commandLine)
: process(std::make_shared<nanosm::RestartingProcess>(ev)),
// tests stuff :) id(tomlId),
void test() { commandLine(commandLine) {
process->SetSpawnCallback([](pid_t pid) {
printf("\"%s\" spawned as pid %d successfully\n", process->GetCommandLine().c_str(), pid);
// Do magic
process->SetExitCallback([](pid_t pid, int exitCode) {
printf("\"%s\" pid %d exited with %d exitcode\n", process->GetCommandLine().c_str(), pid, exitCode);
});
});
process->Spawn("xterm");
} }
int main(int argc, char** argv) { void Spawn() {
ev.Post(test); process->SetSpawnCallback([self = shared_from_this()](pid_t pid) {
printf("\"%s\": command spawned as pid %d successfully\n", self->id.c_str(), pid);
// Set exit callback too
self->process->SetExitCallback([self](pid_t pid, int exitCode) {
printf("\"%s\": pid %d exited with %d exitcode\n", self->id.c_str(), pid, exitCode);
});
});
process->Spawn(commandLine);
}
private:
std::shared_ptr<nanosm::RestartingProcess> process;
std::string id;
std::string commandLine;
};
void AddApp(const std::string& id, const std::string& cmdLine) {
apps.push_back(std::make_shared<App>(ev, id, cmdLine));
}
void SpawnAllApps() {
for(auto& app : apps)
app->Spawn();
}
int Run() {
ev.Post([this]() {
this->SpawnAllApps();
});
ev.Run(); ev.Run();
return 0; return 0;
} }
private:
nanosm::EventLoop ev;
std::vector<std::shared_ptr<App>> apps;
};
int main(int argc, char** argv) {
NanoSm nanosm;
// Run a few hardcoded apps (as a test)
nanosm.AddApp("Xterm", "xterm");
nanosm.AddApp("Xterm2", "xterm");
return nanosm.Run();
}