vinyl-cache/bin/vinyltest/vtest2/src/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 "vsub.h"
56
#include "vtim.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 697039
term_cursor(void *priv, const teken_pos_t *pos)
108
{
109 697039
        (void)priv;
110 697039
        (void)pos;
111 697039
}
112
113
static void
114 5027410
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 5027410
        CAST_OBJ_NOTNULL(pp, priv, PROCESS_MAGIC);
120 5027418
        (void)at;
121 5027418
        if (ch > 126 || ch < 32)
122 2810
                ch = '?';
123 5027418
        assert(pos->tp_row < pp->nlin);
124 5027418
        assert(pos->tp_col < pp->ncol);
125 5027420
        pp->vram[pos->tp_row][pos->tp_col] = ch;
126 5027420
}
127
128
static void
129 9045
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 57220
        for (p.tp_row = r->tr_begin.tp_row;
136 57220
            p.tp_row < r->tr_end.tp_row; p.tp_row++)
137 4445280
                for (p.tp_col = r->tr_begin.tp_col;
138 4445280
                    p.tp_col < r->tr_end.tp_col; p.tp_col++)
139 4445280
                        term_putchar(priv, &p, c, a);
140 9045
}
141
142
static void
143 8485
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 8485
        CAST_OBJ_NOTNULL(pp, priv, PROCESS_MAGIC);
153
154 8485
        nrow = r->tr_end.tp_row - r->tr_begin.tp_row;
155 8485
        ncol = r->tr_end.tp_col - r->tr_begin.tp_col;
156
157 8485
        if (p->tp_row < r->tr_begin.tp_row) {
158
                /* Copy from top to bottom. */
159 117985
                for (y = 0; y < nrow; y++)
160 229600
                        memmove(&pp->vram[p->tp_row + y][p->tp_col],
161 114800
                            &pp->vram[r->tr_begin.tp_row + y][r->tr_begin.tp_col], ncol);
162 3185
        } else {
163
                /* Copy from bottom to top. */
164 29595
                for (y = nrow - 1; y >= 0; y--)
165 48590
                        memmove(&pp->vram[p->tp_row + y][p->tp_col],
166 24295
                            &pp->vram[r->tr_begin.tp_row + y][r->tr_begin.tp_col], ncol);
167
        }
168 8485
}
169
170
static void
171 80
term_respond(void *priv, const void *p, size_t l)
172
{
173
        struct process *pp;
174
        int r;
175
176 80
        CAST_OBJ_NOTNULL(pp, priv, PROCESS_MAGIC);
177
178 80
        vtc_dump(pp->vl, 4, "term_response", p, l);
179 80
        if (pp->ansi_response) {
180 10
                r = write(pp->fd_term, p, l);
181 10
                if (r != l)
182 0
                        vtc_fatal(pp->vl, "Could not write to process: %s",
183 0
                            strerror(errno));
184 10
        }
185 80
}
186
187
static void
188 695
term_param(void *priv, int p, unsigned int v)
189
{
190
        struct process *pp;
191
192 695
        CAST_OBJ_NOTNULL(pp, priv, PROCESS_MAGIC);
193 695
        if (p == TP_132COLS && v)
194 60
                term_resize(pp, pp->nlin, 132);
195 180
        if (p == TP_132COLS && !v)
196 120
                term_resize(pp, pp->nlin, 80);
197 695
}
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 790
term_screen_dump(const struct process *pp)
210
{
211
        int i;
212
        const teken_pos_t *pos;
213
214 20910
        for (i = 0; i < pp->nlin; i++)
215 20120
                vtc_dump(pp->vl, 3, "screen", pp->vram[i], pp->ncol);
216 790
        pos = teken_get_cursor(pp->tek);
217 1580
        vtc_log(pp->vl, 3, "Cursor at line %d column %d",
218 790
            pos->tp_row + 1, pos->tp_col + 1);
219 790
}
220
221
static void
222 520
term_resize(struct process *pp, int lin, int col)
223
{
224
        teken_pos_t pos;
225
        char **vram;
226
        int i, j;
227
228 520
        vram = calloc(lin, sizeof *pp->vram);
229 520
        AN(vram);
230 13760
        for (i = 0; i < lin; i++) {
231 13240
                vram[i] = calloc(col + 1L, 1);
232 13240
                AN(vram[i]);
233 13240
                memset(vram[i], ' ', col);
234 13240
                vram[i][col] = '\0';
235 13240
        }
236 520
        if (pp->vram != NULL) {
237 5620
                for (i = 0; i < lin; i++) {
238 5435
                        if (i >= pp->nlin)
239 40
                                break;
240 5395
                        j = col;
241 5395
                        if (j > pp->ncol)
242 1920
                                j = pp->ncol;
243 5395
                        memcpy(vram[i], pp->vram[i], j);
244 5395
                }
245 5625
                for (i = 0; i < pp->nlin; i++)
246 5400
                        free(pp->vram[i]);
247 225
                free(pp->vram);
248 225
        }
249 520
        pp->vram = vram;
250 520
        pp->nlin = lin;
251 520
        pp->ncol = col;
252
253 520
        pos.tp_row = lin;
254 520
        pos.tp_col = col;
255 520
        teken_set_winsize(pp->tek, &pos);
256 520
}
257
258
static int
259 27152
term_find_textline(const struct process *pp, int *x, int y, const char *pat)
260
{
261
        const char *t;
262
        int l;
263
264 27152
        if (*x == 0) {
265 20803
                t = strstr(pp->vram[y], pat);
266 20803
                if (t != NULL) {
267 365
                        *x = 1 + (t - pp->vram[y]);
268 365
                        return (1);
269
                }
270 26787
        } else if (*x <= pp->ncol) {
271 6349
                t = pp->vram[y] + *x - 1;
272 6349
                l = strlen(pat);
273 6349
                assert((*x - 1) + (l - 1) < pp->ncol);
274 6349
                if (!memcmp(t, pat, l))
275 950
                        return (1);
276 5399
        }
277 25837
        return (0);
278 27152
}
279
280
static int
281 2693
term_find_text(const struct process *pp, int *x, int *y, const char *pat)
282
{
283
        int yy;
284
285 2693
        if (*y == 0) {
286 26236
                for (yy = 0; yy < pp->nlin; yy++) {
287 25555
                        if (term_find_textline(pp, x, yy, pat)) {
288 415
                                *y = yy + 1;
289 415
                                return (1);
290
                        }
291 25140
                }
292 2278
        } else if (*y <= pp->nlin) {
293 1597
                if (term_find_textline(pp, x, *y - 1, pat))
294 900
                        return (1);
295 697
        }
296 1378
        return (0);
297 2693
}
298
299
static void
300 1315
term_expect_text(struct process *pp,
301
    const char *lin, const char *col, const char *pat)
