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 1820
vsm_diag(struct vsm *vd, const char *fmt, ...)
161
{
162
        va_list ap;
163
164 1820
        CHECK_OBJ_NOTNULL(vd, VSM_MAGIC);
165 1820
        AN(fmt);
166
167 1820
        if (vd->diag == NULL)
168 1820
                vd->diag = VSB_new_auto();
169 1820
        AN(vd->diag);
170 1820
        VSB_clear(vd->diag);
171 1820
        va_start(ap, fmt);
172 1820
        VSB_vprintf(vd->diag, fmt, ap);
173 1820
        va_end(ap);
174 1820
        AZ(VSB_finish(vd->diag));
175 1820
        return (-1);
176
}
177
178
/*--------------------------------------------------------------------*/
179
180
static int
181 250689
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 250689
        CHECK_OBJ_NOTNULL(vg, VSM_SEG_MAGIC);
189
190 250689
        if (vg->s != NULL)
191 4040
                return (0);
192
193 246649
        ps = getpagesize();
194
195 246649
        of = strtoul(vg->av[2], NULL, 10);
196 246649
        off = RDN2(of, ps);
197
198 246649
        if (vg->flags & VSM_FLAG_CLUSTER)
199 1709
                assert(of == 0);
200 246649
        assert(vg->cluster == NULL);
201
202 246649
        sz = strtoul(vg->av[3], NULL, 10);
203 246649
        assert(sz > 0);
204 246649
        assert(of >= off);
205 246649
        len = RUP2((of - off) + sz, ps);
206
207 246649
        vsb = VSB_new_auto();
208 246649
        AN(vsb);
209 246649
        VSB_printf(vsb, "%s/%s/%s", vd->wdname, vg->set->dname, vg->av[1]);
210 246649
        AZ(VSB_finish(vsb));
211
212 246649
        fd = open(VSB_data(vsb), O_RDONLY);     // XXX: openat
213 246649
        if (fd < 0) {
214 15
                VSB_destroy(&vsb);
215 15
                return (vsm_diag(vd, "Could not open segment"));
216
        }
217
218 493268
        s = (void*)mmap(NULL, len,
219
            PROT_READ,
220
            MAP_HASSEMAPHORE | MAP_NOSYNC | MAP_SHARED,
221 246634
            fd, (off_t)off);
222
223 246634
        VSB_destroy(&vsb);
224
225 246634
        closefd(&fd);
226 246634
        if (s == MAP_FAILED)
227 0
                return (vsm_diag(vd, "Could not mmap segment"));
228
229 246634
        vg->s = s;
230 246634
        vg->b = (char*)(vg->s) + of - off;
231 246634
        vg->e = (char *)vg->b + sz;
232 246634
        vg->sz = len;
233
234 246634
        return (0);
