varnish-cache/bin/varnishtest/vtc_process.c
0
/*-
1
 * Copyright (c) 2015 Varnish Software AS
2
 * All rights reserved.
3
 *
4
 * Author: Dridi Boukelmoune <dridi@varnish-software.com>
5
 *
6
 * SPDX-License-Identifier: BSD-2-Clause
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
11
 * 1. Redistributions of source code must retain the above copyright
12
 *    notice, this list of conditions and the following disclaimer.
13
 * 2. Redistributions in binary form must reproduce the above copyright
14
 *    notice, this list of conditions and the following disclaimer in the
15
 *    documentation and/or other materials provided with the distribution.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
21
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27
 * SUCH DAMAGE.
28
 *
29
 * XXX:
30
 *      -ignore-stderr (otherwise output to stderr is fail)
31
 */
32
33
#include "config.h"
34
35
#include <sys/ioctl.h>          // Linux: struct winsize
36
37
#include <ctype.h>
38
#include <fcntl.h>
39
#include <inttypes.h>
40
#include <poll.h>
41
#include <stdio.h>
42
#include <stdlib.h>
43
#include <string.h>
44
#ifdef __sun
45
#  include <stropts.h>
46
#endif
47
#include <termios.h>
48
#include <unistd.h>
49
50
#include "vtc.h"
51
52
#include "vre.h"
53
#include "vev.h"
54
#include "vlu.h"
55
#include "vsb.h"
56
#include "vsub.h"
57
58
#include "teken.h"
59
60
struct process {
61
        unsigned                magic;
62
#define PROCESS_MAGIC           0x1617b43e
63
        char                    *name;
64
        struct vtclog           *vl;
65
        VTAILQ_ENTRY(process)   list;
66
67
        char                    *spec;
68
        char                    *dir;
69
        char                    *out;
70
        char                    *err;
71
        int                     fd_term;
72
        int                     fd_stderr;
73
        int                     f_stdout;
74
        int                     f_stderr;
75
        struct vlu              *vlu_stdout;
76
        struct vlu              *vlu_stderr;
77
        int                     log;
78
        pid_t                   pid;
79
        int                     expect_exit;
80
        int                     expect_signal;
81
        int                     allow_core;
82
83
        uintmax_t               stdout_bytes;
84
        uintmax_t               stderr_bytes;
85
86
        pthread_mutex_t         mtx;
87
        pthread_t               tp;
88
        unsigned                hasthread;
89
90
        int                     nlin;
91
        int                     ncol;
92
        int                     ansi_response;
93
        char                    **vram;
94
        teken_t                 tek[1];
95
};
96
97
static VTAILQ_HEAD(, process)   processes =
98
    VTAILQ_HEAD_INITIALIZER(processes);
99
100
static void term_resize(struct process *pp, int lin, int col);
101
102
/**********************************************************************
103
 * Terminal emulation
104
 */
105
106
static void
107 275904
term_cursor(void *priv, const teken_pos_t *pos)
108
{
109 275904
        (void)priv;
110 275904
        (void)pos;
111 275904
}
112
113
static void
114 1746923
term_putchar(void *priv, const teken_pos_t *pos, teken_char_t ch,
115
    const teken_attr_t *at)
116
{
117
        struct process *pp;
118
119 1746923
        CAST_OBJ_NOTNULL(pp, priv, PROCESS_MAGIC);
120 1746927
        (void)at;
121 1746927
        if (ch > 126 || ch < 32)
122 1124
                ch = '?';
123 1746926
        assert(pos->tp_row < pp->nlin);
124 1746933
        assert(pos->tp_col < pp->ncol);
125 1746931
        pp->vram[pos->tp_row][pos->tp_col] = ch;
126 1746931
}
127
128
static void
129 3571
term_fill(void *priv, const teken_rect_t *r, teken_char_t c,
130
    const teken_attr_t *a)