302
{
303 1315
        int x, y, l, d = 10000;
304
        char *t;
305
306 1315
        y = strtoul(lin, NULL, 0);
307 1315
        if (y < 0 || y > pp->nlin)
308 0
                vtc_fatal(pp->vl, "YYY %d nlin %d", y, pp->nlin);
309 1315
        x = strtoul(col, NULL, 0);
310 1335
        for(l = 0; l <= 10 && x > pp->ncol; l++)        // wait for screen change
311 20
                VTIM_sleep(0.1);
312 1315
        if (x < 0 || x > pp->ncol)
313 0
                vtc_fatal(pp->vl, "XXX %d ncol %d", x, pp->ncol);
314 1315
        l = strlen(pat);
315 1315
        if (x + l - 1 > pp->ncol)
316 0
                vtc_fatal(pp->vl, "XXX %d ncol %d", x + l - 1, pp->ncol);
317 1315
        PTOK(pthread_mutex_lock(&pp->mtx));
318 2693
        while (!term_find_text(pp, &x, &y, pat)) {
319 1378
                if (x != 0 && y != 0) {
320 626
                        t = pp->vram[y - 1] + x - 1;
321 1252
                        vtc_log(pp->vl, 4,
322 626
                            "text at %d,%d: '%.*s'", y, x, l, t);
323 626
                }
324 1378
                PTOK(pthread_mutex_unlock(&pp->mtx));
325 1378
                usleep(d);
326 1378
                PTOK(pthread_mutex_lock(&pp->mtx));
327 1378
                if (d < 3000000)
328 1378
                        d += d;
329
        }
330 1315
        PTOK(pthread_mutex_unlock(&pp->mtx));
331 1315
        vtc_log(pp->vl, 4, "found expected text at %d,%d: '%s'", y, x, pat);
332 1315
}
333
334
static void
335 40
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 40
        pos = teken_get_cursor(pp->tek);
341 40
        y = strtoul(lin, NULL, 0);
342 40
        if (y < 0 || y > pp->nlin)
343 0
                vtc_fatal(pp->vl, "YYY %d nlin %d", y, pp->nlin);
344 40
        x = strtoul(col, NULL, 0);
345 40
        for(l = 0; l < 10 && x > pp->ncol; l++) // wait for screen change
346 0
                VTIM_sleep(0.1);
