forked from facebook/watchman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spawn.cpp
319 lines (272 loc) · 8.82 KB
/
spawn.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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/* Copyright 2012-present Facebook, Inc.
* Licensed under the Apache License, Version 2.0 */
#include "watchman_system.h"
#include "make_unique.h"
#include "watchman.h"
using watchman::ChildProcess;
using watchman::FileDescriptor;
using Options = watchman::ChildProcess::Options;
using Environment = watchman::ChildProcess::Environment;
static std::unique_ptr<watchman_stream> prepare_stdin(
struct watchman_trigger_command* cmd,
w_query_res* res) {
char stdin_file_name[WATCHMAN_NAME_MAX];
if (cmd->stdin_style == trigger_input_style::input_dev_null) {
return w_stm_open("/dev/null", O_RDONLY|O_CLOEXEC);
}
// Adjust result to fit within the specified limit
if (cmd->max_files_stdin > 0) {
auto& fileList = res->resultsArray.array();
auto n_files = std::min(size_t(cmd->max_files_stdin), fileList.size());
fileList.resize(std::min(fileList.size(), n_files));
}
/* prepare the input stream for the child process */
snprintf(
stdin_file_name,
sizeof(stdin_file_name),
"%s/wmanXXXXXX",
watchman_tmp_dir);
auto stdin_file = w_mkstemp(stdin_file_name);
if (!stdin_file) {
w_log(W_LOG_ERR, "unable to create a temporary file: %s %s\n",
stdin_file_name, strerror(errno));
return NULL;
}
/* unlink the file, we don't need it in the filesystem;
* we'll pass the fd on to the child as stdin */
unlink(stdin_file_name); // FIXME: windows path translation
switch (cmd->stdin_style) {
case input_json:
{
w_jbuffer_t buffer;
w_log(W_LOG_ERR, "input_json: sending json object to stm\n");
if (!buffer.jsonEncodeToStream(
res->resultsArray, stdin_file.get(), 0)) {
w_log(W_LOG_ERR,
"input_json: failed to write json data to stream: %s\n",
strerror(errno));
return NULL;
}
break;
}
case input_name_list:
for (auto& name : res->resultsArray.array()) {
auto& nameStr = json_to_w_string(name);
if (stdin_file->write(nameStr.data(), nameStr.size()) !=
(int)nameStr.size() ||
stdin_file->write("\n", 1) != 1) {
w_log(
W_LOG_ERR,
"write failure while producing trigger stdin: %s\n",
strerror(errno));
return nullptr;
}
}
break;
case input_dev_null:
// already handled above
break;
}
stdin_file->rewind();
return stdin_file;
}
static void spawn_command(
const std::shared_ptr<w_root_t>& root,
struct watchman_trigger_command* cmd,
w_query_res* res,
struct ClockSpec* since_spec) {
long arg_max;
size_t argspace_remaining;
bool file_overflow = false;
#ifdef _WIN32
arg_max = 32*1024;
#else
arg_max = sysconf(_SC_ARG_MAX);
#endif
if (arg_max <= 0) {
argspace_remaining = UINT_MAX;
} else {
argspace_remaining = (uint32_t)arg_max;
}
// Allow some misc working overhead
argspace_remaining -= 32;
// Record an overflow before we call prepare_stdin(), which mutates
// and resizes the results to fit the specified limit.
if (cmd->max_files_stdin > 0 &&
res->resultsArray.array().size() > cmd->max_files_stdin) {
file_overflow = true;
}
auto stdin_file = prepare_stdin(cmd, res);
if (!stdin_file) {
w_log(
W_LOG_ERR,
"trigger %s:%s %s\n",
root->root_path.c_str(),
cmd->triggername.c_str(),
strerror(errno));
return;
}
// Assumption: that only one thread will be executing on a given
// cmd instance so that mutation of cmd->env is safe.
// This is guaranteed in the current architecture.
// It is way too much of a hassle to try to recreate the clock value if it's
// not a relative clock spec, and it's only going to happen on the first run
// anyway, so just skip doing that entirely.
if (since_spec && since_spec->tag == w_cs_clock) {
cmd->env.set("WATCHMAN_SINCE", since_spec->clock.position.toClockString());
} else {
cmd->env.unset("WATCHMAN_SINCE");
}
cmd->env.set(
"WATCHMAN_CLOCK", res->clockAtStartOfQuery.position().toClockString());
if (cmd->query->relative_root) {
cmd->env.set("WATCHMAN_RELATIVE_ROOT", cmd->query->relative_root);
} else {
cmd->env.unset("WATCHMAN_RELATIVE_ROOT");
}
// Compute args
auto args = json_deep_copy(cmd->command);
if (cmd->append_files) {
// Measure how much space the base args take up
for (size_t i = 0; i < json_array_size(args); i++) {
const char *ele = json_string_value(json_array_get(args, i));
argspace_remaining -= strlen(ele) + 1 + sizeof(char*);
}
// Dry run with env to compute space
size_t env_size;
cmd->env.asEnviron(&env_size);
argspace_remaining -= env_size;
for (const auto& item : res->dedupedFileNames) {
// also: NUL terminator and entry in argv
uint32_t size = item.size() + 1 + sizeof(char*);
if (argspace_remaining < size) {
file_overflow = true;
break;
}
argspace_remaining -= size;
json_array_append_new(args, w_string_to_json(item));
}
}
cmd->env.set("WATCHMAN_FILES_OVERFLOW", file_overflow);
Options opts;
opts.environment() = cmd->env;
#ifndef _WIN32
sigset_t mask;
sigemptyset(&mask);
opts.setSigMask(mask);
#endif
opts.setFlags(POSIX_SPAWN_SETPGROUP);
opts.dup2(stdin_file->getFileDescriptor(), STDIN_FILENO);
if (cmd->stdout_name) {
opts.open(STDOUT_FILENO, cmd->stdout_name, cmd->stdout_flags, 0666);
} else {
opts.dup2(FileDescriptor::stdOut(), STDOUT_FILENO);
}
if (cmd->stderr_name) {
opts.open(STDOUT_FILENO, cmd->stderr_name, cmd->stderr_flags, 0666);
} else {
opts.dup2(FileDescriptor::stdErr(), STDERR_FILENO);
}
// Figure out the appropriate cwd
w_string working_dir(cmd->query->relative_root);
if (!working_dir) {
working_dir = root->root_path;
}
auto cwd = cmd->definition.get_default("chdir");
if (cwd) {
auto target = json_to_w_string(cwd);
if (w_is_path_absolute_cstr_len(target.data(), target.size())) {
working_dir = target;
} else {
working_dir = w_string::pathCat({working_dir, target});
}
}
watchman::log(watchman::DBG, "using ", working_dir, " for working dir\n");
opts.chdir(working_dir.c_str());
try {
if (cmd->current_proc) {
cmd->current_proc->kill();
cmd->current_proc->wait();
}
cmd->current_proc =
watchman::make_unique<ChildProcess>(args, std::move(opts));
} catch (const std::exception& exc) {
watchman::log(
watchman::ERR,
"trigger ",
root->root_path,
":",
cmd->triggername,
" failed: ",
exc.what(),
"\n");
}
// We have integration tests that check for this string
watchman::log(
cmd->current_proc ? watchman::DBG : watchman::ERR,
"posix_spawnp: ",
cmd->triggername,
"\n");
}
bool watchman_trigger_command::maybeSpawn(
const std::shared_ptr<w_root_t>& root) {
bool didRun = false;
// If it looks like we're in a repo undergoing a rebase or
// other similar operation, we want to defer triggers until
// things settle down
if (root->view()->isVCSOperationInProgress()) {
w_log(W_LOG_DBG, "deferring triggers until VCS operations complete\n");
return false;
}
auto since_spec = query->since_spec.get();
if (since_spec && since_spec->tag == w_cs_clock) {
w_log(
W_LOG_DBG,
"running trigger \"%s\" rules! since %" PRIu32 "\n",
triggername.c_str(),
since_spec->clock.position.ticks);
} else {
w_log(W_LOG_DBG, "running trigger \"%s\" rules!\n", triggername.c_str());
}
// Triggers never need to sync explicitly; we are only dispatched
// at settle points which are by definition sync'd to the present time
query->sync_timeout = std::chrono::milliseconds(0);
watchman::log(watchman::DBG, "assessing trigger ", triggername, "\n");
try {
auto res = w_query_execute(query.get(), root, time_generator);
watchman::log(
watchman::DBG,
"trigger \"",
triggername,
"\" generated ",
res.resultsArray.array().size(),
" results\n");
// create a new spec that will be used the next time
auto saved_spec = std::move(query->since_spec);
query->since_spec =
watchman::make_unique<ClockSpec>(res.clockAtStartOfQuery);
watchman::log(
watchman::DBG,
"updating trigger \"",
triggername,
"\" use ",
res.clockAtStartOfQuery.position().ticks,
" ticks next time\n");
if (!res.resultsArray.array().empty()) {
didRun = true;
spawn_command(root, this, &res, saved_spec.get());
}
return didRun;
} catch (const QueryExecError& e) {
watchman::log(
watchman::ERR,
"error running trigger \"",
triggername,
"\" query: ",
e.what(),
"\n");
return false;
}
}
/* vim:ts=2:sw=2:et:
*/