131
{
132
        teken_pos_t p;
133
134
        /* Braindead implementation of fill() - just call putchar(). */
135 19916
        for (p.tp_row = r->tr_begin.tp_row;
136 19916
            p.tp_row < r->tr_end.tp_row; p.tp_row++)
137 1513702
                for (p.tp_col = r->tr_begin.tp_col;
138 1513702
                    p.tp_col < r->tr_end.tp_col; p.tp_col++)
139 1513702
                        term_putchar(priv, &p, c, a);
140 3571
}
141
142
static void
143 3469
term_copy(void *priv, const teken_rect_t *r, const teken_pos_t *p)
144
{
145
        struct process *pp;
146
        int nrow, ncol, y; /* Has to be signed - >= 0 comparison */
147
148
        /*
149
         * Copying is a little tricky. We must make sure we do it in
150
         * correct order, to make sure we don't overwrite our own data.
151
         */
152 3469
        CAST_OBJ_NOTNULL(pp, priv, PROCESS_MAGIC);
153
154 3469
        nrow = r->tr_end.tp_row - r->tr_begin.tp_row;
155 3469
        ncol = r->tr_end.tp_col - r->tr_begin.tp_col;
156
157 3469
        if (p->tp_row < r->tr_begin.tp_row) {
158
                /* Copy from top to bottom. */
159 48252
                for (y = 0; y < nrow; y++)
160 93802
                        memmove(&pp->vram[p->tp_row + y][p->tp_col],
161 46901
                            &pp->vram[r->tr_begin.tp_row + y][r->tr_begin.tp_col], ncol);
162 1351
        } else {
163
                /* Copy from bottom to top. */
164 11830
                for (y = nrow - 1; y >= 0; y--)
165 19424
                        memmove(&pp->vram[p->tp_row + y][p->tp_col],
166 9712
                            &pp->vram[r->tr_begin.tp_row + y][r->tr_begin.tp_col], ncol);
167
        }
168 3469
}
169
170
static void
171 32
term_respond(void *priv, const void *p, size_t l)
172
{
173
        struct process *pp;
174
        int r;
175
176 32
        CAST_OBJ_NOTNULL(pp, priv, PROCESS_MAGIC);
177
178 32
        vtc_dump(pp->vl, 4, "term_response", p, l);
179 32
        if (pp->ansi_response) {
180 4
                r = write(pp->fd_term, p, l);
181 4
                if (r != l)
182 0
                        vtc_fatal(pp->vl, "Could not write to process: %s",
183 0
                            strerror(errno));
184 4
        }
185 32
}
186
187
static void
188 270
term_param(void *priv, int p, unsigned int v)
189
{
190
        struct process *pp;
191
192 270
        CAST_OBJ_NOTNULL(pp, priv, PROCESS_MAGIC);
193 270
        if (p == TP_132COLS && v)
194 24
                term_resize(pp, pp->nlin, 132);
195 72
        if (p == TP_132COLS && !v)
196 48
                term_resize(pp, pp->nlin, 80);
197 270
}
198
199
static const teken_funcs_t process_teken_func = {
200
        .tf_cursor      =       term_cursor,
201
        .tf_putchar     =       term_putchar,
202
        .tf_fill        =       term_fill,
203
        .tf_copy        =       term_copy,
204
        .tf_respond     =       term_respond,
205
        .tf_param       =       term_param,
206
};
207
208
static void
209 312
term_screen_dump(const struct process *pp)
210
{
211
        int i;
212
        const teken_pos_t *pos;
213
214 8152
        for (i = 0; i < pp->nlin; i++)
215 7840
                vtc_dump(pp->vl, 3, "screen", pp->vram[i], pp->ncol);
216 312
        pos = teken_get_cursor(pp->tek);
217 624
        vtc_log(pp->vl, 3, "Cursor at line %d column %d",
218 312
            pos->tp_row + 1, pos->tp_col + 1);
219 312
}
220
221
static void
222 200
term_resize(struct process *pp, int lin, int col)
223
{
224
        teken_pos_t pos;
225
        char **vram;
226
        int i, j;
227
228 200
        vram = calloc(lin, sizeof *pp->vram);
229 200
        AN(vram);
230 5192
        for (i = 0; i < lin; i++) {
231 4992
                vram[i] = calloc(col + 1L, 1);
232 4992
                AN(vram[i]);
233 4992
                memset(vram[i], ' ', col);
234 4992
                vram[i][col] = '\0';
235 4992
        }
236 200
        if (pp->vram != NULL) {
237 2198
                for (i = 0; i < lin; i++) {
238 2124
                        if (i >= pp->nlin)
239 14
                                break;
240 2110
                        j = col;
241 2110
                        if (j > pp->ncol)
242 768
                                j = pp->ncol;
243 2110
                        memcpy(vram[i], pp->vram[i], j);
244 2110
                }
245 2200
                for (i = 0; i < pp->nlin; i++)
246 2112
                        free(pp->vram[i]);
247 88
                free(pp->vram);
248 88
        }
249 200
        pp->vram = vram;
250 200
        pp->nlin = lin;
251 200
        pp->ncol = col;
252
253 200
        pos.tp_row = lin;
254 200
        pos.tp_col = col;
255 200
        teken_set_winsize(pp->tek, &pos);
256 200
}
257
258
static int
259 9674
term_find_textline(const struct process *pp, int *x, int y, const char *pat)
260
{
261
        const char *t;
262
        int l;
263
264 9674
        if (*x == 0) {
265 7596
                t = strstr(pp->vram[y], pat);
266 7596
                if (t != NULL) {
267 138
                        *x = 1 + (t - pp->vram[y]);
268 138
                        return (1);
269
                }
270 9536
        } else if (*x <= pp->ncol) {
271 2078
                t = pp->vram[y] + *x - 1;
272 2078
                l = strlen(pat);
273 2078
                assert((*x - 1) + (l - 1) < pp->ncol);
274 2078
                if (!memcmp(t, pat, l))
275 364
                        return (1);
276 1714
        }
277 9172
        return (0);
278 9674
}
279
280
static int
281 1014
term_find_text(const struct process *pp, int *x, int *y, const char *pat)
282
{
283
        int yy;
284
285 1014
        if (*y == 0) {
286 9272
                for (yy = 0; yy < pp->nlin; yy++) {
287 9042
                        if (term_find_textline(pp, x, yy, pat)) {
288 152
                                *y = yy + 1;
289 152
                                return (1);
290
                        }
291 8890
                }
292 862
        } else if (*y <= pp->nlin) {
293 632
                if (term_find_textline(pp, x, *y - 1, pat))
294 350
                        return (1);
295 282
        }
296 512
        return (0);
297 1014
}
298
299
static void
300 502
term_expect_text(struct process *pp,
301
    const char *lin, const char *col, const char *pat)
302
{
303 502
        int x, y, l, d = 10000;
304
        char *t;
305
306 502
        y = strtoul(lin, NULL, 0);
307 502
        if (y < 0 || y > pp->nlin)
308 0
                vtc_fatal(pp->vl, "YYY %d nlin %d", y, pp->nlin);
309 502
        x = strtoul(col, NULL, 0);
310 510
        for(l = 0; l <= 10 && x > pp->ncol; l++)        // wait for screen change
311 8
                usleep(100000);
312 502
        if (x < 0 || x > pp->ncol)
313 0
                vtc_fatal(pp->vl, "XXX %d ncol %d", x, pp->ncol);
314 502
        l = strlen(pat);
315 502
        if (x + l - 1 > pp->ncol)
316 0
                vtc_fatal(pp->vl, "XXX %d ncol %d", x + l - 1, pp->ncol);
317 502
        PTOK(pthread_mutex_lock(&pp->mtx));
318 1014
        while (!term_find_text(pp, &x, &y, pat)) {
319 512
                if (x != 0 && y != 0) {
320 244
                        t = pp->vram[y - 1] + x - 1;
321 488
                        vtc_log(pp->vl, 4,
322 244
                            "text at %d,%d: '%.*s'", y, x, l, t);
323 244
                }
324 512
                PTOK(pthread_mutex_unlock(&pp->mtx));
325 512
                usleep(d);
326 512
                PTOK(pthread_mutex_lock(&pp->mtx));
327 512
                if (d < 3000000)
328 512
                        d += d;
329
        }
330 502
        PTOK(pthread_mutex_unlock(&pp->mtx));
331 502
        vtc_log(pp->vl, 4, "found expected text at %d,%d: '%s'", y, x, pat);
332 502
}
333
334
static void
335 16
term_expect_cursor(const struct process *pp, const char *lin, const char *col)
336
{
337
        int x, y, l;
338
        const teken_pos_t *pos;
339
340 16
        pos = teken_get_cursor(pp->tek);
341 16
        y = strtoul(lin, NULL, 0);
342 16
        if (y < 0 || y > pp->nlin)
343 0
                vtc_fatal(pp->vl, "YYY %d nlin %d", y, pp->nlin);
344 16
        x = strtoul(col, NULL, 0);
345 16
        for(l = 0; l < 10 && x > pp->ncol; l++) // wait for screen change
346 0
                usleep(100000);
347 16
        if (x < 0 || x > pp->ncol)
348 0
                vtc_fatal(pp->vl, "XXX %d ncol %d", x, pp->ncol);
349 16
        if (y != 0 && (y-1) != pos->tp_row)
350 0
                vtc_fatal(pp->vl, "Cursor on line %d (expected %d)",
351 0
                    pos->tp_row + 1, y);
352 12
        if (x != 0 && (x-1) != pos->tp_col)
353 0
                vtc_fatal(pp->vl, "Cursor in column %d (expected %d)",
354 0
                    pos->tp_col + 1, y);
355 16
}
356
357
static void
358 68
term_match_text(struct process *pp,
359
    const char *lin, const char *col, const char *re)
