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 1824
vsm_diag(struct vsm *vd, const char *fmt, ...)
161
{
162
        va_list ap;
163
164 1824
        CHECK_OBJ_NOTNULL(vd, VSM_MAGIC);
165 1824
        AN(fmt);
166
167 1824
        if (vd->diag == NULL)
168 1824
                vd->diag = VSB_new_auto();
169 1824
        AN(vd->diag);
170 1824
        VSB_clear(vd->diag);
171 1824
        va_start(ap, fmt);
172 1824
        VSB_vprintf(vd->diag, fmt, ap);
173 1824
        va_end(ap);
174 1824
        AZ(VSB_finish(vd->diag));
175 1824
        return (-1);
176
}
177
178
/*--------------------------------------------------------------------*/
179
180
static int
181 238161
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 238161
        CHECK_OBJ_NOTNULL(vg, VSM_SEG_MAGIC);
189
190 238161
        if (vg->s != NULL)
191 4000
                return (0);
192
193 234161
        ps = getpagesize();
194
195 234161
        of = strtoul(vg->av[2], NULL, 10);
196 234161
        off = RDN2(of, ps);
197
198 234161
        if (vg->flags & VSM_FLAG_CLUSTER)
199 1679
                assert(of == 0);
200 234161
        assert(vg->cluster == NULL);
201
202 234161
        sz = strtoul(vg->av[3], NULL, 10);
203 234161
        assert(sz > 0);
204 234161
        assert(of >= off);
205 234161
        len = RUP2((of - off) + sz, ps);
206
207 234161
        vsb = VSB_new_auto();
208 234161
        AN(vsb);
209 234161
        VSB_printf(vsb, "%s/%s/%s", vd->wdname, vg->set->dname, vg->av[1]);
210 234161
        AZ(VSB_finish(vsb));
211
212 234161
        fd = open(VSB_data(vsb), O_RDONLY);     // XXX: openat
213 234161
        if (fd < 0) {
214 16
                VSB_destroy(&vsb);
215 16
                return (vsm_diag(vd, "Could not open segment"));
216
        }
217
218 468290
        s = (void*)mmap(NULL, len,
219
            PROT_READ,
220
            MAP_HASSEMAPHORE | MAP_NOSYNC | MAP_SHARED,
221 234145
            fd, (off_t)off);
222
223 234145
        VSB_destroy(&vsb);
224
225 234145
        closefd(&fd);
226 234145
        if (s == MAP_FAILED)
227 0
                return (vsm_diag(vd, "Could not mmap segment"));
228
229 234145
        vg->s = s;
230 234145
        vg->b = (char*)(vg->s) + of - off;
231 234145
        vg->e = (char *)vg->b + sz;
232 234145
        vg->sz = len;
233
234 234145
        return (0);
