Code Coverage: vinyl-cache/bin/vinyltest/vtest_ext_vinyl_vinyl.c
 
82217aa4fc7094b3610340078afaf78cd95a8ec9
 
0
/*-
1
 * Copyright (c) 2008-2015 Varnish Software AS
2
 * All rights reserved.
3
 *
4
 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
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
30
#include "config.h"
31
32
#include <sys/socket.h>
33
34
#include <errno.h>
35
#include <fcntl.h>
36
#include <fnmatch.h>
37
#include <inttypes.h>
38
#include <limits.h>
39
#include <poll.h>
40
#include <pthread.h>
41
#include <signal.h>
42
#include <stdio.h>
43
#include <stdlib.h>
44
#include <string.h>
45
#include <unistd.h>
46
47
#include "vapi/vsc.h"
48
#include "vapi/vsl.h"
49
#include "vapi/vsm.h"
50
51
#include "vdef.h"
52
#include "vqueue.h"
53
54
#include "miniobj.h"
55
#include "vas.h"
56
#include "vcli.h"
57
#include "vjsn.h"
58
#include "vre.h"
59
#include "vsb.h"
60
#include "vsub.h"
61
#include "vtcp.h"
62
#include "vtim.h"
63
64
#include <vtest_api.h>
65
#include "vtest_ext_vinyl.h"
66
67
struct vinyl {
68
        unsigned                magic;
69
#define VINYL_MAGIC             0x208cd8e4
70
        char                    *me;
71
        char                    *name;
72
        struct vtclog           *vl;
73
        VTAILQ_ENTRY(vinyl)     list;
74
75
        struct vsb              *args;
76
        int                     fds[4];
77
        pid_t                   pid;
78
79
        double                  syntax;
80
81
        pthread_t               tp;
82
        pthread_t               tp_vsl;
83
        int                     tp_started;
84
85
        int                     expect_exit;
86
87
        int                     cli_fd;
88
        int                     vcl_nbr;
89
        char                    *workdir;
90
        char                    *jail;
91
        char                    *proto;
92
93
        struct vsm              *vsm_vsl;
94
        struct vsm              *vsm_vsc;
95
        struct vsc              *vsc;
96
        int                     has_a_arg;
97
98
        unsigned                vsl_tag_count[256];
99
100
        volatile int            vsl_rec;
101
        volatile int            vsl_idle;
102
};
103
104 3180
#define NONSENSE        "%XJEIFLH|)Xspa8P"
105
106
static VTAILQ_HEAD(, vinyl)     vinyles =
107
    VTAILQ_HEAD_INITIALIZER(vinyles);
108
109
/**********************************************************************
110
 * Fatal condition cleanup
111
 * Invalid to call in any code path not followed by vtc_fatal().
112
 */
113
114
static void
115
vinyl_fatal_cleanup(const struct vinyl *v)
116 1
{
117 1
        struct pollfd fd[1];
118 1
        int n;
119
120 1
        if (!pthread_equal(pthread_self(), vtc_thread))
121 0
                return;
122
123 1
        if (!v->tp_started)
124 1
                return;
125
126 0
        memset(fd, 0, sizeof(fd));
127 0
        fd[0].fd = v->fds[0];
128 0
        fd[0].events = POLLIN;
129
130 0
        do {
131 0
                n = poll(fd, sizeof(fd)/sizeof(fd[0]), 10);
132 0
                if (n == 1 && (fd[0].revents & (POLLHUP|POLLERR)) != 0) {
133 0
                        PTOK(pthread_join(v->tp, NULL));
134 0
                        break;
135 0
                }
136
137 0
                if (n == 1)
138 0
                        VTIM_sleep(0.01);
139 0
        } while (n > 0);
140 0
}
141
142
#define vinyl_fatal(v, ...)                                             \
143 1
    do {                                                                \
144 1
            vinyl_fatal_cleanup((v));                                   \
145 1
            vtc_fatal((v)->vl, __VA_ARGS__);                            \
146 1
    } while (0)
147
148
/**********************************************************************
149
 * Ask a question over CLI
150
 */
151
152
static enum VCLI_status_e
153
vinyl_ask_cli(const struct vinyl *v, const char *cmd, char **repl)
154 15535
{
155 15535
        int i;
156 15535
        unsigned retval;
157 15535
        char *r;
158
159 15535
        if (cmd != NULL) {
160 14510
                vtc_dump(v->vl, 4, "CLI TX", cmd, -1);
161 14510
                i = write(v->cli_fd, cmd, vstrlen(cmd));
162 14510
                if (i != vstrlen(cmd) && !vtc_stop)
163 0
                        vinyl_fatal(v, "CLI write failed (%s) = %u %s",
164 14510
                            cmd, errno, strerror(errno));
165 14510
                i = write(v->cli_fd, "\n", 1);
166 14510
                if (i != 1 && !vtc_stop)
167 0
                        vinyl_fatal(v, "CLI write failed (%s) = %u %s",
168 14510
                            cmd, errno, strerror(errno));
169 14510
        }
170 15535
        i = VCLI_ReadResult(v->cli_fd, &retval, &r, vtc_maxdur);
171 15535
        if (i != 0 && !vtc_stop)
172 0
                vinyl_fatal(v, "CLI failed (%s) = %d %u %s",
173 15535
                    cmd != NULL ? cmd : "NULL", i, retval, r);
174 15535
        vtc_log(v->vl, 3, "CLI RX  %u", retval);
175 15535
        vtc_dump(v->vl, 4, "CLI RX", r, -1);
176 15535
        if (repl != NULL)
177 9934
                *repl = r;
178 5601
        else
179 5601
                free(r);
180 15535
        return ((enum VCLI_status_e)retval);
181 15535
}
182
183
/**********************************************************************
184
 *
185
 */
186
187
static void
188
wait_stopped(const struct vinyl *v)
189 1083
{
190 1083
        char *r = NULL;
191 1083
        enum VCLI_status_e st;
192
193 1083
        vtc_log(v->vl, 3, "wait-stopped");
194 1085
        while (1) {
195 1085
                st = vinyl_ask_cli(v, "status", &r);
196 1085
                if (st != CLIS_OK)
197 0
                        vinyl_fatal(v,
198 1085
                            "CLI status command failed: %u %s", st, r);
199 1085
                if (!vstrcmp(r, "Child in state stopped")) {
200 1084
                        free(r);
201 1084
                        break;
202 1084
                }
203 1
                free(r);
204 1
                r = NULL;
205 1
                VTIM_sleep(0.2);
206 1
        }
207 1083
}
208
/**********************************************************************
209
 *
210
 */
211
212
static void
213
wait_running(const struct vinyl *v)
214 1013
{
215 1013
        char *r = NULL;
216 1013
        enum VCLI_status_e st;
217
218 1013
        while (1) {
219 1013
                vtc_log(v->vl, 3, "wait-running");
220 1013
                st = vinyl_ask_cli(v, "status", &r);
221 1013
                if (st != CLIS_OK)
222 0
                        vinyl_fatal(v,
223 1013
                            "CLI status command failed: %u %s", st, r);
224 1013
                if (!vstrcmp(r, "Child in state stopped"))
225 0
                        vinyl_fatal(v,
226 1013
                            "Child stopped before running: %u %s", st, r);
227 1013
                if (!vstrcmp(r, "Child in state running")) {
228 1013
                        free(r);
229 1013
                        r = NULL;
230 1013
                        st = vinyl_ask_cli(v, "debug.listen_address", &r);
231 1013
                        if (st != CLIS_OK)
232 0
                                vinyl_fatal(v,
233 1013
                                    "CLI status command failed: %u %s", st, r);
234 1013
                        free(r);
235 1013
                        break;
236 1013
                }
237 0
                free(r);
238 0
                r = NULL;
239 0
                VTIM_sleep(0.2);
240 0
        }
241 1013
}
242
243
/**********************************************************************
244
 * Vinyllog gatherer thread
245
 */