347 40
        if (x < 0 || x > pp->ncol)
348 0
                vtc_fatal(pp->vl, "XXX %d ncol %d", x, pp->ncol);
349 40
        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 30
        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 40
}
356
357
static void
358 170
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 170
        vsb = VSB_new_auto();
369 170
        AN(vsb);
370
371 170
        y = strtoul(lin, NULL, 0);
372 170
        if (y < 0 || y > pp->nlin)
373 0
                vtc_fatal(pp->vl, "YYY %zd nlin %d", y, pp->nlin);
374 170
        x = strtoul(col, NULL, 0);
375 170
        for(l = 0; l < 10 && x > pp->ncol; l++) // wait for screen change
376 0
                VTIM_sleep(0.1);
377 170
        if (x < 0 || x > pp->ncol)
378 0
                vtc_fatal(pp->vl, "XXX %zd ncol %d", x, pp->ncol);
379
380 170
        if (x)
381 80
                x--;
382
383 90
        if (y)
384 90
                y--;
385
386 170
        vre = VRE_compile(re, 0, &err, &erroff, 1);
387 170
        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 170
        PTOK(pthread_mutex_lock(&pp->mtx));
397
398 170
        len = (pp->nlin - y) * (pp->ncol - x);
399 4120
        for (i = y; i < pp->nlin; i++) {
400 3950
                VSB_bcat(vsb, &pp->vram[i][x], pp->ncol - x);
401 3950
                VSB_putc(vsb, '\n');
402 3950
        }
403
404 170
        AZ(VSB_finish(vsb));
405
406 170
        if (VRE_match(vre, VSB_data(vsb), len, 0, NULL) < 1)
407 0
                vtc_fatal(pp->vl, "match failed: (\"%s\")", re);
408
        else
409 170
                vtc_log(pp->vl, 4, "match succeeded");
410
411 170
        PTOK(pthread_mutex_unlock(&pp->mtx));
412 170
        VSB_destroy(&vsb);
413 170
        VRE_free(&vre);
414 170
}
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 295
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 295
        (void)teken_get_sequence(p->tek, TKEY_UP);
439 295
        (void)teken_get_sequence(p->tek, TKEY_F1);
440 295
        (void)teken_256to8(0);
441 295
        (void)teken_256to16(0);
442 295
        a = teken_get_defattr(p->tek);
443 295
        teken_set_defattr(p->tek, a);
444 295
        a = teken_get_curattr(p->tek);
445 295
        teken_set_curattr(p->tek, a);
446 295
        (void)teken_get_winsize(p->tek);
447 295
        pos.tp_row = 0;
448 295
        pos.tp_col = 8;
449 295
        teken_set_cursor(p->tek, &pos);
450 295
        teken_get_defattr_cons25(p->tek, &fg, &bg);
451 295
}
452
453
static struct process *
454 295
process_new(const char *name)
455
{
456
        struct process *p;
457
        struct vsb *vsb;
458
        char buf[1024];
459
460 295
        ALLOC_OBJ(p, PROCESS_MAGIC);
461 295
        AN(p);
462 295
        REPLACE(p->name, name);
463 295
        PTOK(pthread_mutex_init(&p->mtx, NULL));
464
465 295
        p->vl = vtc_logopen("%s", name);
466 295
        AN(p->vl);
467
468 295
        PROCESS_EXPAND(dir, "${tmpdir}/%s", name);
469 295
        PROCESS_EXPAND(out, "${tmpdir}/%s/term", name);
470 295
        PROCESS_EXPAND(err, "${tmpdir}/%s/stderr", name);
471
472 295
        bprintf(buf, "rm -rf %s ; mkdir -p %s ; touch %s %s",
473
            p->dir, p->dir, p->out, p->err);
474 295
        AZ(system(buf));
475
476 295
        p->fd_term = -1;
477
478 295
        VTAILQ_INSERT_TAIL(&processes, p, list);
479 295
        teken_init(p->tek, &process_teken_func, p);
480 295
        term_resize(p, 24, 80);
481 295
        process_coverage(p);
482 295
        return (p);
483
}
484
485
#undef PROCESS_EXPAND
486
487
/**********************************************************************
488
 * Clean up process
489
 */
