|  | 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 | 
  1829 | 
  vsm_diag(struct vsm *vd, const char *fmt, ...)  | 
| 161 | 
   | 
  { | 
| 162 | 
   | 
          va_list ap;  | 
| 163 | 
   | 
   | 
| 164 | 
  1829 | 
          CHECK_OBJ_NOTNULL(vd, VSM_MAGIC);  | 
| 165 | 
  1829 | 
          AN(fmt);  | 
| 166 | 
   | 
   | 
| 167 | 
  1829 | 
          if (vd->diag == NULL)  | 
| 168 | 
  1829 | 
                  vd->diag = VSB_new_auto();  | 
| 169 | 
  1829 | 
          AN(vd->diag);  | 
| 170 | 
  1829 | 
          VSB_clear(vd->diag);  | 
| 171 | 
  1829 | 
          va_start(ap, fmt);  | 
| 172 | 
  1829 | 
          VSB_vprintf(vd->diag, fmt, ap);  | 
| 173 | 
  1829 | 
          va_end(ap);  | 
| 174 | 
  1829 | 
          AZ(VSB_finish(vd->diag));  | 
| 175 | 
  1829 | 
          return (-1);  | 
| 176 | 
   | 
  }  | 
| 177 | 
   | 
   | 
| 178 | 
   | 
  /*--------------------------------------------------------------------*/  | 
| 179 | 
   | 
   | 
| 180 | 
   | 
  static int  | 
| 181 | 
  266051 | 
  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 | 
  266051 | 
          CHECK_OBJ_NOTNULL(vg, VSM_SEG_MAGIC);  | 
| 189 | 
   | 
   | 
| 190 | 
  266051 | 
          if (vg->s != NULL)  | 
| 191 | 
  4040 | 
                  return (0);  | 
| 192 | 
   | 
   | 
| 193 | 
  262011 | 
          ps = getpagesize();  | 
| 194 | 
   | 
   | 
| 195 | 
  262011 | 
          of = strtoul(vg->av[2], NULL, 10);  | 
| 196 | 
  262011 | 
          off = RDN2(of, ps);  | 
| 197 | 
   | 
   | 
| 198 | 
  262011 | 
          if (vg->flags & VSM_FLAG_CLUSTER)  | 
| 199 | 
  1730 | 
                  assert(of == 0);  | 
| 200 | 
  262011 | 
          assert(vg->cluster == NULL);  | 
| 201 | 
   | 
   | 
| 202 | 
  262011 | 
          sz = strtoul(vg->av[3], NULL, 10);  | 
| 203 | 
  262011 | 
          assert(sz > 0);  | 
| 204 | 
  262011 | 
          assert(of >= off);  | 
| 205 | 
  262011 | 
          len = RUP2((of - off) + sz, ps);  | 
| 206 | 
   | 
   | 
| 207 | 
  262011 | 
          vsb = VSB_new_auto();  | 
| 208 | 
  262011 | 
          AN(vsb);  | 
| 209 | 
  262011 | 
          VSB_printf(vsb, "%s/%s/%s", vd->wdname, vg->set->dname, vg->av[1]);  | 
| 210 | 
  262011 | 
          AZ(VSB_finish(vsb));  | 
| 211 | 
   | 
   | 
| 212 | 
  262011 | 
          fd = open(VSB_data(vsb), O_RDONLY);     // XXX: openat  | 
| 213 | 
  262011 | 
          if (fd < 0) { | 
| 214 | 
  19 | 
                  VSB_destroy(&vsb);  | 
| 215 | 
  19 | 
                  return (vsm_diag(vd, "Could not open segment"));  | 
| 216 | 
   | 
          }  | 
| 217 | 
   | 
   | 
| 218 | 
  523984 | 
          s = (void*)mmap(NULL, len,  | 
| 219 | 
   | 
              PROT_READ,  | 
| 220 | 
   | 
              MAP_HASSEMAPHORE | MAP_NOSYNC | MAP_SHARED,  | 
| 221 | 
  261992 | 
              fd, (off_t)off);  | 
| 222 | 
   | 
   | 
| 223 | 
  261992 | 
          VSB_destroy(&vsb);  | 
| 224 | 
   | 
   | 
| 225 | 
  261992 | 
          closefd(&fd);  | 
| 226 | 
  261992 | 
          if (s == MAP_FAILED)  | 
| 227 | 
  0 | 
                  return (vsm_diag(vd, "Could not mmap segment"));  | 
| 228 | 
   | 
   | 
| 229 | 
  261992 | 
          vg->s = s;  | 
| 230 | 
  261992 | 
          vg->b = (char*)(vg->s) + of - off;  | 
| 231 | 
  261992 | 
          vg->e = (char *)vg->b + sz;  | 
| 232 | 
  261992 | 
          vg->sz = len;  | 
| 233 | 
   | 
   | 
| 234 | 
  261992 | 
          return (0);  | 
| 235 | 
  266051 | 
  }  | 
| 236 | 
   | 
   | 
| 237 | 
   | 
  static void  | 
| 238 | 
  233192 | 
  vsm_unmapseg(struct vsm_seg *vg)  | 
| 239 | 
   | 
  { | 
| 240 | 
   | 
   | 
| 241 | 
  233192 | 
          CHECK_OBJ_NOTNULL(vg, VSM_SEG_MAGIC);  | 
| 242 | 
   | 
   | 
| 243 | 
  233192 | 
          AN(vg->b);  | 
| 244 | 
  233192 | 
          AN(vg->e);  | 
| 245 | 
  233192 | 
          AZ(munmap(vg->s, vg->sz));  | 
| 246 | 
  233192 | 
          vg->s = vg->b = vg->e = NULL;  | 
| 247 | 
  233192 | 
          vg->sz = 0;  | 
| 248 | 
  233192 | 
  }  | 
| 249 | 
   | 
   | 
| 250 | 
   | 
  /*--------------------------------------------------------------------*/  | 
| 251 | 
   | 
   | 
| 252 | 
   | 
  static void  | 
| 253 | 
  3377439 | 
  vsm_delseg(struct vsm_seg *vg, int refsok)  | 
| 254 | 
   | 
  { | 
| 255 | 
   | 
   | 
| 256 | 
  3377439 | 
          CHECK_OBJ_NOTNULL(vg, VSM_SEG_MAGIC);  | 
| 257 | 
   | 
   | 
| 258 | 
  3377439 | 
          if (vg->set->vg == vg) { | 
| 259 | 
  202253 | 
                  AZ(vg->flags & VSM_FLAG_STALE);  | 
| 260 | 
  202253 | 
                  vg->set->vg = VTAILQ_NEXT(vg, list);  | 
| 261 | 
  202253 | 
          }  | 
| 262 | 
   | 
   | 
| 263 | 
  3377439 | 
          if (refsok && vg->refs) { | 
| 264 | 
  5767 | 
                  AZ(vg->flags & VSM_FLAG_STALE);  | 
| 265 | 
  5767 | 
                  vg->flags |= VSM_FLAG_STALE;  | 
| 266 | 
  5767 | 
                  VTAILQ_REMOVE(&vg->set->segs, vg, list);  | 
| 267 | 
  5767 | 
                  VTAILQ_INSERT_TAIL(&vg->set->stale, vg, list);  | 
| 268 | 
  5767 | 
                  return;  | 
| 269 | 
   | 
          }  | 
| 270 | 
   | 
   | 
| 271 | 
  3371672 | 
          if (vg->s != NULL)  | 
| 272 | 
  0 | 
                  vsm_unmapseg(vg);  | 
| 273 | 
   | 
   | 
| 274 | 
  3371672 | 
          if (vg->flags & VSM_FLAG_CLUSTER) { | 
| 275 | 
  78880 | 
                  vg->flags &= ~VSM_FLAG_CLUSTER;  | 
| 276 | 
  78880 | 
                  VTAILQ_REMOVE(&vg->set->clusters, vg, clist);  | 
| 277 | 
  78880 | 
          }  | 
| 278 | 
   | 
   | 
| 279 | 
  3371672 | 
          if (vg->flags & VSM_FLAG_STALE)  | 
| 280 | 
  5767 | 
                  VTAILQ_REMOVE(&vg->set->stale, vg, list);  | 
| 281 | 
   | 
          else  | 
| 282 | 
  3365905 | 
                  VTAILQ_REMOVE(&vg->set->segs, vg, list);  | 
| 283 | 
  3371672 | 
          VAV_Free(vg->av);  | 
| 284 | 
  3371672 | 
          FREE_OBJ(vg);  | 
| 285 | 
  3377439 | 
  }  | 
