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 75137
VCL_Bo2Ctx(struct vrt_ctx *ctx, struct busyobj *bo)
80
{
81
82 75137
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
83 75137
        CHECK_OBJ_NOTNULL(bo, BUSYOBJ_MAGIC);
84 75137
        CHECK_OBJ_NOTNULL(bo->wrk, WORKER_MAGIC);
85 75137
        ctx->vcl = bo->vcl;
86 75137
        ctx->syntax = ctx->vcl->conf->syntax;
87 75137
        ctx->vsl = bo->vsl;
88 75137
        ctx->http_bereq = bo->bereq;
89 75137
        ctx->http_beresp = bo->beresp;
90 75137
        ctx->bo = bo;
91 75137
        ctx->sp = bo->sp;
92 75137
        ctx->now = bo->t_prev;
93 75137
        ctx->ws = bo->ws;
94 75137
        ctx->vpi = bo->wrk->vpi;
95 75137
        ctx->vpi->handling = 0;
96 75137
        ctx->vpi->trace = bo->trace;
97 75137
}
98
99
void
100 120098
VCL_Req2Ctx(struct vrt_ctx *ctx, struct req *req)
101
{
102
103 120098
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
104 120098
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
105 120098
        CHECK_OBJ_NOTNULL(req->doclose, STREAM_CLOSE_MAGIC);
106
107 120098
        ctx->vcl = req->vcl;
108 120098
        ctx->syntax = ctx->vcl->conf->syntax;
109 120098
        ctx->vsl = req->vsl;
110 120098
        ctx->http_req = req->http;
111 120098
        CHECK_OBJ_NOTNULL(req->top, REQTOP_MAGIC);
112 120098
        ctx->http_req_top = req->top->topreq->http;
113 120098
        ctx->http_resp = req->resp;
114 120098
        ctx->req = req;
115 120098
        ctx->sp = req->sp;
116 120098
        ctx->now = req->t_prev;
117 120098
        ctx->ws = req->ws;
118 120098
        ctx->vpi = req->wrk->vpi;
119 120098
        ctx->vpi->handling = 0;
120 120098
        ctx->vpi->trace = req->trace;
121 120098
}
122
123
/*--------------------------------------------------------------------*/
124
125
struct vrt_ctx *
126 18678
VCL_Get_CliCtx(int msg)
127
{
128
129 18678
        ASSERT_CLI();
130 18678
        INIT_OBJ(&ctx_cli, VRT_CTX_MAGIC);
131 18678
        INIT_OBJ(&wrk_vpi_cli, WRK_VPI_MAGIC);
132 18678
        ctx_cli.vpi = &wrk_vpi_cli;
133 18678
        wrk_vpi_cli.trace = FEATURE(FEATURE_TRACE);
134 18678
        ctx_cli.now = VTIM_real();
135 18678
        if (msg) {
136 11770
                ctx_cli.msg = VSB_new_auto();
137 11770
                AN(ctx_cli.msg);
138 11770
        } else {
139 6908
                ctx_cli.vsl = &vsl_cli;
140
        }
141 18678
        ctx_cli.ws = &ws_cli;
142 18678
        WS_Assert(ctx_cli.ws);
143 18678
        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 18673
VCL_Rel_CliCtx(struct vrt_ctx **ctx)
156
{
157 18673
        struct vsb *r = NULL;
158
159 18673
        ASSERT_CLI();
160 18673
        assert(*ctx == &ctx_cli);
161 18673
        AN((*ctx)->vpi);
162 18673
        if (ctx_cli.msg) {
163 11770
                TAKE_OBJ_NOTNULL(r, &ctx_cli.msg, VSB_MAGIC);
164 11770
                AZ(VSB_finish(r));
165 11770
        }
166 18673
        if (ctx_cli.vsl)
167 6903
                VSL_Flush(ctx_cli.vsl, 0);
168 18673
        WS_Assert(ctx_cli.ws);
169 18673
        WS_Rollback(&ws_cli, ws_snapshot_cli);
170 18673
        INIT_OBJ(*ctx, VRT_CTX_MAGIC);
171 18673
        *ctx = NULL;
172
173 18673
        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 12383
vcl_event_handling(VRT_CTX)
189
{
190 12383
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
191
192 12383
        if (ctx->vpi->handling == 0)
193 12168
                return (0);
194
195 215
        assert(ctx->vpi->handling == VCL_RET_FAIL);
196
197 215
        if (ctx->method == VCL_MET_INIT)
198 205
                return (1);
199
200
        /*
201
         * EVENT_WARM / EVENT_COLD: method == 0
202
         * must not set handling
203
         */
204 10
        assert(ctx->method == VCL_MET_FINI);
205
206 10
        ctx->vpi->handling = 0;
207 10
        VRT_fail(ctx, "VRT_fail() from vcl_fini{} has no effect");
208 10
        return (0);
209 12383
}
210
211
static int
212 12388
vcl_send_event(struct vcl *vcl, enum vcl_event_e ev, struct vsb **msg)
213
{
214
        int r, havemsg;
215 12388
        unsigned method = 0;
216
        struct vrt_ctx *ctx;
217
218 12388
        ASSERT_CLI();
219 12388
        ASSERT_VCL_ACTIVE();
220
221 12388
        CHECK_OBJ_NOTNULL(vcl, VCL_MAGIC);
222 12388
        CHECK_OBJ_NOTNULL(vcl->conf, VCL_CONF_MAGIC);
223 12388
        AN(msg);
224 12388
        AZ(*msg);
225
226 12388
        switch (ev) {
227
        case VCL_EVENT_LOAD:
228 5985
                method = VCL_MET_INIT;
229
                /* FALLTHROUGH */
230
        case VCL_EVENT_WARM:
231 11770
                havemsg = 1;
232 11770
                break;
233
        case VCL_EVENT_DISCARD:
234 384
                method = VCL_MET_FINI;
235
                /* FALLTHROUGH */
236
        case VCL_EVENT_COLD:
237 618
                havemsg = 0;
238 618
                break;
239
        default:
240 0
                WRONG("vcl_event");
241 0
        }
242
243 12388
        ctx = VCL_Get_CliCtx(havemsg);
244
245 12388
        AN(ctx->vpi);
246 12388
        AZ(ctx->vpi->handling);
247 12388
        AN(ctx->ws);
248
249 12388
        ctx->vcl = vcl;
250 12388
        ctx->syntax = ctx->vcl->conf->syntax;
251 12388
        ctx->method = method;
252
253 12388
        VCL_TaskEnter(cli_task_privs);
254 12388
        r = ctx->vcl->conf->event_vcl(ctx, ev);
255 12388
        VCL_TaskLeave(ctx, cli_task_privs);
256 12388
        r |= vcl_event_handling(ctx);
257
258 12388
        *msg = VCL_Rel_CliCtx(&ctx);
259
260 12388
        if (r && (ev == VCL_EVENT_COLD || ev == VCL_EVENT_DISCARD))
261 0
                WRONG("A VMOD cannot fail COLD or DISCARD events");
262
263 12388
        return (r);
264
}
265
266
/*--------------------------------------------------------------------*/
267
268
struct vcl *
269 12450
vcl_find(const char *name)
270
{
271
        struct vcl *vcl;
272
273 12450
        ASSERT_CLI();
274 18826
        VTAILQ_FOREACH(vcl, &vcl_head, list) {
275 12711
                if (vcl->discard)
276 6
                        continue;
277 12705
                if (!strcmp(vcl->loaded_name, name))
278 6335
                        return (vcl);
279 6370
        }
280 6115
        return (NULL);
281 12450
}
282
283
/*--------------------------------------------------------------------*/
284
285
static void
286 35
vcl_panic_conf(struct vsb *vsb, const struct VCL_conf *conf)
287
{
288
        unsigned u;
289
        const struct vpi_ii *ii;
290
291 35
        if (PAN_dump_struct(vsb, conf, VCL_CONF_MAGIC, "conf"))
292 0
                return;
293 35
        VSB_printf(vsb, "syntax = \"%u\",\n", conf->syntax);
294 35
        VSB_cat(vsb, "srcname = {\n");
295 35
        VSB_indent(vsb, 2);
296 105
        for (u = 0; u < conf->nsrc; ++u)
297 70
                VSB_printf(vsb, "[%u] = \"%s\",\n", u, conf->srcname[u]);
298 35
        VSB_indent(vsb, -2);
299 35
        VSB_cat(vsb, "},\n");
300 35
        VSB_cat(vsb, "instances = {\n");
301 35
        VSB_indent(vsb, 2);
302 35
        ii = conf->instance_info;
303 55
        while (ii != NULL && ii->p != NULL) {
304 40
                VSB_printf(vsb, "\"%s\" = %p,\n", ii->name,
305 20
                    (const void *)*(const uintptr_t *)ii->p);
306 20
                ii++;
307
        }
308 35
        VSB_indent(vsb, -2);
309 35
        VSB_cat(vsb, "},\n");
310 35
        VSB_indent(vsb, -2);
311 35
        VSB_cat(vsb, "},\n");
312 35
}
313
314
void
315 65
VCL_Panic(struct vsb *vsb, const char *nm, const struct vcl *vcl)
316
{
317
318 65
        AN(vsb);
319 65
        if (PAN_dump_struct(vsb, vcl, VCL_MAGIC, "vcl[%s]", nm))
320 30
                return;
321 35
        VSB_printf(vsb, "name = \"%s\",\n", vcl->loaded_name);
322 35
        VSB_printf(vsb, "busy = %u,\n", vcl->busy);
323 35
        VSB_printf(vsb, "discard = %u,\n", vcl->discard);
324 35
        VSB_printf(vsb, "state = %s,\n", vcl->state);
325 35
        VSB_printf(vsb, "temp = %s,\n", vcl->temp ? vcl->temp->name : "(null)");
326 35
        vcl_panic_conf(vsb, vcl->conf);
327 35
        VSB_indent(vsb, -2);
328 35
        VSB_cat(vsb, "},\n");
329 65
}
330
331
/*--------------------------------------------------------------------*/
332
333
void
334 6903
VCL_Update(struct vcl **vcc, struct vcl *vcl)
335
{
336
        struct vcl *old;
337
338 6903
        AN(vcc);
339
340 6903
        old = *vcc;
341 6903
        *vcc = NULL;
342
343 6903
        CHECK_OBJ_ORNULL(old, VCL_MAGIC);
344 6903
        ASSERT_VCL_ACTIVE();
345
346 6903
        Lck_Lock(&vcl_mtx);
347 6903
        if (old != NULL) {
348 353
                assert(old->busy > 0);
349 353
                old->busy--;
350 353
        }
351
352 6903
        if (vcl == NULL)
353 6838
                vcl = vcl_active; /* Sample vcl_active under lock to avoid
354
                                   * race */
355 6903
        CHECK_OBJ_NOTNULL(vcl, VCL_MAGIC);
356 6903
        if (vcl->label == NULL) {
357 6823
                AN(strcmp(vcl->state, VCL_TEMP_LABEL->name));
358 6823
                *vcc = vcl;
359 6823
        } else {
360 80
                AZ(strcmp(vcl->state, VCL_TEMP_LABEL->name));
361 80
                *vcc = vcl->label;
362
        }
363 6903
        CHECK_OBJ_NOTNULL(*vcc, VCL_MAGIC);
364 6903
        AZ((*vcc)->discard);
365 6903
        (*vcc)->busy++;
366 6903
        Lck_Unlock(&vcl_mtx);
367 6903
        assert((*vcc)->temp->is_warm);
368 6903
}
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 11980
vdire_new(struct lock *mtx, const struct vcltemp **tempp)
380
{
381
        struct vdire *vdire;
382
383 11980
        ALLOC_OBJ(vdire, VDIRE_MAGIC);
384 11980
        AN(vdire);
385 11980
        AN(mtx);
386 11980
        VTAILQ_INIT(&vdire->directors);
387 11980
        VTAILQ_INIT(&vdire->resigning);
388 11980
        vdire->mtx = mtx;
389 11980
        PTOK(pthread_cond_init(&vdire->cond, NULL));
390 11980
        vdire->tempp = tempp;
391 11980
        return (vdire);
392
}
393
394
/* starting an interation prevents removals from the directors list */
395
void
396 11093
vdire_start_iter(struct vdire *vdire)
397
{
398
399 11093
        CHECK_OBJ_NOTNULL(vdire, VDIRE_MAGIC);
400
401
        // https://github.com/varnishcache/varnish-cache/pull/4142#issuecomment-2593091097
402 11093
        ASSERT_CLI();
403
404 11093
        Lck_Lock(vdire->mtx);
405 11093
        while (! VTAILQ_EMPTY(&vdire->resigning))
406 0
                (void)Lck_CondWait(&vdire->cond, vdire->mtx);
407 11093
        vdire->iterating++;
408 11093
        Lck_Unlock(vdire->mtx);
409 11093
}
410
411
void
412 11093
vdire_end_iter(struct vdire *vdire)
413
{
414 11093
        struct vcldir_head resigning = VTAILQ_HEAD_INITIALIZER(resigning);
415 11093
        const struct vcltemp *temp = NULL;
416
        struct vcldir *vdir, *next;
417
        unsigned n;
418
419 11093
        CHECK_OBJ_NOTNULL(vdire, VDIRE_MAGIC);
420
421 11093
        Lck_Lock(vdire->mtx);
422 11093
        AN(vdire->iterating);
423 11093
        n = --vdire->iterating;
424
425 11093
        if (n == 0) {
426 11093
                VTAILQ_SWAP(&vdire->resigning, &resigning, vcldir, resigning_list);
427 11093
                VTAILQ_FOREACH(vdir, &resigning, resigning_list)
428 0
                        VTAILQ_REMOVE(&vdire->directors, vdir, directors_list);
429 11093
                temp = *vdire->tempp;
430 11093
                PTOK(pthread_cond_broadcast(&vdire->cond));
431 11093
        }
432 11093
        Lck_Unlock(vdire->mtx);
433
434 11093
        VTAILQ_FOREACH_SAFE(vdir, &resigning, resigning_list, next)
435 0
                vcldir_retire(vdir, temp);
436 11093
}
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 539
vdire_resign(struct vdire *vdire, struct vcldir *vdir)
442
{
443 539
        const struct vcltemp *temp = NULL;
444
445 539
        CHECK_OBJ_NOTNULL(vdire, VDIRE_MAGIC);
446 539
        AN(vdir);
447
448 539
        Lck_Lock(vdire->mtx);
449 539
        if (vdire->iterating != 0) {
450 0
                VTAILQ_INSERT_TAIL(&vdire->resigning, vdir, resigning_list);
451 0
                vdir = NULL;
452 0
        } else {
453 539
                VTAILQ_REMOVE(&vdire->directors, vdir, directors_list);
454 539
                temp = *vdire->tempp;
455
        }
456 539
        Lck_Unlock(vdire->mtx);
457
458 539
        if (vdir != NULL)
459 539
                vcldir_retire(vdir, temp);
460 539
}
461
462
// unlocked version of vcl_iterdir
463
// pat can also be NULL (to iterate all)
464
static int
465 4715
vdire_iter(struct cli *cli, const char *pat, const struct vcl *vcl,
466
    vcl_be_func *func, void *priv)
467
{
468 4715
        int i, found = 0;
469
        struct vcldir *vdir;
470 4715
        struct vdire *vdire = vcl->vdire;
471
472 4715
        vdire_start_iter(vdire);
473 11620
        VTAILQ_FOREACH(vdir, &vdire->directors, directors_list) {
474 6905
                if (vdir->refcnt == 0)
475 0
                        continue;
476 6905
                if (pat != NULL && fnmatch(pat, vdir->cli_name, 0))
477 465
                        continue;
478 6440
                found++;
479 6440
                i = func(cli, vdir->dir, priv);
480 6440
                if (i < 0) {
481 0
                        found = i;
482 0
                        break;
483
                }
484 6440
                found += i;
485 6440
        }
486 4715
        vdire_end_iter(vdire);
487 4715
        return (found);
488
}
489
490
491
/*--------------------------------------------------------------------*/
492
493
// XXX locked case across VCLs - should do without
494
static int
495 30
vcl_iterdir(struct cli *cli, const char *pat, const struct vcl *vcl,
496
    vcl_be_func *func, void *priv)
497
{
498 30
        int i, found = 0;
499
        struct vcldir *vdir;
500
501 30
        Lck_AssertHeld(&vcl_mtx);
502 75
        VTAILQ_FOREACH(vdir, &vcl->vdire->directors, directors_list) {
503 45
                if (fnmatch(pat, vdir->cli_name, 0))
504 20
                        continue;
505 25
                found++;
506 25
                i = func(cli, vdir->dir, priv);
507 25
                if (i < 0)
508 0
                        return (i);
509 25
                found += i;
510 25
        }
511 30
        return (found);
512 30
}
513
514
int
515 4745
VCL_IterDirector(struct cli *cli, const char *pat,
516
    vcl_be_func *func, void *priv)
517
{
518 4745
        int i, found = 0;
519
        struct vsb *vsb;
520
        struct vcl *vcl;
521
522 4745
        ASSERT_CLI();
523 4745
        ASSERT_VCL_ACTIVE();
524 4745
        vsb = VSB_new_auto();
525 4745
        AN(vsb);
526 4745
        if (pat == NULL || *pat == '\0' || !strcmp(pat, "*")) {
527
                // all backends in active VCL
528 4525
                VSB_printf(vsb, "%s.*", VCL_Name(vcl_active));
529 4525
                vcl = vcl_active;
530 4745
        } else if (strchr(pat, '.') == NULL) {
531
                // pattern applies to active vcl
532 190
                VSB_printf(vsb, "%s.%s", VCL_Name(vcl_active), pat);
533 190
                vcl = vcl_active;
534 190
        } else {
535
                // use pattern as is
536 30
                VSB_cat(vsb, pat);
537 30
                vcl = NULL;
538
        }
539 4745
        AZ(VSB_finish(vsb));
540 4745
        if (vcl != NULL) {
541 4715
                found = vdire_iter(cli, VSB_data(vsb), vcl, func, priv);
542 4715
        } else {
543 30
                Lck_Lock(&vcl_mtx);
544 60
                VTAILQ_FOREACH(vcl, &vcl_head, list) {
545 30
                        i = vcl_iterdir(cli, VSB_data(vsb), vcl, func, priv);
546 30
                        if (i < 0) {
547 0
                                found = i;
548 0
                                break;
549
                        } else {
550 30
                                found += i;
551
                        }
552 30
                }
553 30
                Lck_Unlock(&vcl_mtx);
554
        }
555 4745
        VSB_destroy(&vsb);
556 4745
        return (found);
557
}
558
559
static void
560 5989
vcl_BackendEvent(const struct vcl *vcl, enum vcl_event_e e)
561
{
562
        struct vcldir *vdir;
563
        struct vdire *vdire;
564
565 5989
        ASSERT_CLI();
566 5989
        CHECK_OBJ_NOTNULL(vcl, VCL_MAGIC);
567 5989
        AZ(vcl->busy);
568 5989
        vdire = vcl->vdire;
569
570 5989
        vdire_start_iter(vdire);
571 13588
        VTAILQ_FOREACH(vdir, &vdire->directors, directors_list)
572 7599
                VDI_Event(vdir->dir, e);
573 5989
        vdire_end_iter(vdire);
574 5989
}
575
576
static void
577 384
vcl_KillBackends(const struct vcl *vcl)
578
{
579
        struct vcldir *vdir, *vdir2;
580
        struct vdire *vdire;
581
582 384
        CHECK_OBJ_NOTNULL(vcl, VCL_MAGIC);
583 384
        assert(vcl->temp == VCL_TEMP_COLD || vcl->temp == VCL_TEMP_INIT);
584 384
        vdire = vcl->vdire;
585 384
        CHECK_OBJ_NOTNULL(vdire, VDIRE_MAGIC);
586
587
        /*
588
         * ensure all retirement has finished (vdire_start_iter waits for it)
589
         */
590 384
        vdire_start_iter(vdire);
591 384
        vdire_end_iter(vdire);
592 384
        AZ(vdire->iterating);
593 384
        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 713
        VTAILQ_FOREACH_SAFE(vdir, &vdire->directors, directors_list, vdir2)
600 329
                VDI_Event(vdir->dir, VCL_EVENT_DISCARD);
601 384
        assert(VTAILQ_EMPTY(&vdire->directors));
602 384
}
603
604
/*--------------------------------------------------------------------*/
605
606
static struct vcl *
607 11990
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 11990
        AN(fn);
616 11990
        AN(msg);
617
618
#ifdef RTLD_NOLOAD
619
        /* Detect bogus caching by dlopen(3) */
620 11990
        dlh = dlopen(fn, RTLD_NOW | RTLD_NOLOAD);
621 11990
        AZ(dlh);
622
#endif
623 11990
        dlh = dlopen(fn, RTLD_NOW | RTLD_LOCAL);
624 11990
        if (dlh == NULL) {
625 5
                err = errno;
626 5
                dlerr = dlerror();
627 5
                VSB_cat(msg, "Could not load compiled VCL.\n");
628 5
                if (dlerr != NULL)
629 5
                        VSB_printf(msg, "\tdlopen() = %s\n", dlerr);
630 5
                if (err) {
631 0
                        VSB_printf(msg, "\terror = %s (%d)\n",
632 0
                            strerror(err), err);
633 0
                }
634 5
                VSB_cat(msg, "\thint: check for \"noexec\" mount\n");
635 5
                VSB_cat(msg, "\thint: check \"vmod_path\" parameter\n");
636 5
                return (NULL);
637
        }
638 11985
        cnf = dlsym(dlh, "VCL_conf");
639 11985
        if (cnf == NULL) {
640 0
                VSB_cat(msg, "Compiled VCL lacks metadata.\n");
641 0
                (void)dlclose(dlh);
642 0
                return (NULL);
643
        }
644 11985
        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 11985
        if (cnf->syntax < heritage.min_vcl_version ||
650 11980
            cnf->syntax > heritage.max_vcl_version) {
651 10
                VSB_printf(msg, "Compiled VCL version (%.1f) not supported.\n",
652 5
                    .1 * cnf->syntax);
653 5
                (void)dlclose(dlh);
654 5
                return (NULL);
655
        }
656 11980
        ALLOC_OBJ(vcl, VCL_MAGIC);
657 11980
        AN(vcl);
658 11980
        vcl->dlh = dlh;
659 11980
        vcl->conf = cnf;
660 11980
        vcl->vdire = vdire_new(&vcl_mtx, &vcl->temp);
661 11980
        return (vcl);
662 11990
}
663
664
static void
665 6379
VCL_Close(struct vcl **vclp)
666
{
667
        struct vcl *vcl;
668
669 6379
        TAKE_OBJ_NOTNULL(vcl, vclp, VCL_MAGIC);
670 6379
        assert(VTAILQ_EMPTY(&vcl->filters));
671 6379
        AZ(dlclose(vcl->dlh));
672 6379
        PTOK(pthread_cond_destroy(&vcl->vdire->cond));
673 6379
        FREE_OBJ(vcl->vdire);
674 6379
        FREE_OBJ(vcl);
675 6379
}
676
677
/*--------------------------------------------------------------------
678
 * NB: This function is called in/from the test-load subprocess.
679
 */
680
681
int
682 6005
VCL_TestLoad(const char *fn)
683
{
684
        struct vsb *vsb;
685
        struct vcl *vcl;
686 6005
        int retval = 0;
687
688 6005
        AN(fn);
689 6005
        vsb = VSB_new_auto();
690 6005
        AN(vsb);
691 6005
        vcl = VCL_Open(fn, vsb);
692 6005
        if (vcl == NULL) {
693 10
                AZ(VSB_finish(vsb));
694 10
                fprintf(stderr, "%s", VSB_data(vsb));
695 10
                retval = -1;
696 10
        } else
697 5995
                VCL_Close(&vcl);
698 6005
        VSB_destroy(&vsb);
699 6005
        return (retval);
700
}
701
702
/*--------------------------------------------------------------------*/
703
704
static struct vsb *
705 5
vcl_print_refs(const struct vcl *vcl)
706
{
707
        struct vsb *msg;
708
        struct vclref *ref;
709
710 5
        CHECK_OBJ_NOTNULL(vcl, VCL_MAGIC);
711 5
        msg = VSB_new_auto();
712 5
        AN(msg);
713
714 5
        VSB_printf(msg, "VCL %s is waiting for:", vcl->loaded_name);
715 5
        Lck_Lock(&vcl_mtx);
716 10
        VTAILQ_FOREACH(ref, &vcl->ref_list, list)
717 5
                VSB_printf(msg, "\n\t- %s", ref->desc);
718 5
        Lck_Unlock(&vcl_mtx);
719 5
        AZ(VSB_finish(msg));
720 5
        return (msg);
721
}
722
723
static int
724 6290
vcl_set_state(struct vcl *vcl, const char *state, struct vsb **msg)
725
{
726 6290
        struct vsb *nomsg = NULL;
727 6290
        int i = 0;
728
729 6290
        ASSERT_CLI();
730
731 6290
        CHECK_OBJ_NOTNULL(vcl, VCL_MAGIC);
732 6290
        AN(state);
733 6290
        AN(msg);
734 6290
        AZ(*msg);
735
736 6290
        AN(vcl->temp);
737
738 6290
        switch (state[0]) {
739
        case '0':
740 490
                if (vcl->temp == VCL_TEMP_COLD)
741 10
                        break;
742 480
                if (vcl->busy == 0 && vcl->temp->is_warm) {
743 214
                        Lck_Lock(&vcl_mtx);
744 214
                        vcl->temp = VTAILQ_EMPTY(&vcl->ref_list) ?
745
                            VCL_TEMP_COLD : VCL_TEMP_COOLING;
746 214
                        Lck_Unlock(&vcl_mtx);
747 214
                        vcl_BackendEvent(vcl, VCL_EVENT_COLD);
748 214
                        AZ(vcl_send_event(vcl, VCL_EVENT_COLD, msg));
749 214
                        AZ(*msg);
750 214
                }
751 266
                else if (vcl->busy)
752 156
                        vcl->temp = VCL_TEMP_BUSY;
753 110
                else if (VTAILQ_EMPTY(&vcl->ref_list))
754 90
                        vcl->temp = VCL_TEMP_COLD;
755
                else
756 20
                        vcl->temp = VCL_TEMP_COOLING;
757 480
                break;
758
        case '1':
759 5800
                if (vcl->temp == VCL_TEMP_WARM)
760 5
                        break;
761
                /* The warm VCL hasn't seen a cold event yet */
762 5795
                if (vcl->temp == VCL_TEMP_BUSY)
763 5
                        vcl->temp = VCL_TEMP_WARM;
764
                /* The VCL must first reach a stable cold state */
765 5790
                else if (vcl->temp == VCL_TEMP_COOLING) {
766 5
                        *msg = vcl_print_refs(vcl);
767 5
                        i = -1;
768 5
                }
769
                else {
770 5785
                        Lck_Lock(&vcl_mtx);
771 5785
                        vcl->temp = VCL_TEMP_WARM;
772 5785
                        Lck_Unlock(&vcl_mtx);
773 5785
                        i = vcl_send_event(vcl, VCL_EVENT_WARM, msg);
774 5785
                        if (i == 0) {
775 5770
                                vcl_BackendEvent(vcl, VCL_EVENT_WARM);
776 5770
                                break;
777
                        }
778 15
                        AZ(vcl_send_event(vcl, VCL_EVENT_COLD, &nomsg));
779 15
                        AZ(nomsg);
780 15
                        vcl->temp = VCL_TEMP_COLD;
781
                }
782 25
                break;
783
        default:
784 0
                WRONG("Wrong enum state");
785 0
        }
786 6290
        if (i == 0 && state[1])
787 6040
                bprintf(vcl->state, "%s", state + 1);
788
789 6290
        return (i);
790
}
791
792
static void
793 215
vcl_cancel_load(struct vcl *vcl, struct cli *cli, struct vsb *msg,
794
    const char *name, const char *step)
795
{
796
797 215
        CHECK_OBJ_NOTNULL(vcl, VCL_MAGIC);
798 215
        CHECK_OBJ_NOTNULL(vcl->conf, VCL_CONF_MAGIC);
799
800 215
        VCLI_SetResult(cli, CLIS_CANT);
801 215
        VCLI_Out(cli, "VCL \"%s\" Failed %s", name, step);
802 215
        if (VSB_len(msg))
803 215
                VCLI_Out(cli, "\nMessage:\n\t%s", VSB_data(msg));
804 215
        VSB_destroy(&msg);
805
806 215
        AZ(vcl_send_event(vcl, VCL_EVENT_DISCARD, &msg));
807 215
        AZ(msg);
808
809 215
        vcl_KillBackends(vcl);
810 215
        free(vcl->loaded_name);
811 215
        VCL_Close(&vcl);
812 215
}
813
814
static void
815 5985
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 5985
        ASSERT_CLI();
822 5985
        ASSERT_VCL_ACTIVE();
823
824 5985
        vcl = vcl_find(name);
825 5985
        AZ(vcl);
826
827 5985
        msg = VSB_new_auto();
828 5985
        AN(msg);
829 5985
        vcl = VCL_Open(fn, msg);
830 5985
        AZ(VSB_finish(msg));
831 5985
        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 5985
        VSB_destroy(&msg);
839
840 5985
        vcl->loaded_name = strdup(name);
841 5985
        XXXAN(vcl->loaded_name);
842 5985
        VTAILQ_INIT(&vcl->ref_list);
843 5985
        VTAILQ_INIT(&vcl->filters);
844
845 5985
        vcl->temp = VCL_TEMP_INIT;
846
847 5985
        if (vcl_send_event(vcl, VCL_EVENT_LOAD, &msg)) {
848 210
                vcl_cancel_load(vcl, cli, msg, name, "initialization");
849 210
                return;
850
        }
851 5775
        VSB_destroy(&msg);
852
853 5775
        if (vcl_set_state(vcl, state, &msg)) {
854 5
                assert(*state == '1');
855 5
                vcl_cancel_load(vcl, cli, msg, name, "warmup");
856 5
                return;
857
        }
858 5770
        if (msg)
859 5750
                VSB_destroy(&msg);
860
861 5770
        VCLI_Out(cli, "Loaded \"%s\" as \"%s\"", fn , name);
862 5770
        VTAILQ_INSERT_TAIL(&vcl_head, vcl, list);
863 5770
        VSC_C_main->n_vcl++;
864 5770
        VSC_C_main->n_vcl_avail++;
865 5985
}
866
867
/*--------------------------------------------------------------------*/
868
869
void
870 37734
VCL_Poll(void)
871
{
872 37734
        struct vsb *nomsg = NULL;
873
        struct vcl *vcl, *vcl2;
874
875 37734
        ASSERT_CLI();
876 37734
        ASSERT_VCL_ACTIVE();
877 79498
        VTAILQ_FOREACH_SAFE(vcl, &vcl_head, list, vcl2) {
878 41764
                if (vcl->temp == VCL_TEMP_BUSY ||
879 41624
                    vcl->temp == VCL_TEMP_COOLING)
880 230
                        AZ(vcl_set_state(vcl, "0", &nomsg));
881 41764
                AZ(nomsg);
882 41764
                if (vcl->discard && vcl->temp == VCL_TEMP_COLD) {
883 169
                        AZ(vcl->busy);
884 169
                        assert(vcl != vcl_active);
885 169
                        assert(VTAILQ_EMPTY(&vcl->ref_list));
886 169
                        VTAILQ_REMOVE(&vcl_head, vcl, list);
887 169
                        AZ(vcl_send_event(vcl, VCL_EVENT_DISCARD, &nomsg));
888 169
                        AZ(nomsg);
889 169
                        vcl_KillBackends(vcl);
890 169
                        free(vcl->loaded_name);
891 169
                        VCL_Close(&vcl);
892 169
                        VSC_C_main->n_vcl--;
893 169
                        VSC_C_main->n_vcl_discard--;
894 169
                }
895 41764
        }
896 37734
}
897
898
/*--------------------------------------------------------------------*/
899
900
static void v_matchproto_(cli_func_t)
901 375
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 375
        (void)av;
