varnish-cache/bin/varnishd/cache/cache_vcl.c
0
/*-
1
 * Copyright (c) 2006 Verdens Gang AS
2
 * Copyright (c) 2006-2016 Varnish Software AS
3
 * All rights reserved.
4
 *
5
 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
6
 *
7
 * SPDX-License-Identifier: BSD-2-Clause
8
 *
9
 * Redistribution and use in source and binary forms, with or without
10
 * modification, are permitted provided that the following conditions
11
 * are met:
12
 * 1. Redistributions of source code must retain the above copyright
13
 *    notice, this list of conditions and the following disclaimer.
14
 * 2. Redistributions in binary form must reproduce the above copyright
15
 *    notice, this list of conditions and the following disclaimer in the
16
 *    documentation and/or other materials provided with the distribution.
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
22
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28
 * SUCH DAMAGE.
29
 *
30
 */
31
32
#include "config.h"
33
34
#include <dlfcn.h>
35
#include <fnmatch.h>
36
#include <stdio.h>
37
#include <stdlib.h>
38
39
#include "cache_varnishd.h"
40
#include "common/heritage.h"
41
42
#include "vcl.h"
43
44
#include "cache_director.h"
45
#include "cache_vcl.h"
46
#include "vcli_serve.h"
47
#include "vte.h"
48
#include "vtim.h"
49
#include "vcc_interface.h"
50
51
const struct vcltemp VCL_TEMP_INIT[1] = {{ .name = "init", .is_cold = 1 }};
52
const struct vcltemp VCL_TEMP_COLD[1] = {{ .name = "cold", .is_cold = 1 }};
53
const struct vcltemp VCL_TEMP_WARM[1] = {{ .name = "warm", .is_warm = 1 }};
54
const struct vcltemp VCL_TEMP_BUSY[1] = {{ .name = "busy", .is_warm = 1 }};
55
const struct vcltemp VCL_TEMP_COOLING[1] = {{ .name = "cooling" }};
56
57
// not really a temperature
58
static const struct vcltemp VCL_TEMP_LABEL[1] = {{ .name = "label" }};
59
60
/*
61
 * XXX: Presently all modifications to this list happen from the
62
 * CLI event-engine, so no locking is necessary
63
 */
64
static VTAILQ_HEAD(, vcl)       vcl_head =
65
    VTAILQ_HEAD_INITIALIZER(vcl_head);
66
67
struct lock             vcl_mtx;
68
struct vcl              *vcl_active; /* protected by vcl_mtx */
69
70
static struct vrt_ctx ctx_cli;
71
static struct wrk_vpi wrk_vpi_cli;
72
static struct ws ws_cli;
73
static uintptr_t ws_snapshot_cli;
74
static struct vsl_log vsl_cli;
75
76
/*--------------------------------------------------------------------*/
77
78
void
79 60735
VCL_Bo2Ctx(struct vrt_ctx *ctx, struct busyobj *bo)
80
{
81
82 60735
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
83 60735
        CHECK_OBJ_NOTNULL(bo, BUSYOBJ_MAGIC);
84 60735
        CHECK_OBJ_NOTNULL(bo->wrk, WORKER_MAGIC);
85 60735
        ctx->vcl = bo->vcl;
86 60735
        ctx->syntax = ctx->vcl->conf->syntax;
87 60735
        ctx->vsl = bo->vsl;
88 60735
        ctx->http_bereq = bo->bereq;
89 60735
        ctx->http_beresp = bo->beresp;
90 60735
        ctx->bo = bo;
91 60735
        ctx->sp = bo->sp;
92 60735
        ctx->now = bo->t_prev;
93 60735
        ctx->ws = bo->ws;
94 60735
        ctx->vpi = bo->wrk->vpi;
95 60735
        ctx->vpi->handling = 0;
96 60735
        ctx->vpi->trace = bo->trace;
97 60735
}
98
99
void
100 97249
VCL_Req2Ctx(struct vrt_ctx *ctx, struct req *req)
101
{
102
103 97249
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
104 97249
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
105 97249
        CHECK_OBJ_NOTNULL(req->doclose, STREAM_CLOSE_MAGIC);
106
107 97249
        ctx->vcl = req->vcl;
108 97249
        ctx->syntax = ctx->vcl->conf->syntax;
109 97249
        ctx->vsl = req->vsl;
110 97249
        ctx->http_req = req->http;
111 97249
        CHECK_OBJ_NOTNULL(req->top, REQTOP_MAGIC);
112 97249
        ctx->http_req_top = req->top->topreq->http;
113 97249
        ctx->http_resp = req->resp;
114 97249
        ctx->req = req;
115 97249
        ctx->sp = req->sp;
116 97249
        ctx->now = req->t_prev;
117 97249
        ctx->ws = req->ws;
118 97249
        ctx->vpi = req->wrk->vpi;
119 97249
        ctx->vpi->handling = 0;
120 97249
        ctx->vpi->trace = req->trace;
121 97249
}
122
123
/*--------------------------------------------------------------------*/
124
125
struct vrt_ctx *
126 15020
VCL_Get_CliCtx(int msg)
127
{
128
129 15020
        ASSERT_CLI();
130 15020
        INIT_OBJ(&ctx_cli, VRT_CTX_MAGIC);
131 15020
        INIT_OBJ(&wrk_vpi_cli, WRK_VPI_MAGIC);
132 15020
        ctx_cli.vpi = &wrk_vpi_cli;
133 15020
        wrk_vpi_cli.trace = FEATURE(FEATURE_TRACE);
134 15020
        ctx_cli.now = VTIM_real();
135 15020
        if (msg) {
136 9464
                ctx_cli.msg = VSB_new_auto();
137 9464
                AN(ctx_cli.msg);
138 9464
        } else {
139 5556
                ctx_cli.vsl = &vsl_cli;
140
        }
141 15020
        ctx_cli.ws = &ws_cli;
142 15020
        WS_Assert(ctx_cli.ws);
143 15020
        return (&ctx_cli);
144
}
145
146
/*
147
 * releases CLI ctx
148
 *
149
 * returns finished error msg vsb if VCL_Get_CliCtx(1) was called
150
 *
151
 * caller needs to VSB_destroy a non-NULL return value
152
 *
153
 */