| 286 | 
   | 
   | 
| 287 | 
   | 
  /*--------------------------------------------------------------------*/  | 
| 288 | 
   | 
   | 
| 289 | 
   | 
  static struct vsm_set *  | 
| 290 | 
  184480 | 
  vsm_newset(const char *dirname)  | 
| 291 | 
   | 
  { | 
| 292 | 
   | 
          struct vsm_set *vs;  | 
| 293 | 
   | 
   | 
| 294 | 
  184480 | 
          ALLOC_OBJ(vs, VSM_SET_MAGIC);  | 
| 295 | 
  184480 | 
          AN(vs);  | 
| 296 | 
  184480 | 
          VTAILQ_INIT(&vs->segs);  | 
| 297 | 
  184480 | 
          VTAILQ_INIT(&vs->stale);  | 
| 298 | 
  184480 | 
          VTAILQ_INIT(&vs->clusters);  | 
| 299 | 
  184480 | 
          vs->dname = dirname;  | 
| 300 | 
  184480 | 
          vs->dfd = vs->fd = -1;  | 
| 301 | 
  184480 | 
          vs->vlu = VLU_New(vsm_vlu_func, vs, 0);  | 
| 302 | 
  184480 | 
          AN(vs->vlu);  | 
| 303 | 
  184480 | 
          if (getenv("VSM_NOPID") != NULL) | 
| 304 | 
  0 | 
                  vs->couldkill = -1;  | 
| 305 | 
  184480 | 
          return (vs);  | 
| 306 | 
   | 
  }  | 
| 307 | 
   | 
   | 
| 308 | 
   | 
  static void  | 
| 309 | 
  182240 | 
  vsm_delset(struct vsm_set **p)  | 
| 310 | 
   | 
  { | 
| 311 | 
   | 
          struct vsm_set *vs;  | 
| 312 | 
   | 
          struct vsm_seg *vg;  | 
| 313 | 
   | 
   | 
| 314 | 
  182240 | 
          TAKE_OBJ_NOTNULL(vs, p, VSM_SET_MAGIC);  | 
| 315 | 
   | 
   | 
| 316 | 
  182240 | 
          if (vs->fd >= 0)  | 
| 317 | 
  111934 | 
                  closefd(&vs->fd);  | 
| 318 | 
  182240 | 
          if (vs->dfd >= 0)  | 
| 319 | 
  150120 | 
                  closefd(&vs->dfd);  | 
| 320 | 
  182240 | 
          while ((vg = VTAILQ_FIRST(&vs->stale)) != NULL) { | 
| 321 | 
  0 | 
                  AN(vg->flags & VSM_FLAG_STALE);  | 
| 322 | 
  0 | 
                  vsm_delseg(vg, 0);  | 
| 323 | 
   | 
          }  | 
| 324 | 
  3363836 | 
          while ((vg = VTAILQ_FIRST(&vs->segs)) != NULL) { | 
| 325 | 
  3181596 | 
                  AZ(vg->flags & VSM_FLAG_STALE);  | 
| 326 | 
  3181596 | 
                  vsm_delseg(vg, 0);  | 
| 327 | 
   | 
          }  | 
| 328 | 
  182240 | 
          assert(VTAILQ_EMPTY(&vs->clusters));  | 
| 329 | 
  182240 | 
          VLU_Destroy(&vs->vlu);  | 
| 330 | 
  182240 | 
          FREE_OBJ(vs);  | 
| 331 | 
  182240 | 
  }  | 
| 332 | 
   | 
   | 
| 333 | 
   | 
  static void  | 
| 334 | 
  416417 | 
  vsm_wash_set(const struct vsm_set *vs, int all)  | 
| 335 | 
   | 
  { | 
| 336 | 
   | 
          struct vsm_seg *vg, *vg2;  | 
| 337 | 
   | 
   | 
| 338 | 
  3731770 | 
          VTAILQ_FOREACH_SAFE(vg, &vs->segs, list, vg2) { | 
| 339 | 
  3315353 | 
                  if (all || (vg->flags & VSM_FLAG_MARKSCAN) == 0)  | 
| 340 | 
  112834 | 
                          vsm_delseg(vg, 1);  | 
| 341 | 
  3315353 | 
          }  | 
| 342 | 
  416417 | 
  }  | 
| 343 | 
   | 
   | 
| 344 | 
   | 
  /*--------------------------------------------------------------------*/  | 
| 345 | 
   | 
   | 
| 346 | 
   | 
  struct vsm *  | 
| 347 | 
  92240 | 
  VSM_New(void)  | 
| 348 | 
   | 
  { | 
| 349 | 
   | 
          struct vsm *vd;  | 
| 350 | 
   | 
   | 
| 351 | 
  92240 | 
          ALLOC_OBJ(vd, VSM_MAGIC);  | 
| 352 | 
  92240 | 
          AN(vd);  | 
| 353 | 
   | 
   | 
| 354 | 
  92240 | 
          vd->mgt = vsm_newset(VSM_MGT_DIRNAME);  | 
| 355 | 
  92240 | 
          vd->mgt->flag_running = VSM_MGT_RUNNING;  | 
| 356 | 
  92240 | 
          vd->mgt->flag_changed = VSM_MGT_CHANGED;  | 
| 357 | 
  92240 | 
          vd->mgt->flag_restarted = VSM_MGT_RESTARTED;  | 
| 358 | 
   | 
   | 
| 359 | 
  92240 | 
          vd->child = vsm_newset(VSM_CHILD_DIRNAME);  | 
| 360 | 
  92240 | 
          vd->child->flag_running = VSM_WRK_RUNNING;  | 
| 361 | 
  92240 | 
          vd->child->flag_changed = VSM_WRK_CHANGED;  | 
| 362 | 
  92240 | 
          vd->child->flag_restarted = VSM_WRK_RESTARTED;  | 
| 363 | 
   | 
   | 
| 364 | 
  92240 | 
          vd->mgt->vsm = vd;  | 
| 365 | 
  92240 | 
          vd->child->vsm = vd;  | 
| 366 | 
  92240 | 
          vd->wdfd = -1;  | 
| 367 | 
  92240 | 
          vd->patience = 5;  | 
| 368 | 
  92240 | 
          return (vd);  | 
| 369 | 
   | 
  }  | 
| 370 | 
   | 
   | 
| 371 | 
   | 
  /*--------------------------------------------------------------------*/  | 
| 372 | 
   | 
   | 
| 373 | 
   | 
  int  | 