360
{
361
        int i, l, err, erroff;
362
        struct vsb *vsb, re_vsb[1];
363
        size_t len;
364
        ssize_t x, y;
365
        vre_t *vre;
366
        char errbuf[VRE_ERROR_LEN];
367
368 68
        vsb = VSB_new_auto();
369 68
        AN(vsb);
370
371 68
        y = strtoul(lin, NULL, 0);
372 68
        if (y < 0 || y > pp->nlin)
373 0
                vtc_fatal(pp->vl, "YYY %zd nlin %d", y, pp->nlin);
374 68
        x = strtoul(col, NULL, 0);
375 68
        for(l = 0; l < 10 && x > pp->ncol; l++) // wait for screen change
376 0
                usleep(100000);
377 68
        if (x < 0 || x > pp->ncol)
378 0
                vtc_fatal(pp->vl, "XXX %zd ncol %d", x, pp->ncol);
379
380 68
        if (x)
381 32
                x--;
382
383 36
        if (y)
384 36
                y--;
385
386 68
        vre = VRE_compile(re, 0, &err, &erroff, 1);
387 68
        if (vre == NULL) {
388 0
                AN(VSB_init(re_vsb, errbuf, sizeof errbuf));
389 0
                AZ(VRE_error(re_vsb, err));
390 0
                AZ(VSB_finish(re_vsb));
391 0
                VSB_fini(re_vsb);
392 0
                vtc_fatal(pp->vl, "invalid regexp \"%s\" at %d (%s)",
393 0
                    re, erroff, errbuf);
394
        }
395
396 68
        PTOK(pthread_mutex_lock(&pp->mtx));
397
398 68
        len = (pp->nlin - y) * (pp->ncol - x);
399 1648
        for (i = y; i < pp->nlin; i++) {
400 1580
                VSB_bcat(vsb, &pp->vram[i][x], pp->ncol - x);
401 1580
                VSB_putc(vsb, '\n');
402 1580
        }
403
404 68
        AZ(VSB_finish(vsb));
405
406 68
        if (VRE_match(vre, VSB_data(vsb), len, 0, NULL) < 1)
407 0
                vtc_fatal(pp->vl, "match failed: (\"%s\")", re);
408
        else
409 68
                vtc_log(pp->vl, 4, "match succeeded");
410
411 68
        PTOK(pthread_mutex_unlock(&pp->mtx));
412 68
        VSB_destroy(&vsb);
413 68
        VRE_free(&vre);
414 68
}
415
416
/**********************************************************************
417
 * Allocate and initialize a process
418
 */
419
420
#define PROCESS_EXPAND(field, format, ...)                              \
421
        do {                                                            \
422
                vsb = macro_expandf(p->vl, format, __VA_ARGS__);        \
423
                AN(vsb);                                                \
424
                p->field = strdup(VSB_data(vsb));                       \
425
                AN(p->field);                                           \
426
                VSB_destroy(&vsb);                                      \
427
        } while (0)
428
429
static void
430 112
process_coverage(struct process *p)
431
{
432
        const teken_attr_t *a;
433
        teken_pos_t pos;
434
        int fg, bg;
435
436
        // Code-Coverage of Teken
437
438 112
        (void)teken_get_sequence(p->tek, TKEY_UP);
439 112
        (void)teken_get_sequence(p->tek, TKEY_F1);
440 112
        (void)teken_256to8(0);
441 112
        (void)teken_256to16(0);
442 112
        a = teken_get_defattr(p->tek);
443 112
        teken_set_defattr(p->tek, a);
444 112
        a = teken_get_curattr(p->tek);
445 112
        teken_set_curattr(p->tek, a);
446 112
        (void)teken_get_winsize(p->tek);
447 112
        pos.tp_row = 0;
448 112
        pos.tp_col = 8;
449 112
        teken_set_cursor(p->tek, &pos);
450 112
        teken_get_defattr_cons25(p->tek, &fg, &bg);
451 112
}
452
453
static struct process *
454 112
process_new(const char *name)
455
{
456
        struct process *p;
457
        struct vsb *vsb;
458
        char buf[1024];
459
460 112
        ALLOC_OBJ(p, PROCESS_MAGIC);
461 112
        AN(p);
462 112
        REPLACE(p->name, name);
463 112
        PTOK(pthread_mutex_init(&p->mtx, NULL));
464
465 112
        p->vl = vtc_logopen("%s", name);
466 112
        AN(p->vl);
467
468 112
        PROCESS_EXPAND(dir, "${tmpdir}/%s", name);
469 112
        PROCESS_EXPAND(out, "${tmpdir}/%s/term", name);
470 112
        PROCESS_EXPAND(err, "${tmpdir}/%s/stderr", name);
471
472 112
        bprintf(buf, "rm -rf %s ; mkdir -p %s ; touch %s %s",
473
            p->dir, p->dir, p->out, p->err);
474 112
        AZ(system(buf));
475
476 112
        p->fd_term = -1;
477
478 112
        VTAILQ_INSERT_TAIL(&processes, p, list);
479 112
        teken_init(p->tek, &process_teken_func, p);
480 112
        term_resize(p, 24, 80);
481 112
        process_coverage(p);
482 112
        return (p);
483
}
484
485
#undef PROCESS_EXPAND
486
487
/**********************************************************************
488
 * Clean up process
489
 */