246
247
static void *
248
vinyllog_thread(void *priv)
249 1025
{
250 1025
        struct vinyl *v;
251 1025
        struct VSL_data *vsl;
252 1025
        struct vsm *vsm;
253 1025
        struct VSL_cursor *c;
254 1025
        enum VSL_tag_e tag;
255 1025
        uint64_t vxid;
256 1025
        unsigned len, vs;
257 1025
        const char *tagname, *data;
258 1025
        int type, i, opt;
259 1025
        struct vsb *vsb;
260
261 1025
        CAST_OBJ_NOTNULL(v, priv, VINYL_MAGIC);
262
263 1025
        vsl = VSL_New();
264 1025
        AN(vsl);
265 1025
        vsm = v->vsm_vsl;
266
267 1025
        vsb = VSB_new_auto();
268 1025
        AN(vsb);
269
270 1025
        c = NULL;
271 1025
        opt = 0;
272 30485
        while (v->fds[1] > 0 || c != NULL) {    //lint !e845 bug in flint
273 29460
                if (c == NULL) {
274 5701
                        if (vtc_error)
275 0
                                break;
276 5701
                        VTIM_sleep(0.1);
277 5701
                        (void)VSM_Status(vsm);
278 5701
                        c = VSL_CursorVSM(vsl, vsm, opt);
279 5701
                        if (c == NULL) {
280 3540
                                vtc_log(v->vl, 3, "vsl|%s", VSL_Error(vsl));
281 3540
                                VSL_ResetError(vsl);
282 3540
                                continue;
283 3540
                        }
284 5701
                }
285 25920
                AN(c);
286
287 25920
                opt = VSL_COPT_TAIL;
288
289 380146
                while (1) {
290 380146
                        i = VSL_Next(c);
291 380146
                        if (i != 1)
292 25920
                                break;
293
294 354226
                        v->vsl_rec = 1;
295
296 354226
                        tag = VSL_TAG(c->rec.ptr);
297 354226
                        vxid = VSL_ID(c->rec.ptr);
298 354226
                        if (tag == SLT__Batch)
299 0
                                continue;
300 354226
                        tagname = VSL_tags[tag];
301 354226
                        len = VSL_LEN(c->rec.ptr);
302 354226
                        type = VSL_CLIENT(c->rec.ptr) ?
303 218743
                            'c' : VSL_BACKEND(c->rec.ptr) ?
304 101118
                            'b' : '-';
305 354226
                        data = VSL_CDATA(c->rec.ptr);
306 354226
                        v->vsl_tag_count[tag]++;
307 354226
                        if (VSL_tagflags[tag] & SLT_F_BINARY) {
308 1509
                                VSB_clear(vsb);
309 1509
                                VSB_quote(vsb, data, len, VSB_QUOTE_HEX);
310 1509
                                AZ(VSB_finish(vsb));
311
                                /* +2 to skip "0x" */
312 1509
                                vtc_log(v->vl, 4, "vsl| %10ju %-15s %c [%s]",
313 1509
                                    (uintmax_t)vxid, tagname, type,
314 1509
                                    VSB_data(vsb) + 2);
315 1509
                                continue;
316 1509
                        }
317 352717
                        if (VSL_tagflags[tag] & SLT_F_UNSAFE) {
318
                                // remove trailing NUL
319 42607
                                if (len > 1 && data[len - 1] == '\0')
320 42606
                                        len--;
321 42607
                                VSB_clear(vsb);
322 42607
                                VSB_quote(vsb, data, len, VSB_QUOTE_ESCHEX);
323 42607
                                AZ(VSB_finish(vsb));
324 42607
                                data = VSB_data(vsb);
325 42607
                                len = VSB_len(vsb);
326 42607
                        }
327 352717
                        vtc_log(v->vl, 4, "vsl| %10ju %-15s %c %.*s",
328 352717
                            (uintmax_t)vxid, tagname, type, (int)len,
329 352717
                            data);
330 352717
                }
331 25920
                if (i == 0) {
332
                        /* Nothing to do but wait */
333 25920
                        v->vsl_idle++;
334 25920
                        vs = VSM_Status(vsm) & VSM_WRK_MASK;
335 25920
                        if ((vs & ~VSM_WRK_CHANGED) != VSM_WRK_RUNNING) {
336
                                /* Abandoned - try reconnect */
337 2161
                                VSL_DeleteCursor(c);
338 2161
                                c = NULL;
339 23759
                        } else {
340 23759
                                VTIM_sleep(0.1);
341 23759
                        }
342 25920
                } else if (i == -2) {
343
                        /* Abandoned - try reconnect */
344 0
                        VSL_DeleteCursor(c);
345 0
                        c = NULL;
346 0
                } else
347 0
                        break;
348 25920
        }
349
350 1025
        if (c)
351 0
                VSL_DeleteCursor(c);
352 1025
        VSL_Delete(vsl);
353 1025
        VSB_destroy(&vsb);
354
355 1025
        return (NULL);
356 1025
}
357
358
/**********************************************************************
359
 * Allocate and initialize a vinyl
360
 */
361
362
static struct vinyl *
363
vinyl_new(const char *name)
364 1026
{
365 1026
        struct vinyl *v;
366 1026
        struct vsb *vsb;
367 1026
        char buf[1024];
368
369 1026
        ALLOC_OBJ(v, VINYL_MAGIC);
370 1026
        AN(v);
371 1026
        REPLACE(v->name, name);
372
373 1026
        REPLACE(v->jail, "");
374
375 1026
        v->vl = vtc_logopen("%s", name);
376 1026
        AN(v->vl);
377
378 1026
        vsb = macro_expandf(v->vl, "${tmpdir}/%s", name);
379 1026
        AN(vsb);
380 1026
        v->workdir = strdup(VSB_data(vsb));
381 1026
        AN(v->workdir);
382 1026
        VSB_destroy(&vsb);
383
384 1026
        bprintf(buf, "rm -rf %s ; mkdir -p %s", v->workdir, v->workdir);
385 1026
        AZ(system(buf));
386
387 1026
        v->args = VSB_new_auto();
388
389 1026
        v->cli_fd = -1;
390 1026
        VTAILQ_INSERT_TAIL(&vinyles, v, list);
391
392 1026
        return (v);
393 1026
}
394
395
/**********************************************************************
396
 * Delete a vinyl instance
397
 */
398
399
static void
400
vinyl_delete(struct vinyl *v)
401 1026
{
402
403 1026
        CHECK_OBJ_NOTNULL(v, VINYL_MAGIC);
404 1026
        vtc_logclose(v->vl);
405 1026
        free(v->name);
406 1026
        free(v->jail);
407 1026
        free(v->workdir);
408 1026
        VSB_destroy(&v->args);
409 1026
        if (v->vsc != NULL)
410 1025
                VSC_Destroy(&v->vsc, v->vsm_vsc);
411 1026
        if (v->vsm_vsc != NULL)
412 1025
                VSM_Destroy(&v->vsm_vsc);
413 1026
        if (v->vsm_vsl != NULL)
414 1025
                VSM_Destroy(&v->vsm_vsl);
415
416
        /*
417
         * We do not delete the workdir, it may contain stuff people
418
         * want (coredumps, shmlog/stats etc), and trying to divine
419
         * "may want" is just too much trouble.  Leave it around and
420
         * nuke it at the start of the next test-run.
421
         */
422
423
        /* XXX: MEMLEAK (?) */
424 1026
        FREE_OBJ(v);
425 1026
}
426
427
/**********************************************************************
428
 * Vinyl listener
429
 */
