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 "vbm.h"
47
#include "vcli_serve.h"
48
#include "vte.h"
49
#include "vtim.h"
50
#include "vcc_interface.h"
51
52
const struct vcltemp VCL_TEMP_INIT[1] = {{ .name = "init", .is_cold = 1 }};
53
const struct vcltemp VCL_TEMP_COLD[1] = {{ .name = "cold", .is_cold = 1 }};
54
const struct vcltemp VCL_TEMP_WARM[1] = {{ .name = "warm", .is_warm = 1 }};
55
const struct vcltemp VCL_TEMP_BUSY[1] = {{ .name = "busy", .is_warm = 1 }};
56
const struct vcltemp VCL_TEMP_COOLING[1] = {{ .name = "cooling" }};
57
58
// not really a temperature
59
static const struct vcltemp VCL_TEMP_LABEL[1] = {{ .name = "label" }};
60
61
/*
62
 * XXX: Presently all modifications to this list happen from the
63
 * CLI event-engine, so no locking is necessary
64
 */
65
static VTAILQ_HEAD(, vcl)       vcl_head =
66
    VTAILQ_HEAD_INITIALIZER(vcl_head);
67
68
struct lock             vcl_mtx;
69
struct vcl              *vcl_active; /* protected by vcl_mtx */
70
71
static struct vrt_ctx ctx_cli;
72
static struct wrk_vpi wrk_vpi_cli;
73
static struct ws ws_cli;
74
static uintptr_t ws_snapshot_cli;
75
static struct vsl_log vsl_cli;
76
77
/*--------------------------------------------------------------------*/
78
79
void
80 31215
VCL_Bo2Ctx(struct vrt_ctx *ctx, struct busyobj *bo)
81
{
82
83 31215
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
84 31215
        CHECK_OBJ_NOTNULL(bo, BUSYOBJ_MAGIC);
85 31215
        CHECK_OBJ_NOTNULL(bo->wrk, WORKER_MAGIC);
86 31215
        ctx->vcl = bo->vcl;
87 31215
        ctx->syntax = ctx->vcl->conf->syntax;
88 31215
        ctx->vsl = bo->vsl;
89 31215
        ctx->http_bereq = bo->bereq;
90 31215
        ctx->http_beresp = bo->beresp;
91 31215
        ctx->bo = bo;
92 31215
        ctx->sp = bo->sp;
93 31215
        ctx->now = bo->t_prev;
94 31215
        ctx->ws = bo->ws;
95 31215
        ctx->vpi = bo->wrk->vpi;
96 31215
        ctx->vpi->handling = 0;
97 31215
        ctx->vpi->trace = bo->trace;
98 31215
}
99
100
void
101 50295
VCL_Req2Ctx(struct vrt_ctx *ctx, struct req *req)
102
{
103
104 50295
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
105 50295
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
106 50295
        CHECK_OBJ_NOTNULL(req->doclose, STREAM_CLOSE_MAGIC);
107
108 50295
        ctx->vcl = req->vcl;
109 50295
        ctx->syntax = ctx->vcl->conf->syntax;
110 50295
        ctx->vsl = req->vsl;
111 50295
        ctx->http_req = req->http;
112 50295
        CHECK_OBJ_NOTNULL(req->top, REQTOP_MAGIC);
113 50295
        ctx->http_req_top = req->top->topreq->http;
114 50295
        ctx->http_resp = req->resp;
115 50295
        ctx->req = req;
116 50295
        ctx->sp = req->sp;
117 50295
        ctx->now = req->t_prev;
118 50295
        ctx->ws = req->ws;
119 50295
        ctx->vpi = req->wrk->vpi;
120 50295
        ctx->vpi->handling = 0;
121 50295
        ctx->vpi->trace = req->trace;
122 50295
}
123
124
/*--------------------------------------------------------------------*/
125
126
struct vrt_ctx *
127 7710
VCL_Get_CliCtx(int msg)
128
{
129
130 7710
        ASSERT_CLI();
131 7710
        INIT_OBJ(&ctx_cli, VRT_CTX_MAGIC);
132 7710
        INIT_OBJ(&wrk_vpi_cli, WRK_VPI_MAGIC);
133 7710
        ctx_cli.vpi = &wrk_vpi_cli;
134 7710
        wrk_vpi_cli.trace = FEATURE(FEATURE_TRACE);
135 7710
        ctx_cli.now = VTIM_real();
136 7710
        if (msg) {
137 4888
                ctx_cli.msg = VSB_new_auto();
138 4888
                AN(ctx_cli.msg);
139 4888
        } else {
140 2822
                ctx_cli.vsl = &vsl_cli;
141
        }
142 7710
        ctx_cli.ws = &ws_cli;
143 7710
        WS_Assert(ctx_cli.ws);
144 7710
        return (&ctx_cli);
145
}
146
147
/*
148
 * releases CLI ctx
149
 *
150
 * returns finished error msg vsb if VCL_Get_CliCtx(1) was called
151
 *
152
 * caller needs to VSB_destroy a non-NULL return value
153
 *
154
 */
155
struct vsb *
156 7708
VCL_Rel_CliCtx(struct vrt_ctx **ctx)
157
{
158 7708
        struct vsb *r = NULL;
159
160 7708
        ASSERT_CLI();
161 7708
        assert(*ctx == &ctx_cli);
162 7708
        AN((*ctx)->vpi);
163 7708
        if (ctx_cli.msg) {
164 4888
                TAKE_OBJ_NOTNULL(r, &ctx_cli.msg, VSB_MAGIC);
165 4888
                AZ(VSB_finish(r));
166 4888
        }
167 7708
        if (ctx_cli.vsl)
168 2820
                VSL_Flush(ctx_cli.vsl, 0);
169 7708
        WS_Assert(ctx_cli.ws);
170 7708
        WS_Rollback(&ws_cli, ws_snapshot_cli);
171 7708
        INIT_OBJ(*ctx, VRT_CTX_MAGIC);
172 7708
        *ctx = NULL;
173
174 7708
        return (r);
175
}
176
177
/*--------------------------------------------------------------------*/
178
179
/* VRT_fail() can be called
180
 * - from the vcl sub via a vmod
181
 * - via a PRIV_TASK .fini callback
182
 *
183
 * if this happens during init, we fail it
184
 * if during fini, we ignore, because otherwise VMOD authors would be forced to
185
 * handle VCL_MET_FINI specifically everywhere.
186
 */