490
491
static void
492 295
process_delete(struct process *p)
493
{
494
        int i;
495
496 295
        CHECK_OBJ_NOTNULL(p, PROCESS_MAGIC);
497 295
        PTOK(pthread_mutex_destroy(&p->mtx));
498 295
        vtc_logclose(p->vl);
499 295
        free(p->name);
500 295
        free(p->dir);
501 295
        free(p->out);
502 295
        free(p->err);
503
504 8135
        for (i = 0; i < p->nlin; i++)
505 7840
                free(p->vram[i]);
506 295
        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 295
        FREE_OBJ(p);
516 295
}
517
518
static void
519 295
process_undef(const struct process *p)
520
{
521 295
        CHECK_OBJ_NOTNULL(p, PROCESS_MAGIC);
522
523 295
        macro_undef(p->vl, p->name, "dir");
524 295
        macro_undef(p->vl, p->name, "out");
525 295
        macro_undef(p->vl, p->name, "err");
526 295
}
527
528
/**********************************************************************
529
 * Data stream handling
530
 */
531
532
static int
533 1245
process_vlu_func(void *priv, const char *l)
534
{
535
        struct process *p;
536
537 1245
        CAST_OBJ_NOTNULL(p, priv, PROCESS_MAGIC);
538 1245
        vtc_dump(p->vl, 4, "output", l, -1);
539 1245
        return (0);
540
}
541
542
static int v_matchproto_(vev_cb_f)
543 21979
process_stdout(const struct vev *ev, int what)
544
{
545
        struct process *p;
546
        char buf[BUFSIZ];
547
        int i;
548
549 21979
        CAST_OBJ_NOTNULL(p, ev->priv, PROCESS_MAGIC);
550 21979
        (void)what;
551 21979
        i = read(p->fd_term, buf, sizeof buf);
552 21979
        if (i <= 0) {
553 375
                vtc_log(p->vl, 4, "stdout read %d", i);
554 375
                return (1);
555
        }
556 21604
        PTOK(pthread_mutex_lock(&p->mtx));
557 21604
        p->stdout_bytes += i;
558 21604
        PTOK(pthread_mutex_unlock(&p->mtx));
559 21604
        if (p->log == 1)
560 367
                (void)VLU_Feed(p->vlu_stdout, buf, i);
561 21237
        else if (p->log == 2)
562 1199
                vtc_dump(p->vl, 4, "stdout", buf, i);
563 20038
        else if (p->log == 3)
564 238
                vtc_hexdump(p->vl, 4, "stdout", buf, i);
565 21604
        assert(write(p->f_stdout, buf, i) == i);
566 21604
        PTOK(pthread_mutex_lock(&p->mtx));
567 21604
        teken_input(p->tek, buf, i);
568 21604
        PTOK(pthread_mutex_unlock(&p->mtx));
569 21604
        return (0);
570 21979
}
571
572
static int v_matchproto_(vev_cb_f)
573 445
process_stderr(const struct vev *ev, int what)
574
{
575
        struct process *p;
576
        char buf[BUFSIZ];
577
        int i;
578
579 445
        CAST_OBJ_NOTNULL(p, ev->priv, PROCESS_MAGIC);
580 445
        (void)what;
581 445
        i = read(p->fd_stderr, buf, sizeof buf);
582 445
        if (i <= 0) {
583 375
                vtc_log(p->vl, 4, "stderr read %d", i);
584 375
                return (1);
585
        }
586 70
        PTOK(pthread_mutex_lock(&p->mtx));
587 70
        p->stderr_bytes += i;
588 70
        PTOK(pthread_mutex_unlock(&p->mtx));
589 70
        vtc_dump(p->vl, 4, "stderr", buf, i);
590 70
        assert(write(p->f_stderr, buf, i) == i);
591 70
        return (0);
592 445
}
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 375
process_thread(void *priv)
603
{
604
        struct process *p;
605
        struct vev_root *evb;
606
        struct vev *ev;
607
        int r;
608
609 375
        CAST_OBJ_NOTNULL(p, priv, PROCESS_MAGIC);
610
611 375
        p->f_stdout = open(p->out, O_WRONLY|O_APPEND);
612 375
        assert(p->f_stdout >= 0);
613 375
        p->f_stderr = open(p->err, O_WRONLY|O_APPEND);
614 375
        assert(p->f_stderr >= 0);
615
616 375
        evb = VEV_New();
617 375
        AN(evb);
618 375
        pthread_cleanup_push(process_cleanup, evb);
619
620 375
        ev = VEV_Alloc();
621 375
        AN(ev);
622 375
        ev->fd = p->fd_term;
623 375
        ev->fd_flags = VEV__RD | VEV__HUP | VEV__ERR;
624 375
        ev->callback = process_stdout;
625 375
        ev->priv = p;
626 375
        AZ(VEV_Start(evb, ev));
627
628 375
        ev = VEV_Alloc();
629 375
        AN(ev);
630 375
        ev->fd = p->fd_stderr;
631 375
        ev->fd_flags = VEV__RD | VEV__HUP | VEV__ERR;
632 375
        ev->callback = process_stderr;
633 375
        ev->priv = p;
634 375
        AZ(VEV_Start(evb, ev));
635
636 375
        if (p->log == 1) {
637 75
                p->vlu_stdout = VLU_New(process_vlu_func, p, 1024);
638 75
                AN(p->vlu_stdout);
639 75
                p->vlu_stderr = VLU_New(process_vlu_func, p, 1024);
640 75
                AN(p->vlu_stderr);
641 75
        }
642
643 375
        do {
644 375
                r = VEV_Once(evb);
645 375
        } while (r == 1);
646
647 375
        if (r < 0)
648 0
                vtc_fatal(p->vl, "VEV_Once() = %d, error %s", r,
649 0
                    strerror(errno));
650
651 750
        vtc_wait4(p->vl, p->pid,
652 375
            p->expect_exit, p->expect_signal, p->allow_core);
653 375
        closefd(&p->f_stdout);
654 375
        closefd(&p->f_stderr);
655
656 375
        PTOK(pthread_mutex_lock(&p->mtx));
657
658
        /* NB: We keep the other macros around */
659 375
        macro_undef(p->vl, p->name, "pid");
660 375
        p->pid = -1;
661
662 375
        PTOK(pthread_mutex_unlock(&p->mtx));
663
664 375
        pthread_cleanup_pop(0);
665 375
        VEV_Destroy(&evb);
666 375
        if (p->log == 1) {
667 75
                VLU_Destroy(&p->vlu_stdout);
668 75
                VLU_Destroy(&p->vlu_stderr);
669 75
        }
670 375
        return (NULL);
671
}
672
673
static void
674 420
process_winsz(struct process *p, int fd)
675
{
676
        struct winsize ws;
677
        int i;
678
679 420
        memset(&ws, 0, sizeof ws);
680 420
        ws.ws_row = (short)p->nlin;
681 420
        ws.ws_col = (short)p->ncol;
682 420
        i = ioctl(fd, TIOCSWINSZ, &ws);
683 420
        if (i)
684 20
                vtc_log(p->vl, 4, "TIOCWINSZ %d %s", i, strerror(errno));
685 420
}
686
687
static void
688 375
process_init_term(struct process *p, int fd)
689
{
690
        struct termios tt;
691
        int i;
692
693 375
        process_winsz(p, fd);
694
695 375
        memset(&tt, 0, sizeof tt);
696 375
        tt.c_cflag = CREAD | CS8 | HUPCL;
697 375
        tt.c_iflag = BRKINT | ICRNL | IMAXBEL | IXON | IXANY;
698 375
        tt.c_lflag = ICANON | ISIG | IEXTEN | ECHO | ECHOE | ECHOKE | ECHOCTL;
699 375
        tt.c_oflag = OPOST | ONLCR;
700 375
        i = cfsetispeed(&tt, B9600);
701 375
        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 375
}
716
717
/**********************************************************************
718
 * Start the process thread
719
 */