235 250689
}
236
237
static void
238 220394
vsm_unmapseg(struct vsm_seg *vg)
239
{
240
241 220394
        CHECK_OBJ_NOTNULL(vg, VSM_SEG_MAGIC);
242
243 220394
        AN(vg->b);
244 220394
        AN(vg->e);
245 220394
        AZ(munmap(vg->s, vg->sz));
246 220394
        vg->s = vg->b = vg->e = NULL;
247 220394
        vg->sz = 0;
248 220394
}
249
250
/*--------------------------------------------------------------------*/
251
252
static void
253 3003879
vsm_delseg(struct vsm_seg *vg, int refsok)
254
{
255
256 3003879
        CHECK_OBJ_NOTNULL(vg, VSM_SEG_MAGIC);
257
258 3003879
        if (vg->set->vg == vg) {
259 192255
                AZ(vg->flags & VSM_FLAG_STALE);
260 192255
                vg->set->vg = VTAILQ_NEXT(vg, list);
261 192255
        }
262
263 3003879
        if (refsok && vg->refs) {
264 5291
                AZ(vg->flags & VSM_FLAG_STALE);
265 5291
                vg->flags |= VSM_FLAG_STALE;
266 5291
                VTAILQ_REMOVE(&vg->set->segs, vg, list);
267 5291
                VTAILQ_INSERT_TAIL(&vg->set->stale, vg, list);
268 5291
                return;
269
        }
270
271 2998588
        if (vg->s != NULL)
272 0
                vsm_unmapseg(vg);
273
274 2998588
        if (vg->flags & VSM_FLAG_CLUSTER) {
275 75080
                vg->flags &= ~VSM_FLAG_CLUSTER;
276 75080
                VTAILQ_REMOVE(&vg->set->clusters, vg, clist);
277 75080
        }
278
279 2998588
        if (vg->flags & VSM_FLAG_STALE)
280 5291
                VTAILQ_REMOVE(&vg->set->stale, vg, list);
281
        else
282 2993297
                VTAILQ_REMOVE(&vg->set->segs, vg, list);
283 2998588
        VAV_Free(vg->av);
284 2998588
        FREE_OBJ(vg);
285 3003879
}
286
287
/*--------------------------------------------------------------------*/
288
289
static struct vsm_set *
290 177760
vsm_newset(const char *dirname)
291
{
292
        struct vsm_set *vs;
293
294 177760
        ALLOC_OBJ(vs, VSM_SET_MAGIC);
295 177760
        AN(vs);
296 177760
        VTAILQ_INIT(&vs->segs);
297 177760
        VTAILQ_INIT(&vs->stale);
298 177760
        VTAILQ_INIT(&vs->clusters);
299 177760
        vs->dname = dirname;
300 177760
        vs->dfd = vs->fd = -1;
301 177760
        vs->vlu = VLU_New(vsm_vlu_func, vs, 0);
302 177760
        AN(vs->vlu);
303 177760
        if (getenv("VSM_NOPID") != NULL)
304 0
                vs->couldkill = -1;
305 177760
        return (vs);
306
}
307
308
static void
309 175520
vsm_delset(struct vsm_set **p)
310
{
311
        struct vsm_set *vs;
312
        struct vsm_seg *vg;
313
314 175520
        TAKE_OBJ_NOTNULL(vs, p, VSM_SET_MAGIC);
315
316 175520
        if (vs->fd >= 0)
317 107506
                closefd(&vs->fd);
318 175520
        if (vs->dfd >= 0)
319 144400
                closefd(&vs->dfd);
320 175520
        while ((vg = VTAILQ_FIRST(&vs->stale)) != NULL) {
321 0
                AN(vg->flags & VSM_FLAG_STALE);
322 0
                vsm_delseg(vg, 0);
323
        }
324 3001530
        while ((vg = VTAILQ_FIRST(&vs->segs)) != NULL) {
325 2826010
                AZ(vg->flags & VSM_FLAG_STALE);
326 2826010
                vsm_delseg(vg, 0);
327
        }
328 175520
        assert(VTAILQ_EMPTY(&vs->clusters));
329 175520
        VLU_Destroy(&vs->vlu);
330 175520
        FREE_OBJ(vs);
331 175520
}
332
333
static void
334 398353
vsm_wash_set(const struct vsm_set *vs, int all)
335
{
336
        struct vsm_seg *vg, *vg2;
337
338 3332322
        VTAILQ_FOREACH_SAFE(vg, &vs->segs, list, vg2) {
339 2933969
                if (all || (vg->flags & VSM_FLAG_MARKSCAN) == 0)
340 96192
                        vsm_delseg(vg, 1);
341 2933969
        }
342 398353
}
343
344
/*--------------------------------------------------------------------*/
345
346
struct vsm *
347 88880
VSM_New(void)
348
{
349
        struct vsm *vd;
350
351 88880
        ALLOC_OBJ(vd, VSM_MAGIC);
352 88880
        AN(vd);
353
354 88880
        vd->mgt = vsm_newset(VSM_MGT_DIRNAME);
355 88880
        vd->mgt->flag_running = VSM_MGT_RUNNING;
356 88880
        vd->mgt->flag_changed = VSM_MGT_CHANGED;
357 88880
        vd->mgt->flag_restarted = VSM_MGT_RESTARTED;
358
359 88880
        vd->child = vsm_newset(VSM_CHILD_DIRNAME);
360 88880
        vd->child->flag_running = VSM_WRK_RUNNING;
361 88880
        vd->child->flag_changed = VSM_WRK_CHANGED;
362 88880
        vd->child->flag_restarted = VSM_WRK_RESTARTED;
363
364 88880
        vd->mgt->vsm = vd;
365 88880
        vd->child->vsm = vd;
366 88880
        vd->wdfd = -1;
367 88880
        vd->patience = 5;
368 88880
        return (vd);
369
}
370
371
/*--------------------------------------------------------------------*/
372
373
int
374 90640
VSM_Arg(struct vsm *vd, char flag, const char *arg)
375
{
376 90640
        char *p = NULL;
377
378 90640
        CHECK_OBJ_NOTNULL(vd, VSM_MAGIC);
379
380 90640
        if (arg == NULL)
381 1640
                return (1);
382 89000
        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 88480
                if (vd->wdname != NULL)
396 0
                        free(vd->wdname);
397 88480
                vd->wdname = VIN_n_Arg(arg);
398 88480
                if (vd->wdname == NULL)
399 0
                        return (vsm_diag(vd, "Invalid instance name: %s",
400 0
                            strerror(errno)));
401 88480
                break;
402
        default:
403 0
                return (vsm_diag(vd, "Unknown VSM_Arg('%c')", flag));
404
        }
