varnish-cache/lib/libvarnishapi/vsm.c
0
/*-
1
 * Copyright (c) 2006 Verdens Gang AS
2
 * Copyright (c) 2006-2015 Varnish Software AS
3
 * All rights reserved.
4
 *
5
 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
6
 * Author: Martin Blix Grydeland <martin@varnish-software.com>
7
 *
8
 * SPDX-License-Identifier: BSD-2-Clause
9
 *
10
 * Redistribution and use in source and binary forms, with or without
11
 * modification, are permitted provided that the following conditions
12
 * are met:
13
 * 1. Redistributions of source code must retain the above copyright
14
 *    notice, this list of conditions and the following disclaimer.
15
 * 2. Redistributions in binary form must reproduce the above copyright
16
 *    notice, this list of conditions and the following disclaimer in the
17
 *    documentation and/or other materials provided with the distribution.
18
 *
19
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
23
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29
 * SUCH DAMAGE.
30
 */
31
32
#include "config.h"
33
34
#include <sys/mman.h>
35
#include <sys/stat.h>
36
37
#include <fcntl.h>
38
#include <float.h>
39
#include <math.h>
40
#include <stdarg.h>
41
#include <stdint.h>
42
#include <stdio.h>
43
#include <stdlib.h>
44
#include <string.h>
45
#include <unistd.h>
46
47
#include "vdef.h"
48
#include "vas.h"
49
#include "miniobj.h"
50
51
#include "vav.h"
52
#include "vin.h"
53
#include "vlu.h"
54
#include "vsb.h"
55
#include "vsm_priv.h"
56
#include "vqueue.h"
57
#include "vtim.h"
58
59
#include "vapi/vsig.h"
60
#include "vapi/vsm.h"
61
62
#ifndef MAP_HASSEMAPHORE
63
#  define MAP_HASSEMAPHORE 0 /* XXX Linux */
64
#endif
65
66
#ifndef MAP_NOSYNC
67
#  define MAP_NOSYNC 0 /* XXX Linux */
68
#endif
69
70
const struct vsm_valid VSM_invalid[1] = {{"invalid"}};
71
const struct vsm_valid VSM_valid[1] = {{"valid"}};
72
73
static vlu_f vsm_vlu_func;
74
75
#define VSM_PRIV_SHIFT                                                  \
76
        (sizeof (uint64_t) * 4)
77
#define VSM_PRIV_MASK                                                   \
78
        ((1ULL << VSM_PRIV_SHIFT) - 1)
79
#define VSM_PRIV_LOW(u)                                                 \
80
        ((uint64_t)(u) & VSM_PRIV_MASK)
81
#define VSM_PRIV_HIGH(u)                                                \
82
        (((uint64_t)(u) >> VSM_PRIV_SHIFT) & VSM_PRIV_MASK)
83
#define VSM_PRIV_MERGE(low, high)                                       \
84
        (VSM_PRIV_LOW(low) | (VSM_PRIV_LOW(high) << VSM_PRIV_SHIFT))