187
188
static int
189 5138
vcl_event_handling(VRT_CTX)
190
{
191 5138
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
192
193 5138
        if (ctx->vpi->handling == 0)
194 5052
                return (0);
195
196 86
        assert(ctx->vpi->handling == VCL_RET_FAIL);
197
198 86
        if (ctx->method == VCL_MET_INIT)
199 82
                return (1);
200
201
        /*
202
         * EVENT_WARM / EVENT_COLD: method == 0
203
         * must not set handling
204
         */
205 4
        assert(ctx->method == VCL_MET_FINI);
206
207 4
        ctx->vpi->handling = 0;
208 4
        VRT_fail(ctx, "VRT_fail() from vcl_fini{} has no effect");
209 4
        return (0);
210 5138
}
211
212
static int
213 5140
vcl_send_event(struct vcl *vcl, enum vcl_event_e ev, struct vsb **msg)
214
{
215
        int r, havemsg;
216 5140
        unsigned method = 0;
217
        struct vrt_ctx *ctx;
218 5140
        void *vbm_alloc = NULL;
219
        size_t sz;
220
221 5140
        ASSERT_CLI();
222 5140
        ASSERT_VCL_ACTIVE();
223
224 5140
        CHECK_OBJ_NOTNULL(vcl, VCL_MAGIC);
225 5140
        CHECK_OBJ_NOTNULL(vcl->conf, VCL_CONF_MAGIC);
226 5140
        AN(msg);
227 5140
        AZ(*msg);
228
229 5140
        switch (ev) {
230
        case VCL_EVENT_LOAD:
231 2484
                method = VCL_MET_INIT;
232
                /* FALLTHROUGH */
233
        case VCL_EVENT_WARM:
234 4888
                havemsg = 1;
235 4888
                break;
236
        case VCL_EVENT_DISCARD:
237 156
                method = VCL_MET_FINI;
238
                /* FALLTHROUGH */
239
        case VCL_EVENT_COLD:
240 252
                havemsg = 0;
241 252
                break;
242
        default:
243 0
                WRONG("vcl_event");
244 0
        }
245
246 5140
        ctx = VCL_Get_CliCtx(havemsg);
247
248 5140
        AN(ctx->vpi);
249 5140
        AZ(ctx->vpi->handling);
250 5140
        AN(ctx->ws);
251
252 5140
        ctx->vcl = vcl;
253 5140
        ctx->syntax = ctx->vcl->conf->syntax;
254 5140
        ctx->method = method;
255
256 5140
        if (vcl->conf->nsub > 0) {
257 24
                sz = VBITMAP_SZ(vcl->conf->nsub);
258 24
                vbm_alloc = malloc(sz);
259 24
                AN(vbm_alloc);
260 24
                ctx->called = vbit_init(vbm_alloc, sz);
261 24
        }
262
263 5140
        VCL_TaskEnter(cli_task_privs);
264 5140
        r = ctx->vcl->conf->event_vcl(ctx, ev);
265 5140
        VCL_TaskLeave(ctx, cli_task_privs);
266 5140
        r |= vcl_event_handling(ctx);
267
268 5140
        *msg = VCL_Rel_CliCtx(&ctx);
269
270 5140
        if (r && (ev == VCL_EVENT_COLD || ev == VCL_EVENT_DISCARD))
271 0
                WRONG("A VMOD cannot fail COLD or DISCARD events");
272
273 5140
        free(vbm_alloc);
274
275 5140
        return (r);
276
}
277
278
/*--------------------------------------------------------------------*/
279
280
struct vcl *
281 5156
vcl_find(const char *name)
282
{
283
        struct vcl *vcl;
284
285 5156
        ASSERT_CLI();
286 7836
        VTAILQ_FOREACH(vcl, &vcl_head, list) {
287 5300
                if (vcl->discard)
288 0
                        continue;
289 5300
                if (!strcmp(vcl->loaded_name, name))
290 2620
                        return (vcl);
291 2680
        }
292 2536
        return (NULL);
293 5156
}
294
295
/*--------------------------------------------------------------------*/
296
297
static void
298 14
vcl_panic_conf(struct vsb *vsb, const struct VCL_conf *conf)
299
{
300
        unsigned u;
301
        const struct vpi_ii *ii;
302
303 14
        if (PAN_dump_struct(vsb, conf, VCL_CONF_MAGIC, "conf"))
304 0
                return;
305 14
        VSB_printf(vsb, "syntax = \"%u\",\n", conf->syntax);
306 14
        VSB_cat(vsb, "srcname = {\n");
307 14
        VSB_indent(vsb, 2);
308 42
        for (u = 0; u < conf->nsrc; ++u)
309 28
                VSB_printf(vsb, "[%u] = \"%s\",\n", u, conf->srcname[u]);
310 14
        VSB_indent(vsb, -2);
311 14
        VSB_cat(vsb, "},\n");
312 14
        VSB_cat(vsb, "instances = {\n");
313 14
        VSB_indent(vsb, 2);
314 14
        ii = conf->instance_info;
315 22
        while (ii != NULL && ii->p != NULL) {
316 16
                VSB_printf(vsb, "\"%s\" = %p,\n", ii->name,
317 8
                    (const void *)*(const uintptr_t *)ii->p);
318 8
                ii++;
319
        }
320 14
        VSB_indent(vsb, -2);
321 14
        VSB_cat(vsb, "},\n");
322 14
        VSB_indent(vsb, -2);
323 14
        VSB_cat(vsb, "},\n");
324 14
}
325
326
void
327 26
VCL_Panic(struct vsb *vsb, const char *nm, const struct vcl *vcl)
328
{
329
330 26
        AN(vsb);
331 26
        if (PAN_dump_struct(vsb, vcl, VCL_MAGIC, "vcl[%s]", nm))
332 12
                return;
333 14
        VSB_printf(vsb, "name = \"%s\",\n", vcl->loaded_name);
334 14
        VSB_printf(vsb, "busy = %u,\n", vcl->busy);
335 14
        VSB_printf(vsb, "discard = %u,\n", vcl->discard);
336 14
        VSB_printf(vsb, "state = %s,\n", vcl->state);
337 14
        VSB_printf(vsb, "temp = %s,\n", vcl->temp ? vcl->temp->name : "(null)");
338 14
        vcl_panic_conf(vsb, vcl->conf);
339 14
        VSB_indent(vsb, -2);
340 14
        VSB_cat(vsb, "},\n");
341 26
}
342
343
/*--------------------------------------------------------------------*/
344
345
void
346 2914
VCL_Update(struct vcl **vcc, struct vcl *vcl)
347
{
348
        struct vcl *old;
349
350 2914
        AN(vcc);
351
352 2914
        old = *vcc;
353 2914
        *vcc = NULL;
354
355 2914
        CHECK_OBJ_ORNULL(old, VCL_MAGIC);
356 2914
        ASSERT_VCL_ACTIVE();
357
358 2914
        Lck_Lock(&vcl_mtx);
359 2914
        if (old != NULL) {
360 157
                assert(old->busy > 0);
361 157
                old->busy--;
362 157
        }
363
364 2914
        if (vcl == NULL)
365 2888
                vcl = vcl_active; /* Sample vcl_active under lock to avoid
366
                                   * race */
367 2914
        CHECK_OBJ_NOTNULL(vcl, VCL_MAGIC);
368 2914
        if (vcl->label == NULL) {
369 2882
                AN(strcmp(vcl->state, VCL_TEMP_LABEL->name));
370 2882
                *vcc = vcl;
371 2882
        } else {
372 32
                AZ(strcmp(vcl->state, VCL_TEMP_LABEL->name));
373 32
                *vcc = vcl->label;
374
        }
375 2914
        CHECK_OBJ_NOTNULL(*vcc, VCL_MAGIC);
376 2914
        AZ((*vcc)->discard);
377 2914
        (*vcc)->busy++;
378 2914
        Lck_Unlock(&vcl_mtx);
379 2914
        assert((*vcc)->temp->is_warm);
380 2914
}
381
382
/*--------------------------------------------------------------------
383
 * vdire: Vcl DIrector REsignation Management (born out of a dire situation)
384
 * iterators over the director list need to register.
385
 * while iterating, directors can not retire immediately,
386
 * they get put on a list of resigning directors. The
387
 * last iterator executes the retirement.
388
 */