405 88720
        return (1);
406 90640
}
407
408
/*--------------------------------------------------------------------*/
409
410
void
411 87760
VSM_Destroy(struct vsm **vdp)
412
{
413
        struct vsm *vd;
414
415 87760
        TAKE_OBJ_NOTNULL(vd, vdp, VSM_MAGIC);
416
417 87760
        VSM_ResetError(vd);
418 87760
        REPLACE(vd->wdname, NULL);
419 87760
        if (vd->diag != NULL)
420 0
                VSB_destroy(&vd->diag);
421 87760
        if (vd->wdfd >= 0)
422 87680
                closefd(&vd->wdfd);
423 87760
        vsm_delset(&vd->mgt);
424 87760
        vsm_delset(&vd->child);
425 87760
        FREE_OBJ(vd);
426 87760
}
427
428
/*--------------------------------------------------------------------*/
429
430
const char *
431 455
VSM_Error(const struct vsm *vd)
432
{
433
434 455
        CHECK_OBJ_NOTNULL(vd, VSM_MAGIC);
435
436 455
        if (vd->diag == NULL)
437 0
                return ("No VSM error");
438
        else
439 455
                return (VSB_data(vd->diag));
440 455
}
441
442
/*--------------------------------------------------------------------*/
443
444
void
445 177525
VSM_ResetError(struct vsm *vd)
446
{
447
448 177525
        CHECK_OBJ_NOTNULL(vd, VSM_MAGIC);
449
450 177525
        if (vd->diag == NULL)
451 176065
                return;
452 1460
        VSB_destroy(&vd->diag);
453 177525
}
454
455
/*--------------------------------------------------------------------
456
 */
457
458
static int
459 668371
vsm_cmp_av(char * const *a1, char * const *a2)
460
{
461
462 987343
        while (1) {
463 987343
                if (*a1 == NULL && *a2 == NULL)
464 78338
                        return (0);
465 909005
                if (*a1 == NULL || *a2 == NULL)
466 0
                        return (1);
467 909005
                if (strcmp(*a1, *a2))
468 590033
                        return (1);
469 318972
                a1++;
470 318972
                a2++;
471
        }
472 668371
}
473
474
static struct vsm_seg *
475 81510
vsm_findcluster(const struct vsm_set *vs, const char *cnam)
476
{
477
        struct vsm_seg *vg;
478 81510
        AN(vs);
479 81510
        AN(cnam);
480 114361
        VTAILQ_FOREACH(vg, &vs->clusters, clist) {
481 114361
                AN(vg->av[1]);
482 114361
                if (!strcmp(cnam, vg->av[1]))
483 81510
                        return (vg);
484 32851
        }
485 0
        return (NULL);
486 81510
}
487
488
static unsigned
489 2454594
vsm_running(struct vsm_set *vs, pid_t pid)
490
{
491
492 2454594
        AN(vs);
493
494 2454594
        if (pid == 0)
495 0
                return (0);
496
497 2454594
        if (kill(pid, 0) == 0) {
498 2365763
                vs->couldkill = 1;
499 2365763
                return (1);
500
        }
501 88831
        if (errno == EPERM)     /* a process exists, assume running */
502 0
                return (1);
503 88831
        assert(errno != EINVAL);
504 88831
        return (0);
505 2454594
}
506
507
static int
508 148032
vsm_vlu_hash(struct vsm_set *vs, const char *line)
509
{
510
        int i;
511
        uintmax_t id1, id2;
512
513 148032
        i = sscanf(line, "# %ju %ju", &id1, &id2);
514 148032
        if (i != 2) {
515 0
                vs->retval |= vs->flag_restarted;
516 0
                return (0);
517
        }
518 148032
        if (vs->couldkill >= 0 && vsm_running(vs, id1)) {
519
                /* nothing to do */
520 148032
        } else if (vs->couldkill > 0 && errno == ESRCH) {
521 0
                vs->retval |= vs->flag_restarted | VSM_MGT_CHANGED;
522 0
                return (0);
523
        }
524 148032
        vs->retval |= VSM_MGT_RUNNING;
525 148032
        if (id1 != vs->id1 || id2 != vs->id2) {
526 147960
                vs->retval |= vs->flag_restarted;
527 147960
                vs->id1 = id1;
528 147960
                vs->id2 = id2;
529 147960
        }
530 148032
        return (0);
531 148032
}
532
533
static int
534 3035220
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 3035220
        av = VAV_Parse(line + 1, &ac, 0);