235 238161
}
236
237
static void
238 209825
vsm_unmapseg(struct vsm_seg *vg)
239
{
240
241 209825
        CHECK_OBJ_NOTNULL(vg, VSM_SEG_MAGIC);
242
243 209825
        AN(vg->b);
244 209825
        AN(vg->e);
245 209825
        AZ(munmap(vg->s, vg->sz));
246 209825
        vg->s = vg->b = vg->e = NULL;
247 209825
        vg->sz = 0;
248 209825
}
249
250
/*--------------------------------------------------------------------*/
251
252
static void
253 2789135
vsm_delseg(struct vsm_seg *vg, int refsok)
254
{
255
256 2789135
        CHECK_OBJ_NOTNULL(vg, VSM_SEG_MAGIC);
257
258 2789135
        if (vg->set->vg == vg) {
259 190383
                AZ(vg->flags & VSM_FLAG_STALE);
260 190383
                vg->set->vg = VTAILQ_NEXT(vg, list);
261 190383
        }
262
263 2789135
        if (refsok && vg->refs) {
264 4895
                AZ(vg->flags & VSM_FLAG_STALE);
265 4895
                vg->flags |= VSM_FLAG_STALE;
266 4895
                VTAILQ_REMOVE(&vg->set->segs, vg, list);
267 4895
                VTAILQ_INSERT_TAIL(&vg->set->stale, vg, list);
268 4895
                return;
269
        }
270
271 2784240
        if (vg->s != NULL)
272 0
                vsm_unmapseg(vg);
273
274 2784240
        if (vg->flags & VSM_FLAG_CLUSTER) {
275 73640
                vg->flags &= ~VSM_FLAG_CLUSTER;
276 73640
                VTAILQ_REMOVE(&vg->set->clusters, vg, clist);
277 73640
        }
278
279 2784240
        if (vg->flags & VSM_FLAG_STALE)
280 4895
                VTAILQ_REMOVE(&vg->set->stale, vg, list);
281
        else
282 2779345
                VTAILQ_REMOVE(&vg->set->segs, vg, list);
283 2784240
        VAV_Free(vg->av);
284 2784240
        FREE_OBJ(vg);
285 2789135
}
286
287
/*--------------------------------------------------------------------*/
288
289
static struct vsm_set *
290 175840
vsm_newset(const char *dirname)
291
{
292
        struct vsm_set *vs;
293
294 175840
        ALLOC_OBJ(vs, VSM_SET_MAGIC);
295 175840
        AN(vs);
296 175840
        VTAILQ_INIT(&vs->segs);
297 175840
        VTAILQ_INIT(&vs->stale);
298 175840
        VTAILQ_INIT(&vs->clusters);
299 175840
        vs->dname = dirname;
300 175840
        vs->dfd = vs->fd = -1;
301 175840
        vs->vlu = VLU_New(vsm_vlu_func, vs, 0);
302 175840
        AN(vs->vlu);
303 175840
        if (getenv("VSM_NOPID") != NULL)
304 0
                vs->couldkill = -1;
305 175840
        return (vs);
306
}
307
308
static void
309 173600
vsm_delset(struct vsm_set **p)
310
{
311
        struct vsm_set *vs;
312
        struct vsm_seg *vg;
313
314 173600
        TAKE_OBJ_NOTNULL(vs, p, VSM_SET_MAGIC);
315
316 173600
        if (vs->fd >= 0)
317 106231
                closefd(&vs->fd);
318 173600
        if (vs->dfd >= 0)
319 142760
                closefd(&vs->dfd);
320 173600
        while ((vg = VTAILQ_FIRST(&vs->stale)) != NULL) {
321 0
                AN(vg->flags & VSM_FLAG_STALE);
322 0
                vsm_delseg(vg, 0);
323
        }
324 2799676
        while ((vg = VTAILQ_FIRST(&vs->segs)) != NULL) {
325 2626076
                AZ(vg->flags & VSM_FLAG_STALE);
326 2626076
                vsm_delseg(vg, 0);
327
        }
328 173600
        assert(VTAILQ_EMPTY(&vs->clusters));
329 173600
        VLU_Destroy(&vs->vlu);
330 173600
        FREE_OBJ(vs);
331 173600
}
332
333
static void
334 379118
vsm_wash_set(const struct vsm_set *vs, int all)
335
{
336
        struct vsm_seg *vg, *vg2;
337
338 3021791
        VTAILQ_FOREACH_SAFE(vg, &vs->segs, list, vg2) {
339 2642673
                if (all || (vg->flags & VSM_FLAG_MARKSCAN) == 0)
340 83036
                        vsm_delseg(vg, 1);
341 2642673
        }
342 379118
}
343
344
/*--------------------------------------------------------------------*/
345
346
struct vsm *
347 87920
VSM_New(void)
348
{
349
        struct vsm *vd;
350
351 87920
        ALLOC_OBJ(vd, VSM_MAGIC);
352 87920
        AN(vd);
353
354 87920
        vd->mgt = vsm_newset(VSM_MGT_DIRNAME);
355 87920
        vd->mgt->flag_running = VSM_MGT_RUNNING;
356 87920
        vd->mgt->flag_changed = VSM_MGT_CHANGED;
357 87920
        vd->mgt->flag_restarted = VSM_MGT_RESTARTED;
358
359 87920
        vd->child = vsm_newset(VSM_CHILD_DIRNAME);
360 87920
        vd->child->flag_running = VSM_WRK_RUNNING;
361 87920
        vd->child->flag_changed = VSM_WRK_CHANGED;
362 87920
        vd->child->flag_restarted = VSM_WRK_RESTARTED;
363
364 87920
        vd->mgt->vsm = vd;
365 87920
        vd->child->vsm = vd;
366 87920
        vd->wdfd = -1;
367 87920
        vd->patience = 5;
368 87920
        return (vd);
369
}
370
371
/*--------------------------------------------------------------------*/
372
373
int
374 89640
VSM_Arg(struct vsm *vd, char flag, const char *arg)
375
{
376 89640
        char *p = NULL;
377
378 89640
        CHECK_OBJ_NOTNULL(vd, VSM_MAGIC);
379
380 89640
        if (arg == NULL)
381 1600
                return (1);
382 88040
        switch (flag) {
383
        case 't':
384 520
                if (!strcasecmp(arg, "off")) {
385 0
                        vd->patience = -1;
386 0
                } else {
387 520
                        vd->patience = strtod(arg, &p);
388 840
                        if ((p != NULL && *p != '\0') ||
389 320
                            !isfinite(vd->patience) || vd->patience < 0)
390 560
                                return (vsm_diag(vd,
391 280
                                    "-t: Invalid argument: %s", arg));
392
                }
393 240
                break;
394
        case 'n':
395 87520
                if (vd->wdname != NULL)
396 0
                        free(vd->wdname);
397 87520
                vd->wdname = VIN_n_Arg(arg);
398 87520
                if (vd->wdname == NULL)
399 0
                        return (vsm_diag(vd, "Invalid instance name: %s",
400 0
                            strerror(errno)));
401 87520
                break;
402
        default:
403 0
                return (vsm_diag(vd, "Unknown VSM_Arg('%c')", flag));
404
        }
405 87760
        return (1);
406 89640
}
407
408
/*--------------------------------------------------------------------*/
409
410
void
411 86800
VSM_Destroy(struct vsm **vdp)
412
{
413
        struct vsm *vd;
414
415 86800
        TAKE_OBJ_NOTNULL(vd, vdp, VSM_MAGIC);
416
417 86800
        VSM_ResetError(vd);
418 86800
        REPLACE(vd->wdname, NULL);
419 86800
        if (vd->diag != NULL)
420 0
                VSB_destroy(&vd->diag);
421 86800
        if (vd->wdfd >= 0)
422 86720
                closefd(&vd->wdfd);
423 86800
        vsm_delset(&vd->mgt);
424 86800
        vsm_delset(&vd->child);
425 86800
        FREE_OBJ(vd);
426 86800
}
427
428
/*--------------------------------------------------------------------*/
429
430
const char *
431 456
VSM_Error(const struct vsm *vd)
432
{
433
434 456
        CHECK_OBJ_NOTNULL(vd, VSM_MAGIC);
435
436 456
        if (vd->diag == NULL)
437 0
                return ("No VSM error");
438
        else
439 456
                return (VSB_data(vd->diag));
440 456
}
441
442
/*--------------------------------------------------------------------*/
443
444
void
445 175608
VSM_ResetError(struct vsm *vd)
446
{
447
448 175608
        CHECK_OBJ_NOTNULL(vd, VSM_MAGIC);
449
450 175608
        if (vd->diag == NULL)
451 174144
                return;
452 1464
        VSB_destroy(&vd->diag);
453 175608
}
454
455
/*--------------------------------------------------------------------
456
 */