| 374 | 
  94000 | 
  VSM_Arg(struct vsm *vd, char flag, const char *arg)  | 
| 375 | 
   | 
  { | 
| 376 | 
  94000 | 
          char *p = NULL;  | 
| 377 | 
   | 
   | 
| 378 | 
  94000 | 
          CHECK_OBJ_NOTNULL(vd, VSM_MAGIC);  | 
| 379 | 
   | 
   | 
| 380 | 
  94000 | 
          if (arg == NULL)  | 
| 381 | 
  1640 | 
                  return (1);  | 
| 382 | 
  92360 | 
          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 | 
  91840 | 
                  if (vd->wdname != NULL)  | 
| 396 | 
  0 | 
                          free(vd->wdname);  | 
| 397 | 
  91840 | 
                  vd->wdname = VIN_n_Arg(arg);  | 
| 398 | 
  91840 | 
                  if (vd->wdname == NULL)  | 
| 399 | 
  0 | 
                          return (vsm_diag(vd, "Invalid instance name: %s",  | 
| 400 | 
  0 | 
                              strerror(errno)));  | 
| 401 | 
  91840 | 
                  break;  | 
| 402 | 
   | 
          default:  | 
| 403 | 
  0 | 
                  return (vsm_diag(vd, "Unknown VSM_Arg('%c')", flag)); | 
| 404 | 
   | 
          }  | 
| 405 | 
  92080 | 
          return (1);  | 
| 406 | 
  94000 | 
  }  | 
| 407 | 
   | 
   | 
| 408 | 
   | 
  /*--------------------------------------------------------------------*/  | 
| 409 | 
   | 
   | 
| 410 | 
   | 
  void  | 
| 411 | 
  91120 | 
  VSM_Destroy(struct vsm **vdp)  | 
| 412 | 
   | 
  { | 
| 413 | 
   | 
          struct vsm *vd;  | 
| 414 | 
   | 
   | 
| 415 | 
  91120 | 
          TAKE_OBJ_NOTNULL(vd, vdp, VSM_MAGIC);  | 
| 416 | 
   | 
   | 
| 417 | 
  91120 | 
          VSM_ResetError(vd);  | 
| 418 | 
  91120 | 
          REPLACE(vd->wdname, NULL);  | 
| 419 | 
  91120 | 
          if (vd->diag != NULL)  | 
| 420 | 
  0 | 
                  VSB_destroy(&vd->diag);  | 
| 421 | 
  91120 | 
          if (vd->wdfd >= 0)  | 
| 422 | 
  91040 | 
                  closefd(&vd->wdfd);  | 
| 423 | 
  91120 | 
          vsm_delset(&vd->mgt);  | 
| 424 | 
  91120 | 
          vsm_delset(&vd->child);  | 
| 425 | 
  91120 | 
          FREE_OBJ(vd);  | 
| 426 | 
  91120 | 
  }  | 
| 427 | 
   | 
   | 
| 428 | 
   | 
  /*--------------------------------------------------------------------*/  | 
| 429 | 
   | 
   | 
| 430 | 
   | 
  const char *  | 
| 431 | 
  459 | 
  VSM_Error(const struct vsm *vd)  | 
| 432 | 
   | 
  { | 
| 433 | 
   | 
   | 
| 434 | 
  459 | 
          CHECK_OBJ_NOTNULL(vd, VSM_MAGIC);  | 
| 435 | 
   | 
   | 
| 436 | 
  459 | 
          if (vd->diag == NULL)  | 
| 437 | 
  0 | 
                  return ("No VSM error"); | 
| 438 | 
   | 
          else  | 
| 439 | 
  459 | 
                  return (VSB_data(vd->diag));  | 
| 440 | 
  459 | 
  }  | 
| 441 | 
   | 
   | 
| 442 | 
   | 
  /*--------------------------------------------------------------------*/  | 
| 443 | 
   | 
   | 
| 444 | 
   | 
  void  | 
| 445 | 
  184250 | 
  VSM_ResetError(struct vsm *vd)  | 
| 446 | 
   | 
  { | 
| 447 | 
   | 
   | 
| 448 | 
  184250 | 
          CHECK_OBJ_NOTNULL(vd, VSM_MAGIC);  | 
| 449 | 
   | 
   | 
| 450 | 
  184250 | 
          if (vd->diag == NULL)  | 
| 451 | 
  182781 | 
                  return;  | 
| 452 | 
  1469 | 
          VSB_destroy(&vd->diag);  | 
| 453 | 
  184250 | 
  }  | 
| 454 | 
   | 
   | 
| 455 | 
   | 
  /*--------------------------------------------------------------------  | 
| 456 | 
   | 
   */  | 
| 457 | 
   | 
   | 
| 458 | 
   | 
  static int  | 
| 459 | 
  693822 | 
  vsm_cmp_av(char * const *a1, char * const *a2)  | 
| 460 | 
   | 
  { | 
| 461 | 
   | 
   | 
| 462 | 
  1014334 | 
          while (1) { | 
| 463 | 
  1014334 | 
                  if (*a1 == NULL && *a2 == NULL)  | 
| 464 | 
  79286 | 
                          return (0);  | 
| 465 | 
  935048 | 
                  if (*a1 == NULL || *a2 == NULL)  | 
| 466 | 
  0 | 
                          return (1);  | 
| 467 | 
  935048 | 
                  if (strcmp(*a1, *a2))  | 
| 468 | 
  614536 | 
                          return (1);  | 
| 469 | 
  320512 | 
                  a1++;  | 
| 470 | 
  320512 | 
                  a2++;  | 
| 471 | 
   | 
          }  | 
| 472 | 
  693822 | 
  }  | 
| 473 | 
   | 
   | 
| 474 | 
   | 
  static struct vsm_seg *  | 
| 475 | 
  84799 | 
  vsm_findcluster(const struct vsm_set *vs, const char *cnam)  | 
| 476 | 
   | 
  { | 
| 477 | 
   | 
          struct vsm_seg *vg;  | 
| 478 | 
  84799 | 
          AN(vs);  | 
| 479 | 
  84799 | 
          AN(cnam);  | 
| 480 | 
  120554 | 
          VTAILQ_FOREACH(vg, &vs->clusters, clist) { | 
| 481 | 
  120554 | 
                  AN(vg->av[1]);  | 
| 482 | 
  120554 | 
                  if (!strcmp(cnam, vg->av[1]))  | 
| 483 | 
  84799 | 
                          return (vg);  | 
| 484 | 
  35755 | 
          }  | 
| 485 | 
  0 | 
          return (NULL);  | 
| 486 | 
  84799 | 
  }  | 
| 487 | 
   | 
   | 
| 488 | 
   | 
  static unsigned  | 
| 489 | 
  3018349 | 
  vsm_running(struct vsm_set *vs, pid_t pid)  | 
| 490 | 
   | 
  { | 
| 491 | 
   | 
   | 
| 492 | 
  3018349 | 
          AN(vs);  | 
| 493 | 
   | 
   | 
| 494 | 
  3018349 | 
          if (pid == 0)  | 
| 495 | 
  0 | 
                  return (0);  | 
| 496 | 
   | 
   | 
| 497 | 
  3018349 | 
          if (kill(pid, 0) == 0) { | 
| 498 | 
  2933502 | 
                  vs->couldkill = 1;  | 
| 499 | 
  2933502 | 
                  return (1);  | 
| 500 | 
   | 
          }  | 
| 501 | 
  84847 | 
          if (errno == EPERM)     /* a process exists, assume running */  | 
| 502 | 
  0 | 
                  return (1);  | 
| 503 | 
  84847 | 
          assert(errno != EINVAL);  | 
| 504 | 
  84847 | 
          return (0);  | 
| 505 | 
  3018349 | 
  }  | 