490
491
static void
492 112
process_delete(struct process *p)
493
{
494
        int i;
495
496 112
        CHECK_OBJ_NOTNULL(p, PROCESS_MAGIC);
497 112
        PTOK(pthread_mutex_destroy(&p->mtx));
498 112
        vtc_logclose(p->vl);
499 112
        free(p->name);
500 112
        free(p->dir);
501 112
        free(p->out);
502 112
        free(p->err);
503
504 2992
        for (i = 0; i < p->nlin; i++)
505 2880
                free(p->vram[i]);
506 112
        free(p->vram);
507
508
        /*
509
         * We do not delete the directory, it may contain useful stdout
510
         * and stderr files. They will be deleted on account of belonging
511
         * to the test's tmpdir.
512
         */
513
514
        /* XXX: MEMLEAK (?) */
515 112
        FREE_OBJ(p);
516 112
}
517
518
static void
519 112
process_undef(const struct process *p)
520
{
521 112
        CHECK_OBJ_NOTNULL(p, PROCESS_MAGIC);
522
523 112
        macro_undef(p->vl, p->name, "dir");
524 112
        macro_undef(p->vl, p->name, "out");
525 112
        macro_undef(p->vl, p->name, "err");
526 112
}
527
528
/**********************************************************************
529
 * Data stream handling
530
 */
531
532
static int
533 374
process_vlu_func(void *priv, const char *l)
534
{
535
        struct process *p;
536
537 374
        CAST_OBJ_NOTNULL(p, priv, PROCESS_MAGIC);
538 374
        vtc_dump(p->vl, 4, "output", l, -1);
539 374
        return (0);
540
}
541
542
static int v_matchproto_(vev_cb_f)
543 8766
process_stdout(const struct vev *ev, int what)
544
{
545
        struct process *p;
546
        char buf[BUFSIZ];
547
        int i;
548
549 8766
        CAST_OBJ_NOTNULL(p, ev->priv, PROCESS_MAGIC);
550 8766
        (void)what;
551 8766
        i = read(p->fd_term, buf, sizeof buf);
552 8766
        if (i <= 0) {
553 136
                vtc_log(p->vl, 4, "stdout read %d", i);
554 136
                return (1);
555
        }
556 8630
        PTOK(pthread_mutex_lock(&p->mtx));
557 8630
        p->stdout_bytes += i;
558 8630
        PTOK(pthread_mutex_unlock(&p->mtx));
559 8630
        if (p->log == 1)
560 109
                (void)VLU_Feed(p->vlu_stdout, buf, i);
561 8521
        else if (p->log == 2)
562 442
                vtc_dump(p->vl, 4, "stdout", buf, i);
563 8079
        else if (p->log == 3)
564 98
                vtc_hexdump(p->vl, 4, "stdout", buf, i);
565 8630
        assert(write(p->f_stdout, buf, i) == i);
566 8630
        PTOK(pthread_mutex_lock(&p->mtx));
567 8629
        teken_input(p->tek, buf, i);
568 8629
        PTOK(pthread_mutex_unlock(&p->mtx));
569 8630
        return (0);
570 8766
}
571
572
static int v_matchproto_(vev_cb_f)
573 170
process_stderr(const struct vev *ev, int what)
574
{
575
        struct process *p;
576
        char buf[BUFSIZ];
577
        int i;
578
579 170
        CAST_OBJ_NOTNULL(p, ev->priv, PROCESS_MAGIC);
580 170
        (void)what;
581 170
        i = read(p->fd_stderr, buf, sizeof buf);
582 170
        if (i <= 0) {
583 136
                vtc_log(p->vl, 4, "stderr read %d", i);
584 136
                return (1);
585
        }
586 34
        PTOK(pthread_mutex_lock(&p->mtx));
587 34
        p->stderr_bytes += i;
588 34
        PTOK(pthread_mutex_unlock(&p->mtx));
589 34
        vtc_dump(p->vl, 4, "stderr", buf, i);
590 34
        assert(write(p->f_stderr, buf, i) == i);
591 34
        return (0);
592 170
}
593
594
static void
595 0
process_cleanup(void *priv)
596
{
597 0
        struct vev_root *evb = priv;
598 0
        VEV_Destroy(&evb);
599 0
}
600
601
static void *
602 136
process_thread(void *priv)
603
{
604
        struct process *p;
605
        struct vev_root *evb;
606
        struct vev *ev;
607
        int r;
608
609 136
        CAST_OBJ_NOTNULL(p, priv, PROCESS_MAGIC);
610
611 136
        p->f_stdout = open(p->out, O_WRONLY|O_APPEND);
612 136
        assert(p->f_stdout >= 0);
613 136
        p->f_stderr = open(p->err, O_WRONLY|O_APPEND);
614 136
        assert(p->f_stderr >= 0);
615
616 136
        evb = VEV_New();
617 136
        AN(evb);
618 136
        pthread_cleanup_push(process_cleanup, evb);
619
620 136
        ev = VEV_Alloc();
621 136
        AN(ev);
622 136
        ev->fd = p->fd_term;
623 136
        ev->fd_flags = VEV__RD | VEV__HUP | VEV__ERR;
624 136
        ev->callback = process_stdout;
625 136
        ev->priv = p;
626 136
        AZ(VEV_Start(evb, ev));
627
628 136
        ev = VEV_Alloc();
629 136
        AN(ev);
630 136
        ev->fd = p->fd_stderr;
631 136
        ev->fd_flags = VEV__RD | VEV__HUP | VEV__ERR;
632 136
        ev->callback = process_stderr;
633 136
        ev->priv = p;
634 136
        AZ(VEV_Start(evb, ev));
635
636 136
        if (p->log == 1) {
637 18
                p->vlu_stdout = VLU_New(process_vlu_func, p, 1024);
638 18
                AN(p->vlu_stdout);
639 18
                p->vlu_stderr = VLU_New(process_vlu_func, p, 1024);
640 18
                AN(p->vlu_stderr);
641 18
        }
642
643 136
        do {
644 136
                r = VEV_Once(evb);
645 136
        } while (r == 1);
646
647 136
        if (r < 0)
648 0
                vtc_fatal(p->vl, "VEV_Once() = %d, error %s", r,
649 0
                    strerror(errno));
650
651 272
        vtc_wait4(p->vl, p->pid,
652 136
            p->expect_exit, p->expect_signal, p->allow_core);
653 136
        closefd(&p->f_stdout);
654 136
        closefd(&p->f_stderr);
655
656 136
        PTOK(pthread_mutex_lock(&p->mtx));
657
658
        /* NB: We keep the other macros around */
659 136
        macro_undef(p->vl, p->name, "pid");
660 136
        p->pid = -1;
661
662 136
        PTOK(pthread_mutex_unlock(&p->mtx));
663
664 136
        pthread_cleanup_pop(0);
665 136
        VEV_Destroy(&evb);
666 136
        if (p->log == 1) {
667 18
                VLU_Destroy(&p->vlu_stdout);
668 18
                VLU_Destroy(&p->vlu_stderr);
669 18
        }
670 136
        return (NULL);
671
}
672
673
static void
674 152
process_winsz(struct process *p, int fd)
675
{
676
        struct winsize ws;
677
        int i;
678
679 152
        memset(&ws, 0, sizeof ws);
680 152
        ws.ws_row = (short)p->nlin;
681 152
        ws.ws_col = (short)p->ncol;
682 152
        i = ioctl(fd, TIOCSWINSZ, &ws);
683 152
        if (i)
684 6
                vtc_log(p->vl, 4, "TIOCWINSZ %d %s", i, strerror(errno));
685 152
}
686
687
static void
688 136
process_init_term(struct process *p, int fd)
689
{
690
        struct termios tt;
691
        int i;
692
693 136
        process_winsz(p, fd);
694
695 136
        memset(&tt, 0, sizeof tt);
696 136
        tt.c_cflag = CREAD | CS8 | HUPCL;
697 136
        tt.c_iflag = BRKINT | ICRNL | IMAXBEL | IXON | IXANY;
698 136
        tt.c_lflag = ICANON | ISIG | IEXTEN | ECHO | ECHOE | ECHOKE | ECHOCTL;
699 136
        tt.c_oflag = OPOST | ONLCR;
700 136
        i = cfsetispeed(&tt, B9600);
701 136
        if (i)
702 0
                vtc_log(p->vl, 4, "cfsetispeed %d %s", i, strerror(errno));
703 0
        i = cfsetospeed(&tt, B9600);
704 0
        if (i)
705 0
                vtc_log(p->vl, 4, "cfsetospeed %d %s", i, strerror(errno));
706 0
        tt.c_cc[VEOF] = '\x04';                 // CTRL-D
707 0
        tt.c_cc[VERASE] = '\x08';               // CTRL-H (Backspace)
708 0
        tt.c_cc[VKILL] = '\x15';                // CTRL-U
709 0
        tt.c_cc[VINTR] = '\x03';                // CTRL-C
710 0
        tt.c_cc[VQUIT] = '\x1c';                // CTRL-backslash
711
712 0
        i = tcsetattr(fd, TCSAFLUSH, &tt);
713 0
        if (i)
714 0
                vtc_log(p->vl, 4, "TCSAFLUSH %d %s", i, strerror(errno));
715 136
}
716
717
/**********************************************************************
718
 * Start the process thread
719
 */