389
390
static struct vdire *
391 4978
vdire_new(struct lock *mtx, const struct vcltemp **tempp)
392
{
393
        struct vdire *vdire;
394
395 4978
        ALLOC_OBJ(vdire, VDIRE_MAGIC);
396 4978
        AN(vdire);
397 4978
        AN(mtx);
398 4978
        VTAILQ_INIT(&vdire->directors);
399 4978
        VTAILQ_INIT(&vdire->resigning);
400 4978
        vdire->mtx = mtx;
401 4978
        PTOK(pthread_cond_init(&vdire->cond, NULL));
402 4978
        vdire->tempp = tempp;
403 4978
        return (vdire);
404
}
405
406
/* starting an interation prevents removals from the directors list */
407
void
408 2102
vdire_start_iter(struct vdire *vdire)
409
{
410
411 2102
        CHECK_OBJ_NOTNULL(vdire, VDIRE_MAGIC);
412
413
        // https://github.com/varnishcache/varnish-cache/pull/4142#issuecomment-2593091097
414 2102
        ASSERT_CLI();
415
416 2102
        Lck_Lock(vdire->mtx);
417 2102
        while (! VTAILQ_EMPTY(&vdire->resigning))
418 0
                (void)Lck_CondWait(&vdire->cond, vdire->mtx);
419 2102
        vdire->iterating++;
420 2102
        Lck_Unlock(vdire->mtx);
421 2102
}
422
423
void
424 4596
vdire_end_iter(struct vdire *vdire)
425
{
426 4596
        struct vcldir_head resigning = VTAILQ_HEAD_INITIALIZER(resigning);
427 4596
        const struct vcltemp *temp = NULL;
428
        struct vcldir *vdir, *next;
429
        unsigned n;
430
431 4596
        CHECK_OBJ_NOTNULL(vdire, VDIRE_MAGIC);
432
433 4596
        Lck_Lock(vdire->mtx);
434 4596
        AN(vdire->iterating);
435 4596
        n = --vdire->iterating;
436
437 4596
        if (n == 0) {
438 4596
                VTAILQ_SWAP(&vdire->resigning, &resigning, vcldir, resigning_list);
439 4596
                VTAILQ_FOREACH(vdir, &resigning, resigning_list)
440 0
                        VTAILQ_REMOVE(&vdire->directors, vdir, directors_list);
441 4596
                temp = *vdire->tempp;
442 4596
                PTOK(pthread_cond_broadcast(&vdire->cond));
443 4596
        }
444 4596
        Lck_Unlock(vdire->mtx);
445
446 4596
        VTAILQ_FOREACH_SAFE(vdir, &resigning, resigning_list, next)
447 0
                vcldir_retire(vdir, temp);
448 4596
}
449
450
/*
451
 * like vdire_start_iter, but also prepare for backend_event_*()
452
 * by setting the checkpoint
453
 */
454
static void
455 2494
vdire_start_event(struct vdire *vdire, const struct vcltemp *temp)
456
{
457
458 2494
        CHECK_OBJ_NOTNULL(vdire, VDIRE_MAGIC);
459 2494
        AN(temp);
460
461 2494
        Lck_AssertHeld(vdire->mtx);
462
463
        // https://github.com/varnishcache/varnish-cache/pull/4142#issuecomment-2593091097
464 2494
        ASSERT_CLI();
465
466 2494
        AZ(vdire->checkpoint);
467 2494
        vdire->checkpoint = VTAILQ_LAST(&vdire->directors, vcldir_head);
468 2494
        AN(vdire->tempp);
469 2494
        *vdire->tempp = temp;
470 2494
        vdire->iterating++;
471 2494
}
472
473
static void
474 2494
vdire_end_event(struct vdire *vdire)
475
{
476 2494
        vdire->checkpoint = NULL;
477 2494
        vdire_end_iter(vdire);
478 2494
}
479
480
// if there are no iterators, remove from directors and retire
481
// otherwise put on resigning list to work when iterators end
482
void
483 218
vdire_resign(struct vdire *vdire, struct vcldir *vdir)
484
{
485 218
        const struct vcltemp *temp = NULL;
486
487 218
        CHECK_OBJ_NOTNULL(vdire, VDIRE_MAGIC);
488 218
        AN(vdir);
489
490 218
        Lck_Lock(vdire->mtx);
491 218
        if (vdire->iterating != 0) {
492 0
                VTAILQ_INSERT_TAIL(&vdire->resigning, vdir, resigning_list);
493 0
                vdir = NULL;
494 0
        } else {
495 218
                VTAILQ_REMOVE(&vdire->directors, vdir, directors_list);
496 218
                temp = *vdire->tempp;
497
        }
498 218
        Lck_Unlock(vdire->mtx);
499
500 218
        if (vdir != NULL)
501 218
                vcldir_retire(vdir, temp);
502 218
}
503
504
// unlocked version of vcl_iterdir
505
// pat can also be NULL (to iterate all)
506
static int
507 1944
vdire_iter(struct cli *cli, const char *pat, const struct vcl *vcl,
508
    vcl_be_func *func, void *priv)
509
{
510 1944
        int i, found = 0;
511
        struct vcldir *vdir;
512 1944
        struct vdire *vdire = vcl->vdire;
513
514 1944
        vdire_start_iter(vdire);
515 4758
        VTAILQ_FOREACH(vdir, &vdire->directors, directors_list) {
516 2814
                CHECK_OBJ(vdir, VCLDIR_MAGIC);
517 2814
                if (vdir->refcnt == 0)
518 0
                        continue;
519 2814
                if (pat != NULL && fnmatch(pat, vdir->cli_name, 0))
520 186
                        continue;
521 2628
                found++;
522 2628
                i = func(cli, vdir->dir, priv);
523 2628
                if (i < 0) {
524 0
                        found = i;
525 0
                        break;
526
                }
527 2628
                found += i;
528 2628
        }
529 1944
        vdire_end_iter(vdire);
530 1944
        return (found);
531
}
532
533
534
/*--------------------------------------------------------------------*/
535
536
// XXX locked case across VCLs - should do without
537
static int
538 14
vcl_iterdir(struct cli *cli, const char *pat, const struct vcl *vcl,
539
    vcl_be_func *func, void *priv)