541
542 3035220
        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 3035220
        vg = vs->vg;
550 3035220
        CHECK_OBJ_ORNULL(vg, VSM_SEG_MAGIC);
551 3035220
        if (vg != NULL)
552 1952
                AZ(vg->flags & VSM_FLAG_STALE);
553 3035940
        while (vg != NULL && vsm_cmp_av(&vg->av[1], &av[1]))
554 720
                vg = VTAILQ_NEXT(vg, list);
555 3035220
        if (vg != NULL) {
556
                /* entry compared equal, so it survives */
557 1952
                CHECK_OBJ_NOTNULL(vg, VSM_SEG_MAGIC);
558 1952
                VAV_Free(av);
559 1952
                vg->flags |= VSM_FLAG_MARKSCAN;
560 1952
                vs->vg = VTAILQ_NEXT(vg, list);
561 1952
        } else {
562 3033268
                ALLOC_OBJ(vg, VSM_SEG_MAGIC);
563 3033268
                AN(vg);
564 3033268
                vg->av = av;
565 3033268
                vg->set = vs;
566 3033268
                vg->flags = VSM_FLAG_MARKSCAN;
567 3033268
                vg->serial = vd->serial;
568
569 3033268
                VTAILQ_INSERT_TAIL(&vs->segs, vg, list);
570 3033268
                if (ac == 4) {
571 75720
                        vg->flags |= VSM_FLAG_CLUSTER;
572 75720
                        VTAILQ_INSERT_TAIL(&vs->clusters, vg, clist);
573 3033268
                } else if (*vg->av[2] != '0') {
574 79360
                        vg->cluster = vsm_findcluster(vs, vg->av[1]);
575 79360
                        CHECK_OBJ_NOTNULL(vg->cluster, VSM_SEG_MAGIC);
576 79360
                }
577 3033268
                vs->retval |= vs->flag_changed;
578
        }
579 3035220
        return (0);
580 3035220
}
581
582
static int
583 76386
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 76386
        av = VAV_Parse(line + 1, &ac, 0);
590
591 76386
        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 76386
        if (*av[2] != '0')
600 2150
                vg = vsm_findcluster(vs, av[1]);
601
        else
602 74236
                vg = VTAILQ_FIRST(&vs->segs);
603
604 665699
        for (;vg != NULL; vg = VTAILQ_NEXT(vg, list)) {
605 665699
                if (!vsm_cmp_av(&vg->av[1], &av[1])) {
606 76386
                        vs->retval |= vs->flag_changed;
607 76386
                        vsm_delseg(vg, 1);
608 76386
                        break;
609
                }
610 589313
        }
611 76386
        AN(vg);
612 76386
        VAV_Free(av);
613 76386
        return (0);