154
struct vsb *
155 15016
VCL_Rel_CliCtx(struct vrt_ctx **ctx)
156
{
157 15016
        struct vsb *r = NULL;
158
159 15016
        ASSERT_CLI();
160 15016
        assert(*ctx == &ctx_cli);
161 15016
        AN((*ctx)->vpi);
162 15016
        if (ctx_cli.msg) {
163 9464
                TAKE_OBJ_NOTNULL(r, &ctx_cli.msg, VSB_MAGIC);
164 9464
                AZ(VSB_finish(r));
165 9464
        }
166 15016
        if (ctx_cli.vsl)
167 5552
                VSL_Flush(ctx_cli.vsl, 0);
168 15016
        WS_Assert(ctx_cli.ws);
169 15016
        WS_Rollback(&ws_cli, ws_snapshot_cli);
170 15016
        INIT_OBJ(*ctx, VRT_CTX_MAGIC);
171 15016
        *ctx = NULL;
172
173 15016
        return (r);
174
}
175
176
/*--------------------------------------------------------------------*/
177
178
/* VRT_fail() can be called
179
 * - from the vcl sub via a vmod
180
 * - via a PRIV_TASK .fini callback
181
 *
182
 * if this happens during init, we fail it
183
 * if during fini, we ignore, because otherwise VMOD authors would be forced to
184
 * handle VCL_MET_FINI specifically everywhere.
185
 */
186
187
static int
188 9960
vcl_event_handling(VRT_CTX)
189
{
190 9960
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
191
192 9960
        if (ctx->vpi->handling == 0)
193 9788
                return (0);
194
195 172
        assert(ctx->vpi->handling == VCL_RET_FAIL);
196
197 172
        if (ctx->method == VCL_MET_INIT)
198 164
                return (1);
199
200
        /*
201
         * EVENT_WARM / EVENT_COLD: method == 0
202
         * must not set handling
203
         */
204 8
        assert(ctx->method == VCL_MET_FINI);
205
206 8
        ctx->vpi->handling = 0;
207 8
        VRT_fail(ctx, "VRT_fail() from vcl_fini{} has no effect");
208 8
        return (0);
209 9960
}
210
211
static int
212 9964
vcl_send_event(struct vcl *vcl, enum vcl_event_e ev, struct vsb **msg)
213
{
214
        int r, havemsg;
215 9964
        unsigned method = 0;
216
        struct vrt_ctx *ctx;
217
218 9964
        ASSERT_CLI();
219 9964
        ASSERT_VCL_ACTIVE();
220
221 9964
        CHECK_OBJ_NOTNULL(vcl, VCL_MAGIC);
222 9964
        CHECK_OBJ_NOTNULL(vcl->conf, VCL_CONF_MAGIC);
223 9964
        AN(msg);
224 9964
        AZ(*msg);
225
226 9964
        switch (ev) {
227
        case VCL_EVENT_LOAD:
228 4812
                method = VCL_MET_INIT;
229
                /* FALLTHROUGH */
230
        case VCL_EVENT_WARM:
231 9464
                havemsg = 1;
232 9464
                break;
233
        case VCL_EVENT_DISCARD:
234 311
                method = VCL_MET_FINI;
235
                /* FALLTHROUGH */
236
        case VCL_EVENT_COLD:
237 500
                havemsg = 0;
238 500
                break;
239
        default:
240 0
                WRONG("vcl_event");
241 0
        }
242
243 9964
        ctx = VCL_Get_CliCtx(havemsg);
244
245 9964
        AN(ctx->vpi);
246 9964
        AZ(ctx->vpi->handling);
247 9964
        AN(ctx->ws);
248
249 9964
        ctx->vcl = vcl;
250 9964
        ctx->syntax = ctx->vcl->conf->syntax;
251 9964
        ctx->method = method;
252
253 9964
        VCL_TaskEnter(cli_task_privs);
254 9964
        r = ctx->vcl->conf->event_vcl(ctx, ev);
255 9964
        VCL_TaskLeave(ctx, cli_task_privs);
256 9964
        r |= vcl_event_handling(ctx);
257
258 9964
        *msg = VCL_Rel_CliCtx(&ctx);
259
260 9964
        if (r && (ev == VCL_EVENT_COLD || ev == VCL_EVENT_DISCARD))
261 0
                WRONG("A VMOD cannot fail COLD or DISCARD events");
262
263 9964
        return (r);
264
}
265
266
/*--------------------------------------------------------------------*/
267
268
struct vcl *
269 10008
vcl_find(const char *name)
270
{
271
        struct vcl *vcl;
272
273 10008
        ASSERT_CLI();
274 15106
        VTAILQ_FOREACH(vcl, &vcl_head, list) {
275 10190
                if (vcl->discard)
276 2
                        continue;
277 10188
                if (!strcmp(vcl->loaded_name, name))
278 5092
                        return (vcl);
279 5096
        }
280 4916
        return (NULL);
281 10008
}
282
283
/*--------------------------------------------------------------------*/
284
285
static void
286 28
vcl_panic_conf(struct vsb *vsb, const struct VCL_conf *conf)
287
{
288
        unsigned u;
289
        const struct vpi_ii *ii;
290
291 28
        if (PAN_dump_struct(vsb, conf, VCL_CONF_MAGIC, "conf"))
292 0
                return;
293 28
        VSB_printf(vsb, "syntax = \"%u\",\n", conf->syntax);
294 28
        VSB_cat(vsb, "srcname = {\n");
295 28
        VSB_indent(vsb, 2);
296 84
        for (u = 0; u < conf->nsrc; ++u)
297 56
                VSB_printf(vsb, "[%u] = \"%s\",\n", u, conf->srcname[u]);
298 28
        VSB_indent(vsb, -2);
299 28
        VSB_cat(vsb, "},\n");
300 28
        VSB_cat(vsb, "instances = {\n");
301 28
        VSB_indent(vsb, 2);
302 28
        ii = conf->instance_info;
303 44
        while (ii != NULL && ii->p != NULL) {
304 32
                VSB_printf(vsb, "\"%s\" = %p,\n", ii->name,
305 16
                    (const void *)*(const uintptr_t *)ii->p);
306 16
                ii++;
307
        }
308 28
        VSB_indent(vsb, -2);
309 28
        VSB_cat(vsb, "},\n");
310 28
        VSB_indent(vsb, -2);
311 28
        VSB_cat(vsb, "},\n");
312 28
}
313
314
void
315 52
VCL_Panic(struct vsb *vsb, const char *nm, const struct vcl *vcl)
316
{
317
318 52
        AN(vsb);
319 52
        if (PAN_dump_struct(vsb, vcl, VCL_MAGIC, "vcl[%s]", nm))
320 24
                return;
321 28
        VSB_printf(vsb, "name = \"%s\",\n", vcl->loaded_name);
322 28
        VSB_printf(vsb, "busy = %u,\n", vcl->busy);
323 28
        VSB_printf(vsb, "discard = %u,\n", vcl->discard);
324 28
        VSB_printf(vsb, "state = %s,\n", vcl->state);
325 28
        VSB_printf(vsb, "temp = %s,\n", vcl->temp ? vcl->temp->name : "(null)");
326 28
        vcl_panic_conf(vsb, vcl->conf);
327 28
        VSB_indent(vsb, -2);
328 28
        VSB_cat(vsb, "},\n");
329 52
}
330
331
/*--------------------------------------------------------------------*/
332
333
void
334 5625
VCL_Update(struct vcl **vcc, struct vcl *vcl)
335
{
336
        struct vcl *old;
337
338 5625
        AN(vcc);
339
340 5625
        old = *vcc;
341 5625
        *vcc = NULL;
342
343 5625
        CHECK_OBJ_ORNULL(old, VCL_MAGIC);
344 5625
        ASSERT_VCL_ACTIVE();
345
346 5625
        Lck_Lock(&vcl_mtx);
347 5625
        if (old != NULL) {
348 285
                assert(old->busy > 0);
349 285
                old->busy--;
350 285
        }
351
352 5625
        if (vcl == NULL)
353 5573
                vcl = vcl_active; /* Sample vcl_active under lock to avoid
354
                                   * race */
355 5625
        CHECK_OBJ_NOTNULL(vcl, VCL_MAGIC);
356 5625
        if (vcl->label == NULL) {
357 5561
                AN(strcmp(vcl->state, VCL_TEMP_LABEL->name));
358 5561
                *vcc = vcl;
359 5561
        } else {
360 64
                AZ(strcmp(vcl->state, VCL_TEMP_LABEL->name));
361 64
                *vcc = vcl->label;
362
        }
363 5625
        CHECK_OBJ_NOTNULL(*vcc, VCL_MAGIC);
364 5625
        AZ((*vcc)->discard);
365 5625
        (*vcc)->busy++;
366 5625
        Lck_Unlock(&vcl_mtx);
367 5625
        assert((*vcc)->temp->is_warm);
368 5625
}
369
370
/*--------------------------------------------------------------------
371
 * vdire: Vcl DIrector REsignation Management (born out of a dire situation)
372
 * iterators over the director list need to register.
373
 * while iterating, directors can not retire immediately,
374
 * they get put on a list of resigning directors. The
375
 * last iterator executes the retirement.
376
 */
