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