910 375
        (void)priv;
911 375
        ASSERT_CLI();
912 375
        ASSERT_VCL_ACTIVE();
913 375
        vte = VTE_new(7, 80);
914 375
        AN(vte);
915 1201
        VTAILQ_FOREACH(vcl, &vcl_head, list) {
916 826
                if (vcl == vcl_active) {
917 375
                        flg = "active";
918 826
                } else if (vcl->discard) {
919 21
                        flg = "discarded";
920 21
                } else
921 430
                        flg = "available";
922 1652
                VTE_printf(vte, "%s\t%s\t%s\t\v%u\t%s", flg, vcl->state,
923 826
                    vcl->temp->name, vcl->busy, vcl->loaded_name);
924 826
                if (vcl->label != NULL) {
925 95
                        VTE_printf(vte, "\t->\t%s", vcl->label->loaded_name);
926 95
                        if (vcl->nrefs)
927 40
                                VTE_printf(vte, " (%d return(vcl)%s)",
928 20
                                    vcl->nrefs, vcl->nrefs > 1 ? "'s" : "");
929 826
                } else if (vcl->nlabels > 0) {
930 130
                        VTE_printf(vte, "\t<-\t(%d label%s)",
931 65
                            vcl->nlabels, vcl->nlabels > 1 ? "s" : "");
932 65
                }
933 826
                VTE_cat(vte, "\n");
934 826
        }