85
86
/*--------------------------------------------------------------------*/
87
88
struct vsm_set;
89
90
struct vsm_seg {
91
        unsigned                magic;
92
#define VSM_SEG_MAGIC           0xeb6c6dfd
93
        unsigned                flags;
94
#define VSM_FLAG_MARKSCAN       (1U<<1)
95
#define VSM_FLAG_STALE          (1U<<2)
96
#define VSM_FLAG_CLUSTER        (1U<<3)
97
        VTAILQ_ENTRY(vsm_seg)   list;
98
        VTAILQ_ENTRY(vsm_seg)   clist;
99
        struct vsm_set          *set;
100
        struct vsm_seg          *cluster;
101
        char                    **av;
102
        int                     refs;
103
        void                    *s;
104
        size_t                  sz;
105
        void                    *b;
106
        void                    *e;
107
        uint64_t                serial;
108
};
109
110
struct vsm_set {
111
        unsigned                magic;
112
#define VSM_SET_MAGIC           0xdee401b8
113
        const char              *dname;
114
        struct vsm              *vsm;
115
        VTAILQ_HEAD(,vsm_seg)   segs;
116
        VTAILQ_HEAD(,vsm_seg)   stale;
117
        VTAILQ_HEAD(,vsm_seg)   clusters;
118
119
        int                     dfd;
120
        struct stat             dst;
121
122
        int                     fd;
123
        struct stat             fst;
124
125
        uintmax_t               id1, id2;
126
127
        // _.index reading state
128
        struct vlu              *vlu;
129
        unsigned                retval;
130
        struct vsm_seg          *vg;
131
132
        unsigned                flag_running;
133
        unsigned                flag_changed;
134
        unsigned                flag_restarted;
135
136
        int                     couldkill;
137
};
138
139
struct vsm {
140
        unsigned                magic;
141
#define VSM_MAGIC               0x6e3bd69b
142
143
        struct vsb              *diag;
144
        uint64_t                serial;
145
146
        int                     wdfd;
147
        struct stat             wdst;
148
        char                    *wdname;
149
150
        struct vsm_set          *mgt;
151
        struct vsm_set          *child;
152
153
        int                     attached;
154
        double                  patience;
155
};
156
157
/*--------------------------------------------------------------------*/
158
159
static int
160 94
vsm_diag(struct vsm *vd, const char *fmt, ...)
161
{
162
        va_list ap;
163
164 94
        CHECK_OBJ_NOTNULL(vd, VSM_MAGIC);
165 94
        AN(fmt);
166
167 94
        if (vd->diag == NULL)
168 94
                vd->diag = VSB_new_auto();
169 94
        AN(vd->diag);
170 94
        VSB_clear(vd->diag);
171 94
        va_start(ap, fmt);
172 94
        VSB_vprintf(vd->diag, fmt, ap);
173 94
        va_end(ap);
174 94
        AZ(VSB_finish(vd->diag));
175 94
        return (-1);
176
}
177
178
/*--------------------------------------------------------------------*/
179
180
static int
181 13371
vsm_mapseg(struct vsm *vd, struct vsm_seg *vg)
182
{
183
        size_t of, off, sz, ps, len;
184
        struct vsb *vsb;
185
        void *s;
186
        int fd;
187
188 13371
        CHECK_OBJ_NOTNULL(vg, VSM_SEG_MAGIC);
189
190 13371
        if (vg->s != NULL)
191 202
                return (0);
192
193 13169
        ps = getpagesize();
194
195 13169
        of = strtoul(vg->av[2], NULL, 10);
196 13169
        off = RDN2(of, ps);
197
198 13169
        if (vg->flags & VSM_FLAG_CLUSTER)
199 87
                assert(of == 0);
200 13169
        assert(vg->cluster == NULL);
201
202 13169
        sz = strtoul(vg->av[3], NULL, 10);
203 13169
        assert(sz > 0);
204 13169
        assert(of >= off);
205 13169
        len = RUP2((of - off) + sz, ps);
206
207 13169
        vsb = VSB_new_auto();
208 13169
        AN(vsb);
209 13169
        VSB_printf(vsb, "%s/%s/%s", vd->wdname, vg->set->dname, vg->av[1]);
210 13169
        AZ(VSB_finish(vsb));
211
212 13169
        fd = open(VSB_data(vsb), O_RDONLY);     // XXX: openat
213 13169
        if (fd < 0) {
214 3
                VSB_destroy(&vsb);
215 3
                return (vsm_diag(vd, "Could not open segment"));
216
        }
217
218 26332
        s = (void*)mmap(NULL, len,
219
            PROT_READ,
220
            MAP_HASSEMAPHORE | MAP_NOSYNC | MAP_SHARED,
221 13166
            fd, (off_t)off);
222
223 13166
        VSB_destroy(&vsb);
224
225 13166
        closefd(&fd);
226 13166
        if (s == MAP_FAILED)
227 0
                return (vsm_diag(vd, "Could not mmap segment"));
228
229 13166
        vg->s = s;
230 13166
        vg->b = (char*)(vg->s) + of - off;
231 13166
        vg->e = (char *)vg->b + sz;
232 13166
        vg->sz = len;
233
234 13166
        return (0);
235 13371
}
236
237
static void
238 11726
vsm_unmapseg(struct vsm_seg *vg)
239
{
240
241 11726
        CHECK_OBJ_NOTNULL(vg, VSM_SEG_MAGIC);
242
243 11726
        AN(vg->b);
244 11726
        AN(vg->e);
245 11726
        AZ(munmap(vg->s, vg->sz));
246 11726
        vg->s = vg->b = vg->e = NULL;
247 11726
        vg->sz = 0;
248 11726
}
249
250
/*--------------------------------------------------------------------*/
251
252
static void
253 167584
vsm_delseg(struct vsm_seg *vg, int refsok)
254
{
255
256 167584
        CHECK_OBJ_NOTNULL(vg, VSM_SEG_MAGIC);
257
258 167584
        if (vg->set->vg == vg) {
259 9823
                AZ(vg->flags & VSM_FLAG_STALE);
260 9823
                vg->set->vg = VTAILQ_NEXT(vg, list);
261 9823
        }
262
263 167584
        if (refsok && vg->refs) {
264 286
                AZ(vg->flags & VSM_FLAG_STALE);
265 286
                vg->flags |= VSM_FLAG_STALE;
266 286
                VTAILQ_REMOVE(&vg->set->segs, vg, list);
267 286
                VTAILQ_INSERT_TAIL(&vg->set->stale, vg, list);
268 286
                return;
269
        }
270
271 167298
        if (vg->s != NULL)
272 0
                vsm_unmapseg(vg);
273
274 167298
        if (vg->flags & VSM_FLAG_CLUSTER) {
275 3896
                vg->flags &= ~VSM_FLAG_CLUSTER;
276 3896
                VTAILQ_REMOVE(&vg->set->clusters, vg, clist);
277 3896
        }
278
279 167298
        if (vg->flags & VSM_FLAG_STALE)
280 286
                VTAILQ_REMOVE(&vg->set->stale, vg, list);
281
        else
282 167012
                VTAILQ_REMOVE(&vg->set->segs, vg, list);
283 167298
        VAV_Free(vg->av);
284 167298
        FREE_OBJ(vg);
285 167584
}
286
287
/*--------------------------------------------------------------------*/
288
289
static struct vsm_set *
290 9152
vsm_newset(const char *dirname)
291
{
292
        struct vsm_set *vs;
293
294 9152
        ALLOC_OBJ(vs, VSM_SET_MAGIC);
295 9152
        AN(vs);
296 9152
        VTAILQ_INIT(&vs->segs);
297 9152
        VTAILQ_INIT(&vs->stale);
298 9152
        VTAILQ_INIT(&vs->clusters);
299 9152
        vs->dname = dirname;
300 9152
        vs->dfd = vs->fd = -1;
301 9152
        vs->vlu = VLU_New(vsm_vlu_func, vs, 0);
302 9152
        AN(vs->vlu);
303 9152
        if (getenv("VSM_NOPID") != NULL)
304 0
                vs->couldkill = -1;
305 9152
        return (vs);
306
}
307
308
static void
309 9040
vsm_delset(struct vsm_set **p)
310
{
311
        struct vsm_set *vs;
312
        struct vsm_seg *vg;
313
314 9040
        TAKE_OBJ_NOTNULL(vs, p, VSM_SET_MAGIC);
315
316 9040
        if (vs->fd >= 0)
317 5558
                closefd(&vs->fd);
318 9040
        if (vs->dfd >= 0)
319 7448
                closefd(&vs->dfd);
320 9040
        while ((vg = VTAILQ_FIRST(&vs->stale)) != NULL) {
321 0
                AN(vg->flags & VSM_FLAG_STALE);
322 0
                vsm_delseg(vg, 0);
323
        }
324 166694
        while ((vg = VTAILQ_FIRST(&vs->segs)) != NULL) {
325 157654
                AZ(vg->flags & VSM_FLAG_STALE);
326 157654
                vsm_delseg(vg, 0);
327
        }
328 9040
        assert(VTAILQ_EMPTY(&vs->clusters));
329 9040
        VLU_Destroy(&vs->vlu);
330 9040
        FREE_OBJ(vs);
331 9040
}
332
333
static void
334 20363
vsm_wash_set(const struct vsm_set *vs, int all)
335
{
336
        struct vsm_seg *vg, *vg2;
337
338 184350
        VTAILQ_FOREACH_SAFE(vg, &vs->segs, list, vg2) {
339 163987
                if (all || (vg->flags & VSM_FLAG_MARKSCAN) == 0)
340 5642
                        vsm_delseg(vg, 1);
341 163987
        }
342 20363
}
343
344
/*--------------------------------------------------------------------*/
345
346
struct vsm *
347 4576
VSM_New(void)
348
{
349
        struct vsm *vd;
350
351 4576
        ALLOC_OBJ(vd, VSM_MAGIC);
352 4576
        AN(vd);
353
354 4576
        vd->mgt = vsm_newset(VSM_MGT_DIRNAME);
355 4576
        vd->mgt->flag_running = VSM_MGT_RUNNING;
356 4576
        vd->mgt->flag_changed = VSM_MGT_CHANGED;
357 4576
        vd->mgt->flag_restarted = VSM_MGT_RESTARTED;
358
359 4576
        vd->child = vsm_newset(VSM_CHILD_DIRNAME);
360 4576
        vd->child->flag_running = VSM_WRK_RUNNING;
361 4576
        vd->child->flag_changed = VSM_WRK_CHANGED;
362 4576
        vd->child->flag_restarted = VSM_WRK_RESTARTED;
363
364 4576
        vd->mgt->vsm = vd;
365 4576
        vd->child->vsm = vd;
366 4576
        vd->wdfd = -1;
367 4576
        vd->patience = 5;
368 4576
        return (vd);
369
}
370
371
/*--------------------------------------------------------------------*/
372
373
int
374 4664
VSM_Arg(struct vsm *vd, char flag, const char *arg)
375
{
376 4664
        char *p = NULL;
377
378 4664
        CHECK_OBJ_NOTNULL(vd, VSM_MAGIC);
379
380 4664
        if (arg == NULL)
381 82
                return (1);
382 4582
        switch (flag) {
383
        case 't':
384 26
                if (!strcasecmp(arg, "off")) {
385 0
                        vd->patience = -1;
386 0
                } else {
387 26
                        vd->patience = strtod(arg, &p);
388 42
                        if ((p != NULL && *p != '\0') ||
389 16
                            !isfinite(vd->patience) || vd->patience < 0)
390 28
                                return (vsm_diag(vd,
391 14
                                    "-t: Invalid argument: %s", arg));
392
                }
393 12
                break;
394
        case 'n':
395 4556
                if (vd->wdname != NULL)
396 0
                        free(vd->wdname);
397 4556
                vd->wdname = VIN_n_Arg(arg);
398 4556
                if (vd->wdname == NULL)
399 0
                        return (vsm_diag(vd, "Invalid instance name: %s",
400 0
                            strerror(errno)));
401 4556
                break;
402
        default:
403 0
                return (vsm_diag(vd, "Unknown VSM_Arg('%c')", flag));
404
        }
405 4568
        return (1);
406 4664
}
407
408
/*--------------------------------------------------------------------*/
409
410
void
411 4520
VSM_Destroy(struct vsm **vdp)
412
{
413
        struct vsm *vd;
414
415 4520
        TAKE_OBJ_NOTNULL(vd, vdp, VSM_MAGIC);
416
417 4520
        VSM_ResetError(vd);
418 4520
        REPLACE(vd->wdname, NULL);
419 4520
        if (vd->diag != NULL)
420 0
                VSB_destroy(&vd->diag);
421 4520
        if (vd->wdfd >= 0)
422 4516
                closefd(&vd->wdfd);
423 4520
        vsm_delset(&vd->mgt);
424 4520
        vsm_delset(&vd->child);
425 4520
        FREE_OBJ(vd);
426 4520
}
427
428
/*--------------------------------------------------------------------*/
429
430
const char *
431 25
VSM_Error(const struct vsm *vd)
432
{
433
434 25
        CHECK_OBJ_NOTNULL(vd, VSM_MAGIC);
435
436 25
        if (vd->diag == NULL)
437 0
                return ("No VSM error");
438
        else
439 25
                return (VSB_data(vd->diag));
440 25
}
441
442
/*--------------------------------------------------------------------*/
443
444
void
445 9141
VSM_ResetError(struct vsm *vd)
446
{
447
448 9141
        CHECK_OBJ_NOTNULL(vd, VSM_MAGIC);
449
450 9141
        if (vd->diag == NULL)
451 9065
                return;
452 76
        VSB_destroy(&vd->diag);
453 9141
}
454
455
/*--------------------------------------------------------------------
456
 */