| 506 | 
   | 
   | 
| 507 | 
   | 
  static int  | 
| 508 | 
  153914 | 
  vsm_vlu_hash(struct vsm_set *vs, const char *line)  | 
| 509 | 
   | 
  { | 
| 510 | 
   | 
          int i;  | 
| 511 | 
   | 
          uintmax_t id1, id2;  | 
| 512 | 
   | 
   | 
| 513 | 
  153914 | 
          i = sscanf(line, "# %ju %ju", &id1, &id2);  | 
| 514 | 
  153914 | 
          if (i != 2) { | 
| 515 | 
  0 | 
                  vs->retval |= vs->flag_restarted;  | 
| 516 | 
  0 | 
                  return (0);  | 
| 517 | 
   | 
          }  | 
| 518 | 
  153914 | 
          if (vs->couldkill >= 0 && vsm_running(vs, id1)) { | 
| 519 | 
   | 
                  /* nothing to do */  | 
| 520 | 
  153914 | 
          } else if (vs->couldkill > 0 && errno == ESRCH) { | 
| 521 | 
  0 | 
                  vs->retval |= vs->flag_restarted | VSM_MGT_CHANGED;  | 
| 522 | 
  0 | 
                  return (0);  | 
| 523 | 
   | 
          }  | 
| 524 | 
  153914 | 
          vs->retval |= VSM_MGT_RUNNING;  | 
| 525 | 
  153914 | 
          if (id1 != vs->id1 || id2 != vs->id2) { | 
| 526 | 
  153840 | 
                  vs->retval |= vs->flag_restarted;  | 
| 527 | 
  153840 | 
                  vs->id1 = id1;  | 
| 528 | 
  153840 | 
                  vs->id2 = id2;  | 
| 529 | 
  153840 | 
          }  | 
| 530 | 
  153914 | 
          return (0);  | 
| 531 | 
  153914 | 
  }  | 
| 532 | 
   | 
   | 
| 533 | 
   | 
  static int  | 
| 534 | 
  3410956 | 
  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 | 
  3410956 | 
          av = VAV_Parse(line + 1, &ac, 0);  | 
| 541 | 
   | 
   | 
| 542 | 
  3410956 | 
          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 | 
  3410956 | 
          vg = vs->vg;  | 
| 550 | 
  3410956 | 
          CHECK_OBJ_ORNULL(vg, VSM_SEG_MAGIC);  | 
| 551 | 
  3410956 | 
          if (vg != NULL)  | 
| 552 | 
  2044 | 
                  AZ(vg->flags & VSM_FLAG_STALE);  | 
| 553 | 
  3410956 | 
          while (vg != NULL && vsm_cmp_av(&vg->av[1], &av[1]))  | 
| 554 | 
  0 | 
                  vg = VTAILQ_NEXT(vg, list);  | 
| 555 | 
  3410956 | 
          if (vg != NULL) { | 
| 556 | 
   | 
                  /* entry compared equal, so it survives */  | 
| 557 | 
  2044 | 
                  CHECK_OBJ_NOTNULL(vg, VSM_SEG_MAGIC);  | 
| 558 | 
  2044 | 
                  VAV_Free(av);  | 
| 559 | 
  2044 | 
                  vg->flags |= VSM_FLAG_MARKSCAN;  | 
| 560 | 
  2044 | 
                  vs->vg = VTAILQ_NEXT(vg, list);  | 
| 561 | 
  2044 | 
          } else { | 
| 562 | 
  3408912 | 
                  ALLOC_OBJ(vg, VSM_SEG_MAGIC);  | 
| 563 | 
  3408912 | 
                  AN(vg);  | 
| 564 | 
  3408912 | 
                  vg->av = av;  | 
| 565 | 
  3408912 | 
                  vg->set = vs;  | 
| 566 | 
  3408912 | 
                  vg->flags = VSM_FLAG_MARKSCAN;  | 
| 567 | 
  3408912 | 
                  vg->serial = vd->serial;  | 
| 568 | 
   | 
   | 
| 569 | 
  3408912 | 
                  VTAILQ_INSERT_TAIL(&vs->segs, vg, list);  | 
| 570 | 
  3408912 | 
                  if (ac == 4) { | 
| 571 | 
  79520 | 
                          vg->flags |= VSM_FLAG_CLUSTER;  | 
| 572 | 
  79520 | 
                          VTAILQ_INSERT_TAIL(&vs->clusters, vg, clist);  | 
| 573 | 
  3408912 | 
                  } else if (*vg->av[2] != '0') { | 
| 574 | 
  82720 | 
                          vg->cluster = vsm_findcluster(vs, vg->av[1]);  | 
| 575 | 
  82720 | 
                          CHECK_OBJ_NOTNULL(vg->cluster, VSM_SEG_MAGIC);  | 
| 576 | 
  82720 | 
                  }  | 
| 577 | 
  3408912 | 
                  vs->retval |= vs->flag_changed;  | 
| 578 | 
   | 
          }  | 
| 579 | 
  3410956 | 
          return (0);  | 
| 580 | 
  3410956 | 
  }  | 
| 581 | 
   | 
   | 
| 582 | 
   | 
  static int  | 
| 583 | 
  77242 | 
  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 | 
  77242 | 
          av = VAV_Parse(line + 1, &ac, 0);  | 
| 590 | 
   | 
   | 
| 591 | 
  77242 | 
          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 | 
  77242 | 
          if (*av[2] != '0')  | 
| 600 | 
  2079 | 
                  vg = vsm_findcluster(vs, av[1]);  | 
| 601 | 
   | 
          else  | 
| 602 | 
  75163 | 
                  vg = VTAILQ_FIRST(&vs->segs);  | 
| 603 | 
   | 
   | 
| 604 | 
  691778 | 
          for (;vg != NULL; vg = VTAILQ_NEXT(vg, list)) { | 
| 605 | 
  691778 | 
                  if (!vsm_cmp_av(&vg->av[1], &av[1])) { | 
| 606 | 
  77242 | 
                          vs->retval |= vs->flag_changed;  | 
| 607 | 
  77242 | 
                          vsm_delseg(vg, 1);  | 
| 608 | 
  77242 | 
                          break;  | 
| 609 | 
   | 
                  }  | 
| 610 | 
  614536 | 
          }  | 
| 611 | 
  77242 | 
          AN(vg);  | 
| 612 | 
  77242 | 
          VAV_Free(av);  | 
| 613 | 
  77242 | 
          return (0);  | 
| 614 | 
  77242 | 
  }  | 
| 615 | 
   | 
   | 
| 616 | 
   | 
  static int v_matchproto_(vlu_f)  | 
| 617 | 
  3642112 | 
  vsm_vlu_func(void *priv, const char *line)  | 