720
721
static void
722 136
process_start(struct process *p)
723
{
724
        struct vsb *cl;
725
        int fd2[2];
726
        int master, slave;
727
        const char *slavename;
728
        char c;
729
730 136
        CHECK_OBJ_NOTNULL(p, PROCESS_MAGIC);
731 136
        if (p->hasthread)
732 0
                vtc_fatal(p->vl, "Already running, -wait first");
733
734 136
        vtc_log(p->vl, 4, "CMD: %s", p->spec);
735
736 136
        cl = macro_expand(p->vl, p->spec);
737 136
        AN(cl);
738
739 136
        master = posix_openpt(O_RDWR|O_NOCTTY);
740 136
        assert(master >= 0);
741 136
        AZ(grantpt(master));
742 136
        AZ(unlockpt(master));
743 136
        slavename = ptsname(master);
744 136
        AN(slavename);
745
746 136
        AZ(pipe(fd2));
747
748 136
        p->pid = fork();
749 136
        assert(p->pid >= 0);
750 272
        if (p->pid == 0) {
751 136
                assert(setsid() == getpid());
752 136
                assert(dup2(fd2[1], STDERR_FILENO) == STDERR_FILENO);
753 136
                AZ(close(STDIN_FILENO));
754 136
                slave = open(slavename, O_RDWR);
755 136
                assert(slave == STDIN_FILENO);
756
#ifdef __sun
757
                if (ioctl(slave, I_PUSH, "ptem"))
758
                        vtc_log(p->vl, 4, "PUSH ptem: %s", strerror(errno));
759
                if (ioctl(slave, I_PUSH, "ldterm"))
760
                        vtc_log(p->vl, 4, "PUSH ldterm: %s", strerror(errno));
761
                (void)ioctl(STDIN_FILENO, TIOCSCTTY, NULL);
762
#else
763 136
                AZ(ioctl(STDIN_FILENO, TIOCSCTTY, NULL));
764
#endif
765 136
                AZ(close(STDOUT_FILENO));
766 136
                assert(dup2(slave, STDOUT_FILENO) == STDOUT_FILENO);
767 136
                VSUB_closefrom(STDERR_FILENO + 1);
768 136
                process_init_term(p, slave);
769
770 136
                AZ(setenv("TERM", "xterm", 1));
771 136
                AZ(unsetenv("TERMCAP"));
772
                // Not using NULL because GCC is now even more demented...
773 136
                assert(write(STDERR_FILENO, "+", 1) == 1);
774 136
                AZ(execl("/bin/sh", "/bin/sh", "-c", VSB_data(cl), (char*)0));
775 0
                exit(1);
776
        }
777 136
        vtc_log(p->vl, 3, "PID: %ld", (long)p->pid);
778 136
        VSB_destroy(&cl);
779
780 136
        assert(read(fd2[0], &c, 1) == 1);
781 136
        p->fd_term = master;
782 136
        closefd(&fd2[1]);
783 136
        p->fd_stderr = fd2[0];
784 136
        macro_def(p->vl, p->name, "pid", "%ld", (long)p->pid);
785 136
        macro_def(p->vl, p->name, "dir", "%s", p->dir);
786 136
        macro_def(p->vl, p->name, "out", "%s", p->out);
787 136
        macro_def(p->vl, p->name, "err", "%s", p->err);
788 136
        p->hasthread = 1;
789 136
        PTOK(pthread_create(&p->tp, NULL, process_thread, p));
790 136
}
791
792
/**********************************************************************
793
 * Wait for process thread to stop
794
 */