540
{
541 14
        int i, found = 0;
542
        struct vcldir *vdir;
543
544 14
        AN(vcl->vdire); // no labels
545
546 14
        Lck_AssertHeld(&vcl_mtx);
547 34
        VTAILQ_FOREACH(vdir, &vcl->vdire->directors, directors_list) {
548 20
                CHECK_OBJ(vdir, VCLDIR_MAGIC);
549 20
                if (fnmatch(pat, vdir->cli_name, 0))
550 8
                        continue;
551 12
                found++;
552 12
                i = func(cli, vdir->dir, priv);
553 12
                if (i < 0)
554 0
                        return (i);
555 12
                found += i;
556 12
        }
557 14
        return (found);
558 14
}
559
560
int
561 1958
VCL_IterDirector(struct cli *cli, const char *pat,
562
    vcl_be_func *func, void *priv)
563
{
564 1958
        int i, found = 0;
565
        struct vsb *vsb;
566
        struct vcl *vcl;
567
568 1958
        ASSERT_CLI();
569 1958
        ASSERT_VCL_ACTIVE();
570 1958
        vsb = VSB_new_auto();
571 1958
        AN(vsb);
572 1958
        if (pat == NULL || *pat == '\0' || !strcmp(pat, "*")) {
573
                // all backends in active VCL
574 1868
                VSB_printf(vsb, "%s.*", VCL_Name(vcl_active));
575 1868
                vcl = vcl_active;
576 1958
        } else if (strchr(pat, '.') == NULL) {
577
                // pattern applies to active vcl
578 76
                VSB_printf(vsb, "%s.%s", VCL_Name(vcl_active), pat);
579 76
                vcl = vcl_active;
580 76
        } else {
581
                // use pattern as is
582 14
                VSB_cat(vsb, pat);
583 14
                vcl = NULL;
584
        }
585 1958
        AZ(VSB_finish(vsb));
586 1958
        if (vcl != NULL) {
587 1944
                found = vdire_iter(cli, VSB_data(vsb), vcl, func, priv);
588 1944
        } else {
589 14
                Lck_Lock(&vcl_mtx);
590 30
                VTAILQ_FOREACH(vcl, &vcl_head, list) {
591
                        // skip labels
592 16
                        if (! vcl->vdire)
593 2
                                continue;
594 14
                        i = vcl_iterdir(cli, VSB_data(vsb), vcl, func, priv);
595 14
                        if (i < 0) {
596 0
                                found = i;
597 0
                                break;
598
                        } else {
599 14
                                found += i;
600
                        }
601 14
                }
602 14
                Lck_Unlock(&vcl_mtx);
603
        }
604 1958
        VSB_destroy(&vsb);
605 1958
        return (found);
606
}
607
608
/*
609
 * vdire_start_event() must have been called before
610
 *
611
 * send event to directors pre checkpoint
612
 */
613
static void
614 2488
backend_event_base(const struct vcl *vcl, enum vcl_event_e e)
615
{
616
        struct vcldir *vdir;
617
        struct vdire *vdire;
618
619 2488
        ASSERT_CLI();
620 2488
        CHECK_OBJ_NOTNULL(vcl, VCL_MAGIC);
621 2488
        AZ(vcl->busy);
622 2488
        vdire = vcl->vdire;
623 2488
        AN(vdire->iterating);
624
625 2488
        vdir = vdire->checkpoint;
626 2488
        if (vdir == NULL)
627 276
                return;
628
629 2212
        CHECK_OBJ(vdir, VCLDIR_MAGIC);
630 5146
        VTAILQ_FOREACH_REVERSE_FROM(vdir, &vdire->directors,
631
            vcldir_head, directors_list) {
632 2934
                CHECK_OBJ(vdir, VCLDIR_MAGIC);
633 2934
                VDI_Event(vdir->dir, e);
634 2934
        }
635 2488
}
636
637
/*
638
 * vdire_start_event() must have been called before
639
 *
640
 * set a new temperature.
641
 * send event to directors added post checkpoint, but before
642
 * the new temperature
643
 */