457
458
static int
459 646221
vsm_cmp_av(char * const *a1, char * const *a2)
460
{
461
462 960342
        while (1) {
463 960342
                if (*a1 == NULL && *a2 == NULL)
464 77024
                        return (0);
465 883318
                if (*a1 == NULL || *a2 == NULL)
466 32
                        return (1);
467 883318
                if (strcmp(*a1, *a2))
468 569197
                        return (1);
469 314121
                a1++;
470 314121
                a2++;
471
        }
472 646221
}
473
474
static struct vsm_seg *
475 80015
vsm_findcluster(const struct vsm_set *vs, const char *cnam)
476
{
477
        struct vsm_seg *vg;
478 80015
        AN(vs);
479 80015
        AN(cnam);
480 111164
        VTAILQ_FOREACH(vg, &vs->clusters, clist) {
481 111164
                AN(vg->av[1]);
482 111164
                if (!strcmp(cnam, vg->av[1]))
483 80015
                        return (vg);
484 31149
        }
485 0
        return (NULL);
486 80015
}
487
488
static unsigned
489 2392651
vsm_running(struct vsm_set *vs, pid_t pid)
490
{
491
492 2392651
        AN(vs);
493
494 2392651
        if (pid == 0)
495 0
                return (0);
496 2392651
        assert(pid > 1);
497
498 2392651
        if (kill(pid, 0) == 0) {
499 2309096
                vs->couldkill = 1;
500 2309096
                return (1);
501
        }
502 83555
        if (errno == EPERM)     /* a process exists, assume running */
503 0
                return (1);
504 83555
        assert(errno != EINVAL);
505 83555
        return (0);
506 2392651
}
507
508
static int
509 146236
vsm_vlu_hash(struct vsm_set *vs, const char *line)
510
{
511
        int i;
512
        uintmax_t id1, id2;
513
514 146236
        i = sscanf(line, "# %ju %ju", &id1, &id2);
515 146236
        if (i != 2) {
516 0
                vs->retval |= vs->flag_restarted;
517 0
                return (0);
518
        }
519 146236
        if (vs->couldkill >= 0 && vsm_running(vs, id1)) {
520
                /* nothing to do */
521 146236
        } else if (vs->couldkill > 0 && errno == ESRCH) {
522 0
                vs->retval |= vs->flag_restarted | VSM_MGT_CHANGED;
523 0
                return (0);
524
        }
525 146236
        vs->retval |= VSM_MGT_RUNNING;
526 146236
        if (id1 != vs->id1 || id2 != vs->id2) {
527 146160
                vs->retval |= vs->flag_restarted;
528 146160
                vs->id1 = id1;
529 146160
                vs->id2 = id2;
530 146160
        }
531 146236
        return (0);
532 146236
}
533
534
static int
535 2818896
vsm_vlu_plus(struct vsm *vd, struct vsm_set *vs, const char *line)
536
{
537
        char **av;
538
        int ac;
539
        struct vsm_seg *vg;
540
541 2818896
        av = VAV_Parse(line + 1, &ac, 0);
542
543 2818896
        if (av[0] != NULL || ac < 4 || ac > 6) {
544 0
                (void)(vsm_diag(vd, "vsm_vlu_plus: bad index (%d/%s)",
545 0
                    ac, av[0]));
546 0
                VAV_Free(av);
547 0
                return (-1);
548
        }
549
550 2818896
        vg = vs->vg;
551 2818896
        CHECK_OBJ_ORNULL(vg, VSM_SEG_MAGIC);
552 2818896
        if (vg != NULL)
553 1896
                AZ(vg->flags & VSM_FLAG_STALE);
554 2819576
        while (vg != NULL && vsm_cmp_av(&vg->av[1], &av[1]))
555 680
                vg = VTAILQ_NEXT(vg, list);
556 2818896
        if (vg != NULL) {
557
                /* entry compared equal, so it survives */
558 1896
                CHECK_OBJ_NOTNULL(vg, VSM_SEG_MAGIC);
559 1896
                VAV_Free(av);
560 1896
                vg->flags |= VSM_FLAG_MARKSCAN;
561 1896
                vs->vg = VTAILQ_NEXT(vg, list);
562 1896
        } else {
563 2817000
                ALLOC_OBJ(vg, VSM_SEG_MAGIC);
564 2817000
                AN(vg);
565 2817000
                vg->av = av;
566 2817000
                vg->set = vs;
567 2817000
                vg->flags = VSM_FLAG_MARKSCAN;
568 2817000
                vg->serial = vd->serial;
569
570 2817000
                VTAILQ_INSERT_TAIL(&vs->segs, vg, list);
571 2817000
                if (ac == 4) {
572 74280
                        vg->flags |= VSM_FLAG_CLUSTER;
573 74280
                        VTAILQ_INSERT_TAIL(&vs->clusters, vg, clist);
574 2817000
                } else if (*vg->av[2] != '0') {
575 77880
                        vg->cluster = vsm_findcluster(vs, vg->av[1]);
576 77880
                        CHECK_OBJ_NOTNULL(vg->cluster, VSM_SEG_MAGIC);
577 77880
                }
578 2817000
                vs->retval |= vs->flag_changed;
579
        }
580 2818896
        return (0);
581 2818896
}
582
583
static int
584 75128
vsm_vlu_minus(struct vsm *vd, struct vsm_set *vs, const char *line)
585
{
586
        char **av;
587
        int ac;
588
        struct vsm_seg *vg;
589
590 75128
        av = VAV_Parse(line + 1, &ac, 0);
591
592 75128
        if (av[0] != NULL || ac < 4 || ac > 6) {
593 0
                (void)(vsm_diag(vd, "vsm_vlu_minus: bad index (%d/%s)",
594 0
                    ac, av[0]));
595 0
                VAV_Free(av);
596 0
                return (-1);
597
        }
598
599
        /* Clustered segments cannot come before their cluster */
600 75128
        if (*av[2] != '0')
601 2135
                vg = vsm_findcluster(vs, av[1]);
602
        else
603 72993
                vg = VTAILQ_FIRST(&vs->segs);
604
605 643645
        for (;vg != NULL; vg = VTAILQ_NEXT(vg, list)) {
606 643645
                if (!vsm_cmp_av(&vg->av[1], &av[1])) {
607 75128
                        vs->retval |= vs->flag_changed;
608 75128
                        vsm_delseg(vg, 1);
609 75128
                        break;
610
                }
611 568517
        }
612 75128
        AN(vg);
613 75128
        VAV_Free(av);
614 75128
        return (0);
615 75128
}
616
617
static int v_matchproto_(vlu_f)
618 3040260
vsm_vlu_func(void *priv, const char *line)
619
{
620
        struct vsm *vd;
621
        struct vsm_set *vs;
622 3040260
        int i = 0;
623
624 3040260
        CAST_OBJ_NOTNULL(vs, priv, VSM_SET_MAGIC);
625 3040260
        vd = vs->vsm;
626 3040260
        CHECK_OBJ_NOTNULL(vd, VSM_MAGIC);
627 3040260
        AN(line);
628
629
        /* Up the serial counter. This wraps at UINTPTR_MAX/2
630
         * because thats the highest value we can store in struct
631
         * vsm_fantom. */
632 3040260
        vd->serial = VSM_PRIV_LOW(vd->serial + 1);
633
634 3040260
        switch (line[0]) {
635
        case '#':
636 146236
                i = vsm_vlu_hash(vs, line);
637 231130
                VTAILQ_FOREACH(vs->vg, &vs->segs, list)
638 84894
                        vs->vg->flags &= ~VSM_FLAG_MARKSCAN;
639 146236
                if (!(vs->retval & vs->flag_restarted))
640 76
                        vs->vg = VTAILQ_FIRST(&vs->segs);
641 146236
                break;
642
        case '+':
643 2818896
                i = vsm_vlu_plus(vd, vs, line);
644 2818896
                break;
645
        case '-':
646 75128
                i = vsm_vlu_minus(vd, vs, line);
647 75128
                break;
648
        default:
649 0
                break;
650
        }
651 3040260
        return (i);
652
}
653
654
static void
655 2246422
vsm_readlines(struct vsm_set *vs)
656
{
657
        int i;
658
659 2246422
        do {
660 2557437
                assert(vs->fd >= 0);
661 2557437
                i = VLU_Fd(vs->vlu, vs->fd);
662 2557437
        } while (!i);
663 2246422
        assert(i == -2);
664 2246422
}
665
666
static unsigned
667 2550214
vsm_refresh_set(struct vsm *vd, struct vsm_set *vs)
668
{
669 2550214
        unsigned restarted = 0;
670
        struct stat st;
671
672 2550214
        CHECK_OBJ_NOTNULL(vd, VSM_MAGIC);
673 2550214
        CHECK_OBJ_NOTNULL(vs, VSM_SET_MAGIC);
674 2550214
        vs->retval = 0;
675 4721342
        if (vs->dfd >= 0 && (
676 2173230
            fstatat(vd->wdfd, vs->dname, &st, AT_SYMLINK_NOFOLLOW) ||
677 2173260
            st.st_ino != vs->dst.st_ino ||
678 2171129
            st.st_dev != vs->dst.st_dev ||
679 2171128
            st.st_mode != vs->dst.st_mode ||
680 2171128
            st.st_nlink == 0)) {
681 2210
                closefd(&vs->dfd);
682 2120
                restarted = vs->flag_restarted;
683 2120
        }
684
685 2550186
        if (vs->dfd < 0) {
686 379042
                if (vs->fd >= 0)
687 2113
                        closefd(&vs->fd);
688 379042
                vs->dfd = openat(vd->wdfd, vs->dname, O_RDONLY);
689 379042
        }
690
691 2550186
        if (vs->dfd < 0) {
692 232882
                vs->id1 = vs->id2 = 0;
693 232882
                vsm_wash_set(vs, 1);
694 232882
                return (vs->retval | restarted);
695
        }
696
697 2317304
        AZ(fstat(vs->dfd, &vs->dst));
698
699 4417505
        if (vs->fd >= 0 && (
700 2136755
            fstatat(vs->dfd, "_.index", &st, AT_SYMLINK_NOFOLLOW) ||
701 2100283
            st.st_ino != vs->fst.st_ino ||
702 2100207
            st.st_dev != vs->fst.st_dev ||
703 2100205
            st.st_mode != vs->fst.st_mode ||
704 2100201
            st.st_size < vs->fst.st_size ||
705 2100201
            st.st_nlink < 1)) {
706 36612
                closefd(&vs->fd);
707 36612
                vs->retval |= vs->flag_changed;
708 36612
        }
709
710 2317306
        if (vs->fd >= 0) {
711 2100201
                vs->vg = NULL;
712 2100201
                vsm_readlines(vs);
713 2100201
        } else {
714 652250
                VTAILQ_FOREACH(vs->vg, &vs->segs, list)
715 435145
                        vs->vg->flags &= ~VSM_FLAG_MARKSCAN;
716 217105
                vs->vg = VTAILQ_FIRST(&vs->segs);
717 217105
                vs->fd = openat(vs->dfd, "_.index", O_RDONLY);
718 217105
                if (vs->fd < 0)
719 70869
                        return (vs->retval | restarted);
720 146236
                VLU_Reset(vs->vlu);
721 146236
                AZ(fstat(vs->fd, &vs->fst));
722 146236
                vsm_readlines(vs);
723 146236
                vsm_wash_set(vs, 0);
724
        }
725
726 2246437
        vs->fst.st_size = lseek(vs->fd, 0L, SEEK_CUR);
727
728 2246437
        if (vs->couldkill < 0 || vsm_running(vs, vs->id1))
729 2163000
                vs->retval |= vs->flag_running;
730 2246441
        return (vs->retval);
731 2550192
}
732
733
/*--------------------------------------------------------------------*/
734
735
unsigned
736 1310563
VSM_Status(struct vsm *vd)
737
{
738 1310563
        unsigned retval = 0;
739
        struct stat st;
740
741 1310563
        CHECK_OBJ_NOTNULL(vd, VSM_MAGIC);
742
743
        /* See if the -n workdir changed */
744 1310563
        if (vd->wdfd >= 0) {
745 1221838
                AZ(fstat(vd->wdfd, &st));
746 2443667
                if (st.st_ino != vd->wdst.st_ino ||
747 1221826
                    st.st_dev != vd->wdst.st_dev ||
748 1221827
                    st.st_mode != vd->wdst.st_mode ||
749 1221829
                    st.st_nlink == 0) {
750 24
                        closefd(&vd->wdfd);
751 0
                        vsm_wash_set(vd->mgt, 1);
752 0
                        vsm_wash_set(vd->child, 1);
753 0
                }
754 1221820
        }
755
756
        /* Open workdir */
757 1310545
        if (vd->wdfd < 0) {
758 88728
                retval |= VSM_MGT_RESTARTED | VSM_MGT_CHANGED;
759 88728
                retval |= VSM_WRK_RESTARTED | VSM_WRK_CHANGED;
760 88728
                vd->wdfd = open(vd->wdname, O_RDONLY);
761 88728
                if (vd->wdfd < 0)
762 1368
                        (void)vsm_diag(vd,
763
                            "VSM_Status: Cannot open workdir");
764
                else
765 87360
                        AZ(fstat(vd->wdfd, &vd->wdst));
766 88728
        }
767
768 1310545
        if (vd->wdfd >= 0) {
769 1309191
                retval |= vsm_refresh_set(vd, vd->mgt);
770 1309191
                if (vd->mgt->couldkill > 0 && (retval & VSM_MGT_RESTARTED))
771 87360
                        vd->mgt->couldkill = 0;
772 1309191
                if (retval & VSM_MGT_RUNNING)
773 1240985
                        retval |= vsm_refresh_set(vd, vd->child);
774 1309191
                if (vd->child->couldkill > 0 && (retval & VSM_WRK_RESTARTED))
775 58686
                        vd->child->couldkill = 0;
776 1309191
        }
777 1310545
        return (retval);
778
}
779
780
/*--------------------------------------------------------------------*/
781
782
int
783 87520
VSM_Attach(struct vsm *vd, int progress)
784
{
785
        const char *def;
786
        double t0;
787
        unsigned u;
788 87520
        int i, n = 0;
789
790 87520
        CHECK_OBJ_NOTNULL(vd, VSM_MAGIC);
791
792 87520
        if (vd->patience < 0)
793 0
                t0 = DBL_MAX;
794
        else
795 87520
                t0 = VTIM_mono() + vd->patience;
796
797 87520
        if (vd->wdname == NULL) {
798 280
                def = getenv("VARNISH_DEFAULT_N");
799 280
                if (def == NULL)
800 0
                        def = ""; /* Use default (hostname) */
801 280
                i = VSM_Arg(vd, 'n', def);
802 280
                if (i < 0)
803 0
                        return (i);
804 280
                AN(vd->wdname);
805 280
        }
806
807 87520
        AZ(vd->attached);
808 88848
        while (!VSIG_int && !VSIG_term) {
809 88808
                u = VSM_Status(vd);
810 88808
                VSM_ResetError(vd);
811 88808
                if (u & VSM_MGT_RUNNING) {
812 87360
                        if (progress >= 0 && n > 4)
813 0
                                (void)write(progress, "\n", 1);
814 87360
                        vd->attached = 1;
815 87360
                        return (0);
816
                }
817 1448
                if (t0 < VTIM_mono()) {
818 120
                        if (progress >= 0 && n > 4)
819 40
                                (void)write(progress, "\n", 1);
820 120
                        return (vsm_diag(vd,
821
                            "Could not get hold of varnishd, is it running?"));
822
                }
823 1328
                if (progress >= 0 && !(++n % 4))
824 284
                        (void)write(progress, ".", 1);
825 1328
                VTIM_sleep(.25);
826
        }
827 40
        return (vsm_diag(vd, "Attach interrupted"));
828 87520
}
829
830
/*--------------------------------------------------------------------*/
831
832
static struct vsm_seg *
833 225068
vsm_set_findseg(const struct vsm_set *vs, uintptr_t serial)
834
{
835
        struct vsm_seg *vg;
836
837 2507303
        VTAILQ_FOREACH(vg, &vs->segs, list) {
838 2375274
                if (vg->serial == serial)
839 93039
                        return (vg);
840 2282235
        }
841 234975
        VTAILQ_FOREACH(vg, &vs->stale, list) {
842 107761
                if (vg->serial == serial)
843 4815
                        return (vg);
844 102946
        }
845 127214
        return (NULL);
846 225068
}
847
848
static struct vsm_seg *
849 7042911
vsm_findseg(const struct vsm *vd, const struct vsm_fantom *vf)
850
{
851
        struct vsm_seg *vg;
852
        uint64_t x;
853
854 7042911
        x = VSM_PRIV_HIGH(vf->priv);
855 7042911
        if (x == vd->serial) {
856 6928882
                vg = (struct vsm_seg *)vf->priv2;
857 6928882
                if (!VALID_OBJ(vg, VSM_SEG_MAGIC) ||
858 6928882
                    vg->serial != VSM_PRIV_LOW(vf->priv))
859 0
                        WRONG("Corrupt fantom");
860 6928882
                return (vg);
861
        }
862
863 114029
        x = VSM_PRIV_LOW(vf->priv);
864 114029
        vg = vsm_set_findseg(vd->mgt, x);
865 114029
        if (vg == NULL)
866 111039
                vg = vsm_set_findseg(vd->child, x);
867 114029
        if (vg == NULL)
868 16175
                return (NULL);
869
870
        /* Update the fantom with the new priv so that lookups will be
871
         * fast on the next call. Note that this casts away the const. */
872 97854
        ((struct vsm_fantom *)TRUST_ME(vf))->priv =
873 97854
            VSM_PRIV_MERGE(vg->serial, vd->serial);
874 97854
        return (vg);
875 7042911
}
876
877
/*--------------------------------------------------------------------*/
878
879
void
880 321435
VSM__iter0(const struct vsm *vd, struct vsm_fantom *vf)
881
{
882
883 321435
        CHECK_OBJ_NOTNULL(vd, VSM_MAGIC);
884 321435
        AN(vf);
885
886 321435
        AN(vd->attached);
887 321435
        memset(vf, 0, sizeof *vf);
888 321435
}
889
890
int
891 4132008
VSM__itern(struct vsm *vd, struct vsm_fantom *vf)
892
{
893
        struct vsm_seg *vg;
894
895 4132008
        CHECK_OBJ_NOTNULL(vd, VSM_MAGIC);
896 4132008
        AN(vd->attached);
897 4132008
        AN(vf);
898
899 4132008
        if (vf->priv == 0) {
900 321437
                vg = VTAILQ_FIRST(&vd->mgt->segs);
901 321437
                if (vg == NULL)
902 3
                        return (0);
903 321434
        } else {
904 3810571
                vg = vsm_findseg(vd, vf);
905 3810571
                if (vg == NULL)
906 0
                        return (vsm_diag(vd, "VSM_FOREACH: inconsistency"));
907 3872511
                while (1) {
908 3872511
                        if (vg->set == vd->mgt && VTAILQ_NEXT(vg, list) == NULL)
909 317708
                                vg = VTAILQ_FIRST(&vd->child->segs);
910
                        else
911 3554803
                                vg = VTAILQ_NEXT(vg, list);
912 3872511
                        if (vg == NULL)
913 211145
                                return (0);
914 3661366
                        if (!(vg->flags & VSM_FLAG_CLUSTER))
915 3599426
                                break;
916
                }
917
        }
918 3920860
        memset(vf, 0, sizeof *vf);
919 3920860
        vf->priv = VSM_PRIV_MERGE(vg->serial, vd->serial);
920 3920860
        vf->priv2 = (uintptr_t)vg;
921 3920860
        vf->category = vg->av[4];
922 3920860
        vf->ident = vg->av[5];
923 3920860
        AN(vf->category);
924 3920860
        return (1);
925 4132008
}
926
927
/*--------------------------------------------------------------------*/
928
929
int
930 238161
VSM_Map(struct vsm *vd, struct vsm_fantom *vf)
931
{
932
        struct vsm_seg *vg, *vgc;
933
        size_t of, sz;
934
        int r;
935
936 238161
        CHECK_OBJ_NOTNULL(vd, VSM_MAGIC);
937 238161
        AN(vd->attached);
938 238161
        AN(vf);
939 238161
        vg = vsm_findseg(vd, vf);
940 238161
        if (vg == NULL)
941 0
                return (vsm_diag(vd, "VSM_Map: bad fantom"));
942
943 238161
        assert(vg->serial == VSM_PRIV_LOW(vf->priv));
944 238161
        assert(vg->av[4] == vf->category);
945 238161
        assert(vg->av[5] == vf->ident);
946
947 238161
        if (vg->b != NULL) {
948 0
                assert(vg->refs > 0);
949 0
                AN(vg->e);
950 0
                vf->b = vg->b;
951 0
                vf->e = vg->e;
952 0
                vg->refs++;
953 0
                return (0);
954
        }
955
956 238161
        assert(vg->refs == 0);
957
958 238161
        vgc = vg->cluster;
959
960 238161
        if (vgc == NULL) {
961 232482
                r = vsm_mapseg(vd, vg);
962 232482
                if (r)
963 16
                        return (r);
964 232466
                vf->b = vg->b;
965 232466
                vf->e = vg->e;
966
967 232466
                vg->refs++;
968
969 232466
                return (0);
970
        }
971
972 5679
        CHECK_OBJ_NOTNULL(vgc, VSM_SEG_MAGIC);
973 5679
        assert(vgc->flags & VSM_FLAG_CLUSTER);
974 5679
        assert(vg->s == NULL);
975 5679
        assert(vg->sz == 0);
976
977 5679
        r = vsm_mapseg(vd, vgc);
978 5679
        if (r)
979 0
                return (r);
980 5679
        vgc->refs++;
981
982 5679
        of = strtoul(vg->av[2], NULL, 10);
983 5679
        sz = strtoul(vg->av[3], NULL, 10);
984 5679
        assert(sz > 0);
985
986 5679
        assert(vgc->sz >= of + sz);
987 5679
        assert(vgc->s == vgc->b);
988 5679
        vg->b = (char *)vgc->b + of;
989 5679
        vg->e = (char *)vg->b + sz;
990
991 5679
        vf->b = vg->b;
992 5679
        vf->e = vg->e;
993
994 5679
        vg->refs++;
995
996 5679
        return (0);
997 238161
}
998
999
/*--------------------------------------------------------------------*/
1000
1001
int
1002 209864
VSM_Unmap(struct vsm *vd, struct vsm_fantom *vf)
1003
{
1004
        struct vsm_seg *vg;
1005
1006 209864
        CHECK_OBJ_NOTNULL(vd, VSM_MAGIC);
1007 209864
        AN(vd->attached);
1008 209864
        AN(vf);
1009 209864
        AN(vf->b);
1010 209864
        vg = vsm_findseg(vd, vf);
1011 209864
        if (vg == NULL)
1012 0
                return (vsm_diag(vd, "VSM_Unmap: bad fantom"));
1013 209864
        CHECK_OBJ_NOTNULL(vg, VSM_SEG_MAGIC);
1014 209864
        assert(vg->refs > 0);
1015 209864
        vg->refs--;
1016 209864
        vf->b = NULL;
1017 209864
        vf->e = NULL;
1018 209864
        if (vg->refs > 0)
1019 0
                return (0);
1020
1021 209864
        if (vg->cluster) {
1022 1079
                CHECK_OBJ_NOTNULL(vg->cluster, VSM_SEG_MAGIC);
1023 1079
                assert(vg->s == NULL);
1024 1079
                assert(vg->sz == 0);
1025 1079
                assert(vg->cluster->refs > 0);
1026 1079
                if (--vg->cluster->refs == 0) {
1027 1039
                        vsm_unmapseg(vg->cluster);
1028 1039
                        if (vg->cluster->flags & VSM_FLAG_STALE) {
1029 80
                                AN(vg->flags & VSM_FLAG_STALE);
1030 80
                                vsm_delseg(vg->cluster, 0);
1031 80
                        }
1032 1039
                }
1033 1079
                vg->b = vg->e = NULL;
1034 1079
        } else {
1035 208785
                vsm_unmapseg(vg);
1036
        }
1037 209864
        if (vg->flags & VSM_FLAG_STALE)
1038 4815
                vsm_delseg(vg, 0);
1039 209864
        return (0);
1040 209864
}
1041
1042
/*--------------------------------------------------------------------*/
1043
1044
const struct vsm_valid *
1045 2784742
VSM_StillValid(const struct vsm *vd, const struct vsm_fantom *vf)
1046
{
1047
        struct vsm_seg *vg;
1048
1049 2784742
        CHECK_OBJ_NOTNULL(vd, VSM_MAGIC);
1050 2784742
        AN(vf);
1051 2784742
        vg = vsm_findseg(vd, vf);
1052 2784742
        if (vg == NULL || vg->flags & VSM_FLAG_STALE)
1053 21690
                return (VSM_invalid);
1054 2763052
        return (VSM_valid);
1055 2784742
}
1056
1057
/*--------------------------------------------------------------------*/
1058
1059
int
1060 269752
VSM_Get(struct vsm *vd, struct vsm_fantom *vf,
1061
    const char *category, const char *ident)