720
721
static void
722 375
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 375
        CHECK_OBJ_NOTNULL(p, PROCESS_MAGIC);
731 375
        if (p->hasthread)
732 0
                vtc_fatal(p->vl, "Already running, -wait first");
733
734 375
        vtc_log(p->vl, 4, "CMD: %s", p->spec);
735
736 375
        cl = macro_expand(p->vl, p->spec);
737 375
        AN(cl);
738
739 375
        master = posix_openpt(O_RDWR|O_NOCTTY);
740 375
        assert(master >= 0);
741 375
        AZ(grantpt(master));
742 375
        AZ(unlockpt(master));
743 375
        slavename = ptsname(master);
744 375
        AN(slavename);
745
746 375
        AZ(pipe(fd2));
747
748 375
        p->pid = fork();
749 375
        assert(p->pid >= 0);
750 750
        if (p->pid == 0) {
751 375
                assert(setsid() == getpid());
752 375
                assert(dup2(fd2[1], STDERR_FILENO) == STDERR_FILENO);
753 375
                AZ(close(STDIN_FILENO));
754 375
                slave = open(slavename, O_RDWR);
755 375
                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 375
                AZ(ioctl(STDIN_FILENO, TIOCSCTTY, NULL));
764
#endif
765 375
                AZ(close(STDOUT_FILENO));
766 375
                assert(dup2(slave, STDOUT_FILENO) == STDOUT_FILENO);
767 375
                VSUB_closefrom(STDERR_FILENO + 1);
768 375
                process_init_term(p, slave);
769
770 375
                AZ(setenv("TERM", "xterm", 1));
771 375
                AZ(unsetenv("TERMCAP"));
772
                // Not using NULL because GCC is now even more demented...
773 375
                assert(write(STDERR_FILENO, "+", 1) == 1);
774
                AZ(execl("/bin/sh", "/bin/sh", "-c", VSB_data(cl), (char*)0));
775
                exit(1);
776 375
        }