430
431
static void *
432
vinyl_thread(void *priv)
433 1025
{
434 1025
        struct vinyl *v;
435
436 1025
        CAST_OBJ_NOTNULL(v, priv, VINYL_MAGIC);
437 1025
        return (vtc_record(v->vl, v->fds[0], NULL));
438 1025
}
439
440
/**********************************************************************
441
 * Launch a Vinyl
442
 */
443
444
static void
445
vinyl_launch(struct vinyl *v)
446 1025
{
447 1025
        struct vsb *vsb, *vsb1;
448 1025
        int i, nfd, asock;
449 1025
        char abuf[VTCP_ADDRBUFSIZE];
450 1025
        char pbuf[VTCP_PORTBUFSIZE];
451 1025
        char lbuf[PATH_MAX];
452 1025
        struct pollfd fd[3];
453 1025
        enum VCLI_status_e u;
454 1025
        const char *err;
455 1025
        char *r = NULL;
456
457
        /* Create listener socket */
458 1025
        asock = VTCP_listen_on(default_listen_addr, NULL, 1, &err);
459 1025
        if (err != NULL)
460 0
                vinyl_fatal(v, "Create CLI listen socket failed: %s", err);
461 1025
        assert(asock > 0);
462 1025
        VTCP_myname(asock, abuf, sizeof abuf, pbuf, sizeof pbuf);
463
464 1025
        AZ(VSB_finish(v->args));
465 1025
        vtc_log(v->vl, 2, "Launch");
466 1025
        vsb = VSB_new_auto();
467 1025
        AN(vsb);
468 1025
        VSB_cat(vsb, "cd ${pwd} &&");
469 1025
        VSB_printf(vsb, " exec vinyld %s -d -n %s -i %s",
470 1025
            v->jail, v->workdir, v->name);
471 1025
        if (macro_isdef(NULL, "vinyld_args_prepend")) {
472 0
                VSB_putc(vsb, ' ');
473 0
                macro_cat(v->vl, vsb, "vinyld_args_prepend", NULL);
474 0
        }
475 1025
        VSB_cat(vsb, VSB_data(params_vsb));
476 1025
        if (leave_temp) {
477 0
                VSB_cat(vsb, " -p debug=+vcl_keep");
478 0
                VSB_cat(vsb, " -p debug=+vmod_so_keep");
479 0
                VSB_cat(vsb, " -p debug=+vsm_keep");
480 0
        }
481 1025
        VSB_cat(vsb, " -l 2m");
482 1025
        VSB_cat(vsb, " -p auto_restart=off");
483 1025
        VSB_cat(vsb, " -p syslog_cli_traffic=off");
484 1025
        VSB_cat(vsb, " -p thread_pool_min=10");
485 1025
        VSB_cat(vsb, " -p debug=+vtc_mode");
486 1025
        VSB_cat(vsb, " -p vsl_mask=+Debug,+H2RxHdr,+H2RxBody");
487 1025
        VSB_cat(vsb, " -p h2_initial_window_size=1m");
488 1025
        VSB_cat(vsb, " -p h2_rx_window_low_water=64k");
489 1025
        if (!v->has_a_arg) {
490 986
                VSB_printf(vsb, " -a '%s'", default_listen_addr);
491 986
                if (v->proto != NULL)
492 16
                        VSB_printf(vsb, ",%s", v->proto);
493 986
        }
494 1025
        VSB_printf(vsb, " -M '%s %s'", abuf, pbuf);
495 1025
        VSB_printf(vsb, " -P %s/%sd.pid", v->workdir, v->me);
496 1025
        if (vmod_path != NULL)
497 1024
                VSB_printf(vsb, " -p vmod_path=%s", vmod_path);
498 1025
        VSB_printf(vsb, " %s", VSB_data(v->args));
499 1025
        if (macro_isdef(NULL, "vinyld_args_append")) {
500 0
                VSB_putc(vsb, ' ');
501 0
                macro_cat(v->vl, vsb, "vinyld_args_append", NULL);
502 0
        }
503 1025
        AZ(VSB_finish(vsb));
504 1025
        vtc_log(v->vl, 3, "CMD: %s", VSB_data(vsb));
505 1025
        vsb1 = macro_expand(v->vl, VSB_data(vsb));
506 1025
        AN(vsb1);
507 1025
        VSB_destroy(&vsb);
508 1025
        vsb = vsb1;
509 1025
        vtc_log(v->vl, 3, "CMD: %s", VSB_data(vsb));
510 1025
        AZ(pipe(&v->fds[0]));
511 1025
        AZ(pipe(&v->fds[2]));
512 1025
        v->pid = fork();
513 1025
        assert(v->pid >= 0);
514 1025
        if (v->pid == 0) {
515 1025
                AZ(dup2(v->fds[0], 0));
516 1025
                assert(dup2(v->fds[3], 1) == 1);
517 1025
                assert(dup2(1, 2) == 2);
518 1025
                closefd(&v->fds[0]);
519 1025
                closefd(&v->fds[1]);
520 1025
                closefd(&v->fds[2]);
521 1025
                closefd(&v->fds[3]);
522 1025
                VSUB_closefrom(STDERR_FILENO + 1);
523 1025
                AZ(execl("/bin/sh", "/bin/sh", "-c", VSB_data(vsb), (char*)0));
524 1025
                exit(1);
525 1025
        } else {
526 0
                vtc_log(v->vl, 3, "PID: %ld", (long)v->pid);
527 0
                macro_def(v->vl, v->name, "pid", "%ld", (long)v->pid);
528 0
                macro_def(v->vl, v->name, "name", "%s", v->workdir);
529 0
        }
530 0
        closefd(&v->fds[0]);
531 0
        closefd(&v->fds[3]);
532 0
        v->fds[0] = v->fds[2];
533 0
        v->fds[2] = v->fds[3] = -1;
534 0
        VSB_destroy(&vsb);
535 0
        AZ(v->tp_started);
536 0
        v->tp_started = 1;
537 0
        PTOK(pthread_create(&v->tp, NULL, vinyl_thread, v));
538
539
        /* Wait for the vinyl to call home */
540 1025
        memset(fd, 0, sizeof fd);
541 1025
        fd[0].fd = asock;
542 1025
        fd[0].events = POLLIN;
543 1025
        fd[1].fd = v->fds[1];
544 1025
        fd[1].events = POLLIN;
545 1025
        fd[2].fd = v->fds[2];
546 1025
        fd[2].events = POLLIN;
547 1025
        i = poll(fd, 2, (int)(vtc_maxdur * 1000 / 3));
548 1025
        vtc_log(v->vl, 4, "CLIPOLL %d 0x%x 0x%x 0x%x",
549 1025
            i, fd[0].revents, fd[1].revents, fd[2].revents);
550 1025
        if (i == 0)
551 0
                vinyl_fatal(v, "FAIL timeout waiting for CLI connection");
552 1025
        if (fd[1].revents & POLLHUP)
553 0
                vinyl_fatal(v, "FAIL debug pipe closed");
554 1025
        if (!(fd[0].revents & POLLIN))
555 0
                vinyl_fatal(v, "FAIL CLI connection wait failure");
556 1025
        nfd = accept(asock, NULL, NULL);
557 1025
        closefd(&asock);
558 1025
        if (nfd < 0)
559 0
                vinyl_fatal(v, "FAIL no CLI connection accepted");
560
561 1025
        v->cli_fd = nfd;
562
563 1025
        vtc_log(v->vl, 3, "CLI connection fd = %d", v->cli_fd);
564 1025
        assert(v->cli_fd >= 0);
565
566
        /* Receive the banner or auth response */
567 1025
        u = vinyl_ask_cli(v, NULL, &r);
568 1025
        if (vtc_error)
569 0
                return;
570 1025
        if (u != CLIS_AUTH)
571 0
                vinyl_fatal(v, "CLI auth demand expected: %u %s", u, r);
572
573 1025
        bprintf(lbuf, "%s/_.secret", v->workdir);
574 1025
        nfd = open(lbuf, O_RDONLY);
575 1025
        assert(nfd >= 0);
576
577 1025
        assert(sizeof lbuf >= CLI_AUTH_RESPONSE_LEN + 7);
578 1025
        bstrcpy(lbuf, "auth ");
579 1025
        VCLI_AuthResponse(nfd, r, lbuf + 5);
580 1025
        closefd(&nfd);
581 1025
        free(r);
582 1025
        r = NULL;
583 1025
        strcat(lbuf, "\n");
584
585 1025
        u = vinyl_ask_cli(v, lbuf, &r);
586 1025
        if (vtc_error)
587 0
                return;
588 1025
        if (u != CLIS_OK)
589 0
                vinyl_fatal(v, "CLI auth command failed: %u %s", u, r);
590 1025
        free(r);
591
592 1025
        v->vsm_vsc = VSM_New();
593 1025
        AN(v->vsm_vsc);
594 1025
        v->vsc = VSC_New();
595 1025
        AN(v->vsc);
596 1025
        assert(VSM_Arg(v->vsm_vsc, 'n', v->workdir) > 0);
597 1025
        AZ(VSM_Attach(v->vsm_vsc, -1));
598
599 1025
        v->vsm_vsl = VSM_New();
600 1025
        assert(VSM_Arg(v->vsm_vsl, 'n', v->workdir) > 0);
601 1025
        AZ(VSM_Attach(v->vsm_vsl, -1));
602
603 1025
        PTOK(pthread_create(&v->tp_vsl, NULL, vinyllog_thread, v));
604 1025
}
605
606
#define VINYL_LAUNCH(v)                         \
607 6245
        do {                                            \
608 6245
                CHECK_OBJ_NOTNULL(v, VINYL_MAGIC);      \
609 6245
                if (v->cli_fd < 0)                      \
610 6245
                        vinyl_launch(v);                \
611 6245
                if (vtc_error)                          \
612 6245
                        return;                         \
613 6245
        } while (0)