1062
{
1063
1064 269752
        CHECK_OBJ_NOTNULL(vd, VSM_MAGIC);
1065 269752
        AN(vd->attached);
1066 1939466
        VSM_FOREACH(vf, vd) {
1067 1776277
                if (strcmp(vf->category, category))
1068 1669714
                        continue;
1069 106563
                if (ident != NULL && strcmp(vf->ident, ident))
1070 0
                        continue;
1071 106563
                return (1);
1072
        }
1073 163189
        memset(vf, 0, sizeof *vf);
1074 163189
        return (0);
1075 269752
}
1076
1077
/*--------------------------------------------------------------------*/
1078
1079
char *
1080 3720
VSM_Dup(struct vsm *vd, const char *category, const char *ident)
1081
{
1082
        struct vsm_fantom vf;
1083 3720
        char *p = NULL;
1084
1085 3720
        CHECK_OBJ_NOTNULL(vd, VSM_MAGIC);
1086 3720
        AN(vd->attached);
1087 14920
        VSM_FOREACH(&vf, vd) {
1088 14920
                if (strcmp(vf.category, category))
1089 6400
                        continue;
1090 8520
                if (ident != NULL && strcmp(vf.ident, ident))
1091 4800
                        continue;
1092 3720
                AZ(VSM_Map(vd, &vf));
1093 3720
                AN(vf.b);
1094 3720
                AN(vf.e);
1095 3720
                p = malloc((char*)vf.e - (char*)vf.b);
1096 3720
                AN(p);
1097 3720
                memcpy(p, vf.b, (char*)vf.e - (char*)vf.b);
1098 3720
                AZ(VSM_Unmap(vd, &vf));
1099 3720
                break;
1100
        }
1101 3720
        return (p);
1102
}