457
458
static int
459 35526
vsm_cmp_av(char * const *a1, char * const *a2)
460
{
461
462 52119
        while (1) {
463 52119
                if (*a1 == NULL && *a2 == NULL)
464 4106
                        return (0);
465 48013
                if (*a1 == NULL || *a2 == NULL)
466 0
                        return (1);
467 48013
                if (strcmp(*a1, *a2))
468 31420
                        return (1);
469 16593
                a1++;
470 16593
                a2++;
471
        }
472 35526
}
473
474
static struct vsm_seg *
475 4192
vsm_findcluster(const struct vsm_set *vs, const char *cnam)
476
{
477
        struct vsm_seg *vg;
478 4192
        AN(vs);
479 4192
        AN(cnam);
480 5920
        VTAILQ_FOREACH(vg, &vs->clusters, clist) {
481 5920
                AN(vg->av[1]);
482 5920
                if (!strcmp(cnam, vg->av[1]))
483 4192
                        return (vg);
484 1728
        }
485 0
        return (NULL);
486 4192
}
487
488
static unsigned
489 150114
vsm_running(struct vsm_set *vs, pid_t pid)
490
{
491
492 150114
        AN(vs);
493
494 150114
        if (pid == 0)
495 0
                return (0);
496
497 150114
        if (kill(pid, 0) == 0) {
498 145745
                vs->couldkill = 1;
499 145745
                return (1);
500
        }
501 4369
        if (errno == EPERM)     /* a process exists, assume running */
502 0
                return (1);
503 4369
        assert(errno != EINVAL);
504 4369
        return (0);
505 150114
}
506
507
static int
508 7638
vsm_vlu_hash(struct vsm_set *vs, const char *line)
509
{
510
        int i;
511
        uintmax_t id1, id2;
512
513 7638
        i = sscanf(line, "# %ju %ju", &id1, &id2);
514 7638
        if (i != 2) {
515 0
                vs->retval |= vs->flag_restarted;
516 0
                return (0);
517
        }
518 7638
        if (vs->couldkill >= 0 && vsm_running(vs, id1)) {
519
                /* nothing to do */
520 7638
        } else if (vs->couldkill > 0 && errno == ESRCH) {
521 0
                vs->retval |= vs->flag_restarted | VSM_MGT_CHANGED;
522 0
                return (0);
523
        }
524 7638
        vs->retval |= VSM_MGT_RUNNING;
525 7638
        if (id1 != vs->id1 || id2 != vs->id2) {
526 7634
                vs->retval |= vs->flag_restarted;
527 7634
                vs->id1 = id1;
528 7634
                vs->id2 = id2;
529 7634
        }
530 7638
        return (0);
531 7638
}
532
533
static int
534 169264
vsm_vlu_plus(struct vsm *vd, struct vsm_set *vs, const char *line)
535
{
536
        char **av;
537
        int ac;
538
        struct vsm_seg *vg;
539
540 169264
        av = VAV_Parse(line + 1, &ac, 0);
541
542 169264
        if (av[0] != NULL || ac < 4 || ac > 6) {
543 0
                (void)(vsm_diag(vd, "vsm_vlu_plus: bad index (%d/%s)",
544 0
                    ac, av[0]));
545 0
                VAV_Free(av);
546 0
                return (-1);
547
        }
548
549 169264
        vg = vs->vg;
550 169264
        CHECK_OBJ_ORNULL(vg, VSM_SEG_MAGIC);
551 169264
        if (vg != NULL)
552 104
                AZ(vg->flags & VSM_FLAG_STALE);
553 169264
        while (vg != NULL && vsm_cmp_av(&vg->av[1], &av[1]))
554 0
                vg = VTAILQ_NEXT(vg, list);
555 169264
        if (vg != NULL) {
556
                /* entry compared equal, so it survives */
557 104
                CHECK_OBJ_NOTNULL(vg, VSM_SEG_MAGIC);
558 104
                VAV_Free(av);
559 104
                vg->flags |= VSM_FLAG_MARKSCAN;
560 104
                vs->vg = VTAILQ_NEXT(vg, list);
561 104
        } else {
562 169160
                ALLOC_OBJ(vg, VSM_SEG_MAGIC);
563 169160
                AN(vg);
564 169160
                vg->av = av;
565 169160
                vg->set = vs;
566 169160
                vg->flags = VSM_FLAG_MARKSCAN;
567 169160
                vg->serial = vd->serial;
568
569 169160
                VTAILQ_INSERT_TAIL(&vs->segs, vg, list);
570 169160
                if (ac == 4) {
571 3928
                        vg->flags |= VSM_FLAG_CLUSTER;
572 3928
                        VTAILQ_INSERT_TAIL(&vs->clusters, vg, clist);
573 169160
                } else if (*vg->av[2] != '0') {
574 4088
                        vg->cluster = vsm_findcluster(vs, vg->av[1]);
575 4088
                        CHECK_OBJ_NOTNULL(vg->cluster, VSM_SEG_MAGIC);
576 4088
                }
577 169160
                vs->retval |= vs->flag_changed;
578
        }
579 169264
        return (0);
580 169264
}
581
582
static int
583 4002
vsm_vlu_minus(struct vsm *vd, struct vsm_set *vs, const char *line)
584
{
585
        char **av;
586
        int ac;
587
        struct vsm_seg *vg;
588
589 4002
        av = VAV_Parse(line + 1, &ac, 0);
590
591 4002
        if (av[0] != NULL || ac < 4 || ac > 6) {
592 0
                (void)(vsm_diag(vd, "vsm_vlu_minus: bad index (%d/%s)",
593 0
                    ac, av[0]));
594 0
                VAV_Free(av);
595 0
                return (-1);
596
        }
597
598
        /* Clustered segments cannot come before their cluster */
599 4002
        if (*av[2] != '0')
600 104
                vg = vsm_findcluster(vs, av[1]);
601
        else
602 3898
                vg = VTAILQ_FIRST(&vs->segs);
603
604 35422
        for (;vg != NULL; vg = VTAILQ_NEXT(vg, list)) {
605 35422
                if (!vsm_cmp_av(&vg->av[1], &av[1])) {
606 4002
                        vs->retval |= vs->flag_changed;
607 4002
                        vsm_delseg(vg, 1);
608 4002
                        break;
609
                }
610 31420
        }
611 4002
        AN(vg);
612 4002
        VAV_Free(av);
613 4002
        return (0);
614 4002
}
615
616
static int v_matchproto_(vlu_f)
617 180904
vsm_vlu_func(void *priv, const char *line)
618
{
619
        struct vsm *vd;
620
        struct vsm_set *vs;
621 180904
        int i = 0;
622
623 180904
        CAST_OBJ_NOTNULL(vs, priv, VSM_SET_MAGIC);
624 180904
        vd = vs->vsm;
625 180904
        CHECK_OBJ_NOTNULL(vd, VSM_MAGIC);
626 180904
        AN(line);
627
628
        /* Up the serial counter. This wraps at UINTPTR_MAX/2
629
         * because thats the highest value we can store in struct
630
         * vsm_fantom. */
631 180904
        vd->serial = VSM_PRIV_LOW(vd->serial + 1);
632
633 180904
        switch (line[0]) {
634
        case '#':
635 7638
                i = vsm_vlu_hash(vs, line);
636 13384
                VTAILQ_FOREACH(vs->vg, &vs->segs, list)
637 5746
                        vs->vg->flags &= ~VSM_FLAG_MARKSCAN;
638 7638
                if (!(vs->retval & vs->flag_restarted))
639 4
                        vs->vg = VTAILQ_FIRST(&vs->segs);
640 7638
                break;
641
        case '+':
642 169264
                i = vsm_vlu_plus(vd, vs, line);
643 169264
                break;
644
        case '-':
645 4002
                i = vsm_vlu_minus(vd, vs, line);
646 4002
                break;
647
        default:
648 0
                break;
649
        }
650 180904
        return (i);
651
}
652
653
static void
654 142476
vsm_readlines(struct vsm_set *vs)
655
{
656
        int i;
657
658 142476
        do {
659 161090
                assert(vs->fd >= 0);
660 161090
                i = VLU_Fd(vs->vlu, vs->fd);
661 161090
        } while (!i);
662 142476
        assert(i == -2);
663 142476
}
664
665
static unsigned
666 158864
vsm_refresh_set(struct vsm *vd, struct vsm_set *vs)
667
{
668 158864
        unsigned restarted = 0;
669
        struct stat st;
670
671 158864
        CHECK_OBJ_NOTNULL(vd, VSM_MAGIC);
672 158864
        CHECK_OBJ_NOTNULL(vs, VSM_SET_MAGIC);
673 158864
        vs->retval = 0;
674 297369
        if (vs->dfd >= 0 && (
675 138627
            fstatat(vd->wdfd, vs->dname, &st, AT_SYMLINK_NOFOLLOW) ||
676 138627
            st.st_ino != vs->dst.st_ino ||
677 138505
            st.st_dev != vs->dst.st_dev ||
678 138505
            st.st_mode != vs->dst.st_mode ||
679 138505
            st.st_nlink == 0)) {
680 122
                closefd(&vs->dfd);
681 122
                restarted = vs->flag_restarted;
682 122
        }
683
684 158864
        if (vs->dfd < 0) {
685 20359
                if (vs->fd >= 0)
686 122
                        closefd(&vs->fd);
687 20359
                vs->dfd = openat(vd->wdfd, vs->dname, O_RDONLY);
688 20359
        }
689
690 158864
        if (vs->dfd < 0) {
691 12725
                vs->id1 = vs->id2 = 0;
692 12725
                vsm_wash_set(vs, 1);
693 12725
                return (vs->retval | restarted);
694
        }
695
696 146139
        AZ(fstat(vs->dfd, &vs->dst));
697
698 280978
        if (vs->fd >= 0 && (
699 136733
            fstatat(vs->dfd, "_.index", &st, AT_SYMLINK_NOFOLLOW) ||
700 134843
            st.st_ino != vs->fst.st_ino ||
701 134839
            st.st_dev != vs->fst.st_dev ||
702 134839
            st.st_mode != vs->fst.st_mode ||
703 134839
            st.st_size < vs->fst.st_size ||
704 134839
            st.st_nlink < 1)) {
705 1894
                closefd(&vs->fd);
706 1894
                vs->retval |= vs->flag_changed;
707 1894
        }
708
709 146139
        if (vs->fd >= 0) {
710 134839
                vs->vg = NULL;
711 134839
                vsm_readlines(vs);
712 134839
        } else {
713 35249
                VTAILQ_FOREACH(vs->vg, &vs->segs, list)
714 23949
                        vs->vg->flags &= ~VSM_FLAG_MARKSCAN;
715 11300
                vs->vg = VTAILQ_FIRST(&vs->segs);
716 11300
                vs->fd = openat(vs->dfd, "_.index", O_RDONLY);
717 11300
                if (vs->fd < 0)
718 3662
                        return (vs->retval | restarted);
719 7638
                VLU_Reset(vs->vlu);
720 7638
                AZ(fstat(vs->fd, &vs->fst));
721 7638
                vsm_readlines(vs);
722 7638
                vsm_wash_set(vs, 0);
723
        }
724
725 142477
        vs->fst.st_size = lseek(vs->fd, 0L, SEEK_CUR);
726
727 142477
        if (vs->couldkill < 0 || vsm_running(vs, vs->id1))
728 138114
                vs->retval |= vs->flag_running;
729 142477
        return (vs->retval);
730 158864
}
731
732
/*--------------------------------------------------------------------*/
733
734
unsigned
735 81274
VSM_Status(struct vsm *vd)
736
{
737 81274
        unsigned retval = 0;
738
        struct stat st;
739
740 81274
        CHECK_OBJ_NOTNULL(vd, VSM_MAGIC);
741
742
        /* See if the -n workdir changed */
743 81274
        if (vd->wdfd >= 0) {
744 76658
                AZ(fstat(vd->wdfd, &st));
745 153315
                if (st.st_ino != vd->wdst.st_ino ||
746 76657
                    st.st_dev != vd->wdst.st_dev ||
747 76657
                    st.st_mode != vd->wdst.st_mode ||
748 76657
                    st.st_nlink == 0) {
749 2
                        closefd(&vd->wdfd);
750 0
                        vsm_wash_set(vd->mgt, 1);
751 0
                        vsm_wash_set(vd->child, 1);
752 0
                }
753 76658
        }
754
755
        /* Open workdir */
756 81274
        if (vd->wdfd < 0) {
757 4617
                retval |= VSM_MGT_RESTARTED | VSM_MGT_CHANGED;
758 4617
                retval |= VSM_WRK_RESTARTED | VSM_WRK_CHANGED;
759 4617
                vd->wdfd = open(vd->wdname, O_RDONLY);
760 4617
                if (vd->wdfd < 0)
761 69
                        (void)vsm_diag(vd,
762
                            "VSM_Status: Cannot open workdir");
763
                else
764 4548
                        AZ(fstat(vd->wdfd, &vd->wdst));
765 4617
        }
766
767 81274
        if (vd->wdfd >= 0) {
768 81206
                retval |= vsm_refresh_set(vd, vd->mgt);
769 81206
                if (vd->mgt->couldkill > 0 && (retval & VSM_MGT_RESTARTED))
770 4548
                        vd->mgt->couldkill = 0;
771 81206
                if (retval & VSM_MGT_RUNNING)
772 77658
                        retval |= vsm_refresh_set(vd, vd->child);
773 81206
                if (vd->child->couldkill > 0 && (retval & VSM_WRK_RESTARTED))
774 3083
                        vd->child->couldkill = 0;
775 81206
        }
776 81274
        return (retval);
777
}
778
779
/*--------------------------------------------------------------------*/
780
781
int
782 4556
VSM_Attach(struct vsm *vd, int progress)
783
{
784
        const char *def;
785
        double t0;
786
        unsigned u;
787 4556
        int i, n = 0;
788
789 4556
        CHECK_OBJ_NOTNULL(vd, VSM_MAGIC);
790
791 4556
        if (vd->patience < 0)
792 0
                t0 = DBL_MAX;
793
        else
794 4556
                t0 = VTIM_mono() + vd->patience;
795
796 4556
        if (vd->wdname == NULL) {
797 14
                def = getenv("VARNISH_DEFAULT_N");
798 14
                if (def == NULL)
799 0
                        def = ""; /* Use default (hostname) */
800 14
                i = VSM_Arg(vd, 'n', def);
801 14
                if (i < 0)
802 0
                        return (i);
803 14
                AN(vd->wdname);
804 14
        }
805
806 4556
        AZ(vd->attached);
807 4623
        while (!VSIG_int && !VSIG_term) {
808 4621
                u = VSM_Status(vd);
809 4621
                VSM_ResetError(vd);
810 4621
                if (u & VSM_MGT_RUNNING) {
811 4548
                        if (progress >= 0 && n > 4)
812 0
                                (void)write(progress, "\n", 1);
813 4548
                        vd->attached = 1;
814 4548
                        return (0);
815
                }
816 73
                if (t0 < VTIM_mono()) {
817 6
                        if (progress >= 0 && n > 4)
818 2
                                (void)write(progress, "\n", 1);
819 6
                        return (vsm_diag(vd,
820
                            "Could not get hold of varnishd, is it running?"));
821
                }
822 67
                if (progress >= 0 && !(++n % 4))
823 15
                        (void)write(progress, ".", 1);
824 67
                VTIM_sleep(.25);
825
        }
826 2
        return (vsm_diag(vd, "Attach interrupted"));
827 4556
}
828
829
/*--------------------------------------------------------------------*/
830
831
static struct vsm_seg *
832 12752
vsm_set_findseg(const struct vsm_set *vs, uintptr_t serial)
833
{
834
        struct vsm_seg *vg;
835
836 173329
        VTAILQ_FOREACH(vg, &vs->segs, list) {
837 165618
                if (vg->serial == serial)
838 5041
                        return (vg);
839 160577
        }
840 15081
        VTAILQ_FOREACH(vg, &vs->stale, list) {
841 7652
                if (vg->serial == serial)
842 282
                        return (vg);
843 7370
        }
844 7429
        return (NULL);
845 12752
}
846
847
static struct vsm_seg *
848 433066
vsm_findseg(const struct vsm *vd, const struct vsm_fantom *vf)
849
{
850
        struct vsm_seg *vg;
851
        uint64_t x;
852
853 433066
        x = VSM_PRIV_HIGH(vf->priv);
854 433066
        if (x == vd->serial) {
855 426607
                vg = (struct vsm_seg *)vf->priv2;
856 426607
                if (!VALID_OBJ(vg, VSM_SEG_MAGIC) ||
857 426607
                    vg->serial != VSM_PRIV_LOW(vf->priv))
858 0
                        WRONG("Corrupt fantom");
859 426607
                return (vg);
860
        }
861
862 6459
        x = VSM_PRIV_LOW(vf->priv);
863 6459
        vg = vsm_set_findseg(vd->mgt, x);
864 6459
        if (vg == NULL)
865 6293
                vg = vsm_set_findseg(vd->child, x);
866 6459
        if (vg == NULL)
867 1136
                return (NULL);
868
869
        /* Update the fantom with the new priv so that lookups will be
870
         * fast on the next call. Note that this casts away the const. */
871 5323
        ((struct vsm_fantom *)TRUST_ME(vf))->priv =
872 5323
            VSM_PRIV_MERGE(vg->serial, vd->serial);
873 5323
        return (vg);
874 433066
}
875
876
/*--------------------------------------------------------------------*/
877
878
void
879 17505
VSM__iter0(const struct vsm *vd, struct vsm_fantom *vf)
880
{
881
882 17505
        CHECK_OBJ_NOTNULL(vd, VSM_MAGIC);
883 17505
        AN(vf);
884
885 17505
        AN(vd->attached);
886 17505
        memset(vf, 0, sizeof *vf);
887 17505
}
888
889
int
890 244700
VSM__itern(struct vsm *vd, struct vsm_fantom *vf)
891
{
892
        struct vsm_seg *vg;
893
894 244700
        CHECK_OBJ_NOTNULL(vd, VSM_MAGIC);
895 244700
        AN(vd->attached);
896 244700
        AN(vf);
897
898 244700
        if (vf->priv == 0) {
899 17505
                vg = VTAILQ_FIRST(&vd->mgt->segs);
900 17505
                if (vg == NULL)
901 1
                        return (0);
902 17504
        } else {
903 227195
                vg = vsm_findseg(vd, vf);
904 227195
                if (vg == NULL)
905 0
                        return (vsm_diag(vd, "VSM_FOREACH: inconsistency"));
906 230584
                while (1) {
907 230584
                        if (vg->set == vd->mgt && VTAILQ_NEXT(vg, list) == NULL)
908 17314
                                vg = VTAILQ_FIRST(&vd->child->segs);
909
                        else
910 213270
                                vg = VTAILQ_NEXT(vg, list);
911 230584
                        if (vg == NULL)
912 11739
                                return (0);
913 218845
                        if (!(vg->flags & VSM_FLAG_CLUSTER))
914 215456
                                break;
915
                }
916
        }
917 232960
        memset(vf, 0, sizeof *vf);
918 232960
        vf->priv = VSM_PRIV_MERGE(vg->serial, vd->serial);
919 232960
        vf->priv2 = (uintptr_t)vg;
920 232960
        vf->category = vg->av[4];
921 232960
        vf->ident = vg->av[5];
922 232960
        AN(vf->category);
923 232960
        return (1);
924 244700
}
925
926
/*--------------------------------------------------------------------*/
927
928
int
929 13371
VSM_Map(struct vsm *vd, struct vsm_fantom *vf)
930
{
931
        struct vsm_seg *vg, *vgc;
932
        size_t of, sz;
933
        int r;
934
935 13371
        CHECK_OBJ_NOTNULL(vd, VSM_MAGIC);
936 13371
        AN(vd->attached);
937 13371
        AN(vf);
938 13371
        vg = vsm_findseg(vd, vf);
939 13371
        if (vg == NULL)
940 0
                return (vsm_diag(vd, "VSM_Map: bad fantom"));
941
942 13371
        assert(vg->serial == VSM_PRIV_LOW(vf->priv));
943 13371
        assert(vg->av[4] == vf->category);
944 13371
        assert(vg->av[5] == vf->ident);
945
946 13371
        if (vg->b != NULL) {
947 0
                assert(vg->refs > 0);
948 0
                AN(vg->e);
949 0
                vf->b = vg->b;
950 0
                vf->e = vg->e;
951 0
                vg->refs++;
952 0
                return (0);
953
        }
954
955 13371
        assert(vg->refs == 0);
956
957 13371
        vgc = vg->cluster;
958
959 13371
        if (vgc == NULL) {
960 13082
                r = vsm_mapseg(vd, vg);
961 13082
                if (r)
962 3
                        return (r);
963 13079
                vf->b = vg->b;
964 13079
                vf->e = vg->e;
965
966 13079
                vg->refs++;
967
968 13079
                return (0);
969
        }
970
971 289
        CHECK_OBJ_NOTNULL(vgc, VSM_SEG_MAGIC);
972 289
        assert(vgc->flags & VSM_FLAG_CLUSTER);
973 289
        assert(vg->s == NULL);
974 289
        assert(vg->sz == 0);
975
976 289
        r = vsm_mapseg(vd, vgc);
977 289
        if (r)
978 0
                return (r);
979 289
        vgc->refs++;
980
981 289
        of = strtoul(vg->av[2], NULL, 10);
982 289
        sz = strtoul(vg->av[3], NULL, 10);
983 289
        assert(sz > 0);
984
985 289
        assert(vgc->sz >= of + sz);
986 289
        assert(vgc->s == vgc->b);
987 289
        vg->b = (char *)vgc->b + of;
988 289
        vg->e = (char *)vg->b + sz;
989
990 289
        vf->b = vg->b;
991 289
        vf->e = vg->e;
992
993 289
        vg->refs++;
994
995 289
        return (0);
996 13371
}
997
998
/*--------------------------------------------------------------------*/
999
1000
int
1001 11730
VSM_Unmap(struct vsm *vd, struct vsm_fantom *vf)
1002
{
1003
        struct vsm_seg *vg;
1004
1005 11730
        CHECK_OBJ_NOTNULL(vd, VSM_MAGIC);
1006 11730
        AN(vd->attached);
1007 11730
        AN(vf);
1008 11730
        AN(vf->b);
1009 11730
        vg = vsm_findseg(vd, vf);
1010 11730
        if (vg == NULL)
1011 0
                return (vsm_diag(vd, "VSM_Unmap: bad fantom"));
1012 11730
        CHECK_OBJ_NOTNULL(vg, VSM_SEG_MAGIC);
1013 11730
        assert(vg->refs > 0);
1014 11730
        vg->refs--;
1015 11730
        vf->b = NULL;
1016 11730
        vf->e = NULL;
1017 11730
        if (vg->refs > 0)
1018 0
                return (0);
1019
1020 11730
        if (vg->cluster) {
1021 59
                CHECK_OBJ_NOTNULL(vg->cluster, VSM_SEG_MAGIC);
1022 59
                assert(vg->s == NULL);
1023 59
                assert(vg->sz == 0);
1024 59
                assert(vg->cluster->refs > 0);
1025 59
                if (--vg->cluster->refs == 0) {
1026 55
                        vsm_unmapseg(vg->cluster);
1027 55
                        if (vg->cluster->flags & VSM_FLAG_STALE) {
1028 4
                                AN(vg->flags & VSM_FLAG_STALE);
1029 4
                                vsm_delseg(vg->cluster, 0);
1030 4
                        }
1031 55
                }
1032 59
                vg->b = vg->e = NULL;
1033 59
        } else {
1034 11671
                vsm_unmapseg(vg);
1035
        }
1036 11730
        if (vg->flags & VSM_FLAG_STALE)
1037 282
                vsm_delseg(vg, 0);
1038 11730
        return (0);
1039 11730
}
1040
1041
/*--------------------------------------------------------------------*/
1042
1043
const struct vsm_valid *
1044 180759
VSM_StillValid(const struct vsm *vd, const struct vsm_fantom *vf)
1045
{
1046
        struct vsm_seg *vg;
1047
1048 180759
        CHECK_OBJ_NOTNULL(vd, VSM_MAGIC);
1049 180759
        AN(vf);
1050 180759
        vg = vsm_findseg(vd, vf);
1051 180759
        if (vg == NULL || vg->flags & VSM_FLAG_STALE)
1052 1445
                return (VSM_invalid);
1053 179314
        return (VSM_valid);
1054 180759
}
1055
1056
/*--------------------------------------------------------------------*/
1057
1058
int
1059 14649
VSM_Get(struct vsm *vd, struct vsm_fantom *vf,
1060
    const char *category, const char *ident)