777
        vtc_log(p->vl, 3, "PID: %ld", (long)p->pid);
778 375
        VSB_destroy(&cl);
779 0
780
        assert(read(fd2[0], &c, 1) == 1);
781 375
        p->fd_term = master;
782 375
        closefd(&fd2[1]);
783
        p->fd_stderr = fd2[0];
784 375
        macro_def(p->vl, p->name, "pid", "%ld", (long)p->pid);
785 375
        macro_def(p->vl, p->name, "dir", "%s", p->dir);
786 375
        macro_def(p->vl, p->name, "out", "%s", p->out);
787 375
        macro_def(p->vl, p->name, "err", "%s", p->err);
788 375
        p->hasthread = 1;
789 375
        PTOK(pthread_create(&p->tp, NULL, process_thread, p));
790 375
}
791 375
792 375
/**********************************************************************
793 375
 * Wait for process thread to stop
794 375
 */
795
796
static void
797
process_wait(struct process *p)
798
{
799
        void *v;
800
801 375
        if (p->hasthread) {
802
                PTOK(pthread_join(p->tp, &v));
803
                p->hasthread = 0;
804
        }
805 375
        vtc_log(p->vl, 4, "stdout %ju bytes, stderr %ju bytes",
806 375
            p->stdout_bytes, p->stderr_bytes);
807 375
}
808 375
809 750
/**********************************************************************
810 375
 * Send a signal to a process
811 375
 */
812
813
static void
814
process_kill(struct process *p, const char *sig)
815
{
816
        int j = 0;
817
        pid_t pid;
818 150
819
        CHECK_OBJ_NOTNULL(p, PROCESS_MAGIC);
820 150
        AN(sig);
821
822
        PTOK(pthread_mutex_lock(&p->mtx));
823 150
        pid = p->pid;
824 150
        PTOK(pthread_mutex_unlock(&p->mtx));
825
826 150
        if (pid <= 0)
827 150
                vtc_fatal(p->vl, "Cannot signal a non-running process");
828 150
829
        if (!strcmp(sig, "TERM"))
830 150
                j = SIGTERM;
831 0
        else if (!strcmp(sig, "INT"))
832
                j = SIGINT;
833 150
        else if (!strcmp(sig, "KILL"))
834 110
                j = SIGKILL;
835 40
        else if (!strcmp(sig, "HUP"))
836 5
                j = SIGHUP;
837 35
        else if (*sig == '-')
838 10
                j = strtoul(sig + 1, NULL, 10);
839 25
        else
840 25
                vtc_fatal(p->vl, "Could not grok signal (%s)", sig);
841 0
842 0
        if (p->expect_signal == 0)
843
                p->expect_signal = -j;
844 0
        if (kill(-pid, j) < 0)
845
                vtc_fatal(p->vl, "Failed to send signal %d (%s)",
846 150
                    j, strerror(errno));
847 145
        else
848 150
                vtc_log(p->vl, 4, "Sent signal %d", j);
849 0
}
850 0
851
/**********************************************************************
852 150
 * Write to a process' stdin
853 150
 */
854
855
static void
856
process_write(const struct process *p, const char *text)
857
{
858
        int r, len;
859
860 690
        if (!p->hasthread)
861
                vtc_fatal(p->vl, "Cannot write to a non-running process");
862
863
        len = strlen(text);
864 690
        vtc_log(p->vl, 4, "Writing %d bytes", len);
865 0
        r = write(p->fd_term, text, len);
866
        if (r != len)
867 690
                vtc_fatal(p->vl, "Failed to write: len=%d %s (%d)",
868 690
                    len, strerror(errno), errno);
869 690
}
870 690
871 0
static void
872 0
process_write_hex(const struct process *p, const char *text)
873 690
{
874
        struct vsb *vsb;
875
876 625
        if (!p->hasthread)
877
                vtc_fatal(p->vl, "Cannot write to a non-running process");
878
879
        vsb = vtc_hex_to_bin(p->vl, text);
880 625
        assert(VSB_len(vsb) >= 0);
881 0
        vtc_hexdump(p->vl, 4, "sendhex", VSB_data(vsb), VSB_len(vsb));
882
        AZ(VSB_tofile(vsb, p->fd_term));
883 625
        VSB_destroy(&vsb);
884 625
}
885 625
886 625
static void
887 625
process_close(struct process *p)
888 625
{
889
890
        if (!p->hasthread)
891 10
                vtc_fatal(p->vl, "Cannot close a non-running process");
892
893
        process_kill(p, "HUP");
894 10
}
895 0
896
/* SECTION: process process
897 10
 *
898 10
 * 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
cmd_process(CMD_ARGS)
1032
{
1033
        struct process *p, *p2;
1034
        uintmax_t u, v, bsnap;
1035 9515
        unsigned lin,col;
1036
        int spec_set = 0;
1037
1038
        (void)priv;
1039
1040 9515
        if (av == NULL) {
1041
                /* Reset and free */
