-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathexec.cpp
More file actions
210 lines (161 loc) · 5.34 KB
/
exec.cpp
File metadata and controls
210 lines (161 loc) · 5.34 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/* Node-type for subprocess node-types.
*
* Author: Steffen Vogel <post@steffenvogel.de>
* SPDX-FileCopyrightText: 2014-2023 Institute for Automation of Complex Power Systems, RWTH Aachen University
* SPDX-License-Identifier: Apache-2.0
*/
#include <string>
#include <unistd.h>
#include <villas/format.hpp>
#include <villas/node/config.hpp>
#include <villas/node/exceptions.hpp>
#include <villas/nodes/exec.hpp>
#include <villas/utils.hpp>
using namespace villas;
using namespace villas::node;
using namespace villas::utils;
ExecNode::~ExecNode() {
if (stream_in)
fclose(stream_in);
if (stream_out)
fclose(stream_out);
}
int ExecNode::parse(json_t *json) {
int ret = Node::parse(json);
if (ret)
return ret;
json_error_t err;
int f = 1, s = -1;
json_t *json_exec;
json_t *json_env = nullptr;
json_t *json_format = nullptr;
const char *wd = nullptr;
ret = json_unpack_ex(
json, &err, 0, "{ s: o, s?: o, s?: b, s?: o, s?: b, s?: s }", "exec",
&json_exec, "format", &json_format, "flush", &f, "environment", &json_env,
"shell", &s, "working_directory", &wd);
if (ret)
throw ConfigError(json, err, "node-config-node-exec");
flush = f != 0;
shell = s < 0 ? json_is_string(json_exec) : s != 0;
arguments.clear();
environment.clear();
if (json_is_string(json_exec)) {
if (!shell)
throw ConfigError(
json_exec, "node-config-node-exec-shell",
"The exec setting must be an array if shell mode is disabled.");
command = json_string_value(json_exec);
} else if (json_is_array(json_exec)) {
if (shell)
throw ConfigError(
json_exec, "node-config-node-exec-shell",
"The exec setting must be a string if shell mode is enabled.");
if (json_array_size(json_exec) < 1)
throw ConfigError(json_exec, "node-config-node-exec-exec",
"At least one argument must be given");
size_t i;
json_t *json_arg;
json_array_foreach (json_exec, i, json_arg) {
if (!json_is_string(json_arg))
throw ConfigError(json_arg, "node-config-node-exec-exec",
"All arguments must be of string type");
if (i == 0)
command = json_string_value(json_arg);
arguments.push_back(json_string_value(json_arg));
}
}
if (json_env) {
// obj is a JSON object
const char *key;
json_t *json_value;
json_object_foreach (json_env, key, json_value) {
if (!json_is_string(json_value))
throw ConfigError(json_value, "node-config-node-exec-environment",
"Environment variables must be of string type");
environment[key] = json_string_value(json_value);
}
}
// Format
auto *fmt = json_format ? FormatFactory::make(json_format)
: FormatFactory::make("villas.human");
formatter = Format::Ptr(fmt);
if (!formatter)
throw ConfigError(json_format, "node-config-node-exec-format",
"Invalid format configuration");
state = State::PARSED;
return 0;
}
int ExecNode::prepare() {
assert(state == State::CHECKED);
// Initialize IO
formatter->start(getInputSignals(false));
return Node::prepare();
}
int ExecNode::start() {
// Pass configuration file and node-name via environemnt
environment["VILLAS_NODE_CONFIG"] = configPath;
environment["VILLAS_NODE_NAME"] = name_short;
// Start subprocess
proc = std::make_unique<Popen>(command, arguments, environment, working_dir,
shell);
logger->debug("Started sub-process with pid={}", proc->getPid());
stream_in = fdopen(proc->getFdIn(), "r");
if (!stream_in)
return -1;
stream_out = fdopen(proc->getFdOut(), "w");
if (!stream_out)
return -1;
int ret = Node::start();
if (!ret)
state = State::STARTED;
return 0;
}
int ExecNode::stop() {
int ret = Node::stop();
if (ret)
return ret;
// Stop subprocess
logger->debug("Killing sub-process with pid={}", proc->getPid());
proc->kill(SIGINT);
logger->debug("Waiting for sub-process with pid={} to terminate",
proc->getPid());
proc->close();
// TODO: Check exit code of subprocess?
return 0;
}
int ExecNode::_read(struct Sample *smps[], unsigned cnt) {
return formatter->scan(stream_in, smps, cnt);
}
int ExecNode::_write(struct Sample *smps[], unsigned cnt) {
int ret;
ret = formatter->print(stream_out, smps, cnt);
if (ret < 0)
return ret;
if (flush)
fflush(stream_out);
return cnt;
}
const std::string &ExecNode::getDetails() {
if (details.empty()) {
std::string wd = working_dir;
if (wd.empty()) {
char buf[128];
wd = getcwd(buf, sizeof(buf));
}
details = fmt::format("exec={}, shell={}, flush={}, #environment={}, "
"#arguments={}, working_dir={}",
command, shell ? "yes" : "no", flush ? "yes" : "no",
environment.size(), arguments.size(), wd);
}
return details;
}
std::vector<int> ExecNode::getPollFDs() { return {proc->getFdIn()}; }
// Register node
static char n[] = "exec";
static char d[] = "Run subprocesses with stdin/stdout communication";
static NodePlugin<ExecNode, n, d,
(int)NodeFactory::Flags::SUPPORTS_READ |
(int)NodeFactory::Flags::SUPPORTS_WRITE |
(int)NodeFactory::Flags::SUPPORTS_POLL>
p;