377
378
static struct vdire *
379 9632
vdire_new(struct lock *mtx, const struct vcltemp **tempp)
380
{
381
        struct vdire *vdire;
382
383 9632
        ALLOC_OBJ(vdire, VDIRE_MAGIC);
384 9632
        AN(vdire);
385 9632
        AN(mtx);
386 9632
        VTAILQ_INIT(&vdire->directors);
387 9632
        VTAILQ_INIT(&vdire->resigning);
388 9632
        vdire->mtx = mtx;
389 9632
        PTOK(pthread_cond_init(&vdire->cond, NULL));
390 9632
        vdire->tempp = tempp;
391 9632
        return (vdire);
392
}
393
394
/* starting an interation prevents removals from the directors list */
395
void
396 8928
vdire_start_iter(struct vdire *vdire)
397
{
398
399 8928
        CHECK_OBJ_NOTNULL(vdire, VDIRE_MAGIC);
400
401
        // https://github.com/varnishcache/varnish-cache/pull/4142#issuecomment-2593091097
402 8928
        ASSERT_CLI();
403
404 8928
        Lck_Lock(vdire->mtx);
405 8928
        while (! VTAILQ_EMPTY(&vdire->resigning))
406 0
                (void)Lck_CondWait(&vdire->cond, vdire->mtx);
407 8928
        vdire->iterating++;
408 8928
        Lck_Unlock(vdire->mtx);
409 8928
}
410
411
void
412 8928
vdire_end_iter(struct vdire *vdire)
413
{
414 8928
        struct vcldir_head resigning = VTAILQ_HEAD_INITIALIZER(resigning);
415 8928
        const struct vcltemp *temp = NULL;
416
        struct vcldir *vdir, *next;
417
        unsigned n;
418
419 8928
        CHECK_OBJ_NOTNULL(vdire, VDIRE_MAGIC);
420
421 8928
        Lck_Lock(vdire->mtx);
422 8928
        AN(vdire->iterating);
423 8928
        n = --vdire->iterating;
424
425 8928
        if (n == 0) {
426 8928
                VTAILQ_SWAP(&vdire->resigning, &resigning, vcldir, resigning_list);
427 8928
                VTAILQ_FOREACH(vdir, &resigning, resigning_list)
428 0
                        VTAILQ_REMOVE(&vdire->directors, vdir, directors_list);
429 8928
                temp = *vdire->tempp;
430 8928
                PTOK(pthread_cond_broadcast(&vdire->cond));
431 8928
        }
432 8928
        Lck_Unlock(vdire->mtx);
433
434 8928
        VTAILQ_FOREACH_SAFE(vdir, &resigning, resigning_list, next)
435 0
                vcldir_retire(vdir, temp);
436 8928
}
437
438
// if there are no iterators, remove from directors and retire
439
// otherwise put on resigning list to work when iterators end
440
void
441 433
vdire_resign(struct vdire *vdire, struct vcldir *vdir)
442
{
443 433
        const struct vcltemp *temp = NULL;
444
445 433
        CHECK_OBJ_NOTNULL(vdire, VDIRE_MAGIC);
446 433
        AN(vdir);
447
448 433
        Lck_Lock(vdire->mtx);
449 433
        if (vdire->iterating != 0) {
450 0
                VTAILQ_INSERT_TAIL(&vdire->resigning, vdir, resigning_list);
451 0
                vdir = NULL;
452 0
        } else {
453 433
                VTAILQ_REMOVE(&vdire->directors, vdir, directors_list);
454 433
                temp = *vdire->tempp;
455
        }
456 433
        Lck_Unlock(vdire->mtx);
457
458 433
        if (vdir != NULL)
459 433
                vcldir_retire(vdir, temp);
460 433
}
461
462
// unlocked version of vcl_iterdir
463
// pat can also be NULL (to iterate all)
464
static int
465 3796
vdire_iter(struct cli *cli, const char *pat, const struct vcl *vcl,
466
    vcl_be_func *func, void *priv)