| 618 | 
   | 
  { | 
| 619 | 
   | 
          struct vsm *vd;  | 
| 620 | 
   | 
          struct vsm_set *vs;  | 
| 621 | 
  3642112 | 
          int i = 0;  | 
| 622 | 
   | 
   | 
| 623 | 
  3642112 | 
          CAST_OBJ_NOTNULL(vs, priv, VSM_SET_MAGIC);  | 
| 624 | 
  3642112 | 
          vd = vs->vsm;  | 
| 625 | 
  3642112 | 
          CHECK_OBJ_NOTNULL(vd, VSM_MAGIC);  | 
| 626 | 
  3642112 | 
          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 | 
  3642112 | 
          vd->serial = VSM_PRIV_LOW(vd->serial + 1);  | 
| 632 | 
   | 
   | 
| 633 | 
  3642112 | 
          switch (line[0]) { | 
| 634 | 
   | 
          case '#':  | 
| 635 | 
  153914 | 
                  i = vsm_vlu_hash(vs, line);  | 
| 636 | 
  268792 | 
                  VTAILQ_FOREACH(vs->vg, &vs->segs, list)  | 
| 637 | 
  114878 | 
                          vs->vg->flags &= ~VSM_FLAG_MARKSCAN;  | 
| 638 | 
  153914 | 
                  if (!(vs->retval & vs->flag_restarted))  | 
| 639 | 
  74 | 
                          vs->vg = VTAILQ_FIRST(&vs->segs);  | 
| 640 | 
  153914 | 
                  break;  | 
| 641 | 
   | 
          case '+':  | 
| 642 | 
  3410956 | 
                  i = vsm_vlu_plus(vd, vs, line);  | 
| 643 | 
  3410956 | 
                  break;  | 
| 644 | 
   | 
          case '-':  | 
| 645 | 
  77242 | 
                  i = vsm_vlu_minus(vd, vs, line);  | 
| 646 | 
  77242 | 
                  break;  | 
| 647 | 
   | 
          default:  | 
| 648 | 
  0 | 
                  break;  | 
| 649 | 
   | 
          }  | 
| 650 | 
  3642112 | 
          return (i);  | 
| 651 | 
   | 
  }  | 
| 652 | 
   | 
   | 
| 653 | 
   | 
  static void  | 
| 654 | 
  2864419 | 
  vsm_readlines(struct vsm_set *vs)  | 
| 655 | 
   | 
  { | 
| 656 | 
   | 
          int i;  | 
| 657 | 
   | 
   | 
| 658 | 
  2864419 | 
          do { | 
| 659 | 
  3237311 | 
                  assert(vs->fd >= 0);  | 
| 660 | 
  3237311 | 
                  i = VLU_Fd(vs->vlu, vs->fd);  | 
| 661 | 
  3237311 | 
          } while (!i);  | 
| 662 | 
  2864419 | 
          assert(i == -2);  | 
| 663 | 
  2864419 | 
  }  | 
| 664 | 
   | 
   | 
| 665 | 
   | 
  static unsigned  | 
| 666 | 
  3199399 | 
  vsm_refresh_set(struct vsm *vd, struct vsm_set *vs)  | 
| 667 | 
   | 
  { | 
| 668 | 
  3199399 | 
          unsigned restarted = 0;  | 
| 669 | 
   | 
          struct stat st;  | 
| 670 | 
   | 
   | 
| 671 | 
  3199399 | 
          CHECK_OBJ_NOTNULL(vd, VSM_MAGIC);  | 
| 672 | 
  3199399 | 
          CHECK_OBJ_NOTNULL(vs, VSM_SET_MAGIC);  | 
| 673 | 
  3199399 | 
          vs->retval = 0;  | 
| 674 | 
  5982396 | 
          if (vs->dfd >= 0 && (  | 
| 675 | 
  2785428 | 
              fstatat(vd->wdfd, vs->dname, &st, AT_SYMLINK_NOFOLLOW) ||  | 
| 676 | 
  2785457 | 
              st.st_ino != vs->dst.st_ino ||  | 
| 677 | 
  2782998 | 
              st.st_dev != vs->dst.st_dev ||  | 
| 678 | 
  2782997 | 
              st.st_mode != vs->dst.st_mode ||  | 
| 679 | 
  2782997 | 
              st.st_nlink == 0)) { | 
| 680 | 
  2542 | 
                  closefd(&vs->dfd);  | 
| 681 | 
  2440 | 
                  restarted = vs->flag_restarted;  | 
| 682 | 
  2440 | 
          }  | 
| 683 | 
   | 
   | 
| 684 | 
  3199357 | 
          if (vs->dfd < 0) { | 
| 685 | 
  416343 | 
                  if (vs->fd >= 0)  | 
| 686 | 
  2436 | 
                          closefd(&vs->fd);  | 
| 687 | 
  416343 | 
                  vs->dfd = openat(vd->wdfd, vs->dname, O_RDONLY);  | 
| 688 | 
  416343 | 
          }  | 
| 689 | 
   | 
   | 
| 690 | 
  3199357 | 
          if (vs->dfd < 0) { | 
| 691 | 
  262503 | 
                  vs->id1 = vs->id2 = 0;  | 
| 692 | 
  262503 | 
                  vsm_wash_set(vs, 1);  | 
| 693 | 
  262503 | 
                  return (vs->retval | restarted);  | 
| 694 | 
   | 
          }  | 
| 695 | 
   | 
   | 
| 696 | 
  2936854 | 
          AZ(fstat(vs->dfd, &vs->dst));  | 
| 697 | 
   | 
   | 
| 698 | 
  5647388 | 
          if (vs->fd >= 0 && (  | 
| 699 | 
  2748757 | 
              fstatat(vs->dfd, "_.index", &st, AT_SYMLINK_NOFOLLOW) ||  | 
| 700 | 
  2710617 | 
              st.st_ino != vs->fst.st_ino ||  | 
| 701 | 
  2710543 | 
              st.st_dev != vs->fst.st_dev ||  | 
| 702 | 
  2710543 | 
              st.st_mode != vs->fst.st_mode ||  | 
| 703 | 
  2710535 | 
              st.st_size < vs->fst.st_size ||  | 
| 704 | 
  2710534 | 
              st.st_nlink < 1)) { | 
| 705 | 
  38264 | 
                  closefd(&vs->fd);  | 
| 706 | 
  38264 | 
                  vs->retval |= vs->flag_changed;  | 
| 707 | 
  38264 | 
          }  | 
| 708 | 
   | 
   | 
| 709 | 
  2936854 | 
          if (vs->fd >= 0) { | 
| 710 | 
  2710533 | 
                  vs->vg = NULL;  | 
| 711 | 
  2710533 | 
                  vsm_readlines(vs);  | 
| 712 | 
  2710533 | 
          } else { | 
| 713 | 
  704442 | 
                  VTAILQ_FOREACH(vs->vg, &vs->segs, list)  | 
| 714 | 
  478121 | 
                          vs->vg->flags &= ~VSM_FLAG_MARKSCAN;  | 
| 715 | 
  226321 | 
                  vs->vg = VTAILQ_FIRST(&vs->segs);  | 
| 716 | 
  226321 | 
                  vs->fd = openat(vs->dfd, "_.index", O_RDONLY);  | 
| 717 | 
  226321 | 
                  if (vs->fd < 0)  | 
| 718 | 
  72407 | 
                          return (vs->retval | restarted);  | 
| 719 | 
  153914 | 
                  VLU_Reset(vs->vlu);  | 
| 720 | 
  153914 | 
                  AZ(fstat(vs->fd, &vs->fst));  | 
| 721 | 
  153914 | 
                  vsm_readlines(vs);  | 
| 722 | 
  153914 | 
                  vsm_wash_set(vs, 0);  | 
| 723 | 
   | 
          }  | 
| 724 | 
   | 
   | 
| 725 | 
  2864447 | 
          vs->fst.st_size = lseek(vs->fd, 0L, SEEK_CUR);  | 
| 726 | 
   | 
   | 
| 727 | 
  2864447 | 
          if (vs->couldkill < 0 || vsm_running(vs, vs->id1))  | 
| 728 | 
  2779710 | 
                  vs->retval |= vs->flag_running;  | 
| 729 | 
  2864449 | 
          return (vs->retval);  | 
| 730 | 
  3199359 | 
  }  | 