795
796
static void
797 136
process_wait(struct process *p)
798
{
799
        void *v;
800
801 136
        if (p->hasthread) {
802 136
                PTOK(pthread_join(p->tp, &v));
803 136
                p->hasthread = 0;
804 136
        }
805 272
        vtc_log(p->vl, 4, "stdout %ju bytes, stderr %ju bytes",
806 136
            p->stdout_bytes, p->stderr_bytes);
807 136
}
808
809
/**********************************************************************
810
 * Send a signal to a process
811
 */
812
813
static void
814 54
process_kill(struct process *p, const char *sig)
815
{
816 54
        int j = 0;
817
        pid_t pid;
818
819 54
        CHECK_OBJ_NOTNULL(p, PROCESS_MAGIC);
820 54
        AN(sig);
821
822 54
        PTOK(pthread_mutex_lock(&p->mtx));
823 54
        pid = p->pid;
824 54
        PTOK(pthread_mutex_unlock(&p->mtx));
825
826 54
        if (pid <= 0)
827 0
                vtc_fatal(p->vl, "Cannot signal a non-running process");
828
829 54
        if (!strcmp(sig, "TERM"))
830 42
                j = SIGTERM;
831 12
        else if (!strcmp(sig, "INT"))
832 2
                j = SIGINT;
833 10
        else if (!strcmp(sig, "KILL"))
834 4
                j = SIGKILL;
835 6
        else if (!strcmp(sig, "HUP"))
836 6
                j = SIGHUP;
837 0
        else if (*sig == '-')
838 0
                j = strtoul(sig + 1, NULL, 10);
839
        else
840 0
                vtc_fatal(p->vl, "Could not grok signal (%s)", sig);
841
842 54
        if (p->expect_signal == 0)
843 52
                p->expect_signal = -j;
844 54
        if (kill(-pid, j) < 0)
845 0
                vtc_fatal(p->vl, "Failed to send signal %d (%s)",
846 0
                    j, strerror(errno));
847
        else
848 54
                vtc_log(p->vl, 4, "Sent signal %d", j);
849 54
}
850
851
/**********************************************************************
852
 * Write to a process' stdin
853
 */