614
615
/**********************************************************************
616
 * Start a Vinyl
617
 */
618
619
static void
620
vinyl_listen(const struct vinyl *v, char *la)
621 1010
{
622 1010
        const char *a, *p, *n, *n2;
623 1010
        char m[64], s[256];
624 1010
        unsigned first;
625
626 1010
        n2 = "";
627 1010
        first = 1;
628
629 2028
        while (*la != '\0') {
630 1018
                n = la;
631 1018
                la = strchr(la, ' ');
632 1018
                AN(la);
633 1018
                *la = '\0';
634 1018
                a = ++la;
635 1018
                la = strchr(la, ' ');
636 1018
                AN(la);
637 1018
                *la = '\0';
638 1018
                p = ++la;
639 1018
                la = strchr(la, '\n');
640 1018
                AN(la);
641 1018
                *la = '\0';
642 1018
                la++;
643
644 1018
                AN(*n);
645 1018
                AN(*a);
646 1018
                AN(*p);
647
648 1018
                if (*p == '-') {
649 32
                        bprintf(s, "%s", a);
650 32
                        a = "0.0.0.0";
651 32
                        p = "0";
652 986
                } else if (strchr(a, ':')) {
653 0
                        bprintf(s, "[%s]:%s", a, p);
654 986
                } else {
655 986
                        bprintf(s, "%s:%s", a, p);
656 986
                }
657
658 1018
                if (first) {
659 1010
                        vtc_log(v->vl, 2, "Listen on %s %s", a, p);
660 1010
                        macro_def(v->vl, v->name, "addr", "%s", a);
661 1010
                        macro_def(v->vl, v->name, "port", "%s", p);
662 1010
                        macro_def(v->vl, v->name, "sock", "%s", s);
663 1010
                        first = 0;
664 1010
                }
665
666 1018
                if (!vstrcmp(n, n2))
667 0
                        continue;
668
669 1018
                bprintf(m, "%s_addr", n);
670 1018
                macro_def(v->vl, v->name, m, "%s", a);
671 1018
                bprintf(m, "%s_port", n);
672 1018
                macro_def(v->vl, v->name, m, "%s", p);
673 1018
                bprintf(m, "%s_sock", n);
674 1018
                macro_def(v->vl, v->name, m, "%s", s);
675 1018
                n2 = n;
676 1018
        }
677 1010
}
678
679
static void
680
vinyl_start(struct vinyl *v)
681 1010
{
682 1010
        enum VCLI_status_e u;
683 1010
        char *resp = NULL;
684
685 1010
        VINYL_LAUNCH(v);
686 1010
        vtc_log(v->vl, 2, "Start");
687 1010
        u = vinyl_ask_cli(v, "start", &resp);
688 1010
        if (vtc_error)
689 0
                return;
690 1010
        if (u != CLIS_OK)
691 0
                vinyl_fatal(v, "CLI start command failed: %u %s", u, resp);
692 1010
        wait_running(v);
693 1010
        free(resp);
694 1010
        resp = NULL;
695 1010
        u = vinyl_ask_cli(v, "debug.xid 1000", &resp);
696 1010
        if (vtc_error)
697 0
                return;
698 1010
        if (u != CLIS_OK)
699 0
                vinyl_fatal(v, "CLI debug.xid command failed: %u %s",
700 1010
                    u, resp);
701 1010
        free(resp);
702 1010
        resp = NULL;
703 1010
        u = vinyl_ask_cli(v, "debug.listen_address", &resp);
704 1010
        if (vtc_error)
705 0
                return;
706 1010
        if (u != CLIS_OK)
707 0
                vinyl_fatal(v,
708 1010
                    "CLI debug.listen_address command failed: %u %s", u, resp);
709 1010
        vinyl_listen(v, resp);
710 1010
        free(resp);
711
        /* Wait for vsl logging to get underway */
712 1010
        while (v->vsl_rec == 0)
713 0
                VTIM_sleep(.1);
714 1010
}
715
716
/**********************************************************************
717
 * Stop a Vinyl
718
 */
719
720
static void
721
vinyl_stop(struct vinyl *v)
722 1081
{
723
724 1081
        VINYL_LAUNCH(v);
725 1081
        vtc_log(v->vl, 2, "Stop");
726 1081
        (void)vinyl_ask_cli(v, "stop", NULL);
727 1081
        wait_stopped(v);
728 1081
}
729
730
/**********************************************************************
731
 * Cleanup
732
 */
733
734
static void
735
vinyl_cleanup(struct vinyl *v)
736 1025
{
737 1025
        void *p;
738
739
        /* Close the CLI connection */
740 1025
        closefd(&v->cli_fd);
741
742
        /* Close the STDIN connection. */
743 1025
        closefd(&v->fds[1]);
744
745
        /* Wait until STDOUT+STDERR closes */
746 1025
        AN(v->tp_started);
747 1025
        PTOK(pthread_join(v->tp, &p));
748 1025
        closefd(&v->fds[0]);
749 1025
        v->tp_started = 0;
750
751
        /* Pick up the VSL thread */
752 1025
        PTOK(pthread_join(v->tp_vsl, &p));
753
754 1025
        vtc_wait4(v->vl, v->pid, v->expect_exit, 0, 0);
755 1025
        v->pid = 0;
756 1025
}
757
758
/**********************************************************************
759
 * Wait for a Vinyl
760
 */