| 731 | 
   | 
   | 
| 732 | 
   | 
  /*--------------------------------------------------------------------*/  | 
| 733 | 
   | 
   | 
| 734 | 
   | 
  unsigned  | 
| 735 | 
  1636135 | 
  VSM_Status(struct vsm *vd)  | 
| 736 | 
   | 
  { | 
| 737 | 
  1636135 | 
          unsigned retval = 0;  | 
| 738 | 
   | 
          struct stat st;  | 
| 739 | 
   | 
   | 
| 740 | 
  1636135 | 
          CHECK_OBJ_NOTNULL(vd, VSM_MAGIC);  | 
| 741 | 
   | 
   | 
| 742 | 
   | 
          /* See if the -n workdir changed */  | 
| 743 | 
  1636135 | 
          if (vd->wdfd >= 0) { | 
| 744 | 
  1543101 | 
                  AZ(fstat(vd->wdfd, &st));  | 
| 745 | 
  3086197 | 
                  if (st.st_ino != vd->wdst.st_ino ||  | 
| 746 | 
  1543096 | 
                      st.st_dev != vd->wdst.st_dev ||  | 
| 747 | 
  1543096 | 
                      st.st_mode != vd->wdst.st_mode ||  | 
| 748 | 
  1543096 | 
                      st.st_nlink == 0) { | 
| 749 | 
  10 | 
                          closefd(&vd->wdfd);  | 
| 750 | 
  0 | 
                          vsm_wash_set(vd->mgt, 1);  | 
| 751 | 
  0 | 
                          vsm_wash_set(vd->child, 1);  | 
| 752 | 
  0 | 
                  }  | 
| 753 | 
  1543097 | 
          }  | 
| 754 | 
   | 
   | 
| 755 | 
   | 
          /* Open workdir */  | 
| 756 | 
  1636131 | 
          if (vd->wdfd < 0) { | 
| 757 | 
  93050 | 
                  retval |= VSM_MGT_RESTARTED | VSM_MGT_CHANGED;  | 
| 758 | 
  93050 | 
                  retval |= VSM_WRK_RESTARTED | VSM_WRK_CHANGED;  | 
| 759 | 
  93050 | 
                  vd->wdfd = open(vd->wdname, O_RDONLY);  | 
| 760 | 
  93050 | 
                  if (vd->wdfd < 0)  | 
| 761 | 
  1370 | 
                          (void)vsm_diag(vd,  | 
| 762 | 
   | 
                              "VSM_Status: Cannot open workdir");  | 
| 763 | 
   | 
                  else  | 
| 764 | 
  91680 | 
                          AZ(fstat(vd->wdfd, &vd->wdst));  | 
| 765 | 
  93050 | 
          }  | 
| 766 | 
   | 
   | 
| 767 | 
  1636131 | 
          if (vd->wdfd >= 0) { | 
| 768 | 
  1634773 | 
                  retval |= vsm_refresh_set(vd, vd->mgt);  | 
| 769 | 
  1634773 | 
                  if (vd->mgt->couldkill > 0 && (retval & VSM_MGT_RESTARTED))  | 
| 770 | 
  91680 | 
                          vd->mgt->couldkill = 0;  | 
| 771 | 
  1634773 | 
                  if (retval & VSM_MGT_RUNNING)  | 
| 772 | 
  1564578 | 
                          retval |= vsm_refresh_set(vd, vd->child);  | 
| 773 | 
  1634739 | 
                  if (vd->child->couldkill > 0 && (retval & VSM_WRK_RESTARTED))  | 
| 774 | 
  62042 | 
                          vd->child->couldkill = 0;  | 
| 775 | 
  1634739 | 
          }  | 
| 776 | 
  1636097 | 
          return (retval);  | 
| 777 | 
   | 
  }  | 
| 778 | 
   | 
   | 
| 779 | 
   | 
  /*--------------------------------------------------------------------*/  | 
| 780 | 
   | 
   | 
| 781 | 
   | 
  int  | 
| 782 | 
  91840 | 
  VSM_Attach(struct vsm *vd, int progress)  | 
| 783 | 
   | 
  { | 
| 784 | 
   | 
          const char *def;  | 
| 785 | 
   | 
          double t0;  | 
| 786 | 
   | 
          unsigned u;  | 
| 787 | 
  91840 | 
          int i, n = 0;  | 
| 788 | 
   | 
   | 
| 789 | 
  91840 | 
          CHECK_OBJ_NOTNULL(vd, VSM_MAGIC);  | 
| 790 | 
   | 
   | 
| 791 | 
  91840 | 
          if (vd->patience < 0)  | 
| 792 | 
  0 | 
                  t0 = DBL_MAX;  | 
| 793 | 
   | 
          else  | 
| 794 | 
  91840 | 
                  t0 = VTIM_mono() + vd->patience;  | 
| 795 | 
   | 
   | 
| 796 | 
  91840 | 
          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 | 
  91840 | 
          AZ(vd->attached);  | 
| 807 | 
  93170 | 
          while (!VSIG_int && !VSIG_term) { | 
| 808 | 
  93130 | 
                  u = VSM_Status(vd);  | 
| 809 | 
  93130 | 
                  VSM_ResetError(vd);  | 
| 810 | 
  93130 | 
                  if (u & VSM_MGT_RUNNING) { | 
| 811 | 
  91680 | 
                          if (progress >= 0 && n > 4)  | 
| 812 | 
  0 | 
                                  (void)write(progress, "\n", 1);  | 
| 813 | 
  91680 | 
                          vd->attached = 1;  | 
| 814 | 
  91680 | 
                          return (0);  | 
| 815 | 
   | 
                  }  | 
| 816 | 
  1450 | 
                  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 | 
  1330 | 
                  if (progress >= 0 && !(++n % 4))  | 
| 823 | 
  284 | 
                          (void)write(progress, ".", 1);  | 
| 824 | 
  1330 | 
                  VTIM_sleep(.25);  | 
| 825 | 
   | 
          }  | 
| 826 | 
  40 | 
          return (vsm_diag(vd, "Attach interrupted"));  | 
| 827 | 
  91840 | 
  }  | 
| 828 | 
   | 
   | 
| 829 | 
   | 
  /*--------------------------------------------------------------------*/  | 
| 830 | 
   | 
   | 
| 831 | 
   | 
  static struct vsm_seg *  | 
| 832 | 
  250334 | 
  vsm_set_findseg(const struct vsm_set *vs, uintptr_t serial)  | 
| 833 | 
   | 
  { | 
| 834 | 
   | 
          struct vsm_seg *vg;  | 
| 835 | 
   | 
   | 
| 836 | 
  3444415 | 
          VTAILQ_FOREACH(vg, &vs->segs, list) { | 
| 837 | 
  3292508 | 
                  if (vg->serial == serial)  | 
| 838 | 
  98427 | 
                          return (vg);  | 
| 839 | 
  3194081 | 
          }  | 
| 840 | 
  299317 | 
          VTAILQ_FOREACH(vg, &vs->stale, list) { | 
| 841 | 
  153097 | 
                  if (vg->serial == serial)  | 
| 842 | 
  5687 | 
                          return (vg);  | 
| 843 | 
  147410 | 
          }  | 
| 844 | 
  146220 | 
          return (NULL);  | 
| 845 | 
  250334 | 
  }  | 
| 846 | 
   | 
   | 
| 847 | 
   | 
  static struct vsm_seg *  | 