614 76386
}
615
616
static int v_matchproto_(vlu_f)
617 3259638
vsm_vlu_func(void *priv, const char *line)
618
{
619
        struct vsm *vd;
620
        struct vsm_set *vs;
621 3259638
        int i = 0;
622
623 3259638
        CAST_OBJ_NOTNULL(vs, priv, VSM_SET_MAGIC);
624 3259638
        vd = vs->vsm;
625 3259638
        CHECK_OBJ_NOTNULL(vd, VSM_MAGIC);
626 3259638
        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 3259638
        vd->serial = VSM_PRIV_LOW(vd->serial + 1);
632
633 3259638
        switch (line[0]) {
634
        case '#':
635 148032
                i = vsm_vlu_hash(vs, line);
636 246055
                VTAILQ_FOREACH(vs->vg, &vs->segs, list)
637 98023
                        vs->vg->flags &= ~VSM_FLAG_MARKSCAN;
638 148032
                if (!(vs->retval & vs->flag_restarted))
639 72
                        vs->vg = VTAILQ_FIRST(&vs->segs);
640 148032
                break;
641
        case '+':
642 3035220
                i = vsm_vlu_plus(vd, vs, line);
643 3035220
                break;
644
        case '-':
645 76386
                i = vsm_vlu_minus(vd, vs, line);
646 76386
                break;
647
        default:
648 0
                break;
649
        }
650 3259638
        return (i);
651
}
652
653
static void
654 2306567
vsm_readlines(struct vsm_set *vs)
655
{
656
        int i;
657
658 2306567
        do {
659 2620922
                assert(vs->fd >= 0);
660 2620922
                i = VLU_Fd(vs->vlu, vs->fd);
661 2620922
        } while (!i);
662 2306567
        assert(i == -2);
663 2306567
}
664
665
static unsigned
666 2628032
vsm_refresh_set(struct vsm *vd, struct vsm_set *vs)
667
{
668 2628032
        unsigned restarted = 0;
669
        struct stat st;
670
671 2628032
        CHECK_OBJ_NOTNULL(vd, VSM_MAGIC);
672 2628032
        CHECK_OBJ_NOTNULL(vs, VSM_SET_MAGIC);
673 2628032
        vs->retval = 0;
674 4857782
        if (vs->dfd >= 0 && (
675 2231985
            fstatat(vd->wdfd, vs->dname, &st, AT_SYMLINK_NOFOLLOW) ||
676 2232023
            st.st_ino != vs->dst.st_ino ||
677 2229748
            st.st_dev != vs->dst.st_dev ||
678 2229749
            st.st_mode != vs->dst.st_mode ||
679 2229750
            st.st_nlink == 0)) {
680 2362
                closefd(&vs->dfd);
681 2280
                restarted = vs->flag_restarted;
682 2280
        }
683
684 2628034
        if (vs->dfd < 0) {
685 398281
                if (vs->fd >= 0)
686 2279
                        closefd(&vs->fd);
687 398281
                vs->dfd = openat(vd->wdfd, vs->dname, O_RDONLY);
688 398281
        }
689
690 2628034
        if (vs->dfd < 0) {
691 250321
                vs->id1 = vs->id2 = 0;
692 250321
                vsm_wash_set(vs, 1);
693 250321
                return (vs->retval | restarted);
694
        }
695
696 2377713
        AZ(fstat(vs->dfd, &vs->dst));
697
698 4536262
        if (vs->fd >= 0 && (
699 2195494
            fstatat(vs->dfd, "_.index", &st, AT_SYMLINK_NOFOLLOW) ||
700 2158622
            st.st_ino != vs->fst.st_ino ||
701 2158549
            st.st_dev != vs->fst.st_dev ||
702 2158549
            st.st_mode != vs->fst.st_mode ||
703 2158550
            st.st_size < vs->fst.st_size ||
704 2158549
            st.st_nlink < 1)) {
705 36977
                closefd(&vs->fd);
706 36967
                vs->retval |= vs->flag_changed;
707 36967
        }
708
709 2377709
        if (vs->fd >= 0) {
710 2158544
                vs->vg = NULL;
711 2158544
                vsm_readlines(vs);
712 2158544
        } else {
713 670541
                VTAILQ_FOREACH(vs->vg, &vs->segs, list)
714 451376
                        vs->vg->flags &= ~VSM_FLAG_MARKSCAN;
715 219165
                vs->vg = VTAILQ_FIRST(&vs->segs);
716 219165
                vs->fd = openat(vs->dfd, "_.index", O_RDONLY);
717 219165
                if (vs->fd < 0)
718 71133
                        return (vs->retval | restarted);
719 148032
                VLU_Reset(vs->vlu);
720 148032
                AZ(fstat(vs->fd, &vs->fst));
721 148032
                vsm_readlines(vs);
722 148032
                vsm_wash_set(vs, 0);
723
        }
724
725 2306576
        vs->fst.st_size = lseek(vs->fd, 0L, SEEK_CUR);
726
727 2306576
        if (vs->couldkill < 0 || vsm_running(vs, vs->id1))
728 2217872
                vs->retval |= vs->flag_running;
729 2306582
        return (vs->retval);
730 2628036
}
731
732
/*--------------------------------------------------------------------*/
733
734
unsigned
735 1349791
VSM_Status(struct vsm *vd)
736
{
737 1349791
        unsigned retval = 0;
738
        struct stat st;
739
740 1349791
        CHECK_OBJ_NOTNULL(vd, VSM_MAGIC);
741
742
        /* See if the -n workdir changed */
743 1349791
        if (vd->wdfd >= 0) {
744 1260106
                AZ(fstat(vd->wdfd, &st));
745 2520202
                if (st.st_ino != vd->wdst.st_ino ||
746 1260096
                    st.st_dev != vd->wdst.st_dev ||
747 1260097
                    st.st_mode != vd->wdst.st_mode ||
748 1260096
                    st.st_nlink == 0) {
749 20
                        closefd(&vd->wdfd);
750 0
                        vsm_wash_set(vd->mgt, 1);
751 0
                        vsm_wash_set(vd->child, 1);
752 0
                }
753 1260094
        }
754
755
        /* Open workdir */
756 1349779
        if (vd->wdfd < 0) {
757 89685
                retval |= VSM_MGT_RESTARTED | VSM_MGT_CHANGED;
758 89685
                retval |= VSM_WRK_RESTARTED | VSM_WRK_CHANGED;
759 89685
                vd->wdfd = open(vd->wdname, O_RDONLY);
760 89685
                if (vd->wdfd < 0)
761 1365
                        (void)vsm_diag(vd,
762
                            "VSM_Status: Cannot open workdir");
763
                else
764 88320
                        AZ(fstat(vd->wdfd, &vd->wdst));
765 89685
        }
766
767 1349779
        if (vd->wdfd >= 0) {
768 1348451
                retval |= vsm_refresh_set(vd, vd->mgt);
769 1348451
                if (vd->mgt->couldkill > 0 && (retval & VSM_MGT_RESTARTED))
770 88320
                        vd->mgt->couldkill = 0;
771 1348451
                if (retval & VSM_MGT_RUNNING)
772 1279573
                        retval |= vsm_refresh_set(vd, vd->child);
773 1348451
                if (vd->child->couldkill > 0 && (retval & VSM_WRK_RESTARTED))
774 59533
                        vd->child->couldkill = 0;
775 1348451
        }
776 1349779
        return (retval);
777
}
778
779
/*--------------------------------------------------------------------*/
780
781
int
782 88480
VSM_Attach(struct vsm *vd, int progress)
783
{
784
        const char *def;
785
        double t0;
786
        unsigned u;
787 88480
        int i, n = 0;
788
789 88480
        CHECK_OBJ_NOTNULL(vd, VSM_MAGIC);
790
791 88480
        if (vd->patience < 0)
792 0
                t0 = DBL_MAX;
793
        else
794 88480
                t0 = VTIM_mono() + vd->patience;
795
796 88480
        if (vd->wdname == NULL) {
797 280
                def = getenv("VARNISH_DEFAULT_N");
798 280
                if (def == NULL)
799 0
                        def = ""; /* Use default (hostname) */
800 280
                i = VSM_Arg(vd, 'n', def);
801 280
                if (i < 0)
802 0
                        return (i);
803 280
                AN(vd->wdname);
804 280
        }
805
806 88480
        AZ(vd->attached);
807 89805
        while (!VSIG_int && !VSIG_term) {
808 89765
                u = VSM_Status(vd);
809 89765
                VSM_ResetError(vd);
810 89765
                if (u & VSM_MGT_RUNNING) {
811 88320
                        if (progress >= 0 && n > 4)
812 0
                                (void)write(progress, "\n", 1);
813 88320
                        vd->attached = 1;
814 88320
                        return (0);
815
                }
816 1445
                if (t0 < VTIM_mono()) {
817 120
                        if (progress >= 0 && n > 4)
818 40
                                (void)write(progress, "\n", 1);
819 120
                        return (vsm_diag(vd,
820
                            "Could not get hold of varnishd, is it running?"));
821
                }
822 1325
                if (progress >= 0 && !(++n % 4))
823 283
                        (void)write(progress, ".", 1);
824 1325
                VTIM_sleep(.25);
825
        }
826 40
        return (vsm_diag(vd, "Attach interrupted"));
827 88480
}
828
829
/*--------------------------------------------------------------------*/
830
831
static struct vsm_seg *
832 228787
vsm_set_findseg(const struct vsm_set *vs, uintptr_t serial)
833
{
834
        struct vsm_seg *vg;
835
836 2887025
        VTAILQ_FOREACH(vg, &vs->segs, list) {
837 2748369
                if (vg->serial == serial)
838 90131
                        return (vg);
839 2658238
        }
840 262776
        VTAILQ_FOREACH(vg, &vs->stale, list) {
841 129331
                if (vg->serial == serial)
842 5211
                        return (vg);
843 124120
        }
844 133445
        return (NULL);
845 228787
}
846
847
static struct vsm_seg *
848 7697201
vsm_findseg(const struct vsm *vd, const struct vsm_fantom *vf)
849
{
850
        struct vsm_seg *vg;
851
        uint64_t x;
852
853 7697201
        x = VSM_PRIV_HIGH(vf->priv);
854 7697201
        if (x == vd->serial) {
855 7581231
                vg = (struct vsm_seg *)vf->priv2;
856 7581231
                if (!VALID_OBJ(vg, VSM_SEG_MAGIC) ||
857 7581231
                    vg->serial != VSM_PRIV_LOW(vf->priv))
858 0
                        WRONG("Corrupt fantom");
859 7581231
                return (vg);
860
        }
861
862 115970
        x = VSM_PRIV_LOW(vf->priv);
863 115970
        vg = vsm_set_findseg(vd->mgt, x);
864 115970
        if (vg == NULL)
865 112817
                vg = vsm_set_findseg(vd->child, x);
866 115970
        if (vg == NULL)
867 20628
                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 95342
        ((struct vsm_fantom *)TRUST_ME(vf))->priv =
872 95342
            VSM_PRIV_MERGE(vg->serial, vd->serial);
873 95342
        return (vg);
874 7697201
}
875
876
/*--------------------------------------------------------------------*/
877
878
void
879 343670
VSM__iter0(const struct vsm *vd, struct vsm_fantom *vf)
880
{
881
882 343670
        CHECK_OBJ_NOTNULL(vd, VSM_MAGIC);
883 343670
        AN(vf);
884
885 343670
        AN(vd->attached);
886 343670
        memset(vf, 0, sizeof *vf);
887 343670
}
888
889
int
890 4531386
VSM__itern(struct vsm *vd, struct vsm_fantom *vf)
891
{
892
        struct vsm_seg *vg;
893
894 4531386
        CHECK_OBJ_NOTNULL(vd, VSM_MAGIC);
895 4531386
        AN(vd->attached);
896 4531386
        AN(vf);
897
898 4531386
        if (vf->priv == 0) {
899 343666
                vg = VTAILQ_FIRST(&vd->mgt->segs);
900 343666
                if (vg == NULL)
901 2
                        return (0);
902 343664
        } else {
903 4187720
                vg = vsm_findseg(vd, vf);
904 4187720
                if (vg == NULL)
905 0
                        return (vsm_diag(vd, "VSM_FOREACH: inconsistency"));
906 4252572
                while (1) {
907 4252572
                        if (vg->set == vd->mgt && VTAILQ_NEXT(vg, list) == NULL)
908 339867
                                vg = VTAILQ_FIRST(&vd->child->segs);
909
                        else
910 3912705
                                vg = VTAILQ_NEXT(vg, list);
911 4252572
                        if (vg == NULL)
912 229933
                                return (0);
913 4022639
                        if (!(vg->flags & VSM_FLAG_CLUSTER))
914 3957787
                                break;
915
                }
916
        }
917 4301451
        memset(vf, 0, sizeof *vf);
918 4301451
        vf->priv = VSM_PRIV_MERGE(vg->serial, vd->serial);
919 4301451
        vf->priv2 = (uintptr_t)vg;
920 4301451
        vf->category = vg->av[4];
921 4301451
        vf->ident = vg->av[5];
922 4301451
        AN(vf->category);
923 4301451
        return (1);
924 4531386
}
925
926
/*--------------------------------------------------------------------*/
927
928
int
929 250689
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 250689
        CHECK_OBJ_NOTNULL(vd, VSM_MAGIC);
