add spawn callback to Process

This commit is contained in:
Lily Tsuru 2024-02-01 23:30:02 -05:00
parent 4c1480d364
commit ef904f5403
3 changed files with 22 additions and 12 deletions

View File

@ -53,6 +53,8 @@ namespace nanosm {
} else {
// Parent: monitor FD
eventLoop.AddObject(IoObject::Ptr(shared_from_this()));
if(onProcessSpawn)
eventLoop.Post(onProcessSpawn);
}
}
@ -77,6 +79,10 @@ namespace nanosm {
kill(pid, SIGTERM);
}
void Process::SetSpawnCallback(std::function<void()> f) {
onProcessSpawn = f;
}
void Process::SetExitCallback(std::function<void(int)> f) {
onProcessExit = f;
}

View File

@ -25,6 +25,9 @@ namespace nanosm {
void Kill();
void SetSpawnCallback(std::function<void()> f);
void SetExitCallback(std::function<void(int)> f);
private:
@ -35,6 +38,7 @@ namespace nanosm {
siginfo_t siginfo {};
std::string commLine;
std::function<void()> onProcessSpawn;
std::function<void(int)> onProcessExit;
};

View File

@ -4,10 +4,9 @@
#include <memory>
#include "EventLoop.hpp"
#include "WordExp.hpp"
#include "Timer.hpp"
#include "Process.hpp"
#include "Timer.hpp"
#include "WordExp.hpp"
nanosm::EventLoop ev;
@ -17,7 +16,7 @@ auto timer = std::make_shared<nanosm::Timer>(ev);
auto process = std::make_shared<nanosm::Process>(ev);
void test() {
process->SetSpawnCallback([p = process]() {
// Do magic
process->SetExitCallback([p = process](int exitCode) {
printf("exited with %d exitcode\n", exitCode);
@ -30,6 +29,7 @@ void test() {
// Start the timer to wait a bit before restarting the process
timer->Arm(1);
});
});
ev.AddObject(timer);
process->Spawn("xterm");