467
{
468 3796
        int i, found = 0;
469
        struct vcldir *vdir;
470 3796
        struct vdire *vdire = vcl->vdire;
471
472 3796
        vdire_start_iter(vdire);
473 9344
        VTAILQ_FOREACH(vdir, &vdire->directors, directors_list) {
474 5548
                if (vdir->refcnt == 0)
475 0
                        continue;
476 5548
                if (pat != NULL && fnmatch(pat, vdir->cli_name, 0))
477 372
                        continue;
478 5176
                found++;
479 5176
                i = func(cli, vdir->dir, priv);
480 5176
                if (i < 0) {
481 0
                        found = i;
482 0
                        break;
483
                }
484 5176
                found += i;
485 5176
        }
486 3796
        vdire_end_iter(vdire);
487 3796
        return (found);
488
}
489
490
491
/*--------------------------------------------------------------------*/
492
493
// XXX locked case across VCLs - should do without
494
static int
495 24
vcl_iterdir(struct cli *cli, const char *pat, const struct vcl *vcl,
496
    vcl_be_func *func, void *priv)
497
{
498 24
        int i, found = 0;
499
        struct vcldir *vdir;
500
501 24
        Lck_AssertHeld(&vcl_mtx);
502 60
        VTAILQ_FOREACH(vdir, &vcl->vdire->directors, directors_list) {
503 36
                if (fnmatch(pat, vdir->cli_name, 0))
504 16
                        continue;
505 20
                found++;
506 20
                i = func(cli, vdir->dir, priv);
507 20
                if (i < 0)
508 0
                        return (i);
509 20
                found += i;
510 20
        }
511 24
        return (found);
512 24
}
513
514
int
515 3820
VCL_IterDirector(struct cli *cli, const char *pat,
516
    vcl_be_func *func, void *priv)
517
{
518 3820
        int i, found = 0;
519
        struct vsb *vsb;
520
        struct vcl *vcl;
521
522 3820
        ASSERT_CLI();
523 3820
        ASSERT_VCL_ACTIVE();
524 3820
        vsb = VSB_new_auto();
525 3820
        AN(vsb);
526 3820
        if (pat == NULL || *pat == '\0' || !strcmp(pat, "*")) {
527
                // all backends in active VCL
528 3644
                VSB_printf(vsb, "%s.*", VCL_Name(vcl_active));
529 3644
                vcl = vcl_active;
530 3820
        } else if (strchr(pat, '.') == NULL) {
531
                // pattern applies to active vcl
532 152
                VSB_printf(vsb, "%s.%s", VCL_Name(vcl_active), pat);
533 152
                vcl = vcl_active;
534 152
        } else {
535
                // use pattern as is
536 24
                VSB_cat(vsb, pat);
537 24
                vcl = NULL;
538
        }
539 3820
        AZ(VSB_finish(vsb));
540 3820
        if (vcl != NULL) {
541 3796
                found = vdire_iter(cli, VSB_data(vsb), vcl, func, priv);
542 3796
        } else {
543 24
                Lck_Lock(&vcl_mtx);
544 48
                VTAILQ_FOREACH(vcl, &vcl_head, list) {
545 24
                        i = vcl_iterdir(cli, VSB_data(vsb), vcl, func, priv);
546 24
                        if (i < 0) {
547 0
                                found = i;
548 0
                                break;
549
                        } else {
550 24
                                found += i;
551
                        }
552 24
                }
553 24
                Lck_Unlock(&vcl_mtx);
554
        }
555 3820
        VSB_destroy(&vsb);
556 3820
        return (found);
557
}
558
559
static void
560 4817
vcl_BackendEvent(const struct vcl *vcl, enum vcl_event_e e)
561
{
562
        struct vcldir *vdir;
563
        struct vdire *vdire;
564
565 4817
        ASSERT_CLI();
566 4817
        CHECK_OBJ_NOTNULL(vcl, VCL_MAGIC);
567 4817
        AZ(vcl->busy);
568 4817
        vdire = vcl->vdire;
569
570 4817
        vdire_start_iter(vdire);
571 10920
        VTAILQ_FOREACH(vdir, &vdire->directors, directors_list)
572 6103
                VDI_Event(vdir->dir, e);
573 4817
        vdire_end_iter(vdire);
574 4817
}
575
576
static void
577 311
vcl_KillBackends(const struct vcl *vcl)
578
{
579
        struct vcldir *vdir, *vdir2;
580
        struct vdire *vdire;
581
582 311
        CHECK_OBJ_NOTNULL(vcl, VCL_MAGIC);
583 311
        assert(vcl->temp == VCL_TEMP_COLD || vcl->temp == VCL_TEMP_INIT);
584 311
        vdire = vcl->vdire;
585 311
        CHECK_OBJ_NOTNULL(vdire, VDIRE_MAGIC);
586
587
        /*
588
         * ensure all retirement has finished (vdire_start_iter waits for it)
589
         */
590 311
        vdire_start_iter(vdire);
591 311
        vdire_end_iter(vdire);
592 311
        AZ(vdire->iterating);
593 311
        assert(VTAILQ_EMPTY(&vdire->resigning));
594
595
        /*
596
         * Unlocked and sidelining vdire because no further directors can be added, and the
597
         * remaining ones need to be able to remove themselves.
598
         */
599 578
        VTAILQ_FOREACH_SAFE(vdir, &vdire->directors, directors_list, vdir2)
600 267
                VDI_Event(vdir->dir, VCL_EVENT_DISCARD);
601 311
        assert(VTAILQ_EMPTY(&vdire->directors));
602 311
}
603
604
/*--------------------------------------------------------------------*/
605
606
static struct vcl *
607 9640
VCL_Open(const char *fn, struct vsb *msg)
608
{
609
        struct vcl *vcl;
610
        void *dlh;
611
        struct VCL_conf const *cnf;
612
        const char *dlerr;
613
        int err;
614
615 9640
        AN(fn);
616 9640
        AN(msg);
617
618
#ifdef RTLD_NOLOAD
619
        /* Detect bogus caching by dlopen(3) */
620 9640
        dlh = dlopen(fn, RTLD_NOW | RTLD_NOLOAD);
621 9640
        AZ(dlh);
622
#endif
623 9640
        dlh = dlopen(fn, RTLD_NOW | RTLD_LOCAL);
624 9640
        if (dlh == NULL) {
625 4
                err = errno;
626 4
                dlerr = dlerror();
627 4
                VSB_cat(msg, "Could not load compiled VCL.\n");
628 4
                if (dlerr != NULL)
629 4
                        VSB_printf(msg, "\tdlopen() = %s\n", dlerr);
630 4
                if (err) {
631 0
                        VSB_printf(msg, "\terror = %s (%d)\n",
632 0
                            strerror(err), err);
633 0
                }
634 4
                VSB_cat(msg, "\thint: check for \"noexec\" mount\n");
635 4
                VSB_cat(msg, "\thint: check \"vmod_path\" parameter\n");
636 4
                return (NULL);
637
        }
638 9636
        cnf = dlsym(dlh, "VCL_conf");
639 9636
        if (cnf == NULL) {
640 0
                VSB_cat(msg, "Compiled VCL lacks metadata.\n");
641 0
                (void)dlclose(dlh);
642 0
                return (NULL);
643
        }
644 9636
        if (cnf->magic != VCL_CONF_MAGIC) {
645 0
                VSB_cat(msg, "Compiled VCL has mangled metadata.\n");
646 0
                (void)dlclose(dlh);
647 0
                return (NULL);
648
        }
649 9636
        if (cnf->syntax < heritage.min_vcl_version ||
650 9632
            cnf->syntax > heritage.max_vcl_version) {
651 8
                VSB_printf(msg, "Compiled VCL version (%.1f) not supported.\n",
652 4
                    .1 * cnf->syntax);
653 4
                (void)dlclose(dlh);
654 4
                return (NULL);
655
        }
656 9632
        ALLOC_OBJ(vcl, VCL_MAGIC);
657 9632
        AN(vcl);
658 9632
        vcl->dlh = dlh;
659 9632
        vcl->conf = cnf;
660 9632
        vcl->vdire = vdire_new(&vcl_mtx, &vcl->temp);
661 9632
        return (vcl);
662 9640
}
663
664
static void
665 5131
VCL_Close(struct vcl **vclp)
666
{
667
        struct vcl *vcl;
668
669 5131
        TAKE_OBJ_NOTNULL(vcl, vclp, VCL_MAGIC);
670 5131
        assert(VTAILQ_EMPTY(&vcl->filters));
671 5131
        AZ(dlclose(vcl->dlh));
672 5131
        PTOK(pthread_cond_destroy(&vcl->vdire->cond));
673 5131
        FREE_OBJ(vcl->vdire);
674 5131
        FREE_OBJ(vcl);
675 5131
}
676
677
/*--------------------------------------------------------------------
678
 * NB: This function is called in/from the test-load subprocess.
679
 */