936 250689
        AN(vd->attached);
937 250689
        AN(vf);
938 250689
        vg = vsm_findseg(vd, vf);
939 250689
        if (vg == NULL)
940 0
                return (vsm_diag(vd, "VSM_Map: bad fantom"));
941
942 250689
        assert(vg->serial == VSM_PRIV_LOW(vf->priv));
943 250689
        assert(vg->av[4] == vf->category);
944 250689
        assert(vg->av[5] == vf->ident);
945
946 250689
        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 250689
        assert(vg->refs == 0);
956
957 250689
        vgc = vg->cluster;
958
959 250689
        if (vgc == NULL) {
960 244940
                r = vsm_mapseg(vd, vg);
961 244940
                if (r)
962 15
                        return (r);
963 244925
                vf->b = vg->b;
964 244925
                vf->e = vg->e;
965
966 244925
                vg->refs++;
967
968 244925
                return (0);
969
        }
970
971 5749
        CHECK_OBJ_NOTNULL(vgc, VSM_SEG_MAGIC);
972 5749
        assert(vgc->flags & VSM_FLAG_CLUSTER);
973 5749
        assert(vg->s == NULL);
974 5749
        assert(vg->sz == 0);
975
976 5749
        r = vsm_mapseg(vd, vgc);
977 5749
        if (r)