935 375
        AZ(VTE_finish(vte));
936 375
        AZ(VTE_format(vte, VCLI_VTE_format, cli));
937 375
        VTE_destroy(&vte);
938 375
}
939
940
static void v_matchproto_(cli_func_t)
941 25
vcl_cli_list_json(struct cli *cli, const char * const *av, void *priv)
942
{
943
        struct vcl *vcl;
944
945 25
        (void)priv;
946 25
        ASSERT_CLI();
947 25
        ASSERT_VCL_ACTIVE();
948 25
        VCLI_JSON_begin(cli, 2, av);
949 25
        VCLI_Out(cli, ",\n");
950 105
        VTAILQ_FOREACH(vcl, &vcl_head, list) {
951 80
                VCLI_Out(cli, "{\n");
952 80
                VSB_indent(cli->sb, 2);
953 80
                VCLI_Out(cli, "\"status\": ");
954 80
                if (vcl == vcl_active) {
955 25
                        VCLI_Out(cli, "\"active\",\n");
956 80
                } else if (vcl->discard) {
957 0
                        VCLI_Out(cli, "\"discarded\",\n");
958 0
                } else
959 55
                        VCLI_Out(cli, "\"available\",\n");
960 80
                VCLI_Out(cli, "\"state\": \"%s\",\n", vcl->state);
961 80
                VCLI_Out(cli, "\"temperature\": \"%s\",\n", vcl->temp->name);
962 80
                VCLI_Out(cli, "\"busy\": %u,\n", vcl->busy);
963 80
                VCLI_Out(cli, "\"name\": \"%s\"", vcl->loaded_name);
964 80
                if (vcl->label != NULL) {
965 30
                        VCLI_Out(cli, ",\n");
966 30
                        VCLI_Out(cli, "\"label\": {\n");
967 30
                        VSB_indent(cli->sb, 2);
968 60
                                VCLI_Out(cli, "\"name\": \"%s\"",
969 30
                                         vcl->label->loaded_name);
970 30
                        if (vcl->nrefs)
971 0
                                VCLI_Out(cli, ",\n\"refs\": %d", vcl->nrefs);
972 30
                        VCLI_Out(cli, "\n");
973 30
                        VCLI_Out(cli, "}");
974 30
                        VSB_indent(cli->sb, -2);
975 80
                } else if (vcl->nlabels > 0) {
976 10
                        VCLI_Out(cli, ",\n");
977 10
                        VCLI_Out(cli, "\"labels\": %d", vcl->nlabels);
978 10
                }
979 80
                VSB_indent(cli->sb, -2);
980 80
                VCLI_Out(cli, "\n}");
981 80
                if (VTAILQ_NEXT(vcl, list) != NULL)
982 55
                        VCLI_Out(cli, ",\n");
983 80
        }
984 25
        VCLI_JSON_end(cli);
985 25
}
986
987
static void v_matchproto_(cli_func_t)
988 5985
vcl_cli_load(struct cli *cli, const char * const *av, void *priv)
989
{
990
991 5985
        AZ(priv);
992 5985
        ASSERT_CLI();
993
        // XXX move back code from vcl_load?
994 5985
        vcl_load(cli, av[2], av[3], av[4]);
995 5985
}
996
997
static void v_matchproto_(cli_func_t)
998 290
vcl_cli_state(struct cli *cli, const char * const *av, void *priv)
999
{
1000
        struct vcl *vcl;
1001 290
        struct vsb *msg = NULL;
1002
1003 290
        AZ(priv);
1004 290
        ASSERT_CLI();
1005 290
        ASSERT_VCL_ACTIVE();
1006 290
        AN(av[2]);
1007 290
        AN(av[3]);
1008
1009 290
        vcl = vcl_find(av[2]);
1010 290
        AN(vcl);
1011
1012 290
        if (vcl_set_state(vcl, av[3], &msg)) {
1013 15
                CHECK_OBJ_NOTNULL(msg, VSB_MAGIC);
1014
1015 15
                VCLI_SetResult(cli, CLIS_CANT);
1016 30
                VCLI_Out(cli, "Failed <vcl.state %s %s>", vcl->loaded_name,
1017 15
                    av[3] + 1);
1018 15
                if (VSB_len(msg))
1019 15
                        VCLI_Out(cli, "\nMessage:\n\t%s", VSB_data(msg));
1020 15
        }
1021 290
        if (msg)
1022 35
                VSB_destroy(&msg);
1023 290
}
1024
1025
static void v_matchproto_(cli_func_t)
1026 195
vcl_cli_discard(struct cli *cli, const char * const *av, void *priv)
1027
{
1028
        struct vcl *vcl;
1029
1030 195
        ASSERT_CLI();
1031 195
        ASSERT_VCL_ACTIVE();
1032 195
        (void)cli;
1033 195
        AZ(priv);
1034 195
        vcl = vcl_find(av[2]);
1035 195
        CHECK_OBJ_NOTNULL(vcl, VCL_MAGIC);              // MGT ensures this
1036 195
        Lck_Lock(&vcl_mtx);
1037 195
        assert (vcl != vcl_active);     // MGT ensures this
1038 195
        AZ(vcl->nlabels);               // MGT ensures this
1039 195
        VSC_C_main->n_vcl_discard++;
1040 195
        VSC_C_main->n_vcl_avail--;
1041 195
        vcl->discard = 1;
1042 195
        if (vcl->label != NULL) {
1043 20
                AZ(strcmp(vcl->state, VCL_TEMP_LABEL->name));
1044 20
                vcl->label->nlabels--;
1045 20
                vcl->label= NULL;
1046 20
        }
1047 195
        Lck_Unlock(&vcl_mtx);
1048
1049 195
        if (!strcmp(vcl->state, VCL_TEMP_LABEL->name)) {
1050 20
                VTAILQ_REMOVE(&vcl_head, vcl, list);
1051 20
                free(vcl->loaded_name);
1052 20
                AZ(vcl->vdire);
1053 20
                FREE_OBJ(vcl);
1054 195
        } else if (vcl->temp == VCL_TEMP_COLD) {
1055 130
                VCL_Poll();
1056 130
        }
1057 195
}
1058
1059
static void v_matchproto_(cli_func_t)
1060 135
vcl_cli_label(struct cli *cli, const char * const *av, void *priv)
1061
{
1062
        struct vcl *lbl;
1063
        struct vcl *vcl;
1064
1065 135
        ASSERT_CLI();
1066 135
        ASSERT_VCL_ACTIVE();
1067 135
        (void)cli;
1068 135
        (void)priv;
1069 135
        vcl = vcl_find(av[3]);
1070 135
        AN(vcl);                                // MGT ensures this
1071 135
        lbl = vcl_find(av[2]);
1072 135
        if (lbl == NULL) {
1073 120
                ALLOC_OBJ(lbl, VCL_MAGIC);
1074 120
                AN(lbl);
1075 120
                bprintf(lbl->state, "%s", VCL_TEMP_LABEL->name);
1076 120
                lbl->temp = VCL_TEMP_WARM;
1077 120
                REPLACE(lbl->loaded_name, av[2]);
1078 120
                VTAILQ_INSERT_TAIL(&vcl_head, lbl, list);
1079 120
        }
1080 135
        if (lbl->label != NULL)
1081 15
                lbl->label->nlabels--;
1082 135
        lbl->label = vcl;
1083 135
        vcl->nlabels++;
1084 135
}
1085
1086
static void v_matchproto_(cli_func_t)
1087 5555
vcl_cli_use(struct cli *cli, const char * const *av, void *priv)
1088
{
1089
        struct vcl *vcl;
1090
1091 5555
        ASSERT_CLI();
1092 5555
        ASSERT_VCL_ACTIVE();
1093 5555
        AN(cli);
1094 5555
        AZ(priv);
1095 5555
        vcl = vcl_find(av[2]);
1096 5555
        AN(vcl);                                // MGT ensures this
1097 5555
        assert(vcl->temp == VCL_TEMP_WARM);     // MGT ensures this
1098 5555
        Lck_Lock(&vcl_mtx);
1099 5555
        vcl_active = vcl;
1100 5555
        Lck_Unlock(&vcl_mtx);
1101 5555
}
1102
1103
static void v_matchproto_(cli_func_t)
1104 60
vcl_cli_show(struct cli *cli, const char * const *av, void *priv)
1105
{
1106
        struct vcl *vcl;
1107 60
        int verbose = 0;
1108 60
        int i = 2;
1109
        unsigned u;
1110
1111 60
        ASSERT_CLI();
1112 60
        ASSERT_VCL_ACTIVE();
1113 60
        AZ(priv);
1114
1115 60
        if (av[i] != NULL && !strcmp(av[i], "-v")) {
1116 20
                verbose = 1;
1117 20
                i++;
1118 20
        }
1119
1120 60
        if (av[i] == NULL) {
1121 10
                vcl = vcl_active;
1122 10
                AN(vcl);
1123 10
        } else {
1124 50
                vcl = vcl_find(av[i]);
1125 50
                i++;
1126
        }
1127
1128 60
        if (av[i] != NULL) {
1129 5
                VCLI_Out(cli, "Too many parameters: '%s'", av[i]);
1130 5
                VCLI_SetResult(cli, CLIS_PARAM);
1131 5
                return;
1132
        }
1133
1134 55
        if (vcl == NULL) {
1135 5
                VCLI_Out(cli, "No VCL named '%s'", av[i - 1]);
1136 5
                VCLI_SetResult(cli, CLIS_PARAM);
1137 5
                return;
1138
        }
1139 50
        CHECK_OBJ_NOTNULL(vcl, VCL_MAGIC);
1140 50
        if (vcl->label) {
1141 10
                vcl = vcl->label;
1142 10
                CHECK_OBJ_NOTNULL(vcl, VCL_MAGIC);
1143 10
                AZ(vcl->label);
1144 10
        }
1145 50
        CHECK_OBJ_NOTNULL(vcl->conf, VCL_CONF_MAGIC);
1146 50
        if (verbose) {
1147 60
                for (u = 0; u < vcl->conf->nsrc; u++)
1148 80
                        VCLI_Out(cli, "// VCL.SHOW %u %zd %s\n%s\n",
1149 40
                            u, strlen(vcl->conf->srcbody[u]),
1150 40
                            vcl->conf->srcname[u],
1151 40
                            vcl->conf->srcbody[u]);
1152 20
        } else {
1153 30
                VCLI_Out(cli, "%s", vcl->conf->srcbody[0]);
1154
        }
1155 60
}
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 4613
VCL_Init(void)
1172
{
1173
1174 4613
        assert(cache_param->workspace_client > 0);
1175 9226
        WS_Init(&ws_cli, "cli", malloc(cache_param->workspace_client),
1176 4613
            cache_param->workspace_client);
1177 4613
        ws_snapshot_cli = WS_Snapshot(&ws_cli);
1178 4613
        CLI_AddFuncs(vcl_cmds);
1179 4613
        Lck_New(&vcl_mtx, lck_vcl);
1180 4613
        VSL_Setup(&vsl_cli, NULL, 0);
1181 4613
}