1042 9515
                VTAILQ_FOREACH_SAFE(p, &processes, list, p2) {
1043
                        if (p->pid > 0) {
1044 9515
                                process_kill(p, "TERM");
1045
                                sleep(1);
1046 5750
                                if (p->pid > 0)
1047 295
                                        process_kill(p, "KILL");
1048 30
                        }
1049 30
                        if (p->hasthread)
1050 30
                                process_wait(p);
1051 0
                        VTAILQ_REMOVE(&processes, p, list);
1052 30
                        process_undef(p);
1053 50
                        process_delete(p);
1054 50
                }
1055 295
                return;
1056 295
        }
1057 295
1058 295
        AZ(strcmp(av[0], "process"));
1059 5455
        av++;
1060
1061
        VTC_CHECK_NAME(vl, av[0], "Process", 'p');
1062 4060
        VTAILQ_FOREACH(p, &processes, list)
1063 4060
                if (!strcmp(p->name, av[0]))
1064
                        break;
1065 4060
        if (p == NULL)
1066 9245
                p = process_new(av[0]);
1067 8950
        av++;
1068 3765
1069 7235
        PTOK(pthread_mutex_lock(&p->mtx));
1070 295
        bsnap = p->stdout_bytes;
1071 4060
        PTOK(pthread_mutex_unlock(&p->mtx));