978 0
                return (r);
979 5749
        vgc->refs++;
980
981 5749
        of = strtoul(vg->av[2], NULL, 10);
982 5749
        sz = strtoul(vg->av[3], NULL, 10);
983 5749
        assert(sz > 0);
984
985 5749
        assert(vgc->sz >= of + sz);
986 5749
        assert(vgc->s == vgc->b);
987 5749
        vg->b = (char *)vgc->b + of;
988 5749
        vg->e = (char *)vg->b + sz;
989
990 5749
        vf->b = vg->b;
991 5749
        vf->e = vg->e;
992
993 5749
        vg->refs++;
994
995 5749
        return (0);
996 250689
}
997
998
/*--------------------------------------------------------------------*/
999
1000
int
1001 220474
VSM_Unmap(struct vsm *vd, struct vsm_fantom *vf)
1002
{
1003
        struct vsm_seg *vg;
1004
1005 220474
        CHECK_OBJ_NOTNULL(vd, VSM_MAGIC);
1006 220474
        AN(vd->attached);
1007 220474
        AN(vf);
1008 220474
        AN(vf->b);
1009 220474
        vg = vsm_findseg(vd, vf);
1010 220474
        if (vg == NULL)
1011 0
                return (vsm_diag(vd, "VSM_Unmap: bad fantom"));
1012 220474
        CHECK_OBJ_NOTNULL(vg, VSM_SEG_MAGIC);
1013 220474
        assert(vg->refs > 0);
1014 220474
        vg->refs--;
1015 220474
        vf->b = NULL;
1016 220474
        vf->e = NULL;
1017 220474
        if (vg->refs > 0)
1018 0
                return (0);
1019
1020 220474
        if (vg->cluster) {
1021 1149
                CHECK_OBJ_NOTNULL(vg->cluster, VSM_SEG_MAGIC);
1022 1149
                assert(vg->s == NULL);
1023 1149
                assert(vg->sz == 0);
1024 1149
                assert(vg->cluster->refs > 0);
1025 1149
                if (--vg->cluster->refs == 0) {
1026 1069
                        vsm_unmapseg(vg->cluster);
1027 1069
                        if (vg->cluster->flags & VSM_FLAG_STALE) {
1028 80
                                AN(vg->flags & VSM_FLAG_STALE);
1029 80
                                vsm_delseg(vg->cluster, 0);
1030 80
                        }
1031 1069
                }
1032 1149
                vg->b = vg->e = NULL;
1033 1149
        } else {
1034 219325
                vsm_unmapseg(vg);
1035
        }
1036 220474
        if (vg->flags & VSM_FLAG_STALE)
1037 5211
                vsm_delseg(vg, 0);
1038 220474
        return (0);
1039 220474
}
1040
1041
/*--------------------------------------------------------------------*/
1042
1043
const struct vsm_valid *
1044 3038606
VSM_StillValid(const struct vsm *vd, const struct vsm_fantom *vf)
1045
{
1046
        struct vsm_seg *vg;
1047
1048 3038606
        CHECK_OBJ_NOTNULL(vd, VSM_MAGIC);
1049 3038606
        AN(vf);
1050 3038606
        vg = vsm_findseg(vd, vf);
1051 3038606
        if (vg == NULL || vg->flags & VSM_FLAG_STALE)
1052 26367
                return (VSM_invalid);
1053 3012239
        return (VSM_valid);
1054 3038606
}
1055
1056
/*--------------------------------------------------------------------*/
1057
1058
int
1059 289102
VSM_Get(struct vsm *vd, struct vsm_fantom *vf,
1060
    const char *category, const char *ident)
