/
instance_task.cc
185 lines (152 loc) · 5.31 KB
/
instance_task.cc
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
* Copyright (c) 2014 Juniper Networks, Inc. All right reserved.
*/
#include <tbb/tbb.h>
#include "oper/instance_task.h"
#include "base/logging.h"
#include "io/event_manager.h"
InstanceTask::InstanceTask()
: is_running_(false), start_time_(0), reattempts_(0)
{}
InstanceTaskExecvp::InstanceTaskExecvp(const std::string &name,
const std::string &cmd,
int cmd_type, EventManager *evm) :
name_(name), cmd_(cmd), input_(*(evm->io_service())),
setup_done_(false), pid_(0), cmd_type_(cmd_type), pipe_stdout_(false) {
}
void InstanceTaskExecvp::ReadData(const boost::system::error_code &ec,
size_t read_bytes) {
if (read_bytes) {
if (!on_data_cb_.empty()) {
std::string data(rx_buff_, read_bytes);
on_data_cb_(this, data);
}
}
if (ec) {
boost::system::error_code close_ec;
input_.close(close_ec);
if (!on_exit_cb_.empty()) {
on_exit_cb_(this, ec);
}
return;
}
bzero(rx_buff_, sizeof(rx_buff_));
input_.async_read_some(boost::asio::buffer(rx_buff_, kBufLen),
boost::bind(&InstanceTaskExecvp::ReadData,
this, boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
void InstanceTaskExecvp::Stop() {
assert(pid_);
kill(pid_, SIGTERM);
}
void InstanceTaskExecvp::Terminate() {
assert(pid_);
kill(pid_, SIGKILL);
}
bool InstanceTaskExecvp::IsSetup() {
return setup_done_;
}
// If there is an error before the fork, task is set to "not running"
// and "false" is returned to caller so that caller can take appropriate
// action on task. If an error is encounted after fork, it is very
// likely that child process is running so we keep the task status as
// "running" and return "false" to caller, so that caller does not
// attempt to run the same task again. In this case, the child process
// exit notification can not be received by instance manager, hence
// instance manager has to rely on TaskTimeout delete the task.
bool InstanceTaskExecvp::Run() {
std::vector<std::string> argv;
is_running_ = true;
boost::split(argv, cmd_, boost::is_any_of(" "), boost::token_compress_on);
std::vector<const char *> c_argv(argv.size() + 1);
for (std::size_t i = 0; i != argv.size(); ++i) {
c_argv[i] = argv[i].c_str();
}
int err[2];
if (pipe(err) < 0) {
return is_running_ = false;
}
pid_ = vfork();
if (pid_ == 0) {
close(err[0]);
if (pipe_stdout_) {
dup2(err[1], STDOUT_FILENO);
} else {
dup2(err[1], STDERR_FILENO);
}
close(err[1]);
if (!pipe_stdout_) {
close(STDOUT_FILENO);
close(STDIN_FILENO);
}
/* Close all the open fds before execvp */
CloseTaskFds();
execvp(c_argv[0], (char **) c_argv.data());
perror("execvp");
_exit(127);
}
close(err[1]);
start_time_ = time(NULL);
int fd = ::dup(err[0]);
close(err[0]);
if (fd == -1) {
//is_running_ is still true indicating the child process is
//running. Caller needs to verify the status before acting on
//the task again
return false;
}
boost::system::error_code ec;
input_.assign(fd, ec);
if (ec) {
close(fd);
//is_running_ is still true indicating the child process is
//running. Caller needs to verify the status before acting on
//the task again
return false;
}
setup_done_ = true;
bzero(rx_buff_, sizeof(rx_buff_));
input_.async_read_some(boost::asio::buffer(rx_buff_, kBufLen),
boost::bind(&InstanceTaskExecvp::ReadData,
this, boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
return true;
}
InstanceTaskQueue::InstanceTaskQueue(EventManager *evm) : evm_(evm),
timeout_timer_(TimerManager::CreateTimer(
*evm_->io_service(),
"Instance Manager Task Timeout",
TaskScheduler::GetInstance()->GetTaskId(
INSTANCE_MANAGER_TASK_NAME), 0)) {
}
InstanceTaskQueue::~InstanceTaskQueue() {
TimerManager::DeleteTimer(timeout_timer_);
}
void InstanceTaskQueue::StartTimer(int time) {
timeout_timer_->Start(time,
boost::bind(&InstanceTaskQueue::OnTimerTimeout,
this),
boost::bind(&InstanceTaskQueue::TimerErrorHandler,
this, _1, _2));
}
void InstanceTaskQueue::StopTimer() {
timeout_timer_->Cancel();
}
bool InstanceTaskQueue::OnTimerTimeout() {
if (! on_timeout_cb_.empty()) {
on_timeout_cb_(this);
}
return true;
}
void InstanceTaskQueue::TimerErrorHandler(const std::string &name, std::string error) {
LOG(ERROR, "NetNS timeout error: " << error);
}
void InstanceTaskQueue::Clear() {
timeout_timer_->Cancel();
while(!task_queue_.empty()) {
InstanceTask *task = task_queue_.front();
task_queue_.pop();
delete task;
}
}