761
762
static void
763
vinyl_wait(struct vinyl *v)
764 1022
{
765 1022
        if (v->cli_fd < 0)
766 0
                return;
767
768 1022
        vtc_log(v->vl, 2, "Wait");
769
770 1022
        if (!vtc_error) {
771
                /* Do a backend.list to log if child is still running */
772 1022
                (void)vinyl_ask_cli(v, "backend.list", NULL);
773 1022
        }
774
775
        /* Then stop it */
776 1022
        vinyl_stop(v);
777
778 1022
        if (vinyl_ask_cli(v, "panic.show", NULL) != CLIS_CANT)
779 0
                vinyl_fatal(v, "Unexpected panic");
780
781 1022
        vinyl_cleanup(v);
782 1022
}
783
784
785
/**********************************************************************
786
 * Ask a CLI JSON question
787
 */
788
789
static void
790
vinyl_cli_json(struct vinyl *v, const char *cli)
791 38
{
792 38
        enum VCLI_status_e u;
793 38
        char *resp = NULL;
794 38
        const char *errptr;
795 38
        struct vjsn *vj;
796
797 38
        VINYL_LAUNCH(v);
798 38
        u = vinyl_ask_cli(v, cli, &resp);
799 38
        vtc_log(v->vl, 2, "CLI %03u <%s>", u, cli);
800 38
        if (u != CLIS_OK)
801 0
                vinyl_fatal(v,
802 38
                    "FAIL CLI response %u expected %u", u, CLIS_OK);
803 38
        vj = vjsn_parse(resp, &errptr);
804 38
        if (vj == NULL)
805 0
                vinyl_fatal(v, "FAIL CLI, not good JSON: %s", errptr);
806 38
        vjsn_delete(&vj);
807 38
        free(resp);
808 38
}
809
810
/**********************************************************************
811
 * Ask a CLI question
812
 */
813
814
static void
815
vinyl_cli(struct vinyl *v, const char *cli, unsigned exp, const char *re,
816
    int neg)
817 1353
{
818 1353
        enum VCLI_status_e u;
819 1353
        struct vsb vsb[1];
820 1353
        vre_t *vre = NULL;
821 1353
        char *resp = NULL, errbuf[VRE_ERROR_LEN];
822 1353
        int err, erroff;
823
824 1353
        VINYL_LAUNCH(v);
825 1353
        if (re != NULL) {
826 116
                vre = VRE_compile(re, 0, &err, &erroff, 1);
827 116
                if (vre == NULL) {
828 0
                        AN(VSB_init(vsb, errbuf, sizeof errbuf));
829 0
                        AZ(VRE_error(vsb, err));
830 0
                        AZ(VSB_finish(vsb));
831 0
                        VSB_fini(vsb);
832 0
                        vinyl_fatal(v, "Illegal regexp: %s (@%d)",
833 0
                            errbuf, erroff);
834 0
                }
835 116
        }
836 1353
        u = vinyl_ask_cli(v, cli, &resp);
837 1353
        vtc_log(v->vl, 2, "CLI %03u <%s>", u, cli);
838 1353
        if (exp != 0 && exp != (unsigned)u)
839 0
                vinyl_fatal(v, "FAIL CLI response %u expected %u", u, exp);
840 1353
        if (vre != NULL) {
841 116
                err = VRE_match(vre, resp, 0, 0, NULL);
842 116
                if (err < VRE_ERROR_NOMATCH) {
843 0
                        AN(VSB_init(vsb, errbuf, sizeof errbuf));
844 0
                        AZ(VRE_error(vsb, err));
845 0
                        AZ(VSB_finish(vsb));
846 0
                        VSB_fini(vsb);
847 0
                        vinyl_fatal(v, "Regexp failed: %s with %s (%d)",
848 0
                            re, errbuf, err);
849 116
                } else if ((err == VRE_ERROR_NOMATCH) == (neg == 0)) { /*lint !e731 */
850 0
                        vinyl_fatal(v, "Expect failed: regexp \"%s\" did%s "
851 0
                            "match response \"%s\"", re, neg ? "" : " not",
852 0
                            resp);
853 0
                }
854 116
                VRE_free(&vre);
855 116
        }
856 1353
        free(resp);
857 1353
}
858
859
static const char *
860
vcl_prepend(void)
861 1590
{
862 1590
        const char *vcl_pfx;
863
864 1590
        vcl_pfx = getenv("VTEST_VINYL_VCL_PREPEND");
865 1590
        return vcl_pfx ? vcl_pfx : "";
866 1590
}
867
868
/**********************************************************************
869
 * Load a VCL program
870
 */
871
872
static void
873
vinyl_vcl(struct vinyl *v, const char *vcl, int fail, char **resp)
874 661
{
875 661
        struct vsb *vsb;
876 661
        enum VCLI_status_e u;
877
878 661
        VINYL_LAUNCH(v);
879 661
        vsb = VSB_new_auto();
880 661
        AN(vsb);
881
882 661
        VSB_printf(vsb, "vcl.inline vcl%d << %s\nvcl %.1f;\n%s%s\n%s\n",
883 661
            ++v->vcl_nbr, NONSENSE, v->syntax,
884 661
            vcl_prepend(), vcl, NONSENSE);
885 661
        AZ(VSB_finish(vsb));
886
887 661
        u = vinyl_ask_cli(v, VSB_data(vsb), resp);
888 661
        if (u == CLIS_OK) {
889 309
                VSB_clear(vsb);
890 309
                VSB_printf(vsb, "vcl.use vcl%d", v->vcl_nbr);
891 309
                AZ(VSB_finish(vsb));
892 309
                u = vinyl_ask_cli(v, VSB_data(vsb), NULL);
893 309
        }
894 661
        if (u == CLIS_OK && fail) {
895 0
                VSB_destroy(&vsb);
896 0
                vinyl_fatal(v, "VCL compilation succeeded expected failure");
897 661
        } else if (u != CLIS_OK && !fail) {
898 0
                VSB_destroy(&vsb);
899 0
                vinyl_fatal(v, "VCL compilation failed expected success");
900 661
        } else if (fail)
901 352
                vtc_log(v->vl, 2, "VCL compilation failed (as expected)");
902 661
        VSB_destroy(&vsb);
903 661
}
904
905
/**********************************************************************
906
 * Load a VCL program prefixed by backend decls for our servers
907
 */