1061
{
1062
1063 14649
        CHECK_OBJ_NOTNULL(vd, VSM_MAGIC);
1064 14649
        AN(vd->attached);
1065 104581
        VSM_FOREACH(vf, vd) {
1066 95507
                if (strcmp(vf->category, category))
1067 89932
                        continue;
1068 5575
                if (ident != NULL && strcmp(vf->ident, ident))
1069 0
                        continue;
1070 5575
                return (1);
1071
        }
1072 9074
        memset(vf, 0, sizeof *vf);
1073 9074
        return (0);
1074 14649
}
1075
1076
/*--------------------------------------------------------------------*/
1077
1078
char *
1079 190
VSM_Dup(struct vsm *vd, const char *category, const char *ident)
1080
{
1081
        struct vsm_fantom vf;
1082 190
        char *p = NULL;
1083
1084 190
        CHECK_OBJ_NOTNULL(vd, VSM_MAGIC);
1085 190
        AN(vd->attached);
1086 764
        VSM_FOREACH(&vf, vd) {
1087 764
                if (strcmp(vf.category, category))
1088 328
                        continue;
1089 436
                if (ident != NULL && strcmp(vf.ident, ident))
1090 246
                        continue;
1091 190
                AZ(VSM_Map(vd, &vf));
1092 190
                AN(vf.b);
1093 190
                AN(vf.e);
1094 190
                p = malloc((char*)vf.e - (char*)vf.b);
1095 190
                AN(p);
1096 190
                memcpy(p, vf.b, (char*)vf.e - (char*)vf.b);
1097 190
                AZ(VSM_Unmap(vd, &vf));
1098 190
                break;
1099
        }
1100 190
        return (p);
1101
}