-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathremote.c
621 lines (540 loc) · 18.6 KB
/
remote.c
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
/************************************************************\
* Copyright 2018 Lawrence Livermore National Security, LLC
* (c.f. AUTHORS, NOTICE.LLNS, COPYING)
*
* This file is part of the Flux resource manager framework.
* For details, see https://github.com/flux-framework.
*
* SPDX-License-Identifier: LGPL-3.0
\************************************************************/
#if HAVE_CONFIG_H
# include "config.h"
#endif
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdarg.h>
#include <errno.h>
#include <assert.h>
#include <flux/core.h>
#include "ccan/str/str.h"
#include "src/common/libczmqcontainers/czmq_containers.h"
#include "src/common/libutil/errprintf.h"
#include "src/common/libutil/log.h"
#include "src/common/libutil/fdwalk.h"
#include "src/common/libutil/macros.h"
#include "src/common/libutil/llog.h"
#include "src/common/libioencode/ioencode.h"
#include "subprocess.h"
#include "subprocess_private.h"
#include "command_private.h"
#include "remote.h"
#include "util.h"
#include "client.h"
static void remote_kill_nowait (flux_subprocess_t *p, int signum);
static void set_failed (flux_subprocess_t *p, const char *fmt, ...)
{
va_list ap;
va_start (ap, fmt);
verrprintf (&p->failed_error, fmt, ap);
p->failed_errno = errno;
va_end (ap);
}
static void stop_channel_watchers (flux_subprocess_t *p, bool in, bool out)
{
struct subprocess_channel *c;
c = zhash_first (p->channels);
while (c) {
if (out) {
flux_watcher_stop (c->out_prep_w);
flux_watcher_stop (c->out_idle_w);
flux_watcher_stop (c->out_check_w);
}
c = zhash_next (p->channels);
}
}
static void stop_io_watchers (flux_subprocess_t *p)
{
stop_channel_watchers (p, true, true);
}
static void stop_in_watchers (flux_subprocess_t *p)
{
stop_channel_watchers (p, true, false);
}
#if 0
static void stop_out_watchers (flux_subprocess_t *p)
{
stop_channel_watchers (p, false, true);
}
#endif
static void sigpending_cb (flux_future_t *f, void *arg)
{
flux_future_t *prev = arg;
/* fulfill the original future (prev) returned to the caller of
* flux_subprocess_kill() with the result from the actual
* remote kill(2) (f).
*/
flux_future_fulfill_with (prev, f);
/* Note, f is not destroyed here since it is owned by prev */
}
static void fwd_pending_signal (flux_subprocess_t *p)
{
flux_future_t *prev = flux_subprocess_aux_get (p, "sp::signal_future");
if (p->state == FLUX_SUBPROCESS_RUNNING) {
/* Remote process is now running, send pending signal */
flux_future_t *f = flux_subprocess_kill (p, p->signal_pending);
if (!f || (flux_future_then (f, -1., sigpending_cb, prev) < 0))
flux_future_fulfill_error (prev, errno, NULL);
/* If 'prev' is destroyed, then also destroy 'f'. Otherwise,
* use-after-free will occur with 'prev' if it is destroyed between
* now and when sigpending_cb() is run:
*/
if (flux_future_aux_set (prev,
NULL,
f,
(flux_free_f) flux_future_destroy) < 0) {
flux_future_fulfill_error (prev, errno, NULL);
flux_future_destroy (f);
}
}
else {
/* Remote process exited or failed, not able to send signal */
flux_future_fulfill_error (prev, EINVAL, NULL);
}
p->signal_pending = 0;
/* Now drop the reference on 'prev' added in add_pending_signal().
* This may destroy 'f' created above if the caller destroyed 'prev'
* before this callback was called.
*/
flux_future_decref (prev);
}
static void process_new_state (flux_subprocess_t *p,
flux_subprocess_state_t state)
{
if (p->state == FLUX_SUBPROCESS_FAILED)
return;
if (state == FLUX_SUBPROCESS_STOPPED) {
if (p->ops.on_state_change) {
/* always a chance caller may destroy subprocess in
* callback */
subprocess_incref (p);
(*p->ops.on_state_change) (p, FLUX_SUBPROCESS_STOPPED);
subprocess_decref (p);
}
return;
}
p->state = state;
if (state == FLUX_SUBPROCESS_EXITED) {
stop_in_watchers (p);
}
else if (state == FLUX_SUBPROCESS_FAILED) {
stop_io_watchers (p);
}
if (p->signal_pending)
fwd_pending_signal (p);
if (p->state != p->state_reported)
state_change_start (p);
}
static void process_add_credit (flux_subprocess_t *p, json_t *channels)
{
if (p->ops.on_credit) {
const char *key;
json_t *value;
/* always a chance caller may destroy subprocess in callback */
subprocess_incref (p);
json_object_foreach (channels, key, value) {
int bytes = json_integer_value (value);
p->ops.on_credit (p, key, bytes);
}
subprocess_decref (p);
}
}
static bool remote_out_data_available (struct subprocess_channel *c)
{
/* no need to handle failure states, on fatal error, the
* io watchers are closed */
/* N.B. if line buffered and buffer full, gotta flush it
* regardless if there's a line or not */
if ((c->line_buffered
&& (fbuf_has_line (c->read_buffer) || !fbuf_space (c->read_buffer)))
|| (!c->line_buffered && fbuf_bytes (c->read_buffer) > 0)
|| (c->read_eof_received && !c->eof_sent_to_caller))
return true;
return false;
}
static void remote_out_prep_cb (flux_reactor_t *r,
flux_watcher_t *w,
int revents,
void *arg)
{
struct subprocess_channel *c = arg;
if (remote_out_data_available (c))
flux_watcher_start (c->out_idle_w);
}
static void remote_out_check_cb (flux_reactor_t *r,
flux_watcher_t *w,
int revents,
void *arg)
{
struct subprocess_channel *c = arg;
flux_watcher_stop (c->out_idle_w);
/* always a chance caller may destroy subprocess in callback */
subprocess_incref (c->p);
if ((c->line_buffered
&& (fbuf_has_line (c->read_buffer)
|| !fbuf_space (c->read_buffer)
|| (c->read_eof_received
&& fbuf_bytes (c->read_buffer) > 0)))
|| (!c->line_buffered && fbuf_bytes (c->read_buffer) > 0)) {
c->output_cb (c->p, c->name);
}
if (!fbuf_bytes (c->read_buffer)
&& c->read_eof_received
&& !c->eof_sent_to_caller) {
c->output_cb (c->p, c->name);
c->eof_sent_to_caller = true;
c->p->channels_eof_sent++;
}
/* no need to handle failure states, on fatal error, the
* io watchers are closed */
if (!remote_out_data_available (c) || c->eof_sent_to_caller) {
/* if no data in buffer, shut down prep/check */
flux_watcher_stop (c->out_prep_w);
flux_watcher_stop (c->out_check_w);
/* close input side as well if eof sent to caller */
if (c->eof_sent_to_caller)
c->closed = true;
}
if (c->p->state == FLUX_SUBPROCESS_EXITED && c->eof_sent_to_caller)
subprocess_check_completed (c->p);
subprocess_decref (c->p);
}
static int remote_channel_setup (flux_subprocess_t *p,
flux_subprocess_output_f output_cb,
const char *name,
int channel_flags)
{
struct subprocess_channel *c = NULL;
char *e = NULL;
int save_errno;
if (!(c = channel_create (p, output_cb, name, channel_flags))) {
llog_debug (p, "channel_create: %s", strerror (errno));
goto error;
}
if (channel_flags & CHANNEL_READ) {
int wflag;
if ((wflag = cmd_option_line_buffer (p, name)) < 0) {
llog_debug (p, "cmd_option_line_buffer: %s", strerror (errno));
goto error;
}
if (wflag)
c->line_buffered = true;
if (!(p->flags & FLUX_SUBPROCESS_FLAGS_LOCAL_UNBUF)) {
int buffer_size;
if ((buffer_size = cmd_option_bufsize (p, name)) < 0) {
llog_debug (p, "cmd_option_bufsize: %s", strerror (errno));
goto error;
}
if (!(c->read_buffer = fbuf_create (buffer_size))) {
llog_debug (p, "fbuf_create: %s", strerror (errno));
goto error;
}
if (!(c->out_prep_w = flux_prepare_watcher_create (p->reactor,
remote_out_prep_cb,
c))) {
llog_debug (p, "flux_prepare_watcher_create: %s", strerror (errno));
goto error;
}
if (!(c->out_idle_w = flux_idle_watcher_create (p->reactor,
NULL,
c))) {
llog_debug (p, "flux_idle_watcher_create: %s", strerror (errno));
goto error;
}
if (!(c->out_check_w = flux_check_watcher_create (p->reactor,
remote_out_check_cb,
c))) {
llog_debug (p, "flux_check_watcher_create: %s", strerror (errno));
goto error;
}
/* the output check should be called before other check
* callbacks, to ensure that the output buffer is emptied
* before any check callbacks that may move data into the
* buffer. So we up the priority of the output check
* watcher.
*/
flux_watcher_set_priority (c->out_check_w, 1);
/* don't start these watchers until we've reached the running
* state */
}
p->channels_eof_expected++;
}
if (zhash_insert (p->channels, name, c) < 0) {
llog_debug (p, "zhash_insert failed");
errno = EEXIST;
goto error;
}
if (!zhash_freefn (p->channels, name, channel_destroy)) {
llog_debug (p, "zhash_freefn failed");
goto error;
}
/* now error is in subprocess_free()'s responsibility
*/
c = NULL;
free (e);
return 0;
error:
save_errno = errno;
channel_destroy (c);
free (e);
errno = save_errno;
return -1;
}
static int remote_setup_stdio (flux_subprocess_t *p)
{
/* stdio is identical to channels, except they are limited to read
* and/or write, and the buffer's automatically get a NUL char
* appended on reads */
if (remote_channel_setup (p,
NULL,
"stdin",
CHANNEL_WRITE) < 0)
return -1;
if (p->ops.on_stdout) {
if (remote_channel_setup (p,
p->ops.on_stdout,
"stdout",
CHANNEL_READ) < 0)
return -1;
}
if (p->ops.on_stderr) {
if (remote_channel_setup (p,
p->ops.on_stderr,
"stderr",
CHANNEL_READ) < 0)
return -1;
}
return 0;
}
static int remote_setup_channels (flux_subprocess_t *p)
{
zlist_t *channels = cmd_channel_list (p->cmd);
const char *name;
int channel_flags = CHANNEL_READ | CHANNEL_WRITE | CHANNEL_FD;
if (zlist_size (channels) == 0)
return 0;
if (!p->ops.on_channel_out)
channel_flags &= ~CHANNEL_READ;
name = zlist_first (channels);
while (name) {
if (remote_channel_setup (p,
p->ops.on_channel_out,
name,
channel_flags) < 0)
return -1;
name = zlist_next (channels);
}
return 0;
}
int subprocess_remote_setup (flux_subprocess_t *p, const char *service_name)
{
if (remote_setup_stdio (p) < 0)
return -1;
if (remote_setup_channels (p) < 0)
return -1;
if (!(p->service_name = strdup (service_name)))
return -1;
return 0;
}
static int remote_output_local_unbuf (flux_subprocess_t *p,
const char *stream,
const char *data,
int len,
bool eof)
{
struct subprocess_channel *c;
int rv = -1;
/* always a chance caller may destroy subprocess in callback */
subprocess_incref (p);
if (!(c = zhash_lookup (p->channels, stream))) {
llog_debug (p,
"Error returning %d bytes received from remote"
" subprocess pid %d %s: unknown channel name",
len,
(int)flux_subprocess_pid (p),
stream);
errno = EPROTO;
set_failed (p, "error returning unknown channel %s", stream);
goto out;
}
if (data && len) {
c->unbuf_data = data;
c->unbuf_len = len;
if (eof)
c->read_eof_received = true;
c->output_cb (c->p, c->name);
}
/* N.B. any data not consumed by the user is lost, so if eof is
* seen, we send it immediately */
if (eof && !c->eof_sent_to_caller) {
c->read_eof_received = true;
c->unbuf_data = NULL;
c->unbuf_len = 0;
c->output_cb (c->p, c->name);
c->eof_sent_to_caller = true;
c->p->channels_eof_sent++;
}
rv = 0;
out:
subprocess_decref (p);
return rv;
}
static int remote_output_buffered (flux_subprocess_t *p,
const char *stream,
const char *data,
int len,
bool eof)
{
struct subprocess_channel *c;
if (!(c = zhash_lookup (p->channels, stream))) {
llog_debug (p,
"Error buffering %d bytes received from remote"
" subprocess pid %d %s: unknown channel name",
len,
(int)flux_subprocess_pid (p),
stream);
errno = EPROTO;
set_failed (p, "error buffering unknown channel %s", stream);
return -1;
}
if (data && len) {
int tmp;
tmp = fbuf_write (c->read_buffer, data, len);
if (tmp >= 0 && tmp < len) {
errno = ENOSPC; // short write is promoted to fatal error
tmp = -1;
}
if (tmp < 0) {
llog_debug (p,
"Error buffering %d bytes received from remote"
" subprocess pid %d %s: %s",
len,
(int)flux_subprocess_pid (p),
stream,
strerror (errno));
set_failed (p, "error buffering %d bytes of data", len);
return -1;
}
}
if (eof) {
c->read_eof_received = true;
if (fbuf_readonly (c->read_buffer) < 0)
llog_debug (p, "fbuf_readonly: %s", strerror (errno));
}
if (remote_out_data_available (c)) {
/* read buffer has stuff in it, start watchers */
flux_watcher_start (c->out_prep_w);
flux_watcher_start (c->out_check_w);
}
return 0;
}
static void rexec_continuation (flux_future_t *f, void *arg)
{
flux_subprocess_t *p = arg;
const char *stream;
const char *data;
json_t *channels = NULL;
int len;
bool eof;
if (subprocess_rexec_get (f) < 0) {
if (errno == ENODATA) {
p->remote_completed = true;
/* Per RFC42, when remote processes are launched, the
* process should return finished (i.e. state EXITED)
* before returning ENODATA. It is otherwise considered a
* protocol error.
*
* N.B. There is evidence that the sdexec module has
* violated the protocol before #5956.
*/
if (p->state != FLUX_SUBPROCESS_EXITED) {
errno = EPROTO;
set_failed (p, "%s", strerror (errno));
goto error;
}
subprocess_check_completed (p);
return;
}
set_failed (p, "%s", future_strerror (f, errno));
goto error;
}
if (subprocess_rexec_is_started (f, &p->pid)) {
p->pid_set = true;
process_new_state (p, FLUX_SUBPROCESS_RUNNING);
}
else if (subprocess_rexec_is_stopped (f)) {
process_new_state (p, FLUX_SUBPROCESS_STOPPED);
}
else if (subprocess_rexec_is_finished (f, &p->status)) {
process_new_state (p, FLUX_SUBPROCESS_EXITED);
}
else if (subprocess_rexec_is_add_credit (f, &channels)) {
process_add_credit (p, channels);
}
else if (subprocess_rexec_is_output (f, &stream, &data, &len, &eof)) {
if (p->flags & FLUX_SUBPROCESS_FLAGS_LOCAL_UNBUF) {
if (remote_output_local_unbuf (p, stream, data, len, eof) < 0)
goto error;
}
else {
if (remote_output_buffered (p, stream, data, len, eof) < 0)
goto error;
}
}
flux_future_reset (f);
return;
error:
/* c->p->failed_errno and c->p->failed_error expected to be
* set before this point (typically via set_failed())
*/
process_new_state (p, FLUX_SUBPROCESS_FAILED);
remote_kill_nowait (p, SIGKILL);
}
int remote_exec (flux_subprocess_t *p)
{
flux_future_t *f;
int flags = 0;
if (zlist_size (cmd_channel_list (p->cmd)) > 0)
flags |= SUBPROCESS_REXEC_CHANNEL;
if (p->ops.on_stdout)
flags |= SUBPROCESS_REXEC_STDOUT;
if (p->ops.on_stderr)
flags |= SUBPROCESS_REXEC_STDERR;
if (p->ops.on_credit)
flags |= SUBPROCESS_REXEC_WRITE_CREDIT;
if (!(f = subprocess_rexec (p->h, p->service_name, p->rank, p->cmd, flags))
|| flux_future_then (f, -1., rexec_continuation, p) < 0) {
llog_debug (p,
"error sending rexec.exec request: %s",
strerror (errno));
flux_future_destroy (f);
return -1;
}
p->f = f;
return 0;
}
flux_future_t *remote_kill (flux_subprocess_t *p, int signum)
{
return subprocess_kill (p->h, p->service_name, p->rank, p->pid, signum);
}
static void remote_kill_nowait (flux_subprocess_t *p, int signum)
{
if (p->pid_set) {
flux_future_t *f;
f = remote_kill (p, signum);
flux_future_destroy (f);
}
}
/*
* vi: ts=4 sw=4 expandtab
*/