-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchildprocess.cpp
157 lines (134 loc) · 4.93 KB
/
childprocess.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include "childprocess.h"
// POSIX
#include <assert.h>
// PUT
#include <put/specialized/procstat.h>
#include <put/specialized/eventbackend.h>
#include <put/cxxutils/vterm.h>
static std::unordered_map<pid_t, ChildProcess*> process_map; // do not try to own Process memory
void ChildProcess::init_once(void) noexcept
{
static bool first = true;
if(first)
{
first = false;
struct sigaction actions;
actions.sa_handler = &handler; // don't bother sending signal info (it's provided by waitpid())
sigemptyset(&actions.sa_mask);
actions.sa_flags = SA_RESTART;
flaw(::sigaction(SIGCHLD, &actions, NULL) == posix::error_response,
terminal::critical,
posix::exit(errno),,
"Unable assign action to a signal: %s", posix::strerror(errno))
}
}
void ChildProcess::handler(int signum) noexcept
{
flaw(signum != SIGCHLD,
terminal::warning,
posix::error(posix::errc::invalid_argument),,
"Process::reaper() has been called improperly")
pid_t pid = posix::error_response; // set value just in case
int status = 0;
while((pid = posix::waitpid(pid_t(-1), &status, WNOHANG | WCONTINUED | WUNTRACED)) > 0) // get the next dead process (if there is one)... while the currently reaped process was valid
{
auto process_map_iter = process_map.find(pid); // find dead process
if(process_map_iter != process_map.end()) // if the dead process exists...
{
ChildProcess* p = process_map_iter->second;
if(WIFEXITED(status))
{
EventBackend::remove(p->getStdOut(), EventBackend::SimplePollReadFlags);
EventBackend::remove(p->getStdErr(), EventBackend::SimplePollReadFlags);
posix::close(p->getStdOut());
posix::close(p->getStdErr());
posix::close(p->getStdIn());
p->m_state = ChildProcess::State::Finished;
if(WIFSIGNALED(status))
Object::enqueue_copy(p->killed, p->processId(), posix::Signal::EId(WTERMSIG(status)));
else
Object::enqueue_copy(p->finished, p->processId(), posix::error_t(WEXITSTATUS(status)));
process_map.erase(process_map_iter); // remove finished process from the process map
}
else if(WIFSTOPPED(status))
{
p->m_state = ChildProcess::State::Stopped;
Object::enqueue_copy(p->stopped, p->processId());
}
else if(WIFCONTINUED(status))
{
p->m_state = ChildProcess::State::Running;
Object::enqueue_copy(p->started, p->processId());
}
}
}
}
ChildProcess::ChildProcess(void) noexcept
: m_state(State::Initializing)
{
init_once();
process_map.emplace(processId(), this); // add self to process map
}
ChildProcess::~ChildProcess(void) noexcept
{
EventBackend::remove(getStdOut(), EventBackend::SimplePollReadFlags);
EventBackend::remove(getStdErr(), EventBackend::SimplePollReadFlags);
}
bool ChildProcess::setOption(const std::string& name, const std::string& value) noexcept
{
m_iobuf.reset();
return !(m_iobuf << name << value).hadError() && writeStdIn(m_iobuf);
}
bool ChildProcess::sendSignal(posix::Signal::EId id, int value) const noexcept
{
return posix::Signal::send(processId(), id, value);
}
bool ChildProcess::invoke(void) noexcept
{
posix::success();
flaw(m_state != State::Initializing,
terminal::severe,,
posix::error(posix::errc::device_or_resource_busy),
"Called Process::invoke() on an active process!");
EventBackend::add(getStdOut(), EventBackend::SimplePollReadFlags,
[this](posix::fd_t lambda_fd, native_flags_t) noexcept
{ Object::enqueue(stdoutMessage, lambda_fd); });
EventBackend::add(getStdErr(), EventBackend::SimplePollReadFlags,
[this](posix::fd_t lambda_fd, native_flags_t) noexcept
{ Object::enqueue(stderrMessage, lambda_fd); });
m_iobuf.reset();
if((m_iobuf << "Execute").hadError() ||
!writeStdIn(m_iobuf))
return false;
m_state = State::Invalid;
if(state() == State::Running)
Object::enqueue_copy(started, processId());
return posix::is_success();
}
ChildProcess::State ChildProcess::state(void) noexcept
{
switch(m_state)
{
case State::Finished:
case State::Initializing:
break;
default:
process_state_t data;
flaw(!::procstat(processId(), data) && m_state != State::Finished,
terminal::severe,
m_state = State::Invalid,
m_state,
"Process %i does not exist.", processId()); // process must exist (rare case where process could exit durring this call is handled)
switch (data.state)
{
case WaitingInterruptable:
case WaitingUninterruptable:
m_state = State::Waiting; break;
case Zombie : m_state = State::Zombie ; break;
case Stopped: m_state = State::Stopped; break;
case Running: m_state = State::Running; break;
default: break; // invalid state (process exited before it could be stat'd
}
}
return m_state;
}