1061
{
1062
1063 289102
        CHECK_OBJ_NOTNULL(vd, VSM_MAGIC);
1064 289102
        AN(vd->attached);
1065 2061996
        VSM_FOREACH(vf, vd) {
1066 1882829
                if (strcmp(vf->category, category))
1067 1772894
                        continue;
1068 109935
                if (ident != NULL && strcmp(vf->ident, ident))
1069 0
                        continue;
1070 109935
                return (1);
1071
        }
1072 179167
        memset(vf, 0, sizeof *vf);
1073 179167
        return (0);
1074 289102
}
1075
1076
/*--------------------------------------------------------------------*/
1077
1078
char *
1079 3800
VSM_Dup(struct vsm *vd, const char *category, const char *ident)
1080
{
1081
        struct vsm_fantom vf;
1082 3800
        char *p = NULL;
1083
1084 3800
        CHECK_OBJ_NOTNULL(vd, VSM_MAGIC);
1085 3800
        AN(vd->attached);
1086 15280
        VSM_FOREACH(&vf, vd) {
1087 15280
                if (strcmp(vf.category, category))
1088 6560
                        continue;
1089 8720
                if (ident != NULL && strcmp(vf.ident, ident))
1090 4920
                        continue;
1091 3800
                AZ(VSM_Map(vd, &vf));
1092 3800
                AN(vf.b);
1093 3800
                AN(vf.e);
1094 3800
                p = malloc((char*)vf.e - (char*)vf.b);
1095 3800
                AN(p);
1096 3800
                memcpy(p, vf.b, (char*)vf.e - (char*)vf.b);
1097 3800
                AZ(VSM_Unmap(vd, &vf));
1098 3800
                break;
1099
        }
1100 3800
        return (p);
1101
}