644
static void
645 6
backend_event_delta(const struct vcl *vcl, enum vcl_event_e e, const struct vcltemp *temp)
646
{
647
        struct vcldir *vdir, *end;
648
        struct vdire *vdire;
649
650 6
        ASSERT_CLI();
651 6
        CHECK_OBJ_NOTNULL(vcl, VCL_MAGIC);
652 6
        AZ(vcl->busy);
653 6
        vdire = vcl->vdire;
654 6
        AN(temp);
655 6
        AN(vdire->iterating);
656
657 6
        Lck_Lock(vdire->mtx);
658 6
        if (vdire->checkpoint == NULL)
659 0
                vdir = VTAILQ_FIRST(&vdire->directors);
660
        else
661 6
                vdir = VTAILQ_NEXT(vdire->checkpoint, directors_list);
662 6
        AN(vdire->tempp);
663 6
        *vdire->tempp = temp;
664 6
        end = VTAILQ_LAST(&vdire->directors, vcldir_head);
665 6
        Lck_Unlock(vdire->mtx);
666
667 6
        while (vdir != NULL) {
668 0
                CHECK_OBJ(vdir, VCLDIR_MAGIC);
669 0
                VDI_Event(vdir->dir, e);
670 0
                if (vdir == end)
671 0
                        break;
672 0
                vdir = VTAILQ_NEXT(vdir, directors_list);
673
        }
674 6
}
675
676
static void
677 156
vcl_KillBackends(const struct vcl *vcl)
678
{
679
        struct vcldir *vdir, *vdir2;
680
        struct vdire *vdire;
681
682 156
        CHECK_OBJ_NOTNULL(vcl, VCL_MAGIC);
683 156
        assert(vcl->temp == VCL_TEMP_COLD || vcl->temp == VCL_TEMP_INIT);
684 156
        vdire = vcl->vdire;
685 156
        CHECK_OBJ_NOTNULL(vdire, VDIRE_MAGIC);
686
687
        /*
688
         * ensure all retirement has finished (vdire_start_iter waits for it)
689
         */
690 156
        vdire_start_iter(vdire);
691 156
        vdire_end_iter(vdire);
692 156
        AZ(vdire->iterating);
693 156
        assert(VTAILQ_EMPTY(&vdire->resigning));
694
695
        /*
696
         * Unlocked and sidelining vdire because no further directors can be added, and the
697
         * remaining ones need to be able to remove themselves.
698
         */
699 290
        VTAILQ_FOREACH_SAFE(vdir, &vdire->directors, directors_list, vdir2) {
700 134
                CHECK_OBJ(vdir, VCLDIR_MAGIC);
701 134
                VDI_Event(vdir->dir, VCL_EVENT_DISCARD);
702 134
        }
703 156
        assert(VTAILQ_EMPTY(&vdire->directors));
704 156
}
705
706
/*--------------------------------------------------------------------*/
707
708
static struct vcl *
709 4982
VCL_Open(const char *fn, struct vsb *msg)
710
{
711
        struct vcl *vcl;
712
        void *dlh;
713
        struct VCL_conf const *cnf;
714
        const char *dlerr;
715
        int err;
716
717 4982
        AN(fn);
718 4982
        AN(msg);
719
720
#ifdef RTLD_NOLOAD
721
        /* Detect bogus caching by dlopen(3) */
722 4982
        dlh = dlopen(fn, RTLD_NOW | RTLD_NOLOAD);
723 4982
        AZ(dlh);
724
#endif
725 4982
        dlh = dlopen(fn, RTLD_NOW | RTLD_LOCAL);
726 4982
        if (dlh == NULL) {
727 2
                err = errno;
728 2
                dlerr = dlerror();
729 2
                VSB_cat(msg, "Could not load compiled VCL.\n");
730 2
                if (dlerr != NULL)
731 2
                        VSB_printf(msg, "\tdlopen() = %s\n", dlerr);
732 2
                if (err) {
733 0
                        VSB_printf(msg, "\terror = %s (%d)\n",
734 0
                            strerror(err), err);
735 0
                }
736 2
                VSB_cat(msg, "\thint: check for \"noexec\" mount\n");
737 2
                VSB_cat(msg, "\thint: check \"vmod_path\" parameter\n");
738 2
                return (NULL);
739
        }
740 4980
        cnf = dlsym(dlh, "VCL_conf");
741 4980
        if (cnf == NULL) {
742 0
                VSB_cat(msg, "Compiled VCL lacks metadata.\n");
743 0
                (void)dlclose(dlh);
744 0
                return (NULL);
745
        }
746 4980
        if (cnf->magic != VCL_CONF_MAGIC) {
747 0
                VSB_cat(msg, "Compiled VCL has mangled metadata.\n");
748 0
                (void)dlclose(dlh);
749 0
                return (NULL);
750
        }
751 4980
        if (cnf->syntax < heritage.min_vcl_version ||
752 4978
            cnf->syntax > heritage.max_vcl_version) {
753 4
                VSB_printf(msg, "Compiled VCL version (%.1f) not supported.\n",
754 2
                    .1 * cnf->syntax);
755 2
                (void)dlclose(dlh);
756 2
                return (NULL);
757
        }
758 4978
        ALLOC_OBJ(vcl, VCL_MAGIC);
759 4978
        AN(vcl);
760 4978
        vcl->dlh = dlh;
761 4978
        vcl->conf = cnf;
762 4978
        vcl->vdire = vdire_new(&vcl_mtx, &vcl->temp);
763 4978
        return (vcl);
764 4982
}
765
766
static void
767 2650
VCL_Close(struct vcl **vclp)
768
{
769
        struct vcl *vcl;
770
771 2650
        TAKE_OBJ_NOTNULL(vcl, vclp, VCL_MAGIC);
772 2650
        assert(VTAILQ_EMPTY(&vcl->filters));
773 2650
        AZ(dlclose(vcl->dlh));
774 2650
        PTOK(pthread_cond_destroy(&vcl->vdire->cond));
775 2650
        FREE_OBJ(vcl->vdire);
776 2650
        FREE_OBJ(vcl);
777 2650
}
778
779
/*--------------------------------------------------------------------
780
 * NB: This function is called in/from the test-load subprocess.
781
 */
782
783
int
784 2498
VCL_TestLoad(const char *fn)
785
{
786
        struct vsb *vsb;
787
        struct vcl *vcl;
788 2498
        int retval = 0;
789
790 2498
        AN(fn);
791 2498
        vsb = VSB_new_auto();
792 2498
        AN(vsb);
793 2498
        vcl = VCL_Open(fn, vsb);
794 2498
        if (vcl == NULL) {
795 4
                AZ(VSB_finish(vsb));
796 4
                fprintf(stderr, "%s", VSB_data(vsb));
797 4
                retval = -1;
798 4
        } else
799 2494
                VCL_Close(&vcl);
800 2498
        VSB_destroy(&vsb);
801 2498
        return (retval);
802
}
803
804
/*--------------------------------------------------------------------*/
805
806
static struct vsb *
807 2
vcl_print_refs(const struct vcl *vcl)
808
{
809
        struct vsb *msg;
810
        struct vclref *ref;
811
812 2
        CHECK_OBJ_NOTNULL(vcl, VCL_MAGIC);
813 2
        msg = VSB_new_auto();
814 2
        AN(msg);
815
816 2
        VSB_printf(msg, "VCL %s is waiting for:", vcl->loaded_name);
817 2
        Lck_Lock(&vcl_mtx);
818 4
        VTAILQ_FOREACH(ref, &vcl->ref_list, list)
819 2
                VSB_printf(msg, "\n\t- %s", ref->desc);
820 2
        Lck_Unlock(&vcl_mtx);
821 2
        AZ(VSB_finish(msg));
822 2
        return (msg);
823
}
824
825
static int
826 2594
vcl_set_state(struct vcl *vcl, const char *state, struct vsb **msg)
827
{
828 2594
        struct vsb *nomsg = NULL;
829 2594
        int i = 0;
830
831 2594
        ASSERT_CLI();
832
833 2594
        CHECK_OBJ_NOTNULL(vcl, VCL_MAGIC);
834 2594
        AN(state);
835 2594
        AN(msg);
836 2594
        AZ(*msg);
837
838 2594
        AN(vcl->temp);
839
840 2594
        switch (state[0]) {
841
        case '0':
842 184
                if (vcl->temp == VCL_TEMP_COLD)
843 4
                        break;
844 180
                if (vcl->busy == 0 && vcl->temp->is_warm) {
845 88
                        Lck_Lock(&vcl_mtx);
846 88
                        vdire_start_event(vcl->vdire, VTAILQ_EMPTY(&vcl->ref_list) ?
847
                            VCL_TEMP_COLD : VCL_TEMP_COOLING);
848 88
                        Lck_Unlock(&vcl_mtx);
849 88
                        backend_event_base(vcl, VCL_EVENT_COLD);
850 88
                        vdire_end_event(vcl->vdire);
851
                        // delta directors at VCL_TEMP_COLD do not need an event
852 88
                        AZ(vcl_send_event(vcl, VCL_EVENT_COLD, msg));
853 88
                        AZ(*msg);
854 88
                }
855 92
                else if (vcl->busy)
856 48
                        vcl->temp = VCL_TEMP_BUSY;
857 44
                else if (VTAILQ_EMPTY(&vcl->ref_list))
858 36
                        vcl->temp = VCL_TEMP_COLD;
859
                else
860 8
                        vcl->temp = VCL_TEMP_COOLING;
861 180
                break;
862
        case '1':
863 2410
                if (vcl->temp == VCL_TEMP_WARM)
864 2
                        break;
865
                /* The warm VCL hasn't seen a cold event yet */
866 2408
                if (vcl->temp == VCL_TEMP_BUSY)
867 2
                        vcl->temp = VCL_TEMP_WARM;
868
                /* The VCL must first reach a stable cold state */
869 2406
                else if (vcl->temp == VCL_TEMP_COOLING) {
870 2
                        *msg = vcl_print_refs(vcl);
871 2
                        i = -1;
872 2
                }
873
                else {
874 2404
                        Lck_Lock(&vcl_mtx);
875 2404
                        vdire_start_event(vcl->vdire, VCL_TEMP_WARM);
876 2404
                        Lck_Unlock(&vcl_mtx);
877 2404
                        i = vcl_send_event(vcl, VCL_EVENT_WARM, msg);
878 2404
                        if (i == 0) {
879 2398
                                backend_event_base(vcl, VCL_EVENT_WARM);
880
                                // delta directors are already warm
881 2398
                                vdire_end_event(vcl->vdire);
882 2398
                                break;
883
                        }
884 6
                        AZ(vcl_send_event(vcl, VCL_EVENT_COLD, &nomsg));
885 6
                        AZ(nomsg);
886 6
                        backend_event_delta(vcl, VCL_EVENT_COLD, VCL_TEMP_COLD);
887 6
                        vdire_end_event(vcl->vdire);
888
                }
889 10
                break;
890
        default:
891 0
                WRONG("Wrong enum state");
892 0
        }
893 2594
        if (i == 0 && state[1])
894 2506
                bprintf(vcl->state, "%s", state + 1);
895
896 2594
        return (i);
897
}
898
899
static void
900 86
vcl_cancel_load(struct vcl *vcl, struct cli *cli, struct vsb *msg,
901
    const char *name, const char *step)