854
855
static void
856 262
process_write(const struct process *p, const char *text)
857
{
858
        int r, len;
859
860 262
        if (!p->hasthread)
861 0
                vtc_fatal(p->vl, "Cannot write to a non-running process");
862
863 262
        len = strlen(text);
864 262
        vtc_log(p->vl, 4, "Writing %d bytes", len);
865 262
        r = write(p->fd_term, text, len);
866 262
        if (r != len)
867 0
                vtc_fatal(p->vl, "Failed to write: len=%d %s (%d)",
868 0
                    len, strerror(errno), errno);
869 262
}
870
871
static void
872 250
process_write_hex(const struct process *p, const char *text)
873
{
874
        struct vsb *vsb;
875
876 250
        if (!p->hasthread)
877 0
                vtc_fatal(p->vl, "Cannot write to a non-running process");
878
879 250
        vsb = vtc_hex_to_bin(p->vl, text);
880 250
        assert(VSB_len(vsb) >= 0);
881 250
        vtc_hexdump(p->vl, 4, "sendhex", VSB_data(vsb), VSB_len(vsb));
882 250
        AZ(VSB_tofile(vsb, p->fd_term));
883 250
        VSB_destroy(&vsb);
884 250
}
885
886
static void
887 0
process_close(struct process *p)
888
{
889
890 0
        if (!p->hasthread)
891 0
                vtc_fatal(p->vl, "Cannot close a non-running process");
892
893 0
        process_kill(p, "HUP");
894 0
}
895
896
/* SECTION: process process
897
 *
898
 * Run a process with stdin+stdout on a pseudo-terminal and stderr on a pipe.
899
 *
900
 * Output from the pseudo-terminal is copied verbatim to ${pNAME_out},
901
 * and the -log/-dump/-hexdump flags will also put it in the vtc-log.
902
 *
903
 * The pseudo-terminal is not in ECHO mode, but if the programs run set
904
 * it to ECHO mode ("stty sane") any input sent to the process will also
905
 * appear in this stream because of the ECHO.
906
 *
907
 * Output from the stderr-pipe is copied verbatim to ${pNAME_err}, and
908
 * is always included in the vtc_log.
909
 *
910
 *      process pNAME SPEC [-allow-core] [-expect-exit N] [-expect-signal N]
911
 *              [-dump] [-hexdump] [-log]
912
 *              [-run] [-close] [-kill SIGNAL] [-start] [-stop] [-wait]
913
 *              [-write STRING] [-writeln STRING] [-writehex HEXSTRING]
914
 *              [-need-bytes [+]NUMBER]
915
 *              [-screen-dump] [-winsz LINES COLUMNS] [-ansi-response]
916
 *              [-expect-cursor LINE COLUMN] [-expect-text LINE COLUMN TEXT]
917
 *              [-match-text LINE COLUMN REGEXP]
918
 *
919
 * pNAME
920
 *      Name of the process. It must start with 'p'.
921
 *
922
 * SPEC
923
 *      The command(s) to run in this process.
924
 *
925
 * \-hexdump
926
 *      Log output with vtc_hexdump(). Must be before -start/-run.
927
 *
928
 * \-dump
929
 *      Log output with vtc_dump(). Must be before -start/-run.
930
 *
931
 * \-log
932
 *      Log output with VLU/vtc_log(). Must be before -start/-run.
933
 *
934
 * \-start
935
 *      Start the process.
936
 *
937
 * \-expect-exit N
938
 *      Expect exit status N
939
 *
940
 * \-expect-signal N
941
 *      Expect signal in exit status N
942
 *
943
 * \-allow-core
944
 *      Core dump in exit status is OK
945
 *
946
 * \-wait
947
 *      Wait for the process to finish.
948
 *
949
 * \-run
950
 *      Shorthand for -start -wait.
951
 *
952
 *      In most cases, if you just want to start a process and wait for it
953
 *      to finish, you can use the ``shell`` command instead.
954
 *      The following commands are equivalent::
955
 *
956
 *          shell "do --something"
957
 *
958
 *          process p1 "do --something" -run
959
 *
960
 *      However, you may use the ``process`` variant to conveniently
961
 *      collect the standard input and output without dealing with shell
962
 *      redirections yourself. The ``shell`` command can also expect an
963
 *      expression from either output, consider using it if you only need
964
 *      to match one.
965
 *
966
 * \-key KEYSYM
967
 *      Send emulated key-press.
968
 *      KEYSYM can be one of (NPAGE, PPAGE, HOME, END)
969
 *
970
 *
971
 * \-kill SIGNAL
972
 *      Send a signal to the process. The argument can be either
973
 *      the string "TERM", "INT", or "KILL" for SIGTERM, SIGINT or SIGKILL
974
 *      signals, respectively, or a hyphen (-) followed by the signal
975
 *      number.
976
 *
977
 *      If you need to use other signal names, you can use the ``kill``\(1)
978
 *      command directly::
979
 *
980
 *          shell "kill -USR1 ${pNAME_pid}"
981
 *
982
 *      Note that SIGHUP usage is discouraged in test cases.
983
 *
984
 * \-stop
985
 *      Shorthand for -kill TERM.
986
 *
987
 * \-close
988
 *      Alias for "-kill HUP"
989
 *
990
 * \-winsz LINES COLUMNS
991
 *      Change the terminal window size to LIN lines and COL columns.
992
 *
993
 * \-write STRING
994
 *      Write a string to the process' stdin.
995
 *
996
 * \-writeln STRING
997
 *      Same as -write followed by a newline (\\n).
998
 *
999
 * \-writehex HEXSTRING
1000
 *      Same as -write but interpreted as hexadecimal bytes.
1001
 *
1002
 * \-need-bytes [+]NUMBER
1003
 *      Wait until at least NUMBER bytes have been received in total.
1004
 *      If '+' is prefixed, NUMBER new bytes must be received.
1005
 *
1006
 * \-ansi-response
1007
 *      Respond to terminal respond-back sequences
1008
 *
1009
 * \-expect-cursor LINE COLUMN
1010
 *      Expect cursors location
1011
 *
1012
 * \-expect-text LINE COLUMNS TEXT
1013
 *      Wait for TEXT to appear at LIN,COL on the virtual screen.
1014
 *      Lines and columns are numbered 1...N
1015
 *      LIN==0 means "on any line"
1016
 *      COL==0 means "anywhere on the line"
1017
 *
1018
 * \-match-text LINE COLUMN REGEXP
1019
 *      Wait for the PAT regular expression to match the text at LIN,COL on the virtual screen.
1020
 *      Lines and columns are numbered 1...N
1021
 *      LIN==0 means "on any line"
1022
 *      COL==0 means "anywhere on the line"
1023
 *
1024
 *
1025
 * \-screen-dump
1026
 *      Dump the virtual screen into vtc_log
1027
 *
1028
 */