| 848 | 
  8643889 | 
  vsm_findseg(const struct vsm *vd, const struct vsm_fantom *vf)  | 
| 849 | 
   | 
  { | 
| 850 | 
   | 
          struct vsm_seg *vg;  | 
| 851 | 
   | 
          uint64_t x;  | 
| 852 | 
   | 
   | 
| 853 | 
  8643889 | 
          x = VSM_PRIV_HIGH(vf->priv);  | 
| 854 | 
  8643889 | 
          if (x == vd->serial) { | 
| 855 | 
  8517062 | 
                  vg = (struct vsm_seg *)vf->priv2;  | 
| 856 | 
  8517062 | 
                  if (!VALID_OBJ(vg, VSM_SEG_MAGIC) ||  | 
| 857 | 
  8517062 | 
                      vg->serial != VSM_PRIV_LOW(vf->priv))  | 
| 858 | 
  0 | 
                          WRONG("Corrupt fantom"); | 
| 859 | 
  8517062 | 
                  return (vg);  | 
| 860 | 
   | 
          }  | 
| 861 | 
   | 
   | 
| 862 | 
  126827 | 
          x = VSM_PRIV_LOW(vf->priv);  | 
| 863 | 
  126827 | 
          vg = vsm_set_findseg(vd->mgt, x);  | 
| 864 | 
  126827 | 
          if (vg == NULL)  | 
| 865 | 
  123507 | 
                  vg = vsm_set_findseg(vd->child, x);  | 
| 866 | 
  126827 | 
          if (vg == NULL)  | 
| 867 | 
  22713 | 
                  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 | 
  104114 | 
          ((struct vsm_fantom *)TRUST_ME(vf))->priv =  | 
| 872 | 
  104114 | 
              VSM_PRIV_MERGE(vg->serial, vd->serial);  | 
| 873 | 
  104114 | 
          return (vg);  | 
| 874 | 
  8643889 | 
  }  | 
| 875 | 
   | 
   | 
| 876 | 
   | 
  /*--------------------------------------------------------------------*/  | 
| 877 | 
   | 
   | 
| 878 | 
   | 
  void  | 
| 879 | 
  356029 | 
  VSM__iter0(const struct vsm *vd, struct vsm_fantom *vf)  | 
| 880 | 
   | 
  { | 
| 881 | 
   | 
   | 
| 882 | 
  356029 | 
          CHECK_OBJ_NOTNULL(vd, VSM_MAGIC);  | 
| 883 | 
  356029 | 
          AN(vf);  | 
| 884 | 
   | 
   | 
| 885 | 
  356029 | 
          AN(vd->attached);  | 
| 886 | 
  356029 | 
          memset(vf, 0, sizeof *vf);  | 
| 887 | 
  356029 | 
  }  | 
| 888 | 
   | 
   | 
| 889 | 
   | 
  int  | 
| 890 | 
  4923334 | 
  VSM__itern(struct vsm *vd, struct vsm_fantom *vf)  | 
| 891 | 
   | 
  { | 
| 892 | 
   | 
          struct vsm_seg *vg;  | 
| 893 | 
   | 
   | 
| 894 | 
  4923334 | 
          CHECK_OBJ_NOTNULL(vd, VSM_MAGIC);  | 
| 895 | 
  4923334 | 
          AN(vd->attached);  | 
| 896 | 
  4923334 | 
          AN(vf);  | 
| 897 | 
   | 
   | 
| 898 | 
  4923334 | 
          if (vf->priv == 0) { | 
| 899 | 
  356028 | 
                  vg = VTAILQ_FIRST(&vd->mgt->segs);  | 
| 900 | 
  356028 | 
                  if (vg == NULL)  | 
| 901 | 
  5 | 
                          return (0);  | 
| 902 | 
  356023 | 
          } else { | 
| 903 | 
  4567306 | 
                  vg = vsm_findseg(vd, vf);  | 
| 904 | 
  4567306 | 
                  if (vg == NULL)  | 
| 905 | 
  0 | 
                          return (vsm_diag(vd, "VSM_FOREACH: inconsistency"));  | 
| 906 | 
  4634899 | 
                  while (1) { | 
| 907 | 
  4634899 | 
                          if (vg->set == vd->mgt && VTAILQ_NEXT(vg, list) == NULL)  | 
| 908 | 
  352222 | 
                                  vg = VTAILQ_FIRST(&vd->child->segs);  | 
| 909 | 
   | 
                          else  | 
| 910 | 
  4282677 | 
                                  vg = VTAILQ_NEXT(vg, list);  | 
| 911 | 
  4634899 | 
                          if (vg == NULL)  | 
| 912 | 
  242080 | 
                                  return (0);  | 
| 913 | 
  4392819 | 
                          if (!(vg->flags & VSM_FLAG_CLUSTER))  | 
| 914 | 
  4325226 | 
                                  break;  | 
| 915 | 
   | 
                  }  | 
| 916 | 
   | 
          }  | 
| 917 | 
  4681249 | 
          memset(vf, 0, sizeof *vf);  | 
| 918 | 
  4681249 | 
          vf->priv = VSM_PRIV_MERGE(vg->serial, vd->serial);  | 
| 919 | 
  4681249 | 
          vf->priv2 = (uintptr_t)vg;  | 
| 920 | 
  4681249 | 
          vf->category = vg->av[4];  | 
| 921 | 
  4681249 | 
          vf->ident = vg->av[5];  | 
| 922 | 
  4681249 | 
          AN(vf->category);  | 
| 923 | 
  4681249 | 
          return (1);  | 
| 924 | 
  4923334 | 
  }  | 
| 925 | 
   | 
   | 
| 926 | 
   | 
  /*--------------------------------------------------------------------*/  | 
| 927 | 
   | 
   | 
| 928 | 
   | 
  int  | 
| 929 | 
  266051 | 
  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 | 
  266051 | 
          CHECK_OBJ_NOTNULL(vd, VSM_MAGIC);  | 
| 936 | 
  266051 | 
          AN(vd->attached);  | 
| 937 | 
  266051 | 
          AN(vf);  | 
| 938 | 
  266051 | 
          vg = vsm_findseg(vd, vf);  | 
| 939 | 
  266051 | 
          if (vg == NULL)  | 
| 940 | 
  0 | 
                  return (vsm_diag(vd, "VSM_Map: bad fantom"));  | 
| 941 | 
   | 
   | 
| 942 | 
  266051 | 
          assert(vg->serial == VSM_PRIV_LOW(vf->priv));  | 
| 943 | 
  266051 | 
          assert(vg->av[4] == vf->category);  | 
| 944 | 
  266051 | 
          assert(vg->av[5] == vf->ident);  | 
| 945 | 
   | 
   | 
| 946 | 
  266051 | 
          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 | 
  266051 | 
          assert(vg->refs == 0);  | 
| 956 | 
   | 
   | 
| 957 | 
  266051 | 
          vgc = vg->cluster;  | 
| 958 | 
   | 
   | 
| 959 | 
  266051 | 
          if (vgc == NULL) { | 
| 960 | 
  260281 | 
                  r = vsm_mapseg(vd, vg);  | 
| 961 | 
  260281 | 
                  if (r)  | 
| 962 | 
  19 | 
                          return (r);  | 
| 963 | 
  260262 | 
                  vf->b = vg->b;  | 
| 964 | 
  260262 | 
                  vf->e = vg->e;  | 
| 965 | 
   | 
   | 
| 966 | 
  260262 | 
                  vg->refs++;  | 
| 967 | 
   | 
   | 
| 968 | 
  260262 | 
                  return (0);  | 
| 969 | 
   | 
          }  | 