680
681
int
682 4828
VCL_TestLoad(const char *fn)
683
{
684
        struct vsb *vsb;
685
        struct vcl *vcl;
686 4828
        int retval = 0;
687
688 4828
        AN(fn);
689 4828
        vsb = VSB_new_auto();
690 4828
        AN(vsb);
691 4828
        vcl = VCL_Open(fn, vsb);
692 4828
        if (vcl == NULL) {
693 8
                AZ(VSB_finish(vsb));
694 8
                fprintf(stderr, "%s", VSB_data(vsb));
695 8
                retval = -1;
696 8
        } else
697 4820
                VCL_Close(&vcl);
698 4828
        VSB_destroy(&vsb);
699 4828
        return (retval);
700
}
701
702
/*--------------------------------------------------------------------*/
703
704
static struct vsb *
705 4
vcl_print_refs(const struct vcl *vcl)
706
{
707
        struct vsb *msg;
708
        struct vclref *ref;
709
710 4
        CHECK_OBJ_NOTNULL(vcl, VCL_MAGIC);
711 4
        msg = VSB_new_auto();
712 4
        AN(msg);
713
714 4
        VSB_printf(msg, "VCL %s is waiting for:", vcl->loaded_name);
715 4
        Lck_Lock(&vcl_mtx);
716 8
        VTAILQ_FOREACH(ref, &vcl->ref_list, list)
717 4
                VSB_printf(msg, "\n\t- %s", ref->desc);
718 4
        Lck_Unlock(&vcl_mtx);
719 4
        AZ(VSB_finish(msg));
720 4
        return (msg);
721
}
722
723
static int
724 5044
vcl_set_state(struct vcl *vcl, const char *state, struct vsb **msg)
725
{
726 5044
        struct vsb *nomsg = NULL;
727 5044
        int i = 0;
728
729 5044
        ASSERT_CLI();
730
731 5044
        CHECK_OBJ_NOTNULL(vcl, VCL_MAGIC);
732 5044
        AN(state);
733 5044
        AN(msg);
734 5044
        AZ(*msg);
735
736 5044
        AN(vcl->temp);
737
738 5044
        switch (state[0]) {
739
        case '0':
740 380
                if (vcl->temp == VCL_TEMP_COLD)
741 8
                        break;
742 372
                if (vcl->busy == 0 && vcl->temp->is_warm) {
743 173
                        Lck_Lock(&vcl_mtx);
744 173
                        vcl->temp = VTAILQ_EMPTY(&vcl->ref_list) ?
745
                            VCL_TEMP_COLD : VCL_TEMP_COOLING;
746 173
                        Lck_Unlock(&vcl_mtx);
747 173
                        vcl_BackendEvent(vcl, VCL_EVENT_COLD);
748 173
                        AZ(vcl_send_event(vcl, VCL_EVENT_COLD, msg));
749 173
                        AZ(*msg);
750 173
                }
751 199
                else if (vcl->busy)
752 113
                        vcl->temp = VCL_TEMP_BUSY;
753 86
                else if (VTAILQ_EMPTY(&vcl->ref_list))
754 70
                        vcl->temp = VCL_TEMP_COLD;
755
                else
756 16
                        vcl->temp = VCL_TEMP_COOLING;
757 372
                break;
758
        case '1':
759 4664
                if (vcl->temp == VCL_TEMP_WARM)
760 4
                        break;
761
                /* The warm VCL hasn't seen a cold event yet */
762 4660
                if (vcl->temp == VCL_TEMP_BUSY)
763 4
                        vcl->temp = VCL_TEMP_WARM;
764
                /* The VCL must first reach a stable cold state */
765 4656
                else if (vcl->temp == VCL_TEMP_COOLING) {
766 4
                        *msg = vcl_print_refs(vcl);
767 4
                        i = -1;
768 4
                }
769
                else {
770 4652
                        Lck_Lock(&vcl_mtx);
771 4652
                        vcl->temp = VCL_TEMP_WARM;
772 4652
                        Lck_Unlock(&vcl_mtx);
773 4652
                        i = vcl_send_event(vcl, VCL_EVENT_WARM, msg);
774 4652
                        if (i == 0) {
775 4640
                                vcl_BackendEvent(vcl, VCL_EVENT_WARM);
776 4640
                                break;
777
                        }
778 12
                        AZ(vcl_send_event(vcl, VCL_EVENT_COLD, &nomsg));
779 12
                        AZ(nomsg);
780 12
                        vcl->temp = VCL_TEMP_COLD;
781
                }
782 20
                break;
783
        default:
784 0
                WRONG("Wrong enum state");
785 0
        }
786 5044
        if (i == 0 && state[1])
787 4856
                bprintf(vcl->state, "%s", state + 1);
788
789 5044
        return (i);
790
}
791
792
static void
793 172
vcl_cancel_load(struct vcl *vcl, struct cli *cli, struct vsb *msg,
794
    const char *name, const char *step)