908
909
static void
910
vinyl_vclbackend(struct vinyl *v, const char *vcl)
911 929
{
912 929
        struct vsb *vsb, *vsb2;
913 929
        enum VCLI_status_e u;
914
915 929
        VINYL_LAUNCH(v);
916 929
        vsb = VSB_new_auto();
917 929
        AN(vsb);
918
919 929
        vsb2 = VSB_new_auto();
920 929
        AN(vsb2);
921
922 929
        VSB_printf(vsb2, "vcl %.1f;\n", v->syntax);
923
924 929
        cmd_server_gen_vcl(vsb2);
925
926 929
        AZ(VSB_finish(vsb2));
927
928 929
        VSB_printf(vsb, "vcl.inline vcl%d << %s\n%s\n%s%s\n%s\n",
929 929
            ++v->vcl_nbr, NONSENSE, VSB_data(vsb2), vcl_prepend(),
930 929
            vcl, NONSENSE);
931 929
        AZ(VSB_finish(vsb));
932
933 929
        u = vinyl_ask_cli(v, VSB_data(vsb), NULL);
934 929
        if (u != CLIS_OK) {
935 0
                VSB_destroy(&vsb);
936 0
                VSB_destroy(&vsb2);
937 0
                vinyl_fatal(v, "FAIL VCL does not compile");
938 0
        }
939 929
        VSB_clear(vsb);
940 929
        VSB_printf(vsb, "vcl.use vcl%d", v->vcl_nbr);
941 929
        AZ(VSB_finish(vsb));
942 929
        u = vinyl_ask_cli(v, VSB_data(vsb), NULL);
943 929
        assert(u == CLIS_OK);
944 929
        VSB_destroy(&vsb);
945 929
        VSB_destroy(&vsb2);
946 929
}
947
948
/**********************************************************************
949
 */
950
951
struct dump_priv {
952
        const char *arg;
953
        const struct vinyl *v;
954
};
955
956
static int
957
do_stat_dump_cb(void *priv, const struct VSC_point * const pt)
958 4154
{
959 4154
        const struct vinyl *v;
960 4154
        struct dump_priv *dp;
961 4154
        uint64_t u;
962
963 4154
        if (pt == NULL)
964 0
                return (0);
965 4154
        dp = priv;
966 4154
        v = dp->v;
967
968 4154
        if (vstrcmp(pt->ctype, "uint64_t"))
969 0
                return (0);
970 4154
        u = VSC_Value(pt);
971
972 4154
        if (vstrcmp(dp->arg, "*")) {
973 4154
                if (fnmatch(dp->arg, pt->name, 0))
974 4015
                        return (0);
975 4154
        }
976
977 139
        vtc_log(v->vl, 4, "VSC %s %ju",  pt->name, (uintmax_t)u);
978 139
        return (0);
979 4154
}
980
981
static void
982
vinyl_vsc(struct vinyl *v, const char *arg)
983 10
{
984 10
        struct dump_priv dp;
985
986 10
        VINYL_LAUNCH(v);
987 10
        memset(&dp, 0, sizeof dp);
988 10
        dp.v = v;
989 10
        dp.arg = arg;
990 10
        (void)VSM_Status(v->vsm_vsc);
991 10
        (void)VSC_Iter(v->vsc, v->vsm_vsc, do_stat_dump_cb, &dp);
992 10
}
993
994
/**********************************************************************
995
 * Check statistics
996
 */
997
998
struct stat_arg {
999
        const char      *pattern;
1000
        uintmax_t       val;
1001
        unsigned        good;
1002
};
1003
1004
struct stat_priv {
1005
        struct stat_arg lhs;
1006
        struct stat_arg rhs;
1007
};
1008
1009
static int
1010
stat_match(const char *pattern, const char *name)
1011 302118
{
1012
1013 302118
        if (strchr(pattern, '.') == NULL) {
1014 67018
                if (fnmatch("MAIN.*", name, 0))
1015 4725
                        return (FNM_NOMATCH);
1016 62293
                name += 5;
1017 62293
        }
1018 297393
        return (fnmatch(pattern, name, 0));
1019 302118
}
1020
1021
static int
1022
do_expect_cb(void *priv, const struct VSC_point * const pt)
1023 301957
{
1024 301957
        struct stat_priv *sp = priv;
1025
1026 301957
        if (pt == NULL)
1027 0
                return (0);
1028
1029 301957
        if (!sp->lhs.good && stat_match(sp->lhs.pattern, pt->name) == 0) {
1030 1129
                AZ(vstrcmp(pt->ctype, "uint64_t"));
1031 1129
                AN(pt->ptr);
1032 1129
                sp->lhs.val = VSC_Value(pt);
1033 1129
                sp->lhs.good = 1;
1034 1129
        }
1035
1036 301957
        if (sp->rhs.pattern == NULL) {
1037 301792
                sp->rhs.good = 1;
1038 301792
        } else if (!sp->rhs.good &&
1039 165
            stat_match(sp->rhs.pattern, pt->name) == 0) {
1040 4
                AZ(vstrcmp(pt->ctype, "uint64_t"));
1041 4
                AN(pt->ptr);
1042 4
                sp->rhs.val = VSC_Value(pt);
1043 4
                sp->rhs.good = 1;
1044 4
        }
1045
1046 301957
        return (sp->lhs.good && sp->rhs.good);
1047 301957
}
1048
1049
/**********************************************************************
1050
 */