902
{
903
904 86
        CHECK_OBJ_NOTNULL(vcl, VCL_MAGIC);
905 86
        CHECK_OBJ_NOTNULL(vcl->conf, VCL_CONF_MAGIC);
906
907 86
        VCLI_SetResult(cli, CLIS_CANT);
908 86
        VCLI_Out(cli, "VCL \"%s\" Failed %s", name, step);
909 86
        if (VSB_len(msg))
910 86
                VCLI_Out(cli, "\nMessage:\n\t%s", VSB_data(msg));
911 86
        VSB_destroy(&msg);
912
913 86
        AZ(vcl_send_event(vcl, VCL_EVENT_DISCARD, &msg));
914 86
        AZ(msg);
915
916 86
        vcl_KillBackends(vcl);
917 86
        free(vcl->loaded_name);
918 86
        VCL_Close(&vcl);
919 86
}
920
921
static void
922 2484
vcl_load(struct cli *cli,
923
    const char *name, const char *fn, const char *state)
924
{
925
        struct vcl *vcl;
926
        struct vsb *msg;
927
928 2484
        ASSERT_CLI();
929 2484
        ASSERT_VCL_ACTIVE();
930
931 2484
        vcl = vcl_find(name);
932 2484
        AZ(vcl);
933
934 2484
        msg = VSB_new_auto();
935 2484
        AN(msg);
936 2484
        vcl = VCL_Open(fn, msg);
937 2484
        AZ(VSB_finish(msg));
938 2484
        if (vcl == NULL) {
939 0
                VCLI_SetResult(cli, CLIS_PARAM);
940 0
                VCLI_Out(cli, "%s", VSB_data(msg));
941 0
                VSB_destroy(&msg);
942 0
                return;
943
        }
944
945 2484
        VSB_destroy(&msg);
946
947 2484
        vcl->loaded_name = strdup(name);
948 2484
        XXXAN(vcl->loaded_name);
949 2484
        VTAILQ_INIT(&vcl->ref_list);
950 2484
        VTAILQ_INIT(&vcl->filters);
951
952 2484
        vcl->temp = VCL_TEMP_INIT;
953
954 2484
        if (vcl_send_event(vcl, VCL_EVENT_LOAD, &msg)) {
955 84
                vcl_cancel_load(vcl, cli, msg, name, "initialization");
956 84
                return;
957
        }
958 2400
        VSB_destroy(&msg);
959
960 2400
        if (vcl_set_state(vcl, state, &msg)) {
961 2
                assert(*state == '1');
962 2
                vcl_cancel_load(vcl, cli, msg, name, "warmup");
963 2
                return;
964
        }
965 2398
        if (msg)
966 2390
                VSB_destroy(&msg);
967
968 2398
        VCLI_Out(cli, "Loaded \"%s\" as \"%s\"", fn , name);
969 2398
        VTAILQ_INSERT_TAIL(&vcl_head, vcl, list);
970 2398
        VSC_C_main->n_vcl++;
971 2398
        VSC_C_main->n_vcl_avail++;
972 2484
}
973
974
/*--------------------------------------------------------------------*/
975
976
void
977 15595
VCL_Poll(void)
978
{
979 15595
        struct vsb *nomsg = NULL;
980
        struct vcl *vcl, *vcl2;
981
982 15595
        ASSERT_CLI();
983 15595
        ASSERT_VCL_ACTIVE();
984 32874
        VTAILQ_FOREACH_SAFE(vcl, &vcl_head, list, vcl2) {
985 17279
                if (vcl->temp == VCL_TEMP_BUSY ||
986 17235
                    vcl->temp == VCL_TEMP_COOLING)
987 80
                        AZ(vcl_set_state(vcl, "0", &nomsg));
988 17279
                AZ(nomsg);
989 17279
                if (vcl->discard && vcl->temp == VCL_TEMP_COLD) {
990 70
                        AZ(vcl->busy);
991 70
                        assert(vcl != vcl_active);
992 70
                        assert(VTAILQ_EMPTY(&vcl->ref_list));
993 70
                        VTAILQ_REMOVE(&vcl_head, vcl, list);
994 70
                        AZ(vcl_send_event(vcl, VCL_EVENT_DISCARD, &nomsg));
995 70
                        AZ(nomsg);
996 70
                        vcl_KillBackends(vcl);
997 70
                        free(vcl->loaded_name);
998 70
                        VCL_Close(&vcl);
999 70
                        VSC_C_main->n_vcl--;
1000 70
                        VSC_C_main->n_vcl_discard--;
1001 70
                }
1002 17279
        }
1003 15595
}
1004
1005
/*--------------------------------------------------------------------*/
1006
1007
static void v_matchproto_(cli_func_t)
1008 150
vcl_cli_list(struct cli *cli, const char * const *av, void *priv)
1009
{
1010
        struct vcl *vcl;
1011
        const char *flg;
1012
        struct vte *vte;
1013
1014
        /* NB: Shall generate same output as mcf_vcl_list() */
1015
1016 150
        (void)av;
1017 150
        (void)priv;
1018 150
        ASSERT_CLI();
1019 150
        ASSERT_VCL_ACTIVE();
1020 150
        vte = VTE_new(7, 80);
1021 150
        AN(vte);
1022 479
        VTAILQ_FOREACH(vcl, &vcl_head, list) {
1023 329
                if (vcl == vcl_active) {
1024 150
                        flg = "active";
1025 329
                } else if (vcl->discard) {
1026 7
                        flg = "discarded";
1027 7
                } else
1028 172
                        flg = "available";
1029 658
                VTE_printf(vte, "%s\t%s\t%s\t\v%u\t%s", flg, vcl->state,
1030 329
                    vcl->temp->name, vcl->busy, vcl->loaded_name);
1031 329
                if (vcl->label != NULL) {
1032 38
                        VTE_printf(vte, "\t->\t%s", vcl->label->loaded_name);
1033 38
                        if (vcl->nrefs)
1034 16
                                VTE_printf(vte, " (%d return(vcl)%s)",
1035 8
                                    vcl->nrefs, vcl->nrefs > 1 ? "'s" : "");
1036 329
                } else if (vcl->nlabels > 0) {
1037 52
                        VTE_printf(vte, "\t<-\t(%d label%s)",
1038 26
                            vcl->nlabels, vcl->nlabels > 1 ? "s" : "");
1039 26
                }
1040 329
                VTE_cat(vte, "\n");
1041 329
        }
1042 150
        AZ(VTE_finish(vte));
1043 150
        AZ(VTE_format(vte, VCLI_VTE_format, cli));
1044 150
        VTE_destroy(&vte);
1045 150
}
1046
1047
static void v_matchproto_(cli_func_t)
1048 10
vcl_cli_list_json(struct cli *cli, const char * const *av, void *priv)
1049
{
1050
        struct vcl *vcl;
1051
1052 10
        (void)priv;
1053 10
        ASSERT_CLI();
1054 10
        ASSERT_VCL_ACTIVE();
1055 10
        VCLI_JSON_begin(cli, 2, av);
1056 10
        VCLI_Out(cli, ",\n");
1057 42
        VTAILQ_FOREACH(vcl, &vcl_head, list) {
1058 32
                VCLI_Out(cli, "{\n");
1059 32
                VSB_indent(cli->sb, 2);
1060 32
                VCLI_Out(cli, "\"status\": ");
1061 32
                if (vcl == vcl_active) {
1062 10
                        VCLI_Out(cli, "\"active\",\n");
1063 32
                } else if (vcl->discard) {
1064 0
                        VCLI_Out(cli, "\"discarded\",\n");
1065 0
                } else
1066 22
                        VCLI_Out(cli, "\"available\",\n");
1067 32
                VCLI_Out(cli, "\"state\": \"%s\",\n", vcl->state);
1068 32
                VCLI_Out(cli, "\"temperature\": \"%s\",\n", vcl->temp->name);
1069 32
                VCLI_Out(cli, "\"busy\": %u,\n", vcl->busy);
1070 32
                VCLI_Out(cli, "\"name\": \"%s\"", vcl->loaded_name);
1071 32
                if (vcl->label != NULL) {
1072 12
                        VCLI_Out(cli, ",\n");
1073 12
                        VCLI_Out(cli, "\"label\": {\n");
1074 12
                        VSB_indent(cli->sb, 2);
1075 24
                                VCLI_Out(cli, "\"name\": \"%s\"",
1076 12
                                         vcl->label->loaded_name);
1077 12
                        if (vcl->nrefs)
1078 0
                                VCLI_Out(cli, ",\n\"refs\": %d", vcl->nrefs);
1079 12
                        VCLI_Out(cli, "\n");
1080 12
                        VCLI_Out(cli, "}");
1081 12
                        VSB_indent(cli->sb, -2);
1082 32
                } else if (vcl->nlabels > 0) {
1083 4
                        VCLI_Out(cli, ",\n");
1084 4
                        VCLI_Out(cli, "\"labels\": %d", vcl->nlabels);
1085 4
                }
1086 32
                VSB_indent(cli->sb, -2);
1087 32
                VCLI_Out(cli, "\n}");
1088 32
                if (VTAILQ_NEXT(vcl, list) != NULL)
1089 22
                        VCLI_Out(cli, ",\n");
1090 32
        }
1091 10
        VCLI_JSON_end(cli);
1092 10
}
1093
1094
static void v_matchproto_(cli_func_t)
1095 2484
vcl_cli_load(struct cli *cli, const char * const *av, void *priv)
1096
{
1097
1098 2484
        AZ(priv);
1099 2484
        ASSERT_CLI();
1100
        // XXX move back code from vcl_load?
1101 2484
        vcl_load(cli, av[2], av[3], av[4]);
1102 2484
}
1103
1104
static void v_matchproto_(cli_func_t)
1105 116
vcl_cli_state(struct cli *cli, const char * const *av, void *priv)
1106
{
1107
        struct vcl *vcl;
1108 116
        struct vsb *msg = NULL;
1109
1110 116
        AZ(priv);
1111 116
        ASSERT_CLI();
1112 116
        ASSERT_VCL_ACTIVE();
1113 116
        AN(av[2]);
1114 116
        AN(av[3]);
1115
1116 116
        vcl = vcl_find(av[2]);
1117 116
        AN(vcl);
1118
1119 116
        if (vcl_set_state(vcl, av[3], &msg)) {
1120 6
                CHECK_OBJ_NOTNULL(msg, VSB_MAGIC);
1121
1122 6
                VCLI_SetResult(cli, CLIS_CANT);
1123 12
                VCLI_Out(cli, "Failed <vcl.state %s %s>", vcl->loaded_name,
1124 6
                    av[3] + 1);
1125 6
                if (VSB_len(msg))
1126 6
                        VCLI_Out(cli, "\nMessage:\n\t%s", VSB_data(msg));
1127 6
        }
1128 116
        if (msg)
1129 14
                VSB_destroy(&msg);
1130 116
}
1131
1132
static void v_matchproto_(cli_func_t)
1133 78
vcl_cli_discard(struct cli *cli, const char * const *av, void *priv)
1134
{
1135
        struct vcl *vcl;
1136
1137 78
        ASSERT_CLI();
1138 78
        ASSERT_VCL_ACTIVE();
1139 78
        (void)cli;
1140 78
        AZ(priv);
1141 78
        vcl = vcl_find(av[2]);
1142 78
        CHECK_OBJ_NOTNULL(vcl, VCL_MAGIC);              // MGT ensures this
1143 78
        Lck_Lock(&vcl_mtx);
1144 78
        assert (vcl != vcl_active);     // MGT ensures this
1145 78
        AZ(vcl->nlabels);               // MGT ensures this
1146 78
        VSC_C_main->n_vcl_discard++;
1147 78
        VSC_C_main->n_vcl_avail--;
1148 78
        vcl->discard = 1;
1149 78
        if (vcl->label != NULL) {
1150 8
                AZ(strcmp(vcl->state, VCL_TEMP_LABEL->name));
1151 8
                vcl->label->nlabels--;
1152 8
                vcl->label= NULL;
1153 8
        }
1154 78
        Lck_Unlock(&vcl_mtx);
1155
1156 78
        if (!strcmp(vcl->state, VCL_TEMP_LABEL->name)) {
1157 8
                VTAILQ_REMOVE(&vcl_head, vcl, list);
1158 8
                free(vcl->loaded_name);
1159 8
                AZ(vcl->vdire);
1160 8
                FREE_OBJ(vcl);
1161 78
        } else if (vcl->temp == VCL_TEMP_COLD) {
1162 54
                VCL_Poll();
1163 54
        }
1164 78
}
1165
1166
static void v_matchproto_(cli_func_t)
1167 54
vcl_cli_label(struct cli *cli, const char * const *av, void *priv)
1168
{
1169
        struct vcl *lbl;
1170
        struct vcl *vcl;
1171
1172 54
        ASSERT_CLI();
1173 54
        ASSERT_VCL_ACTIVE();
1174 54
        (void)cli;
1175 54
        (void)priv;
1176 54
        vcl = vcl_find(av[3]);
1177 54
        AN(vcl);                                // MGT ensures this
1178 54
        lbl = vcl_find(av[2]);
1179 54
        if (lbl == NULL) {
1180 48
                ALLOC_OBJ(lbl, VCL_MAGIC);
1181 48
                AN(lbl);
1182 48
                bprintf(lbl->state, "%s", VCL_TEMP_LABEL->name);
1183 48
                lbl->temp = VCL_TEMP_WARM;
1184 48
                REPLACE(lbl->loaded_name, av[2]);
1185 48
                VTAILQ_INSERT_TAIL(&vcl_head, lbl, list);
1186 48
        }
1187 54
        if (lbl->label != NULL)
1188 6
                lbl->label->nlabels--;
1189 54
        lbl->label = vcl;
1190 54
        vcl->nlabels++;
1191 54
}
1192
1193
static void v_matchproto_(cli_func_t)
1194 2308
vcl_cli_use(struct cli *cli, const char * const *av, void *priv)
1195
{
1196
        struct vcl *vcl;
1197
1198 2308
        ASSERT_CLI();
1199 2308
        ASSERT_VCL_ACTIVE();
1200 2308
        AN(cli);
1201 2308
        AZ(priv);
1202 2308
        vcl = vcl_find(av[2]);
1203 2308
        AN(vcl);                                // MGT ensures this
1204 2308
        assert(vcl->temp == VCL_TEMP_WARM);     // MGT ensures this
1205 2308
        Lck_Lock(&vcl_mtx);
1206 2308
        vcl_active = vcl;
1207 2308
        Lck_Unlock(&vcl_mtx);
1208 2308
}
1209
1210
static void v_matchproto_(cli_func_t)
1211 24
vcl_cli_show(struct cli *cli, const char * const *av, void *priv)
1212
{
1213
        struct vcl *vcl;
1214 24
        int verbose = 0;
1215 24
        int i = 2;
1216
        unsigned u;
1217
1218 24
        ASSERT_CLI();
1219 24
        ASSERT_VCL_ACTIVE();
1220 24
        AZ(priv);
1221
1222 24
        if (av[i] != NULL && !strcmp(av[i], "-v")) {
1223 8
                verbose = 1;
1224 8
                i++;
1225 8
        }
1226
1227 24
        if (av[i] == NULL) {
1228 4
                vcl = vcl_active;
1229 4
                AN(vcl);
1230 4
        } else {
1231 20
                vcl = vcl_find(av[i]);
1232 20
                i++;
1233
        }
1234
1235 24
        if (av[i] != NULL) {
1236 2
                VCLI_Out(cli, "Too many parameters: '%s'", av[i]);
1237 2
                VCLI_SetResult(cli, CLIS_PARAM);
1238 2
                return;
1239
        }
1240
1241 22
        if (vcl == NULL) {
1242 2
                VCLI_Out(cli, "No VCL named '%s'", av[i - 1]);
1243 2
                VCLI_SetResult(cli, CLIS_PARAM);
1244 2
                return;
1245
        }
1246 20
        CHECK_OBJ_NOTNULL(vcl, VCL_MAGIC);
1247 20
        if (vcl->label) {
1248 4
                vcl = vcl->label;
1249 4
                CHECK_OBJ_NOTNULL(vcl, VCL_MAGIC);
1250 4
                AZ(vcl->label);
1251 4
        }
1252 20
        CHECK_OBJ_NOTNULL(vcl->conf, VCL_CONF_MAGIC);
1253 20
        if (verbose) {
1254 24
                for (u = 0; u < vcl->conf->nsrc; u++)
1255 32
                        VCLI_Out(cli, "// VCL.SHOW %u %zd %s\n%s\n",
1256 16
                            u, strlen(vcl->conf->srcbody[u]),
1257 16
                            vcl->conf->srcname[u],
1258 16
                            vcl->conf->srcbody[u]);
1259 8
        } else {
1260 12
                VCLI_Out(cli, "%s", vcl->conf->srcbody[0]);
1261
        }
1262 24
}
1263
1264
/*--------------------------------------------------------------------*/
1265
1266
static struct cli_proto vcl_cmds[] = {
1267
        { CLICMD_VCL_LOAD,              "", vcl_cli_load },
1268
        { CLICMD_VCL_LIST,              "", vcl_cli_list, vcl_cli_list_json },
1269
        { CLICMD_VCL_STATE,             "", vcl_cli_state },
1270
        { CLICMD_VCL_DISCARD,           "", vcl_cli_discard },
1271
        { CLICMD_VCL_USE,               "", vcl_cli_use },
1272
        { CLICMD_VCL_SHOW,              "", vcl_cli_show },
1273
        { CLICMD_VCL_LABEL,             "", vcl_cli_label },
1274
        { NULL }
1275
};
1276
1277
void
1278 1907
VCL_Init(void)
1279
{
1280
1281 1907
        assert(cache_param->workspace_client > 0);
1282 3814
        WS_Init(&ws_cli, "cli", malloc(cache_param->workspace_client),
1283 1907
            cache_param->workspace_client);
1284 1907
        ws_snapshot_cli = WS_Snapshot(&ws_cli);
1285 1907
        CLI_AddFuncs(vcl_cmds);
1286 1907
        Lck_New(&vcl_mtx, lck_vcl);
1287 1907
        VSL_Setup(&vsl_cli, NULL, 0);
1288 1907
}
1289
1290
void
1291 1882
VCL_Shutdown(void)
1292
{
1293
        struct vcl *vcl;
1294 1882
        unsigned c = 0;
1295
1296 14812
        while (1) {
1297 14812
                Lck_Lock(&vcl_mtx);
1298 19479
                VTAILQ_FOREACH(vcl, &vcl_head, list)
1299 17597
                        if (vcl->busy)
1300 12930
                                break;
1301 14812
                Lck_Unlock(&vcl_mtx);
1302 14812
                if (vcl == NULL)
1303 1882
                        return;
1304 12930
                if (++c % 10 == 0) {
1305 48
                        fprintf(stderr, "shutdown waiting for %u reference%s "
1306 24
                            "on %s\n", vcl->busy, vcl->busy > 1 ? "s" : "",
1307 24
                            vcl->loaded_name);
1308 24
                }
1309 12930
                usleep(100 * 1000);
1310
        }
1311
}