1029
1030
void
1031 3570
cmd_process(CMD_ARGS)
1032
{
1033
        struct process *p, *p2;
1034
        uintmax_t u, v, bsnap;
1035
        unsigned lin,col;
1036 3570
        int spec_set = 0;
1037
1038 3570
        (void)priv;
1039
1040 3570
        if (av == NULL) {
1041
                /* Reset and free */
1042 2126
                VTAILQ_FOREACH_SAFE(p, &processes, list, p2) {
1043 112
                        if (p->pid > 0) {
1044 10
                                process_kill(p, "TERM");
1045 10
                                sleep(1);
1046 10
                                if (p->pid > 0)
1047 0
                                        process_kill(p, "KILL");
1048 10
                        }
1049 18
                        if (p->hasthread)
1050 18
                                process_wait(p);
1051 112
                        VTAILQ_REMOVE(&processes, p, list);
1052 112
                        process_undef(p);
1053 112
                        process_delete(p);
1054 112
                }
1055 2014
                return;
1056
        }
1057
1058 1556
        AZ(strcmp(av[0], "process"));
1059 1556
        av++;
1060
1061 1556
        VTC_CHECK_NAME(vl, av[0], "Process", 'p');
1062 3620
        VTAILQ_FOREACH(p, &processes, list)
1063 3508
                if (!strcmp(p->name, av[0]))
1064 1444
                        break;
1065 2776
        if (p == NULL)
1066 112
                p = process_new(av[0]);
1067 1556
        av++;
1068
1069 1556
        PTOK(pthread_mutex_lock(&p->mtx));
1070 1556
        bsnap = p->stdout_bytes;
1071 1556
        PTOK(pthread_mutex_unlock(&p->mtx));
1072
1073 3512
        for (; *av != NULL; av++) {
1074 1956
                if (vtc_error)
1075 0
                        break;
1076
1077 1956
                if (!strcmp(*av, "-allow-core")) {
1078 0
                        p->allow_core = 1;
1079 0
                        continue;
1080
                }
1081 1956
                if (!strcmp(*av, "-close")) {
1082 0
                        process_close(p);
1083 0
                        continue;
1084
                }
1085 1956
                if (!strcmp(*av, "-dump")) {
1086 38
                        if (p->hasthread)
1087 0
                                vtc_fatal(p->vl,
1088
                                    "Cannot dump a running process");
1089 38
                        p->log = 2;
1090 38
                        continue;
1091
                }
1092 1918
                if (!strcmp(*av, "-expect-exit")) {
1093 32
                        p->expect_exit = strtoul(av[1], NULL, 0);
1094 32
                        av++;
1095 32
                        continue;
1096
                }
1097 1886
                if (!strcmp(*av, "-expect-signal")) {
1098 0
                        p->expect_signal = strtoul(av[1], NULL, 0);
1099 0
                        av++;
1100 0
                        continue;
1101
                }
1102 1886
                if (!strcmp(*av, "-hexdump")) {
1103 8
                        if (p->hasthread)
1104 0
                                vtc_fatal(p->vl,
1105
                                    "Cannot dump a running process");
1106 8
                        p->log = 3;
1107 8
                        continue;
1108
                }
1109 1878
                if (!strcmp(*av, "-key")) {
1110 8
                        if (!strcmp(av[1], "NPAGE"))
1111 2
                                process_write(p, "\x1b\x5b\x36\x7e");
1112 6
                        else if (!strcmp(av[1], "PPAGE"))
1113 2
                                process_write(p, "\x1b\x5b\x35\x7e");
1114 4
                        else if (!strcmp(av[1], "HOME"))
1115 2
                                process_write(p, "\x1b\x4f\x48");
1116 2
                        else if (!strcmp(av[1], "END"))
1117 2
                                process_write(p, "\x1b\x4f\x46");
1118
                        else
1119 0
                                vtc_fatal(p->vl, "Unknown key %s", av[1]);
1120 8
                        continue;
1121
                }
1122 1870
                if (!strcmp(*av, "-kill")) {
1123 24
                        process_kill(p, av[1]);
1124 24
                        av++;
1125 24
                        continue;
1126
                }
1127 1846
                if (!strcmp(*av, "-log")) {
1128 18
                        if (p->hasthread)
1129 0
                                vtc_fatal(p->vl,
1130
                                    "Cannot log a running process");
1131 18
                        p->log = 1;
1132 18
                        continue;
1133
                }
1134 1828
                if (!strcmp(*av, "-need-bytes")) {
1135 18
                        u = strtoumax(av[1], NULL, 0);
1136 18
                        if (av[1][0] == '+')
1137 14
                                u += bsnap;
1138 18
                        av++;
1139 18
                        do {
1140 18
                                PTOK(pthread_mutex_lock(&p->mtx));
1141 37
                                v = p->stdout_bytes;
1142 37
                                PTOK(pthread_mutex_unlock(&p->mtx));
1143 37
                                vtc_log(p->vl, 4, "Have %ju bytes", v);
1144 37
                                usleep(500000);
1145 37
                        } while(v < u);
1146 18
                        continue;
1147
                }
1148 1810
                if (!strcmp(*av, "-run")) {
1149 38
                        process_start(p);
1150 38
                        process_wait(p);
1151 38
                        continue;
1152
                }
1153 1772
                if (!strcmp(*av, "-ansi-response")) {
1154 4
                        p->ansi_response = 1;
1155 4
                        continue;
1156
                }
1157 1768
                if (!strcmp(*av, "-expect-text")) {
1158 502
                        AN(av[1]);
1159 502
                        AN(av[2]);
1160 502
                        AN(av[3]);
1161 502
                        term_expect_text(p, av[1], av[2], av[3]);
1162 502
                        av += 3;
1163 502
                        continue;
1164
                }
1165 1266
                if (!strcmp(*av, "-expect-cursor")) {
1166 16
                        AN(av[1]);
1167 16
                        AN(av[2]);
1168 16
                        term_expect_cursor(p, av[1], av[2]);
1169 16
                        av += 2;
1170 16
                        continue;
1171
                }
1172 1250
                if (!strcmp(*av, "-match-text")) {
1173 68
                        AN(av[1]);
1174 68
                        AN(av[2]);
1175 68
                        AN(av[3]);
1176 68
                        term_match_text(p, av[1], av[2], av[3]);
1177 68
                        av += 3;
1178 68
                        continue;
1179
                }
1180 1182
                if (!strcmp(*av, "-screen_dump") ||
1181 892
                    !strcmp(*av, "-screen-dump")) {
1182 312
                        term_screen_dump(p);
1183 312
                        continue;
1184
                }
1185 870
                if (!strcmp(*av, "-start")) {
1186 98
                        process_start(p);
1187 98
                        continue;
1188
                }
1189 772
                if (!strcmp(*av, "-stop")) {
1190 20
                        process_kill(p, "TERM");
1191 20
                        sleep(1);
1192 20
                        continue;
1193
                }
1194 752
                if (!strcmp(*av, "-wait")) {
1195 80
                        process_wait(p);
1196 80
                        continue;
1197
                }
1198 672
                if (!strcmp(*av, "-winsz")) {
1199 16
                        lin = atoi(av[1]);
1200 16
                        assert(lin > 1);
1201 16
                        col = atoi(av[2]);
1202 16
                        assert(col > 1);
1203 16
                        av += 2;
1204 16
                        PTOK(pthread_mutex_lock(&p->mtx));
1205 16
                        term_resize(p, lin, col);
1206 16
                        PTOK(pthread_mutex_unlock(&p->mtx));
1207 16
                        process_winsz(p, p->fd_term);
1208 16
                        continue;
1209
                }
1210 656
                if (!strcmp(*av, "-write")) {
1211 222
                        process_write(p, av[1]);
1212 222
                        av++;
1213 222
                        continue;
1214
                }
1215 434
                if (!strcmp(*av, "-writehex")) {
1216 250
                        process_write_hex(p, av[1]);
1217 250
                        av++;
1218 250
                        continue;
1219
                }
1220 184
                if (!strcmp(*av, "-writeln")) {
1221 16
                        process_write(p, av[1]);
1222 16
                        process_write(p, "\n");
1223 16
                        av++;
1224 16
                        continue;
1225
                }
1226 168
                if (**av == '-' || spec_set)
1227 0
                        vtc_fatal(p->vl, "Unknown process argument: %s", *av);
1228 168
                REPLACE(p->spec, *av);
1229 168
                spec_set = 1;
1230 168
        }
1231 3570
}