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