1051
1052
static void
1053
vinyl_expect(struct vinyl *v, char * const *av)
1054 1014
{
1055 1014
        struct stat_priv sp;
1056 1014
        int good, i, not;
1057 1014
        uintmax_t u;
1058 1014
        char *l, *p;
1059
1060 1014
        VINYL_LAUNCH(v);
1061 1014
        ZERO_OBJ(&sp, sizeof sp);
1062 1014
        l = av[0];
1063 1014
        not = (*l == '!');
1064 1014
        if (not) {
1065 5
                l++;
1066 5
                AZ(av[1]);
1067 1009
        } else {
1068 1009
                AN(av[1]);
1069 1009
                AN(av[2]);
1070 1009
                errno = 0;
1071 1009
                u = strtoumax(av[2], &p, 0);
1072 1009
                if (errno != ERANGE && *p == '\0')
1073 1005
                        sp.rhs.val = u;
1074 4
                else
1075 4
                        sp.rhs.pattern = av[2];
1076 1009
        }
1077
1078 1014
        sp.lhs.pattern = l;
1079
1080 1384
        for (i = 0; i < 50; i++, (void)usleep(100000)) {
1081 1379
                (void)VSM_Status(v->vsm_vsc);
1082 1379
                sp.lhs.good = sp.rhs.good = 0;
1083 1379
                good = VSC_Iter(v->vsc, v->vsm_vsc, do_expect_cb, &sp);
1084 1379
                if (!good)
1085 250
                        good = -2;
1086 1379
                if (good < 0)
1087 250
                        continue;
1088
1089 1129
                if (not)
1090 0
                        vinyl_fatal(v, "Found (not expected): %s", l);
1091
1092 1129
                good = -1;
1093 1129
                if (!strcmp(av[1], "==")) good = (sp.lhs.val == sp.rhs.val);
1094 1129
                if (!strcmp(av[1], "!=")) good = (sp.lhs.val != sp.rhs.val);
1095 1129
                if (!strcmp(av[1], ">" )) good = (sp.lhs.val >  sp.rhs.val);
1096 1129
                if (!strcmp(av[1], "<" )) good = (sp.lhs.val <  sp.rhs.val);
1097 1129
                if (!strcmp(av[1], ">=")) good = (sp.lhs.val >= sp.rhs.val);
1098 1129
                if (!strcmp(av[1], "<=")) good = (sp.lhs.val <= sp.rhs.val);
1099 1129
                if (good == -1)
1100 0
                        vinyl_fatal(v, "comparison %s unknown", av[1]);
1101 1129
                if (good)
1102 1009
                        break;
1103 1129
        }
1104 1014
        if (good == -1) {
1105 0
                vinyl_fatal(v, "VSM error: %s", VSM_Error(v->vsm_vsc));
1106 0
        }
1107 1014
        if (good == -2) {
1108 5
                if (not) {
1109 5
                        vtc_log(v->vl, 2, "not found (as expected): %s", l);
1110 5
                        return;
1111 5
                }
1112 0
                vinyl_fatal(v, "stats field %s unknown",
1113 0
                    sp.lhs.good ? sp.rhs.pattern : sp.lhs.pattern);
1114 0
        }
1115
1116 1009
        if (good == 1) {
1117 1009
                vtc_log(v->vl, 2, "as expected: %s (%ju) %s %s (%ju)",
1118 1009
                    av[0], sp.lhs.val, av[1], av[2], sp.rhs.val);
1119 1009
        } else {
1120 0
                vinyl_fatal(v, "Not true: %s (%ju) %s %s (%ju)",
1121 0
                    av[0], sp.lhs.val, av[1], av[2], sp.rhs.val);
1122 0
        }
1123 1009
}
1124
1125
static void
1126
vsl_catchup(struct vinyl *v)
1127 149
{
1128 149
        int vsl_idle;
1129
1130 149
        VINYL_LAUNCH(v);
1131 149
        vsl_idle = v->vsl_idle;
1132 298
        while (!vtc_error && vsl_idle == v->vsl_idle)
1133 149
                VTIM_sleep(0.1);
1134 149
}
1135
1136
/* SECTION: vinyl vinyl
1137
 *
1138
 * Define and interact with vinyl instances.
1139
 *
1140
 * To define a ``vinyld``, you'll use this syntax::
1141
 *
1142
 *      vinyl vNAME [-arg STRING] [-vcl STRING] [-vcl+backend STRING]
1143
 *              [-errvcl STRING STRING] [-jail STRING] [-proto PROXY]
1144
 *
1145
 * The first ``vinyl vNAME`` invocation will start the vinyld master
1146
 * process in the background, waiting for the ``-start`` switch to actually
1147
 * start the child.
1148
 *
1149
 * Types used in the description below:
1150
 *
1151
 * PATTERN
1152
 *         is a 'glob' style pattern (ie: fnmatch(3)) as used in shell filename
1153
 *         expansion.
1154
 *
1155
 * Arguments:
1156
 *
1157
 * vNAME
1158
 *         Identify the ``vinyld`` server with a string, it must starts with 'v'.
1159
 *
1160
 * \-arg STRING
1161
 *         Pass an argument to vinyld, for example "-h simple_list".
1162
 *
1163
 *         If the ${vinyld_args_prepend} or ${vinyld_args_append} macros are
1164
 *         defined, they are expanded and inserted before / appended to the
1165
 *         vinyld command line as constructed by vinyltest, before the
1166
 *         command line itself is expanded. This enables tweaks to the vinyld
1167
 *         command line without editing test cases. This macros can be defined
1168
 *         using the ``-D`` option for vinyltest.
1169
 *
1170
 * \-vcl STRING
1171
 *         Specify the VCL to load on this ``vinyld`` instance. You'll probably
1172
 *         want to use multi-lines strings for this ({...}).
1173
 *
1174
 * \-vcl+backend STRING
1175
 *         Do the exact same thing as -vcl, but adds the definition block of
1176
 *         known backends (ie. already defined).
1177
 *
1178
 * \-errvcl STRING1 STRING2
1179
 *         Load STRING2 as VCL, expecting it to fail, and ``vinyld`` to send an
1180
 *         error string matching STRING1
1181
 *
1182
 * \-jail STRING
1183
 *         Look at ``man vinyld`` (-j) for more information.
1184
 *
1185
 * \-proto PROXY
1186
 *         Have ``vinyld`` use the proxy protocol. Note that PROXY here is the
1187
 *         actual string.
1188
 *
1189
 * You can decide to start the ``vinyld`` instance and/or wait for several events::
1190
 *
1191
 *         vinyl vNAME [-start] [-wait] [-wait-running] [-wait-stopped]
1192
 *
1193
 * \-start
1194
 *         Start the child process.
1195
 *
1196
 *         Once successfully started, the following macros are available for
1197
 *         the default listen address: ``${vNAME_addr}``, ``${vNAME_port}``
1198
 *         and ``${vNAME_sock}``. Additional macros are available, including
1199
 *         the listen address name for each address vNAME listens to, like for
1200
 *         example: ``${vNAME_a0_addr}``.
1201
 *
1202
 * \-stop
1203
 *         Stop the child process.
1204
 *
1205
 * \-syntax
1206
 *         Set the VCL syntax level for this command (default: 4.1)
1207
 *
1208
 * \-wait
1209
 *         Wait for that instance to terminate.
1210
 *
1211
 * \-wait-running
1212
 *         Wait for the ``vinyld`` child process to be started.
1213
 *
1214
 * \-wait-stopped
1215
 *         Wait for the ``vinyld`` child process to stop.
1216
 *
1217
 * \-cleanup
1218
 *         Once ``vinyld`` is stopped, clean everything after it. This is only used
1219
 *         in very few tests and you should never need it.
1220
 *
1221
 * \-expectexit NUMBER
1222
 *         Expect vinyld to exit(3) with this value
1223
 *
1224
 * Once ``vinyld`` is started, you can talk to it (as you would through
1225
 * ``vinyladm``) with these additional switches::
1226
 *
1227
 *         vinyl vNAME [-cli STRING] [-cliok STRING] [-clierr STRING]
1228
 *                       [-clijson STRING]
1229
 *
1230
 * \-cli STRING|-cliok STRING|-clierr STATUS STRING|-cliexpect [!] REGEXP STRING
1231
 *         All four of these will send STRING to the CLI, the only difference
1232
 *         is what they expect the result to be. -cli doesn't expect
1233
 *         anything, -cliok expects 200, -clierr expects STATUS, and
1234
 *         -cliexpect expects the REGEXP to match the returned response.
1235
 *         -cliexpect ! ... negates the match
1236
 *
1237
 * \-clijson STRING
1238
 *         Send STRING to the CLI, expect success (CLIS_OK/200) and check
1239
 *         that the response is parsable JSON.
1240
 *
1241
 * It is also possible to interact with its shared memory (as you would
1242
 * through tools like ``vinylstat``) with additional switches:
1243
 *
1244
 * \-expect \!PATTERN|PATTERN OP NUMBER|PATTERN OP PATTERN
1245
 *         Look into the VSM and make sure the first VSC counter identified by
1246
 *         PATTERN has a correct value. OP can be ==, >, >=, <, <=. For
1247
 *         example::
1248
 *
1249
 *                 vinyl v1 -expect SM?.s1.g_space > 1000000
1250
 *                 vinyl v1 -expect cache_hit >= cache_hit_grace
1251
 *
1252
 *         In the \! form the test fails if a counter matches PATTERN.
1253
 *
1254
 *         The ``MAIN.`` namespace can be omitted from PATTERN.
1255
 *
1256
 *         The test takes up to 5 seconds before timing out.
1257
 *
1258
 * \-vsc PATTERN
1259
 *         Dump VSC counters matching PATTERN.
1260
 *
1261
 * \-vsl_catchup
1262
 *         Wait until the logging thread has idled to make sure that all
1263
 *         the generated log is flushed
1264
 *
1265
 * Environment variables:
1266
 *
1267
 * VTEST_VINYL_VCL_PREPEND can be used to inject VCL code into all VCL created
1268
 * by the vinyl command before VCL provided by the caller using the -vcl or
1269
 * -vcl+backend argument. That is, VTEST_VINYL_VCL_PREPEND is injected after the
1270
 * implicit "vcl x.y;" statement and backend definitions generated by the vinyl
1271
 * command.
1272
 */