| 970 | 
   | 
   | 
| 971 | 
  5770 | 
          CHECK_OBJ_NOTNULL(vgc, VSM_SEG_MAGIC);  | 
| 972 | 
  5770 | 
          assert(vgc->flags & VSM_FLAG_CLUSTER);  | 
| 973 | 
  5770 | 
          assert(vg->s == NULL);  | 
| 974 | 
  5770 | 
          assert(vg->sz == 0);  | 
| 975 | 
   | 
   | 
| 976 | 
  5770 | 
          r = vsm_mapseg(vd, vgc);  | 
| 977 | 
  5770 | 
          if (r)  | 
| 978 | 
  0 | 
                  return (r);  | 
| 979 | 
  5770 | 
          vgc->refs++;  | 
| 980 | 
   | 
   | 
| 981 | 
  5770 | 
          of = strtoul(vg->av[2], NULL, 10);  | 
| 982 | 
  5770 | 
          sz = strtoul(vg->av[3], NULL, 10);  | 
| 983 | 
  5770 | 
          assert(sz > 0);  | 
| 984 | 
   | 
   | 
| 985 | 
  5770 | 
          assert(vgc->sz >= of + sz);  | 
| 986 | 
  5770 | 
          assert(vgc->s == vgc->b);  | 
| 987 | 
  5770 | 
          vg->b = (char *)vgc->b + of;  | 
| 988 | 
  5770 | 
          vg->e = (char *)vg->b + sz;  | 
| 989 | 
   | 
   | 
| 990 | 
  5770 | 
          vf->b = vg->b;  | 
| 991 | 
  5770 | 
          vf->e = vg->e;  | 
| 992 | 
   | 
   | 
| 993 | 
  5770 | 
          vg->refs++;  | 
| 994 | 
   | 
   | 
| 995 | 
  5770 | 
          return (0);  | 
| 996 | 
  266051 | 
  }  | 
| 997 | 
   | 
   | 
| 998 | 
   | 
  /*--------------------------------------------------------------------*/  | 
| 999 | 
   | 
   | 
| 1000 | 
   | 
  int  | 
| 1001 | 
  233272 | 
  VSM_Unmap(struct vsm *vd, struct vsm_fantom *vf)  | 
| 1002 | 
   | 
  { | 
| 1003 | 
   | 
          struct vsm_seg *vg;  | 
| 1004 | 
   | 
   | 
| 1005 | 
  233272 | 
          CHECK_OBJ_NOTNULL(vd, VSM_MAGIC);  | 
| 1006 | 
  233272 | 
          AN(vd->attached);  | 
| 1007 | 
  233272 | 
          AN(vf);  | 
| 1008 | 
  233272 | 
          AN(vf->b);  | 
| 1009 | 
  233272 | 
          vg = vsm_findseg(vd, vf);  | 
| 1010 | 
  233272 | 
          if (vg == NULL)  | 
| 1011 | 
  0 | 
                  return (vsm_diag(vd, "VSM_Unmap: bad fantom"));  | 
| 1012 | 
  233272 | 
          CHECK_OBJ_NOTNULL(vg, VSM_SEG_MAGIC);  | 
| 1013 | 
  233272 | 
          assert(vg->refs > 0);  | 
| 1014 | 
  233272 | 
          vg->refs--;  | 
| 1015 | 
  233272 | 
          vf->b = NULL;  | 
| 1016 | 
  233272 | 
          vf->e = NULL;  | 
| 1017 | 
  233272 | 
          if (vg->refs > 0)  | 
| 1018 | 
  0 | 
                  return (0);  | 
| 1019 | 
   | 
   | 
| 1020 | 
  233272 | 
          if (vg->cluster) { | 
| 1021 | 
  1170 | 
                  CHECK_OBJ_NOTNULL(vg->cluster, VSM_SEG_MAGIC);  | 
| 1022 | 
  1170 | 
                  assert(vg->s == NULL);  | 
| 1023 | 
  1170 | 
                  assert(vg->sz == 0);  | 
| 1024 | 
  1170 | 
                  assert(vg->cluster->refs > 0);  | 
| 1025 | 
  1170 | 
                  if (--vg->cluster->refs == 0) { | 
| 1026 | 
  1090 | 
                          vsm_unmapseg(vg->cluster);  | 
| 1027 | 
  1090 | 
                          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 | 
  1090 | 
                  }  | 
| 1032 | 
  1170 | 
                  vg->b = vg->e = NULL;  | 
| 1033 | 
  1170 | 
          } else { | 
| 1034 | 
  232102 | 
                  vsm_unmapseg(vg);  | 
| 1035 | 
   | 
          }  | 
| 1036 | 
  233272 | 
          if (vg->flags & VSM_FLAG_STALE)  | 
| 1037 | 
  5687 | 
                  vsm_delseg(vg, 0);  | 
| 1038 | 
  233272 | 
          return (0);  | 
| 1039 | 
  233272 | 
  }  | 
| 1040 | 
   | 
   | 
| 1041 | 
   | 
  /*--------------------------------------------------------------------*/  | 
| 1042 | 
   | 
   | 
| 1043 | 
   | 
  const struct vsm_valid *  | 
| 1044 | 
  3577418 | 
  VSM_StillValid(const struct vsm *vd, const struct vsm_fantom *vf)  | 
| 1045 | 
   | 
  { | 
| 1046 | 
   | 
          struct vsm_seg *vg;  | 
| 1047 | 
   | 
   | 
| 1048 | 
  3577418 | 
          CHECK_OBJ_NOTNULL(vd, VSM_MAGIC);  | 
| 1049 | 
  3577418 | 
          AN(vf);  | 
| 1050 | 
  3577418 | 
          vg = vsm_findseg(vd, vf);  | 
| 1051 | 
  3577418 | 
          if (vg == NULL || vg->flags & VSM_FLAG_STALE)  | 
| 1052 | 
  28818 | 
                  return (VSM_invalid);  | 
| 1053 | 
  3548600 | 
          return (VSM_valid);  | 
| 1054 | 
  3577418 | 
  }  | 
| 1055 | 
   | 
   | 
| 1056 | 
   | 
  /*--------------------------------------------------------------------*/  | 
| 1057 | 
   | 
   | 
| 1058 | 
   | 
  int  | 
| 1059 | 
  299025 | 
  VSM_Get(struct vsm *vd, struct vsm_fantom *vf,  | 
| 1060 | 
   | 
      const char *category, const char *ident)  | 
| 1061 | 
   | 
  { | 
| 1062 | 
   | 
   | 
| 1063 | 
  299025 | 
          CHECK_OBJ_NOTNULL(vd, VSM_MAGIC);  | 
| 1064 | 
  299025 | 
          AN(vd->attached);  | 
| 1065 | 
  2126816 | 
          VSM_FOREACH(vf, vd) { | 
| 1066 | 
  1937933 | 
                  if (strcmp(vf->category, category))  | 
| 1067 | 
  1827791 | 
                          continue;  | 
| 1068 | 
  110142 | 
                  if (ident != NULL && strcmp(vf->ident, ident))  | 
| 1069 | 
  0 | 
                          continue;  | 
| 1070 | 
  110142 | 
                  return (1);  | 
| 1071 | 
   | 
          }  | 
| 1072 | 
  188883 | 
          memset(vf, 0, sizeof *vf);  | 
| 1073 | 
  188883 | 
          return (0);  | 
| 1074 | 
  299025 | 
  }  | 
| 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 | 
   | 
  }  |