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