1273
1274
void
1275
cmd_vinyl(CMD_ARGS)
1276 6509
{
1277 6509
        struct vinyl *v, *v2;
1278 6509
        const char *me;
1279
1280 6509
        (void)priv;
1281
1282 6509
        if (av == NULL) {
1283
                /* Reset and free */
1284 2244
                VTAILQ_FOREACH_SAFE(v, &vinyles, list, v2) {
1285 1026
                        if (v->cli_fd >= 0)
1286 1015
                                vinyl_wait(v);
1287 1026
                        VTAILQ_REMOVE(&vinyles, v, list);
1288 1026
                        vinyl_delete(v);
1289 1026
                }
1290 2244
                return;
1291 2244
        }
1292
1293 4265
        me = av[0];
1294
        // AZ(strcmp(av[0], "vinyl"));
1295 4265
        av++;
1296
1297 4265
        VTC_CHECK_NAME(vl, av[0], "Vinyl", 'v');
1298 4265
        VTAILQ_FOREACH(v, &vinyles, list)
1299 3357
                if (!vstrcmp(v->name, av[0]))
1300 3239
                        break;
1301 4265
        if (v == NULL) {
1302 1026
                v = vinyl_new(av[0]);
1303 1026
                v->me =strdup(me);
1304 1026
                AN(v->me);
1305 1026
        }
1306 4265
        av++;
1307 4265
        v->syntax = 4.1;
1308
1309 9918
        for (; *av != NULL; av++) {
1310 5654
                if (vtc_error)
1311 0
                        break;
1312 5654
                if (!vstrcmp(*av, "-arg")) {
1313 327
                        AN(av[1]);
1314 327
                        AZ(v->pid);
1315 327
                        VSB_cat(v->args, " ");
1316 327
                        VSB_cat(v->args, av[1]);
1317 327
                        if (av[1][0] == '-' && av[1][1] == 'a')
1318 41
                                v->has_a_arg = 1;
1319 327
                        av++;
1320 327
                        continue;
1321 327
                }
1322 5327
                if (!vstrcmp(*av, "-cleanup")) {
1323 3
                        AZ(av[1]);
1324 3
                        vinyl_cleanup(v);
1325 3
                        continue;
1326 3
                }
1327 5324
                if (!vstrcmp(*av, "-cli")) {
1328 48
                        AN(av[1]);
1329 48
                        vinyl_cli(v, av[1], 0, NULL, 0);
1330 48
                        av++;
1331 48
                        continue;
1332 48
                }
1333 5276
                if (!vstrcmp(*av, "-clierr")) {
1334 136
                        AN(av[1]);
1335 136
                        AN(av[2]);
1336 136
                        vinyl_cli(v, av[2], atoi(av[1]), NULL, 0);
1337 136
                        av += 2;
1338 136
                        continue;
1339 136
                }
1340 5140
                if (!vstrcmp(*av, "-cliexpect")) {
1341 116
                        int neg = 0;
1342
1343 116
                        AN(av[1]);
1344 116
                        AN(av[2]);
1345 116
                        if (*av[1] == '!') {
1346 2
                                neg = 1;
1347 2
                                av++;
1348 2
                                AN(av[2]);
1349 2
                        }
1350 116
                        vinyl_cli(v, av[2], 0, av[1], neg);
1351 116
                        av += 2;
1352 116
                        continue;
1353 116
                }
1354 5024
                if (!vstrcmp(*av, "-clijson")) {
1355 38
                        AN(av[1]);
1356 38
                        vinyl_cli_json(v, av[1]);
1357 38
                        av++;
1358 38
                        continue;
1359 38
                }
1360 4986
                if (!vstrcmp(*av, "-cliok")) {
1361 1053
                        AN(av[1]);
1362 1053
                        vinyl_cli(v, av[1], (unsigned)CLIS_OK, NULL, 0);
1363 1053
                        av++;
1364 1053
                        continue;
1365 1053
                }
1366 3933
                if (!vstrcmp(*av, "-errvcl")) {
1367 352
                        char *r = NULL;
1368 352
                        AN(av[1]);
1369 352
                        AN(av[2]);
1370 352
                        vinyl_vcl(v, av[2], 1, &r);
1371 352
                        if (strstr(r, av[1]) == NULL)
1372 0
                                vinyl_fatal(v,
1373 352
                                    "Did not find expected string: (\"%s\")",
1374 352
                                    av[1]);
1375 352
                        else
1376 352
                                vtc_log(v->vl, 3,
1377 352
                                    "Found expected string: (\"%s\")",
1378 352
                                    av[1]);
1379 352
                        free(r);
1380 352
                        av += 2;
1381 352
                        continue;
1382 352
                }
1383 3581
                if (!vstrcmp(*av, "-expect")) {
1384 1014
                        av++;
1385 1014
                        vinyl_expect(v, av);
1386 1014
                        av += 2;
1387 1014
                        continue;
1388 1014
                }
1389 2567
                if (!vstrcmp(*av, "-expectexit")) {
1390 10
                        v->expect_exit = strtoul(av[1], NULL, 0);
1391 10
                        av++;
1392 10
                        continue;
1393 10
                }
1394 2557
                if (!vstrcmp(*av, "-jail")) {
1395 5
                        AN(av[1]);
1396 5
                        AZ(v->pid);
1397 5
                        REPLACE(v->jail, av[1]);
1398 5
                        av++;
1399 5
                        continue;
1400 5
                }
1401 2552
                if (!vstrcmp(*av, "-proto")) {
1402 16
                        AN(av[1]);
1403 16
                        AZ(v->pid);
1404 16
                        REPLACE(v->proto, av[1]);
1405 16
                        av++;
1406 16
                        continue;
1407 16
                }
1408 2536
                if (!vstrcmp(*av, "-start")) {
1409 1010
                        vinyl_start(v);
1410 1010
                        continue;
1411 1010
                }
1412 1526
                if (!vstrcmp(*av, "-stop")) {
1413 59
                        vinyl_stop(v);
1414 59
                        continue;
1415 59
                }
1416 1467
                if (!vstrcmp(*av, "-syntax")) {
1417 56
                        AN(av[1]);
1418 56
                        v->syntax = strtod(av[1], NULL);
1419 56
                        av++;
1420 56
                        continue;
1421 56
                }
1422 1411
                if (!vstrcmp(*av, "-vcl")) {
1423 309
                        AN(av[1]);
1424 309
                        vinyl_vcl(v, av[1], 0, NULL);
1425 309
                        av++;
1426 309
                        continue;
1427 309
                }
1428 1102
                if (!vstrcmp(*av, "-vcl+backend")) {
1429 929
                        AN(av[1]);
1430 929
                        vinyl_vclbackend(v, av[1]);
1431 929
                        av++;
1432 929
                        continue;
1433 929
                }
1434 173
                if (!vstrcmp(*av, "-vsc")) {
1435 10
                        AN(av[1]);
1436 10
                        vinyl_vsc(v, av[1]);
1437 10
                        av++;
1438 10
                        continue;
1439 10
                }
1440 163
                if (!vstrcmp(*av, "-wait-stopped")) {
1441 3
                        wait_stopped(v);
1442 3
                        continue;
1443 3
                }
1444 160
                if (!vstrcmp(*av, "-wait-running")) {
1445 3
                        wait_running(v);
1446 3
                        continue;
1447 3
                }
1448 157
                if (!vstrcmp(*av, "-wait")) {
1449 7
                        vinyl_wait(v);
1450 7
                        continue;
1451 7
                }
1452 150
                if (!vstrcmp(*av, "-vsl_catchup")) {
1453 149
                        vsl_catchup(v);
1454 149
                        continue;
1455 149
                }
1456 1
                vinyl_fatal(v, "Unknown %s argument: %s", me, *av);
1457 1
        }
1458 4265
}