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