795
{
796
797 172
        CHECK_OBJ_NOTNULL(vcl, VCL_MAGIC);
798 172
        CHECK_OBJ_NOTNULL(vcl->conf, VCL_CONF_MAGIC);
799
800 172
        VCLI_SetResult(cli, CLIS_CANT);
801 172
        VCLI_Out(cli, "VCL \"%s\" Failed %s", name, step);
802 172
        if (VSB_len(msg))
803 172
                VCLI_Out(cli, "\nMessage:\n\t%s", VSB_data(msg));
804 172
        VSB_destroy(&msg);
805
806 172
        AZ(vcl_send_event(vcl, VCL_EVENT_DISCARD, &msg));
807 172
        AZ(msg);
808
809 172
        vcl_KillBackends(vcl);
810 172
        free(vcl->loaded_name);
811 172
        VCL_Close(&vcl);
812 172
}
813
814
static void
815 4812
vcl_load(struct cli *cli,
816
    const char *name, const char *fn, const char *state)
817
{
818
        struct vcl *vcl;
819
        struct vsb *msg;
820
821 4812
        ASSERT_CLI();
822 4812
        ASSERT_VCL_ACTIVE();
823
824 4812
        vcl = vcl_find(name);
825 4812
        AZ(vcl);
826
827 4812
        msg = VSB_new_auto();
828 4812
        AN(msg);
829 4812
        vcl = VCL_Open(fn, msg);
830 4812
        AZ(VSB_finish(msg));
831 4812
        if (vcl == NULL) {
832 0
                VCLI_SetResult(cli, CLIS_PARAM);
833 0
                VCLI_Out(cli, "%s", VSB_data(msg));
834 0
                VSB_destroy(&msg);
835 0
                return;
836
        }
837
838 4812
        VSB_destroy(&msg);
839
840 4812
        vcl->loaded_name = strdup(name);
841 4812
        XXXAN(vcl->loaded_name);
842 4812
        VTAILQ_INIT(&vcl->ref_list);
843 4812
        VTAILQ_INIT(&vcl->filters);
844
845 4812
        vcl->temp = VCL_TEMP_INIT;
846
847 4812
        if (vcl_send_event(vcl, VCL_EVENT_LOAD, &msg)) {
848 168
                vcl_cancel_load(vcl, cli, msg, name, "initialization");
849 168
                return;
850
        }
851 4644
        VSB_destroy(&msg);
852
853 4644
        if (vcl_set_state(vcl, state, &msg)) {
854 4
                assert(*state == '1');
855 4
                vcl_cancel_load(vcl, cli, msg, name, "warmup");
856 4
                return;
857
        }
858 4640
        if (msg)
859 4624
                VSB_destroy(&msg);
860
861 4640
        VCLI_Out(cli, "Loaded \"%s\" as \"%s\"", fn , name);
862 4640
        VTAILQ_INSERT_TAIL(&vcl_head, vcl, list);
863 4640
        VSC_C_main->n_vcl++;
864 4640
        VSC_C_main->n_vcl_avail++;
865 4812
}
866
867
/*--------------------------------------------------------------------*/
868
869
void
870 30371
VCL_Poll(void)
871
{
872 30371
        struct vsb *nomsg = NULL;
873
        struct vcl *vcl, *vcl2;
874
875 30371
        ASSERT_CLI();
876 30371
        ASSERT_VCL_ACTIVE();
877 63933
        VTAILQ_FOREACH_SAFE(vcl, &vcl_head, list, vcl2) {
878 33562
                if (vcl->temp == VCL_TEMP_BUSY ||
879 33460
                    vcl->temp == VCL_TEMP_COOLING)
880 172
                        AZ(vcl_set_state(vcl, "0", &nomsg));
881 33562
                AZ(nomsg);
882 33562
                if (vcl->discard && vcl->temp == VCL_TEMP_COLD) {
883 139
                        AZ(vcl->busy);
884 139
                        assert(vcl != vcl_active);
885 139
                        assert(VTAILQ_EMPTY(&vcl->ref_list));
886 139
                        VTAILQ_REMOVE(&vcl_head, vcl, list);
887 139
                        AZ(vcl_send_event(vcl, VCL_EVENT_DISCARD, &nomsg));
888 139
                        AZ(nomsg);
889 139
                        vcl_KillBackends(vcl);
890 139
                        free(vcl->loaded_name);
891 139
                        VCL_Close(&vcl);
892 139
                        VSC_C_main->n_vcl--;
893 139
                        VSC_C_main->n_vcl_discard--;
894 139
                }
895 33562
        }
896 30371
}
897
898
/*--------------------------------------------------------------------*/
899
900
static void v_matchproto_(cli_func_t)
901 300
vcl_cli_list(struct cli *cli, const char * const *av, void *priv)
902
{
903
        struct vcl *vcl;
904
        const char *flg;
905
        struct vte *vte;
906
907
        /* NB: Shall generate same output as mcf_vcl_list() */
908
909 300
        (void)av;
910 300
        (void)priv;
911 300
        ASSERT_CLI();
912 300
        ASSERT_VCL_ACTIVE();
913 300
        vte = VTE_new(7, 80);
914 300
        AN(vte);
915 961
        VTAILQ_FOREACH(vcl, &vcl_head, list) {
916 661
                if (vcl == vcl_active) {
917 300
                        flg = "active";
918 661
                } else if (vcl->discard) {
919 17
                        flg = "discarded";
920 17
                } else
921 344
                        flg = "available";
922 1322
                VTE_printf(vte, "%s\t%s\t%s\t\v%u\t%s", flg, vcl->state,
923 661
                    vcl->temp->name, vcl->busy, vcl->loaded_name);
924 661
                if (vcl->label != NULL) {
925 76
                        VTE_printf(vte, "\t->\t%s", vcl->label->loaded_name);
926 76
                        if (vcl->nrefs)
927 32
                                VTE_printf(vte, " (%d return(vcl)%s)",
928 16
                                    vcl->nrefs, vcl->nrefs > 1 ? "'s" : "");
929 661
                } else if (vcl->nlabels > 0) {
930 104
                        VTE_printf(vte, "\t<-\t(%d label%s)",
931 52
                            vcl->nlabels, vcl->nlabels > 1 ? "s" : "");
932 52
                }
933 661
                VTE_cat(vte, "\n");
934 661
        }
935 300
        AZ(VTE_finish(vte));
936 300
        AZ(VTE_format(vte, VCLI_VTE_format, cli));
937 300
        VTE_destroy(&vte);
938 300
}
939
940
static void v_matchproto_(cli_func_t)
941 20
vcl_cli_list_json(struct cli *cli, const char * const *av, void *priv)
942
{
943
        struct vcl *vcl;
944
945 20
        (void)priv;
946 20
        ASSERT_CLI();
947 20
        ASSERT_VCL_ACTIVE();
948 20
        VCLI_JSON_begin(cli, 2, av);
949 20
        VCLI_Out(cli, ",\n");
950 84
        VTAILQ_FOREACH(vcl, &vcl_head, list) {
951 64
                VCLI_Out(cli, "{\n");
952 64
                VSB_indent(cli->sb, 2);
953 64
                VCLI_Out(cli, "\"status\": ");
954 64
                if (vcl == vcl_active) {
955 20
                        VCLI_Out(cli, "\"active\",\n");
956 64
                } else if (vcl->discard) {
957 0
                        VCLI_Out(cli, "\"discarded\",\n");
958 0
                } else
959 44
                        VCLI_Out(cli, "\"available\",\n");
960 64
                VCLI_Out(cli, "\"state\": \"%s\",\n", vcl->state);
961 64
                VCLI_Out(cli, "\"temperature\": \"%s\",\n", vcl->temp->name);
962 64
                VCLI_Out(cli, "\"busy\": %u,\n", vcl->busy);
963 64
                VCLI_Out(cli, "\"name\": \"%s\"", vcl->loaded_name);
964 64
                if (vcl->label != NULL) {
965 24
                        VCLI_Out(cli, ",\n");
966 24
                        VCLI_Out(cli, "\"label\": {\n");
967 24
                        VSB_indent(cli->sb, 2);
968 48
                                VCLI_Out(cli, "\"name\": \"%s\"",
969 24
                                         vcl->label->loaded_name);
970 24
                        if (vcl->nrefs)
971 0
                                VCLI_Out(cli, ",\n\"refs\": %d", vcl->nrefs);
972 24
                        VCLI_Out(cli, "\n");
973 24
                        VCLI_Out(cli, "}");
974 24
                        VSB_indent(cli->sb, -2);
975 64
                } else if (vcl->nlabels > 0) {
976 8
                        VCLI_Out(cli, ",\n");
977 8
                        VCLI_Out(cli, "\"labels\": %d", vcl->nlabels);
978 8
                }
979 64
                VSB_indent(cli->sb, -2);
980 64
                VCLI_Out(cli, "\n}");
981 64
                if (VTAILQ_NEXT(vcl, list) != NULL)
982 44
                        VCLI_Out(cli, ",\n");
983 64
        }
984 20
        VCLI_JSON_end(cli);
985 20
}
986
987
static void v_matchproto_(cli_func_t)
988 4812
vcl_cli_load(struct cli *cli, const char * const *av, void *priv)
989
{
990
991 4812
        AZ(priv);
992 4812
        ASSERT_CLI();
993
        // XXX move back code from vcl_load?
994 4812
        vcl_load(cli, av[2], av[3], av[4]);
995 4812
}
996
997
static void v_matchproto_(cli_func_t)
998 232
vcl_cli_state(struct cli *cli, const char * const *av, void *priv)
999
{
1000
        struct vcl *vcl;
1001 232
        struct vsb *msg = NULL;
1002
1003 232
        AZ(priv);
1004 232
        ASSERT_CLI();
1005 232
        ASSERT_VCL_ACTIVE();
1006 232
        AN(av[2]);
1007 232
        AN(av[3]);
1008
1009 232
        vcl = vcl_find(av[2]);
1010 232
        AN(vcl);
1011
1012 232
        if (vcl_set_state(vcl, av[3], &msg)) {
1013 12
                CHECK_OBJ_NOTNULL(msg, VSB_MAGIC);
1014
1015 12
                VCLI_SetResult(cli, CLIS_CANT);
1016 24
                VCLI_Out(cli, "Failed <vcl.state %s %s>", vcl->loaded_name,
1017 12
                    av[3] + 1);
1018 12
                if (VSB_len(msg))
1019 12
                        VCLI_Out(cli, "\nMessage:\n\t%s", VSB_data(msg));
1020 12
        }
1021 232
        if (msg)
1022 28
                VSB_destroy(&msg);
1023 232
}
1024
1025
static void v_matchproto_(cli_func_t)
1026 156
vcl_cli_discard(struct cli *cli, const char * const *av, void *priv)
1027
{
1028
        struct vcl *vcl;
1029
1030 156
        ASSERT_CLI();
1031 156
        ASSERT_VCL_ACTIVE();
1032 156
        (void)cli;
1033 156
        AZ(priv);
1034 156
        vcl = vcl_find(av[2]);
1035 156
        CHECK_OBJ_NOTNULL(vcl, VCL_MAGIC);              // MGT ensures this
1036 156
        Lck_Lock(&vcl_mtx);
1037 156
        assert (vcl != vcl_active);     // MGT ensures this
1038 156
        AZ(vcl->nlabels);               // MGT ensures this
1039 156
        VSC_C_main->n_vcl_discard++;
1040 156
        VSC_C_main->n_vcl_avail--;
1041 156
        vcl->discard = 1;
1042 156
        if (vcl->label != NULL) {
1043 16
                AZ(strcmp(vcl->state, VCL_TEMP_LABEL->name));
1044 16
                vcl->label->nlabels--;
1045 16
                vcl->label= NULL;
1046 16
        }
1047 156
        Lck_Unlock(&vcl_mtx);
1048
1049 156
        if (!strcmp(vcl->state, VCL_TEMP_LABEL->name)) {
1050 16
                VTAILQ_REMOVE(&vcl_head, vcl, list);
1051 16
                free(vcl->loaded_name);
1052 16
                AZ(vcl->vdire);
1053 16
                FREE_OBJ(vcl);
1054 156
        } else if (vcl->temp == VCL_TEMP_COLD) {
1055 106
                VCL_Poll();
1056 106
        }
1057 156
}
1058
1059
static void v_matchproto_(cli_func_t)
1060 108
vcl_cli_label(struct cli *cli, const char * const *av, void *priv)
1061
{
1062
        struct vcl *lbl;
1063
        struct vcl *vcl;
1064
1065 108
        ASSERT_CLI();
1066 108
        ASSERT_VCL_ACTIVE();
1067 108
        (void)cli;
1068 108
        (void)priv;
1069 108
        vcl = vcl_find(av[3]);
1070 108
        AN(vcl);                                // MGT ensures this
1071 108
        lbl = vcl_find(av[2]);
1072 108
        if (lbl == NULL) {
1073 96
                ALLOC_OBJ(lbl, VCL_MAGIC);
1074 96
                AN(lbl);
1075 96
                bprintf(lbl->state, "%s", VCL_TEMP_LABEL->name);
1076 96
                lbl->temp = VCL_TEMP_WARM;
1077 96
                REPLACE(lbl->loaded_name, av[2]);
1078 96
                VTAILQ_INSERT_TAIL(&vcl_head, lbl, list);
1079 96
        }
1080 108
        if (lbl->label != NULL)
1081 12
                lbl->label->nlabels--;
1082 108
        lbl->label = vcl;
1083 108
        vcl->nlabels++;
1084 108
}
1085
1086
static void v_matchproto_(cli_func_t)
1087 4468
vcl_cli_use(struct cli *cli, const char * const *av, void *priv)
1088
{
1089
        struct vcl *vcl;
1090
1091 4468
        ASSERT_CLI();
1092 4468
        ASSERT_VCL_ACTIVE();
1093 4468
        AN(cli);
1094 4468
        AZ(priv);
1095 4468
        vcl = vcl_find(av[2]);
1096 4468
        AN(vcl);                                // MGT ensures this
1097 4468
        assert(vcl->temp == VCL_TEMP_WARM);     // MGT ensures this
1098 4468
        Lck_Lock(&vcl_mtx);
1099 4468
        vcl_active = vcl;
1100 4468
        Lck_Unlock(&vcl_mtx);
1101 4468
}
1102
1103
static void v_matchproto_(cli_func_t)
1104 48
vcl_cli_show(struct cli *cli, const char * const *av, void *priv)
1105
{
1106
        struct vcl *vcl;
1107 48
        int verbose = 0;
1108 48
        int i = 2;
1109
        unsigned u;
1110
1111 48
        ASSERT_CLI();
1112 48
        ASSERT_VCL_ACTIVE();
1113 48
        AZ(priv);
1114
1115 48
        if (av[i] != NULL && !strcmp(av[i], "-v")) {
1116 16
                verbose = 1;
1117 16
                i++;
1118 16
        }
1119
1120 48
        if (av[i] == NULL) {
1121 8
                vcl = vcl_active;
1122 8
                AN(vcl);
1123 8
        } else {
1124 40
                vcl = vcl_find(av[i]);
1125 40
                i++;
1126
        }
1127
1128 48
        if (av[i] != NULL) {
1129 4
                VCLI_Out(cli, "Too many parameters: '%s'", av[i]);
1130 4
                VCLI_SetResult(cli, CLIS_PARAM);
1131 4
                return;
1132
        }
1133
1134 44
        if (vcl == NULL) {
1135 4
                VCLI_Out(cli, "No VCL named '%s'", av[i - 1]);
1136 4
                VCLI_SetResult(cli, CLIS_PARAM);
1137 4
                return;
1138
        }
1139 40
        CHECK_OBJ_NOTNULL(vcl, VCL_MAGIC);
1140 40
        if (vcl->label) {
1141 8
                vcl = vcl->label;
1142 8
                CHECK_OBJ_NOTNULL(vcl, VCL_MAGIC);
1143 8
                AZ(vcl->label);
1144 8
        }
1145 40
        CHECK_OBJ_NOTNULL(vcl->conf, VCL_CONF_MAGIC);
1146 40
        if (verbose) {
1147 48
                for (u = 0; u < vcl->conf->nsrc; u++)
1148 64
                        VCLI_Out(cli, "// VCL.SHOW %u %zd %s\n%s\n",
1149 32
                            u, strlen(vcl->conf->srcbody[u]),
1150 32
                            vcl->conf->srcname[u],
1151 32
                            vcl->conf->srcbody[u]);
1152 16
        } else {
1153 24
                VCLI_Out(cli, "%s", vcl->conf->srcbody[0]);
1154
        }
1155 48
}
1156
1157
/*--------------------------------------------------------------------*/
1158
1159
static struct cli_proto vcl_cmds[] = {
1160
        { CLICMD_VCL_LOAD,              "", vcl_cli_load },
1161
        { CLICMD_VCL_LIST,              "", vcl_cli_list, vcl_cli_list_json },
1162
        { CLICMD_VCL_STATE,             "", vcl_cli_state },
1163
        { CLICMD_VCL_DISCARD,           "", vcl_cli_discard },
1164
        { CLICMD_VCL_USE,               "", vcl_cli_use },
1165
        { CLICMD_VCL_SHOW,              "", vcl_cli_show },
1166
        { CLICMD_VCL_LABEL,             "", vcl_cli_label },
1167
        { NULL }
1168
};
1169
1170
void
1171 3714
VCL_Init(void)
1172
{
1173
1174 3714
        assert(cache_param->workspace_client > 0);
1175 7428
        WS_Init(&ws_cli, "cli", malloc(cache_param->workspace_client),
1176 3714
            cache_param->workspace_client);
1177 3714
        ws_snapshot_cli = WS_Snapshot(&ws_cli);
1178 3714
        CLI_AddFuncs(vcl_cmds);
1179 3714
        Lck_New(&vcl_mtx, lck_vcl);
1180 3714
        VSL_Setup(&vsl_cli, NULL, 0);
1181 3714
}