-
Notifications
You must be signed in to change notification settings - Fork 390
/
instance_task.cc
174 lines (142 loc) · 4.54 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
/*
* Copyright (c) 2014 Juniper Networks, Inc. All right reserved.
*/
#include "oper/instance_task.h"
#include "base/logging.h"
#include "io/event_manager.h"
/*
* InstanceTask class
*/
InstanceTask::InstanceTask(const std::string &cmd, int cmd_type, EventManager *evm) :
cmd_(cmd), errors_(*(evm->io_service())), is_running_(false),
pid_(0), cmd_type_(cmd_type), start_time_(0) {
}
void InstanceTask::ReadErrors(const boost::system::error_code &ec,
size_t read_bytes) {
if (read_bytes) {
errors_data_ << rx_buff_;
}
if (ec) {
boost::system::error_code close_ec;
errors_.close(close_ec);
std::string errors = errors_data_.str();
if (errors.length() > 0) {
LOG(ERROR, "NetNS run errors: " << std::endl << errors);
if (!on_error_cb_.empty()) {
on_error_cb_(this, errors);
}
}
errors_data_.clear();
return;
}
bzero(rx_buff_, sizeof(rx_buff_));
boost::asio::async_read(
errors_,
boost::asio::buffer(rx_buff_, kBufLen),
boost::bind(&InstanceTask::ReadErrors,
this, boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
void InstanceTask::Stop() {
assert(pid_);
kill(pid_, SIGTERM);
}
void InstanceTask::Terminate() {
assert(pid_);
kill(pid_, SIGKILL);
}
pid_t InstanceTask::Run() {
std::vector<std::string> argv;
LOG(DEBUG, "NetNS run command: " << cmd_);
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 -1;
}
/*
* temporarily block SIGCHLD signals
*/
sigset_t mask;
sigset_t orig_mask;
sigemptyset (&mask);
sigaddset (&mask, SIGCHLD);
if (sigprocmask(SIG_BLOCK, &mask, &orig_mask) < 0) {
LOG(ERROR, "NetNS error: sigprocmask, " << strerror(errno));
}
pid_ = vfork();
if (pid_ == 0) {
close(err[0]);
dup2(err[1], STDERR_FILENO);
close(err[1]);
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);
}
if (sigprocmask(SIG_SETMASK, &orig_mask, NULL) < 0) {
LOG(ERROR, "NetNS error: sigprocmask, " << strerror(errno));
}
close(err[1]);
start_time_ = time(NULL);
boost::system::error_code ec;
errors_.assign(::dup(err[0]), ec);
close(err[0]);
if (ec) {
is_running_ = false;
return -1;
}
bzero(rx_buff_, sizeof(rx_buff_));
boost::asio::async_read(errors_, boost::asio::buffer(rx_buff_, kBufLen),
boost::bind(&InstanceTask::ReadErrors,
this, boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
return pid_;
}
/*
*
*/
InstanceTaskQueue::InstanceTaskQueue(EventManager *evm) : evm_(evm),
timeout_timer_(TimerManager::CreateTimer(
*evm_->io_service(),
"Instance Manager Task Timeout",
TaskScheduler::GetInstance()->GetTaskId(
"db::DBTable"), 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;
}
}