1072
1073 4060
        for (; *av != NULL; av++) {
1074 4060
                if (vtc_error)
1075 4060
                        break;
1076
1077 9215
                if (!strcmp(*av, "-allow-core")) {
1078 5155
                        p->allow_core = 1;
1079 0
                        continue;
1080
                }
1081 5155
                if (!strcmp(*av, "-close")) {
1082 0
                        process_close(p);
1083 0
                        continue;
1084
                }
1085 5155
                if (!strcmp(*av, "-dump")) {
1086 10
                        if (p->hasthread)
1087 10
                                vtc_fatal(p->vl,
1088
                                    "Cannot dump a running process");
1089 5145
                        p->log = 2;
1090 100
                        continue;
1091 0
                }
1092
                if (!strcmp(*av, "-expect-exit")) {
1093 100
                        p->expect_exit = strtoul(av[1], NULL, 0);
1094 100
                        av++;
1095
                        continue;
1096 5045
                }
1097 80
                if (!strcmp(*av, "-expect-signal")) {
1098 80
                        p->expect_signal = strtoul(av[1], NULL, 0);
1099 80
                        av++;
1100
                        continue;
1101 4965
                }
1102 0
                if (!strcmp(*av, "-hexdump")) {
1103 0
                        if (p->hasthread)
1104 0
                                vtc_fatal(p->vl,
1105
                                    "Cannot dump a running process");
1106 4965
                        p->log = 3;
1107 20
                        continue;
1108 0
                }
1109
                if (!strcmp(*av, "-key")) {
1110 20
                        if (!strcmp(av[1], "NPAGE"))
1111 20
                                process_write(p, "\x1b\x5b\x36\x7e");
1112
                        else if (!strcmp(av[1], "PPAGE"))
1113 4945
                                process_write(p, "\x1b\x5b\x35\x7e");
1114 30
                        else if (!strcmp(av[1], "HOME"))
1115 10
                                process_write(p, "\x1b\x4f\x48");
1116 20
                        else if (!strcmp(av[1], "END"))
1117 10
                                process_write(p, "\x1b\x4f\x46");
1118 10
                        else
1119 5
                                vtc_fatal(p->vl, "Unknown key %s", av[1]);
1120 5
                        continue;
1121 5
                }
1122
                if (!strcmp(*av, "-kill")) {
1123 0
                        process_kill(p, av[1]);
1124 30
                        av++;
1125
                        continue;
1126 4915
                }
1127 60
                if (!strcmp(*av, "-log")) {
1128 60
                        if (p->hasthread)
1129 60
                                vtc_fatal(p->vl,
1130
                                    "Cannot log a running process");
1131 4855
                        p->log = 1;
1132 75
                        continue;
1133 0
                }
1134
                if (!strcmp(*av, "-need-bytes")) {
1135 75
                        u = strtoumax(av[1], NULL, 0);
1136 75
                        if (av[1][0] == '+')
1137
                                u += bsnap;
1138 4780
                        av++;
1139 45
                        do {
1140 45
                                PTOK(pthread_mutex_lock(&p->mtx));
1141 35
                                v = p->stdout_bytes;
1142 45
                                PTOK(pthread_mutex_unlock(&p->mtx));
1143 45
                                vtc_log(p->vl, 4, "Have %ju bytes", v);
1144 45
                                VTIM_sleep(0.5);
1145 85
                        } while(v < u);
1146 85
                        continue;
1147 85
                }
1148 85
                if (!strcmp(*av, "-run")) {
1149 85
                        process_start(p);
1150 45
                        process_wait(p);
1151
                        continue;
1152 4735
                }
1153 95
                if (!strcmp(*av, "-ansi-response")) {
1154 95
                        p->ansi_response = 1;
1155 95
                        continue;
1156
                }
1157 4640
                if (!strcmp(*av, "-expect-text")) {
1158 10
                        AN(av[1]);
1159 10
                        AN(av[2]);
1160
                        AN(av[3]);
1161 4630
                        term_expect_text(p, av[1], av[2], av[3]);
1162 1315
                        av += 3;
1163 1315
                        continue;
1164 1315
                }
1165 1315
                if (!strcmp(*av, "-expect-cursor")) {
1166 1315
                        AN(av[1]);
1167 1315
                        AN(av[2]);
1168
                        term_expect_cursor(p, av[1], av[2]);
1169 3315
                        av += 2;
1170 40
                        continue;
1171 40
                }
1172 40
                if (!strcmp(*av, "-match-text")) {
1173 40
                        AN(av[1]);
1174 40
                        AN(av[2]);
1175
                        AN(av[3]);
1176 3275
                        term_match_text(p, av[1], av[2], av[3]);
1177 170
                        av += 3;
1178 170
                        continue;
1179 170
                }
1180 170
                if (!strcmp(*av, "-screen_dump") ||
1181 170
                    !strcmp(*av, "-screen-dump")) {
1182 170
                        term_screen_dump(p);
1183
                        continue;
1184 3105
                }
1185 2370
                if (!strcmp(*av, "-start")) {
1186 790
                        process_start(p);
1187 790
                        continue;
1188
                }
1189 2315
                if (!strcmp(*av, "-stop")) {
1190
                        process_kill(p, "TERM");
1191 280
                        sleep(1);
1192 280
                        continue;
1193 280
                }
1194
                if (!strcmp(*av, "-wait")) {
1195 2035
                        process_wait(p);
1196 50
                        continue;
1197 50
                }
1198 50
                if (!strcmp(*av, "-winsz")) {
1199
                        lin = atoi(av[1]);
1200 1985
                        assert(lin > 1);
1201 230
                        col = atoi(av[2]);
1202 230
                        assert(col > 1);
1203
                        av += 2;
1204 1755
                        PTOK(pthread_mutex_lock(&p->mtx));
1205 45
                        term_resize(p, lin, col);
1206 45
                        PTOK(pthread_mutex_unlock(&p->mtx));
1207 45
                        process_winsz(p, p->fd_term);
1208 45
                        continue;
1209 45
                }
1210 45
                if (!strcmp(*av, "-write")) {
1211 45
                        process_write(p, av[1]);
1212 45
                        av++;
1213 45
                        continue;
1214 45
                }
1215
                if (!strcmp(*av, "-writehex")) {
1216 1710
                        process_write_hex(p, av[1]);
1217 580
                        av++;
1218 580
                        continue;
1219 580
                }
1220
                if (!strcmp(*av, "-writeln")) {
1221 1130
                        process_write(p, av[1]);
1222 625
                        process_write(p, "\n");
1223 625
                        av++;
1224 625
                        continue;
1225
                }
1226 505
                if (**av == '-' || spec_set)
1227 40
                        vtc_fatal(p->vl, "Unknown process argument: %s", *av);
1228 40
                REPLACE(p->spec, *av);
1229 40
                spec_set = 1;
1230 40
        }
1231
}