|  | varnish-cache/bin/varnishtest/vtc_varnish.c | 
| 0 | 
   | 
  /*-  | 
| 1 | 
   | 
   * Copyright (c) 2008-2015 Varnish Software AS  | 
| 2 | 
   | 
   * All rights reserved.  | 
| 3 | 
   | 
   *  | 
| 4 | 
   | 
   * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>  | 
| 5 | 
   | 
   *  | 
| 6 | 
   | 
   * SPDX-License-Identifier: BSD-2-Clause  | 
| 7 | 
   | 
   *  | 
| 8 | 
   | 
   * Redistribution and use in source and binary forms, with or without  | 
| 9 | 
   | 
   * modification, are permitted provided that the following conditions  | 
| 10 | 
   | 
   * are met:  | 
| 11 | 
   | 
   * 1. Redistributions of source code must retain the above copyright  | 
| 12 | 
   | 
   *    notice, this list of conditions and the following disclaimer.  | 
| 13 | 
   | 
   * 2. Redistributions in binary form must reproduce the above copyright  | 
| 14 | 
   | 
   *    notice, this list of conditions and the following disclaimer in the  | 
| 15 | 
   | 
   *    documentation and/or other materials provided with the distribution.  | 
| 16 | 
   | 
   *  | 
| 17 | 
   | 
   * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND  | 
| 18 | 
   | 
   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE  | 
| 19 | 
   | 
   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE  | 
| 20 | 
   | 
   * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE  | 
| 21 | 
   | 
   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL  | 
| 22 | 
   | 
   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS  | 
| 23 | 
   | 
   * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)  | 
| 24 | 
   | 
   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT  | 
| 25 | 
   | 
   * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY  | 
| 26 | 
   | 
   * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF  | 
| 27 | 
   | 
   * SUCH DAMAGE.  | 
| 28 | 
   | 
   */  | 
| 29 | 
   | 
   | 
| 30 | 
   | 
  #ifdef VTEST_WITH_VTC_VARNISH  | 
| 31 | 
   | 
   | 
| 32 | 
   | 
  #include "config.h"  | 
| 33 | 
   | 
   | 
| 34 | 
   | 
  #include <sys/types.h>  | 
| 35 | 
   | 
  #include <sys/socket.h>  | 
| 36 | 
   | 
   | 
| 37 | 
   | 
  #include <fcntl.h>  | 
| 38 | 
   | 
  #include <fnmatch.h>  | 
| 39 | 
   | 
  #include <inttypes.h>  | 
| 40 | 
   | 
  #include <poll.h>  | 
| 41 | 
   | 
  #include <stdio.h>  | 
| 42 | 
   | 
  #include <stdlib.h>  | 
| 43 | 
   | 
  #include <string.h>  | 
| 44 | 
   | 
  #include <unistd.h>  | 
| 45 | 
   | 
   | 
| 46 | 
   | 
  #include "vtc.h"  | 
| 47 | 
   | 
   | 
| 48 | 
   | 
  #include "vapi/vsc.h"  | 
| 49 | 
   | 
  #include "vapi/vsl.h"  | 
| 50 | 
   | 
  #include "vapi/vsm.h"  | 
| 51 | 
   | 
  #include "vcli.h"  | 
| 52 | 
   | 
  #include "vjsn.h"  | 
| 53 | 
   | 
  #include "vre.h"  | 
| 54 | 
   | 
  #include "vsub.h"  | 
| 55 | 
   | 
  #include "vtcp.h"  | 
| 56 | 
   | 
  #include "vtim.h"  | 
| 57 | 
   | 
   | 
| 58 | 
   | 
  struct varnish { | 
| 59 | 
   | 
          unsigned                magic;  | 
| 60 | 
   | 
  #define VARNISH_MAGIC           0x208cd8e3  | 
| 61 | 
   | 
          char                    *name;  | 
| 62 | 
   | 
          struct vtclog           *vl;  | 
| 63 | 
   | 
          VTAILQ_ENTRY(varnish)   list;  | 
| 64 | 
   | 
   | 
| 65 | 
   | 
          struct vsb              *args;  | 
| 66 | 
   | 
          int                     fds[4];  | 
| 67 | 
   | 
          pid_t                   pid;  | 
| 68 | 
   | 
   | 
| 69 | 
   | 
          double                  syntax;  | 
| 70 | 
   | 
   | 
| 71 | 
   | 
          pthread_t               tp;  | 
| 72 | 
   | 
          pthread_t               tp_vsl;  | 
| 73 | 
   | 
          int                     tp_started;  | 
| 74 | 
   | 
   | 
| 75 | 
   | 
          int                     expect_exit;  | 
| 76 | 
   | 
   | 
| 77 | 
   | 
          int                     cli_fd;  | 
| 78 | 
   | 
          int                     vcl_nbr;  | 
| 79 | 
   | 
          char                    *workdir;  | 
| 80 | 
   | 
          char                    *jail;  | 
| 81 | 
   | 
          char                    *proto;  | 
| 82 | 
   | 
   | 
| 83 | 
   | 
          struct vsm              *vsm_vsl;  | 
| 84 | 
   | 
          struct vsm              *vsm_vsc;  | 
| 85 | 
   | 
          struct vsc              *vsc;  | 
| 86 | 
   | 
          int                     has_a_arg;  | 
| 87 | 
   | 
   | 
| 88 | 
   | 
          unsigned                vsl_tag_count[256];  | 
| 89 | 
   | 
   | 
| 90 | 
   | 
          volatile int            vsl_rec;  | 
| 91 | 
   | 
          volatile int            vsl_idle;  | 
| 92 | 
   | 
  };  | 
| 93 | 
   | 
   | 
| 94 | 
   | 
  #define NONSENSE        "%XJEIFLH|)Xspa8P"  | 
| 95 | 
   | 
   | 
| 96 | 
   | 
  static VTAILQ_HEAD(, varnish)   varnishes =  | 
| 97 | 
   | 
      VTAILQ_HEAD_INITIALIZER(varnishes);  | 
| 98 | 
   | 
   | 
| 99 | 
   | 
  /**********************************************************************  | 
| 100 | 
   | 
   * Fatal condition cleanup  | 
| 101 | 
   | 
   * Invalid to call in any code path not followed by vtc_fatal().  | 
| 102 | 
   | 
   */  | 
| 103 | 
   | 
   | 
| 104 | 
   | 
  static void  | 
| 105 | 
  0 | 
  varnish_fatal_cleanup(const struct varnish *v)  | 
| 106 | 
   | 
  { | 
| 107 | 
   | 
          struct pollfd fd[1];  | 
| 108 | 
   | 
          int n;  | 
| 109 | 
   | 
   | 
| 110 | 
  0 | 
          if (!pthread_equal(pthread_self(), vtc_thread))  | 
| 111 | 
  0 | 
                  return;  | 
| 112 | 
   | 
   | 
| 113 | 
  0 | 
          if (!v->tp_started)  | 
| 114 | 
  0 | 
                  return;  | 
| 115 | 
   | 
   | 
| 116 | 
  0 | 
          memset(fd, 0, sizeof(fd));  | 
| 117 | 
  0 | 
          fd[0].fd = v->fds[0];  | 
| 118 | 
  0 | 
          fd[0].events = POLLIN;  | 
| 119 | 
   | 
   | 
| 120 | 
  0 | 
          do { | 
| 121 | 
  0 | 
                  n = poll(fd, sizeof(fd)/sizeof(fd[0]), 10);  | 
| 122 | 
  0 | 
                  if (n == 1 && (fd[0].revents & (POLLHUP|POLLERR)) != 0) { | 
| 123 | 
  0 | 
                          PTOK(pthread_join(v->tp, NULL));  | 
| 124 | 
  0 | 
                          break;  | 
| 125 | 
   | 
                  }  | 
| 126 | 
   | 
   | 
| 127 | 
  0 | 
                  if (n == 1)  | 
| 128 | 
  0 | 
                          VTIM_sleep(0.01);  | 
| 129 | 
  0 | 
          } while (n > 0);  | 
| 130 | 
  0 | 
  }  | 
| 131 | 
   | 
   | 
| 132 | 
   | 
  #define varnish_fatal(v, ...)                                           \  | 
| 133 | 
   | 
      do {                                                                \ | 
| 134 | 
   | 
              varnish_fatal_cleanup((v));                                 \  | 
| 135 | 
   | 
              vtc_fatal((v)->vl, __VA_ARGS__);                            \  | 
| 136 | 
   | 
      } while (0)  | 
| 137 | 
   | 
   | 
| 138 | 
   | 
  /**********************************************************************  | 
| 139 | 
   | 
   * Ask a question over CLI  | 
| 140 | 
   | 
   */  | 
| 141 | 
   | 
   | 
| 142 | 
   | 
  static enum VCLI_status_e  | 
| 143 | 
  14374 | 
  varnish_ask_cli(const struct varnish *v, const char *cmd, char **repl)  | 
| 144 | 
   | 
  { | 
| 145 | 
   | 
          int i;  | 
| 146 | 
   | 
          unsigned retval;  | 
| 147 | 
   | 
          char *r;  | 
| 148 | 
   | 
   | 
| 149 | 
  14374 | 
          if (cmd != NULL) { | 
| 150 | 
  13427 | 
                  vtc_dump(v->vl, 4, "CLI TX", cmd, -1);  | 
| 151 | 
  13427 | 
                  i = write(v->cli_fd, cmd, strlen(cmd));  | 
| 152 | 
  13427 | 
                  if (i != strlen(cmd) && !vtc_stop)  | 
| 153 | 
  0 | 
                          varnish_fatal(v, "CLI write failed (%s) = %u %s",  | 
| 154 | 
   | 
                              cmd, errno, strerror(errno));  | 
| 155 | 
  0 | 
                  i = write(v->cli_fd, "\n", 1);  | 
| 156 | 
  0 | 
                  if (i != 1 && !vtc_stop)  | 
| 157 | 
  0 | 
                          varnish_fatal(v, "CLI write failed (%s) = %u %s",  | 
| 158 | 
   | 
                              cmd, errno, strerror(errno));  | 
| 159 | 
  13427 | 
          }  | 
| 160 | 
  26854 | 
          i = VCLI_ReadResult(v->cli_fd, &retval, &r, vtc_maxdur);  | 
| 161 | 
  26854 | 
          if (i != 0 && !vtc_stop)  | 
| 162 | 
  0 | 
                  varnish_fatal(v, "CLI failed (%s) = %d %u %s",  | 
| 163 | 
   | 
                      cmd != NULL ? cmd : "NULL", i, retval, r);  | 
| 164 | 
  14374 | 
          vtc_log(v->vl, 3, "CLI RX  %u", retval);  | 
| 165 | 
  14374 | 
          vtc_dump(v->vl, 4, "CLI RX", r, -1);  | 
| 166 | 
  14374 | 
          if (repl != NULL)  | 
| 167 | 
  9203 | 
                  *repl = r;  | 
| 168 | 
   | 
          else  | 
| 169 | 
  5171 | 
                  free(r);  | 
| 170 | 
  14374 | 
          return ((enum VCLI_status_e)retval);  | 
| 171 | 
   | 
  }  | 
| 172 | 
   | 
   | 
| 173 | 
   | 
  /**********************************************************************  | 
| 174 | 
   | 
   *  | 
| 175 | 
   | 
   */  | 
| 176 | 
   | 
   | 
| 177 | 
   | 
  static void  | 
| 178 | 
  1000 | 
  wait_stopped(const struct varnish *v)  | 
| 179 | 
   | 
  { | 
| 180 | 
  1000 | 
          char *r = NULL;  | 
| 181 | 
   | 
          enum VCLI_status_e st;  | 
| 182 | 
   | 
   | 
| 183 | 
  1000 | 
          vtc_log(v->vl, 3, "wait-stopped");  | 
| 184 | 
  1000 | 
          while (1) { | 
| 185 | 
  1000 | 
                  st = varnish_ask_cli(v, "status", &r);  | 
| 186 | 
  1000 | 
                  if (st != CLIS_OK)  | 
| 187 | 
  0 | 
                          varnish_fatal(v,  | 
| 188 | 
   | 
                              "CLI status command failed: %u %s", st, r);  | 
| 189 | 
  1000 | 
                  if (!strcmp(r, "Child in state stopped")) { | 
| 190 | 
  1000 | 
                          free(r);  | 
| 191 | 
  1000 | 
                          break;  | 
| 192 | 
   | 
                  }  | 
| 193 | 
  0 | 
                  free(r);  | 
| 194 | 
  0 | 
                  r = NULL;  | 
| 195 | 
  0 | 
                  VTIM_sleep(0.2);  | 
| 196 | 
   | 
          }  | 
| 197 | 
  1000 | 
  }  | 
| 198 | 
   | 
  /**********************************************************************  | 
| 199 | 
   | 
   *  | 
| 200 | 
   | 
   */  | 
| 201 | 
   | 
   | 
| 202 | 
   | 
  static void  | 
| 203 | 
  942 | 
  wait_running(const struct varnish *v)  | 
| 204 | 
   | 
  { | 
| 205 | 
  942 | 
          char *r = NULL;  | 
| 206 | 
   | 
          enum VCLI_status_e st;  | 
| 207 | 
   | 
   | 
| 208 | 
  942 | 
          while (1) { | 
| 209 | 
  942 | 
                  vtc_log(v->vl, 3, "wait-running");  | 
| 210 | 
  942 | 
                  st = varnish_ask_cli(v, "status", &r);  | 
| 211 | 
  942 | 
                  if (st != CLIS_OK)  | 
| 212 | 
  0 | 
                          varnish_fatal(v,  | 
| 213 | 
   | 
                              "CLI status command failed: %u %s", st, r);  | 
| 214 | 
  0 | 
                  if (!strcmp(r, "Child in state stopped"))  | 
| 215 | 
  0 | 
                          varnish_fatal(v,  | 
| 216 | 
   | 
                              "Child stopped before running: %u %s", st, r);  | 
| 217 | 
  942 | 
                  if (!strcmp(r, "Child in state running")) { | 
| 218 | 
  942 | 
                          free(r);  | 
| 219 | 
  942 | 
                          r = NULL;  | 
| 220 | 
  942 | 
                          st = varnish_ask_cli(v, "debug.listen_address", &r);  | 
| 221 | 
  942 | 
                          if (st != CLIS_OK)  | 
| 222 | 
  0 | 
                                  varnish_fatal(v,  | 
| 223 | 
   | 
                                      "CLI status command failed: %u %s", st, r);  | 
| 224 | 
  942 | 
                          free(r);  | 
| 225 | 
  942 | 
                          break;  | 
| 226 | 
   | 
                  }  | 
| 227 | 
  0 | 
                  free(r);  | 
| 228 | 
  0 | 
                  r = NULL;  | 
| 229 | 
  0 | 
                  VTIM_sleep(0.2);  | 
| 230 | 
   | 
          }  | 
| 231 | 
  942 | 
  }  | 
| 232 | 
   | 
   | 
| 233 | 
   | 
  /**********************************************************************  | 
| 234 | 
   | 
   * Varnishlog gatherer thread  | 
| 235 | 
   | 
   */  | 
| 236 | 
   | 
   | 
| 237 | 
   | 
  static void *  | 
| 238 | 
  947 | 
  varnishlog_thread(void *priv)  | 
| 239 | 
   | 
  { | 
| 240 | 
   | 
          struct varnish *v;  | 
| 241 | 
   | 
          struct VSL_data *vsl;  | 
| 242 | 
   | 
          struct vsm *vsm;  | 
| 243 | 
   | 
          struct VSL_cursor *c;  | 
| 244 | 
   | 
          enum VSL_tag_e tag;  | 
| 245 | 
   | 
          uint64_t vxid;  | 
| 246 | 
   | 
          unsigned len, vs;  | 
| 247 | 
   | 
          const char *tagname, *data;  | 
| 248 | 
   | 
          int type, i, opt;  | 
| 249 | 
  947 | 
          struct vsb *vsb = NULL;  | 
| 250 | 
   | 
   | 
| 251 | 
  947 | 
          CAST_OBJ_NOTNULL(v, priv, VARNISH_MAGIC);  | 
| 252 | 
   | 
   | 
| 253 | 
  947 | 
          vsl = VSL_New();  | 
| 254 | 
  947 | 
          AN(vsl);  | 
| 255 | 
  947 | 
          vsm = v->vsm_vsl;  | 
| 256 | 
   | 
   | 
| 257 | 
  947 | 
          c = NULL;  | 
| 258 | 
  947 | 
          opt = 0;  | 
| 259 | 
  21310 | 
          while (v->fds[1] > 0 || c != NULL) {    //lint !e845 bug in flint | 
| 260 | 
  20363 | 
                  if (c == NULL) { | 
| 261 | 
  6861 | 
                          if (vtc_error)  | 
| 262 | 
  0 | 
                                  break;  | 
| 263 | 
  6861 | 
                          VTIM_sleep(0.1);  | 
| 264 | 
  6861 | 
                          (void)VSM_Status(vsm);  | 
| 265 | 
  6861 | 
                          c = VSL_CursorVSM(vsl, vsm, opt);  | 
| 266 | 
  6861 | 
                          if (c == NULL) { | 
| 267 | 
  4468 | 
                                  vtc_log(v->vl, 3, "vsl|%s", VSL_Error(vsl));  | 
| 268 | 
  4468 | 
                                  VSL_ResetError(vsl);  | 
| 269 | 
  4468 | 
                                  continue;  | 
| 270 | 
   | 
                          }  | 
| 271 | 
  2393 | 
                  }  | 
| 272 | 
  15895 | 
                  AN(c);  | 
| 273 | 
   | 
   | 
| 274 | 
  15895 | 
                  opt = VSL_COPT_TAIL;  | 
| 275 | 
   | 
   | 
| 276 | 
  312593 | 
                  while (1) { | 
| 277 | 
  312593 | 
                          i = VSL_Next(c);  | 
| 278 | 
  312593 | 
                          if (i != 1)  | 
| 279 | 
  15895 | 
                                  break;  | 
| 280 | 
   | 
   | 
| 281 | 
  296698 | 
                          v->vsl_rec = 1;  | 
| 282 | 
   | 
   | 
| 283 | 
  296698 | 
                          tag = VSL_TAG(c->rec.ptr);  | 
| 284 | 
  296698 | 
                          vxid = VSL_ID(c->rec.ptr);  | 
| 285 | 
  296698 | 
                          if (tag == SLT__Batch)  | 
| 286 | 
  0 | 
                                  continue;  | 
| 287 | 
  296698 | 
                          tagname = VSL_tags[tag];  | 
| 288 | 
  296698 | 
                          len = VSL_LEN(c->rec.ptr);  | 
| 289 | 
  296698 | 
                          type = VSL_CLIENT(c->rec.ptr) ?  | 
| 290 | 
  118224 | 
                              'c' : VSL_BACKEND(c->rec.ptr) ?  | 
| 291 | 
   | 
                              'b' : '-';  | 
| 292 | 
  296698 | 
                          data = VSL_CDATA(c->rec.ptr);  | 
| 293 | 
  296698 | 
                          v->vsl_tag_count[tag]++;  | 
| 294 | 
  296698 | 
                          if (VSL_tagflags[tag] & SLT_F_BINARY) { | 
| 295 | 
  1469 | 
                                  if (vsb == NULL)  | 
| 296 | 
  52 | 
                                          vsb = VSB_new_auto();  | 
| 297 | 
  1469 | 
                                  VSB_clear(vsb);  | 
| 298 | 
  1469 | 
                                  VSB_quote(vsb, data, len, VSB_QUOTE_HEX);  | 
| 299 | 
  1469 | 
                                  AZ(VSB_finish(vsb));  | 
| 300 | 
   | 
                                  /* +2 to skip "0x" */  | 
| 301 | 
  2938 | 
                                  vtc_log(v->vl, 4, "vsl| %10ju %-15s %c [%s]",  | 
| 302 | 
  1469 | 
                                      (uintmax_t)vxid, tagname, type,  | 
| 303 | 
  1469 | 
                                      VSB_data(vsb) + 2);  | 
| 304 | 
  1469 | 
                          } else { | 
| 305 | 
  590458 | 
                                  vtc_log(v->vl, 4, "vsl| %10ju %-15s %c %.*s",  | 
| 306 | 
  295229 | 
                                      (uintmax_t)vxid, tagname, type, (int)len,  | 
| 307 | 
  295229 | 
                                      data);  | 
| 308 | 
   | 
                          }  | 
| 309 | 
   | 
                  }  | 
| 310 | 
  15895 | 
                  if (i == 0) { | 
| 311 | 
   | 
                          /* Nothing to do but wait */  | 
| 312 | 
  15895 | 
                          v->vsl_idle++;  | 
| 313 | 
  15895 | 
                          vs = VSM_Status(vsm) & VSM_WRK_MASK;  | 
| 314 | 
  15895 | 
                          if ((vs & ~VSM_WRK_CHANGED) != VSM_WRK_RUNNING) { | 
| 315 | 
   | 
                                  /* Abandoned - try reconnect */  | 
| 316 | 
  2393 | 
                                  VSL_DeleteCursor(c);  | 
| 317 | 
  2393 | 
                                  c = NULL;  | 
| 318 | 
  2393 | 
                          } else { | 
| 319 | 
  13500 | 
                                  VTIM_sleep(0.1);  | 
| 320 | 
   | 
                          }  | 
| 321 | 
  15893 | 
                  } else if (i == -2) { | 
| 322 | 
   | 
                          /* Abandoned - try reconnect */  | 
| 323 | 
  0 | 
                          VSL_DeleteCursor(c);  | 
| 324 | 
  0 | 
                          c = NULL;  | 
| 325 | 
  0 | 
                  } else  | 
| 326 | 
  0 | 
                          break;  | 
| 327 | 
   | 
          }  | 
| 328 | 
   | 
   | 
| 329 | 
  0 | 
          if (c)  | 
| 330 | 
  0 | 
                  VSL_DeleteCursor(c);  | 
| 331 | 
  52 | 
          VSL_Delete(vsl);  | 
| 332 | 
  52 | 
          if (vsb != NULL)  | 
| 333 | 
  52 | 
                  VSB_destroy(&vsb);  | 
| 334 | 
   | 
   | 
| 335 | 
  947 | 
          return (NULL);  | 
| 336 | 
   | 
  }  | 
| 337 | 
   | 
   | 
| 338 | 
   | 
  /**********************************************************************  | 
| 339 | 
   | 
   * Allocate and initialize a varnish  | 
| 340 | 
   | 
   */  | 
| 341 | 
   | 
   | 
| 342 | 
   | 
  static struct varnish *  | 
| 343 | 
  947 | 
  varnish_new(const char *name)  | 
| 344 | 
   | 
  { | 
| 345 | 
   | 
          struct varnish *v;  | 
| 346 | 
   | 
          struct vsb *vsb;  | 
| 347 | 
   | 
          char buf[1024];  | 
| 348 | 
   | 
   | 
| 349 | 
  947 | 
          ALLOC_OBJ(v, VARNISH_MAGIC);  | 
| 350 | 
  947 | 
          AN(v);  | 
| 351 | 
  947 | 
          REPLACE(v->name, name);  | 
| 352 | 
   | 
   | 
| 353 | 
  947 | 
          REPLACE(v->jail, "");  | 
| 354 | 
   | 
   | 
| 355 | 
  947 | 
          v->vl = vtc_logopen("%s", name); | 
| 356 | 
  947 | 
          AN(v->vl);  | 
| 357 | 
   | 
   | 
| 358 | 
  947 | 
          vsb = macro_expandf(v->vl, "${tmpdir}/%s", name); | 
| 359 | 
  947 | 
          AN(vsb);  | 
| 360 | 
  947 | 
          v->workdir = strdup(VSB_data(vsb));  | 
| 361 | 
  947 | 
          AN(v->workdir);  | 
| 362 | 
  947 | 
          VSB_destroy(&vsb);  | 
| 363 | 
   | 
   | 
| 364 | 
  947 | 
          bprintf(buf, "rm -rf %s ; mkdir -p %s", v->workdir, v->workdir);  | 
| 365 | 
  947 | 
          AZ(system(buf));  | 
| 366 | 
   | 
   | 
| 367 | 
  947 | 
          v->args = VSB_new_auto();  | 
| 368 | 
   | 
   | 
| 369 | 
  947 | 
          v->cli_fd = -1;  | 
| 370 | 
  947 | 
          VTAILQ_INSERT_TAIL(&varnishes, v, list);  | 
| 371 | 
   | 
   | 
| 372 | 
  947 | 
          return (v);  | 
| 373 | 
   | 
  }  | 
| 374 | 
   | 
   | 
| 375 | 
   | 
  /**********************************************************************  | 
| 376 | 
   | 
   * Delete a varnish instance  | 
| 377 | 
   | 
   */  | 
| 378 | 
   | 
   | 
| 379 | 
   | 
  static void  | 
| 380 | 
  947 | 
  varnish_delete(struct varnish *v)  | 
| 381 | 
   | 
  { | 
| 382 | 
   | 
   | 
| 383 | 
  947 | 
          CHECK_OBJ_NOTNULL(v, VARNISH_MAGIC);  | 
| 384 | 
  947 | 
          vtc_logclose(v->vl);  | 
| 385 | 
  947 | 
          free(v->name);  | 
| 386 | 
  947 | 
          free(v->jail);  | 
| 387 | 
  947 | 
          free(v->workdir);  | 
| 388 | 
  947 | 
          VSB_destroy(&v->args);  | 
| 389 | 
  947 | 
          if (v->vsc != NULL)  | 
| 390 | 
  947 | 
                  VSC_Destroy(&v->vsc, v->vsm_vsc);  | 
| 391 | 
  947 | 
          if (v->vsm_vsc != NULL)  | 
| 392 | 
  947 | 
                  VSM_Destroy(&v->vsm_vsc);  | 
| 393 | 
  947 | 
          if (v->vsm_vsl != NULL)  | 
| 394 | 
  947 | 
                  VSM_Destroy(&v->vsm_vsl);  | 
| 395 | 
   | 
   | 
| 396 | 
   | 
          /*  | 
| 397 | 
   | 
           * We do not delete the workdir, it may contain stuff people  | 
| 398 | 
   | 
           * want (coredumps, shmlog/stats etc), and trying to divine  | 
| 399 | 
   | 
           * "may want" is just too much trouble.  Leave it around and  | 
| 400 | 
   | 
           * nuke it at the start of the next test-run.  | 
| 401 | 
   | 
           */  | 
| 402 | 
   | 
   | 
| 403 | 
   | 
          /* XXX: MEMLEAK (?) */  | 
| 404 | 
  947 | 
          FREE_OBJ(v);  | 
| 405 | 
  947 | 
  }  | 
| 406 | 
   | 
   | 
| 407 | 
   | 
  /**********************************************************************  | 
| 408 | 
   | 
   * Varnish listener  | 
| 409 | 
   | 
   */  | 
| 410 | 
   | 
   | 
| 411 | 
   | 
  static void *  | 
| 412 | 
  947 | 
  varnish_thread(void *priv)  | 
| 413 | 
   | 
  { | 
| 414 | 
   | 
          struct varnish *v;  | 
| 415 | 
   | 
   | 
| 416 | 
  947 | 
          CAST_OBJ_NOTNULL(v, priv, VARNISH_MAGIC);  | 
| 417 | 
  947 | 
          return (vtc_record(v->vl, v->fds[0], NULL));  | 
| 418 | 
   | 
  }  | 
| 419 | 
   | 
   | 
| 420 | 
   | 
  /**********************************************************************  | 
| 421 | 
   | 
   * Launch a Varnish  | 
| 422 | 
   | 
   */  | 
| 423 | 
   | 
   | 
| 424 | 
   | 
  static void  | 
| 425 | 
  947 | 
  varnish_launch(struct varnish *v)  | 
| 426 | 
   | 
  { | 
| 427 | 
   | 
          struct vsb *vsb, *vsb1;  | 
| 428 | 
   | 
          int i, nfd, asock;  | 
| 429 | 
   | 
          char abuf[VTCP_ADDRBUFSIZE];  | 
| 430 | 
   | 
          char pbuf[VTCP_PORTBUFSIZE];  | 
| 431 | 
   | 
          char lbuf[PATH_MAX];  | 
| 432 | 
   | 
          struct pollfd fd[3];  | 
| 433 | 
   | 
          enum VCLI_status_e u;  | 
| 434 | 
   | 
          const char *err;  | 
| 435 | 
  947 | 
          char *r = NULL;  | 
| 436 | 
   | 
   | 
| 437 | 
   | 
          /* Create listener socket */  | 
| 438 | 
  947 | 
          asock = VTCP_listen_on(default_listen_addr, NULL, 1, &err);  | 
| 439 | 
  947 | 
          if (err != NULL)  | 
| 440 | 
  0 | 
                  varnish_fatal(v, "Create CLI listen socket failed: %s", err);  | 
| 441 | 
  947 | 
          assert(asock > 0);  | 
| 442 | 
  947 | 
          VTCP_myname(asock, abuf, sizeof abuf, pbuf, sizeof pbuf);  | 
| 443 | 
   | 
   | 
| 444 | 
  947 | 
          AZ(VSB_finish(v->args));  | 
| 445 | 
  947 | 
          vtc_log(v->vl, 2, "Launch");  | 
| 446 | 
  947 | 
          vsb = VSB_new_auto();  | 
| 447 | 
  947 | 
          AN(vsb);  | 
| 448 | 
  947 | 
          VSB_cat(vsb, "cd ${pwd} &&"); | 
| 449 | 
  1894 | 
          VSB_printf(vsb, " exec varnishd %s -d -n %s -i %s",  | 
| 450 | 
  947 | 
              v->jail, v->workdir, v->name);  | 
| 451 | 
  947 | 
          if (macro_isdef(NULL, "varnishd_args_prepend")) { | 
| 452 | 
  0 | 
                  VSB_putc(vsb, ' ');  | 
| 453 | 
  0 | 
                  macro_cat(v->vl, vsb, "varnishd_args_prepend", NULL);  | 
| 454 | 
  0 | 
          }  | 
| 455 | 
  0 | 
          VSB_cat(vsb, VSB_data(params_vsb));  | 
| 456 | 
  0 | 
          if (leave_temp) { | 
| 457 | 
  0 | 
                  VSB_cat(vsb, " -p debug=+vcl_keep");  | 
| 458 | 
  0 | 
                  VSB_cat(vsb, " -p debug=+vmod_so_keep");  | 
| 459 | 
  0 | 
                  VSB_cat(vsb, " -p debug=+vsm_keep");  | 
| 460 | 
  0 | 
          }  | 
| 461 | 
  920 | 
          VSB_cat(vsb, " -l 2m");  | 
| 462 | 
  920 | 
          VSB_cat(vsb, " -p auto_restart=off");  | 
| 463 | 
  920 | 
          VSB_cat(vsb, " -p syslog_cli_traffic=off");  | 
| 464 | 
  920 | 
          VSB_cat(vsb, " -p thread_pool_min=10");  | 
| 465 | 
  920 | 
          VSB_cat(vsb, " -p debug=+vtc_mode");  | 
| 466 | 
  920 | 
          VSB_cat(vsb, " -p vsl_mask=+Debug,+H2RxHdr,+H2RxBody");  | 
| 467 | 
  920 | 
          VSB_cat(vsb, " -p h2_initial_window_size=1m");  | 
| 468 | 
  920 | 
          VSB_cat(vsb, " -p h2_rx_window_low_water=64k");  | 
| 469 | 
  920 | 
          if (!v->has_a_arg) { | 
| 470 | 
  920 | 
                  VSB_printf(vsb, " -a '%s'", default_listen_addr);  | 
| 471 | 
  920 | 
                  if (v->proto != NULL)  | 
| 472 | 
  15 | 
                          VSB_printf(vsb, ",%s", v->proto);  | 
| 473 | 
  920 | 
          }  | 
| 474 | 
  946 | 
          VSB_printf(vsb, " -M '%s %s'", abuf, pbuf);  | 
| 475 | 
  946 | 
          VSB_printf(vsb, " -P %s/varnishd.pid", v->workdir);  | 
| 476 | 
  946 | 
          if (vmod_path != NULL)  | 
| 477 | 
  946 | 
                  VSB_printf(vsb, " -p vmod_path=%s", vmod_path);  | 
| 478 | 
  1892 | 
          VSB_printf(vsb, " %s", VSB_data(v->args));  | 
| 479 | 
  1892 | 
          if (macro_isdef(NULL, "varnishd_args_append")) { | 
| 480 | 
  0 | 
                  VSB_putc(vsb, ' ');  | 
| 481 | 
  0 | 
                  macro_cat(v->vl, vsb, "varnishd_args_append", NULL);  | 
| 482 | 
  0 | 
          }  | 
| 483 | 
  947 | 
          AZ(VSB_finish(vsb));  | 
| 484 | 
  947 | 
          vtc_log(v->vl, 3, "CMD: %s", VSB_data(vsb));  | 
| 485 | 
  947 | 
          vsb1 = macro_expand(v->vl, VSB_data(vsb));  | 
| 486 | 
  947 | 
          AN(vsb1);  | 
| 487 | 
  947 | 
          VSB_destroy(&vsb);  | 
| 488 | 
  947 | 
          vsb = vsb1;  | 
| 489 | 
  947 | 
          vtc_log(v->vl, 3, "CMD: %s", VSB_data(vsb));  | 
| 490 | 
  947 | 
          AZ(pipe(&v->fds[0]));  | 
| 491 | 
  947 | 
          AZ(pipe(&v->fds[2]));  | 
| 492 | 
  947 | 
          v->pid = fork();  | 
| 493 | 
  947 | 
          assert(v->pid >= 0);  | 
| 494 | 
  1894 | 
          if (v->pid == 0) { | 
| 495 | 
  947 | 
                  AZ(dup2(v->fds[0], 0));  | 
| 496 | 
  947 | 
                  assert(dup2(v->fds[3], 1) == 1);  | 
| 497 | 
  947 | 
                  assert(dup2(1, 2) == 2);  | 
| 498 | 
  947 | 
                  closefd(&v->fds[0]);  | 
| 499 | 
  947 | 
                  closefd(&v->fds[1]);  | 
| 500 | 
  947 | 
                  closefd(&v->fds[2]);  | 
| 501 | 
  947 | 
                  closefd(&v->fds[3]);  | 
| 502 | 
  947 | 
                  VSUB_closefrom(STDERR_FILENO + 1);  | 
| 503 | 
  947 | 
                  AZ(execl("/bin/sh", "/bin/sh", "-c", VSB_data(vsb), (char*)0)); | 
| 504 | 
  0 | 
                  exit(1);  | 
| 505 | 
   | 
          } else { | 
| 506 | 
  947 | 
                  vtc_log(v->vl, 3, "PID: %ld", (long)v->pid);  | 
| 507 | 
  947 | 
                  macro_def(v->vl, v->name, "pid", "%ld", (long)v->pid);  | 
| 508 | 
  947 | 
                  macro_def(v->vl, v->name, "name", "%s", v->workdir);  | 
| 509 | 
   | 
          }  | 
| 510 | 
  947 | 
          closefd(&v->fds[0]);  | 
| 511 | 
  947 | 
          closefd(&v->fds[3]);  | 
| 512 | 
  947 | 
          v->fds[0] = v->fds[2];  | 
| 513 | 
  947 | 
          v->fds[2] = v->fds[3] = -1;  | 
| 514 | 
  947 | 
          VSB_destroy(&vsb);  | 
| 515 | 
  947 | 
          AZ(v->tp_started);  | 
| 516 | 
  947 | 
          v->tp_started = 1;  | 
| 517 | 
  947 | 
          PTOK(pthread_create(&v->tp, NULL, varnish_thread, v));  | 
| 518 | 
   | 
   | 
| 519 | 
   | 
          /* Wait for the varnish to call home */  | 
| 520 | 
  947 | 
          memset(fd, 0, sizeof fd);  | 
| 521 | 
  947 | 
          fd[0].fd = asock;  | 
| 522 | 
  947 | 
          fd[0].events = POLLIN;  | 
| 523 | 
  947 | 
          fd[1].fd = v->fds[1];  | 
| 524 | 
  947 | 
          fd[1].events = POLLIN;  | 
| 525 | 
  947 | 
          fd[2].fd = v->fds[2];  | 
| 526 | 
  947 | 
          fd[2].events = POLLIN;  | 
| 527 | 
  947 | 
          i = poll(fd, 2, (int)(vtc_maxdur * 1000 / 3));  | 
| 528 | 
  1894 | 
          vtc_log(v->vl, 4, "CLIPOLL %d 0x%x 0x%x 0x%x",  | 
| 529 | 
  947 | 
              i, fd[0].revents, fd[1].revents, fd[2].revents);  | 
| 530 | 
  947 | 
          if (i == 0)  | 
| 531 | 
  0 | 
                  varnish_fatal(v, "FAIL timeout waiting for CLI connection");  | 
| 532 | 
  0 | 
          if (fd[1].revents & POLLHUP)  | 
| 533 | 
  0 | 
                  varnish_fatal(v, "FAIL debug pipe closed");  | 
| 534 | 
  0 | 
          if (!(fd[0].revents & POLLIN))  | 
| 535 | 
  0 | 
                  varnish_fatal(v, "FAIL CLI connection wait failure");  | 
| 536 | 
  947 | 
          nfd = accept(asock, NULL, NULL);  | 
| 537 | 
  947 | 
          closefd(&asock);  | 
| 538 | 
  947 | 
          if (nfd < 0)  | 
| 539 | 
  0 | 
                  varnish_fatal(v, "FAIL no CLI connection accepted");  | 
| 540 | 
   | 
   | 
| 541 | 
  947 | 
          v->cli_fd = nfd;  | 
| 542 | 
   | 
   | 
| 543 | 
  947 | 
          vtc_log(v->vl, 3, "CLI connection fd = %d", v->cli_fd);  | 
| 544 | 
  947 | 
          assert(v->cli_fd >= 0);  | 
| 545 | 
   | 
   | 
| 546 | 
   | 
          /* Receive the banner or auth response */  | 
| 547 | 
  947 | 
          u = varnish_ask_cli(v, NULL, &r);  | 
| 548 | 
  947 | 
          if (vtc_error)  | 
| 549 | 
  0 | 
                  return;  | 
| 550 | 
  947 | 
          if (u != CLIS_AUTH)  | 
| 551 | 
  0 | 
                  varnish_fatal(v, "CLI auth demand expected: %u %s", u, r);  | 
| 552 | 
   | 
   | 
| 553 | 
  947 | 
          bprintf(lbuf, "%s/_.secret", v->workdir);  | 
| 554 | 
  947 | 
          nfd = open(lbuf, O_RDONLY);  | 
| 555 | 
  947 | 
          assert(nfd >= 0);  | 
| 556 | 
   | 
   | 
| 557 | 
  947 | 
          assert(sizeof lbuf >= CLI_AUTH_RESPONSE_LEN + 7);  | 
| 558 | 
  947 | 
          bstrcpy(lbuf, "auth ");  | 
| 559 | 
  947 | 
          VCLI_AuthResponse(nfd, r, lbuf + 5);  | 
| 560 | 
  947 | 
          closefd(&nfd);  | 
| 561 | 
  947 | 
          free(r);  | 
| 562 | 
  947 | 
          r = NULL;  | 
| 563 | 
  947 | 
          strcat(lbuf, "\n");  | 
| 564 | 
   | 
   | 
| 565 | 
  947 | 
          u = varnish_ask_cli(v, lbuf, &r);  | 
| 566 | 
  947 | 
          if (vtc_error)  | 
| 567 | 
  0 | 
                  return;  | 
| 568 | 
  947 | 
          if (u != CLIS_OK)  | 
| 569 | 
  0 | 
                  varnish_fatal(v, "CLI auth command failed: %u %s", u, r);  | 
| 570 | 
  947 | 
          free(r);  | 
| 571 | 
   | 
   | 
| 572 | 
  947 | 
          v->vsm_vsc = VSM_New();  | 
| 573 | 
  947 | 
          AN(v->vsm_vsc);  | 
| 574 | 
  947 | 
          v->vsc = VSC_New();  | 
| 575 | 
  947 | 
          AN(v->vsc);  | 
| 576 | 
  947 | 
          assert(VSM_Arg(v->vsm_vsc, 'n', v->workdir) > 0);  | 
| 577 | 
  947 | 
          AZ(VSM_Attach(v->vsm_vsc, -1));  | 
| 578 | 
   | 
   | 
| 579 | 
  947 | 
          v->vsm_vsl = VSM_New();  | 
| 580 | 
  947 | 
          assert(VSM_Arg(v->vsm_vsl, 'n', v->workdir) > 0);  | 
| 581 | 
  947 | 
          AZ(VSM_Attach(v->vsm_vsl, -1));  | 
| 582 | 
   | 
   | 
| 583 | 
  947 | 
          PTOK(pthread_create(&v->tp_vsl, NULL, varnishlog_thread, v));  | 
| 584 | 
  947 | 
  }  | 
| 585 | 
   | 
   | 
| 586 | 
   | 
  #define VARNISH_LAUNCH(v)                               \  | 
| 587 | 
   | 
          do {                                            \ | 
| 588 | 
   | 
                  CHECK_OBJ_NOTNULL(v, VARNISH_MAGIC);    \  | 
| 589 | 
   | 
                  if (v->cli_fd < 0)                      \  | 
| 590 | 
   | 
                          varnish_launch(v);              \  | 
| 591 | 
   | 
                  if (vtc_error)                          \  | 
| 592 | 
   | 
                          return;                         \  | 
| 593 | 
   | 
          } while (0)  | 
| 594 | 
   | 
   | 
| 595 | 
   | 
  /**********************************************************************  | 
| 596 | 
   | 
   * Start a Varnish  | 
| 597 | 
   | 
   */  | 
| 598 | 
   | 
   | 
| 599 | 
   | 
  static void  | 
| 600 | 
  939 | 
  varnish_listen(const struct varnish *v, char *la)  | 
| 601 | 
   | 
  { | 
| 602 | 
   | 
          const char *a, *p, *n, *n2;  | 
| 603 | 
   | 
          char m[64], s[256];  | 
| 604 | 
   | 
          unsigned first;  | 
| 605 | 
   | 
   | 
| 606 | 
  939 | 
          n2 = "";  | 
| 607 | 
  939 | 
          first = 1;  | 
| 608 | 
   | 
   | 
| 609 | 
  1884 | 
          while (*la != '\0') { | 
| 610 | 
  945 | 
                  n = la;  | 
| 611 | 
  945 | 
                  la = strchr(la, ' ');  | 
| 612 | 
  945 | 
                  AN(la);  | 
| 613 | 
  945 | 
                  *la = '\0';  | 
| 614 | 
  945 | 
                  a = ++la;  | 
| 615 | 
  945 | 
                  la = strchr(la, ' ');  | 
| 616 | 
  945 | 
                  AN(la);  | 
| 617 | 
  945 | 
                  *la = '\0';  | 
| 618 | 
  945 | 
                  p = ++la;  | 
| 619 | 
  945 | 
                  la = strchr(la, '\n');  | 
| 620 | 
  945 | 
                  AN(la);  | 
| 621 | 
  945 | 
                  *la = '\0';  | 
| 622 | 
  945 | 
                  la++;  | 
| 623 | 
   | 
   | 
| 624 | 
  945 | 
                  AN(*n);  | 
| 625 | 
  945 | 
                  AN(*a);  | 
| 626 | 
  945 | 
                  AN(*p);  | 
| 627 | 
   | 
   | 
| 628 | 
  945 | 
                  if (*p == '-') { | 
| 629 | 
  27 | 
                          bprintf(s, "%s", a);  | 
| 630 | 
  27 | 
                          a = "0.0.0.0";  | 
| 631 | 
  27 | 
                          p = "0";  | 
| 632 | 
  945 | 
                  } else if (strchr(a, ':')) { | 
| 633 | 
  1 | 
                          bprintf(s, "[%s]:%s", a, p);  | 
| 634 | 
  1 | 
                  } else { | 
| 635 | 
  917 | 
                          bprintf(s, "%s:%s", a, p);  | 
| 636 | 
   | 
                  }  | 
| 637 | 
   | 
   | 
| 638 | 
  945 | 
                  if (first) { | 
| 639 | 
  939 | 
                          vtc_log(v->vl, 2, "Listen on %s %s", a, p);  | 
| 640 | 
  939 | 
                          macro_def(v->vl, v->name, "addr", "%s", a);  | 
| 641 | 
  939 | 
                          macro_def(v->vl, v->name, "port", "%s", p);  | 
| 642 | 
  939 | 
                          macro_def(v->vl, v->name, "sock", "%s", s);  | 
| 643 | 
  939 | 
                          first = 0;  | 
| 644 | 
  939 | 
                  }  | 
| 645 | 
   | 
   | 
| 646 | 
  945 | 
                  if (!strcmp(n, n2))  | 
| 647 | 
  1 | 
                          continue;  | 
| 648 | 
   | 
   | 
| 649 | 
  944 | 
                  bprintf(m, "%s_addr", n);  | 
| 650 | 
  944 | 
                  macro_def(v->vl, v->name, m, "%s", a);  | 
| 651 | 
  944 | 
                  bprintf(m, "%s_port", n);  | 
| 652 | 
  944 | 
                  macro_def(v->vl, v->name, m, "%s", p);  | 
| 653 | 
  944 | 
                  bprintf(m, "%s_sock", n);  | 
| 654 | 
  944 | 
                  macro_def(v->vl, v->name, m, "%s", s);  | 
| 655 | 
  944 | 
                  n2 = n;  | 
| 656 | 
   | 
          }  | 
| 657 | 
  939 | 
  }  | 
| 658 | 
   | 
   | 
| 659 | 
   | 
  static void  | 
| 660 | 
  939 | 
  varnish_start(struct varnish *v)  | 
| 661 | 
   | 
  { | 
| 662 | 
   | 
          enum VCLI_status_e u;  | 
| 663 | 
  939 | 
          char *resp = NULL;  | 
| 664 | 
   | 
   | 
| 665 | 
  939 | 
          VARNISH_LAUNCH(v);  | 
| 666 | 
  939 | 
          vtc_log(v->vl, 2, "Start");  | 
| 667 | 
  939 | 
          u = varnish_ask_cli(v, "start", &resp);  | 
| 668 | 
  939 | 
          if (vtc_error)  | 
| 669 | 
  0 | 
                  return;  | 
| 670 | 
  939 | 
          if (u != CLIS_OK)  | 
| 671 | 
  0 | 
                  varnish_fatal(v, "CLI start command failed: %u %s", u, resp);  | 
| 672 | 
  939 | 
          wait_running(v);  | 
| 673 | 
  939 | 
          free(resp);  | 
| 674 | 
  939 | 
          resp = NULL;  | 
| 675 | 
  939 | 
          u = varnish_ask_cli(v, "debug.xid 1000", &resp);  | 
| 676 | 
  939 | 
          if (vtc_error)  | 
| 677 | 
  0 | 
                  return;  | 
| 678 | 
  939 | 
          if (u != CLIS_OK)  | 
| 679 | 
  0 | 
                  varnish_fatal(v, "CLI debug.xid command failed: %u %s",  | 
| 680 | 
   | 
                      u, resp);  | 
| 681 | 
  939 | 
          free(resp);  | 
| 682 | 
  939 | 
          resp = NULL;  | 
| 683 | 
  939 | 
          u = varnish_ask_cli(v, "debug.listen_address", &resp);  | 
| 684 | 
  939 | 
          if (vtc_error)  | 
| 685 | 
  0 | 
                  return;  | 
| 686 | 
  939 | 
          if (u != CLIS_OK)  | 
| 687 | 
  0 | 
                  varnish_fatal(v,  | 
| 688 | 
   | 
                      "CLI debug.listen_address command failed: %u %s", u, resp);  | 
| 689 | 
  939 | 
          varnish_listen(v, resp);  | 
| 690 | 
  939 | 
          free(resp);  | 
| 691 | 
   | 
          /* Wait for vsl logging to get underway */  | 
| 692 | 
  939 | 
          while (v->vsl_rec == 0)  | 
| 693 | 
  0 | 
                  VTIM_sleep(.1);  | 
| 694 | 
  939 | 
  }  | 
| 695 | 
   | 
   | 
| 696 | 
   | 
  /**********************************************************************  | 
| 697 | 
   | 
   * Stop a Varnish  | 
| 698 | 
   | 
   */  | 
| 699 | 
   | 
   | 
| 700 | 
   | 
  static void  | 
| 701 | 
  997 | 
  varnish_stop(struct varnish *v)  | 
| 702 | 
   | 
  { | 
| 703 | 
   | 
   | 
| 704 | 
  997 | 
          VARNISH_LAUNCH(v);  | 
| 705 | 
  997 | 
          vtc_log(v->vl, 2, "Stop");  | 
| 706 | 
  997 | 
          (void)varnish_ask_cli(v, "stop", NULL);  | 
| 707 | 
  997 | 
          wait_stopped(v);  | 
| 708 | 
  997 | 
  }  | 
| 709 | 
   | 
   | 
| 710 | 
   | 
  /**********************************************************************  | 
| 711 | 
   | 
   * Cleanup  | 
| 712 | 
   | 
   */  | 
| 713 | 
   | 
   | 
| 714 | 
   | 
  static void  | 
| 715 | 
  947 | 
  varnish_cleanup(struct varnish *v)  | 
| 716 | 
   | 
  { | 
| 717 | 
   | 
          void *p;  | 
| 718 | 
   | 
   | 
| 719 | 
   | 
          /* Close the CLI connection */  | 
| 720 | 
  947 | 
          closefd(&v->cli_fd);  | 
| 721 | 
   | 
   | 
| 722 | 
   | 
          /* Close the STDIN connection. */  | 
| 723 | 
  947 | 
          closefd(&v->fds[1]);  | 
| 724 | 
   | 
   | 
| 725 | 
   | 
          /* Wait until STDOUT+STDERR closes */  | 
| 726 | 
  947 | 
          AN(v->tp_started);  | 
| 727 | 
  947 | 
          PTOK(pthread_join(v->tp, &p));  | 
| 728 | 
  947 | 
          closefd(&v->fds[0]);  | 
| 729 | 
  947 | 
          v->tp_started = 0;  | 
| 730 | 
   | 
   | 
| 731 | 
   | 
          /* Pick up the VSL thread */  | 
| 732 | 
  947 | 
          PTOK(pthread_join(v->tp_vsl, &p));  | 
| 733 | 
   | 
   | 
| 734 | 
  947 | 
          vtc_wait4(v->vl, v->pid, v->expect_exit, 0, 0);  | 
| 735 | 
  947 | 
          v->pid = 0;  | 
| 736 | 
  947 | 
  }  | 
| 737 | 
   | 
   | 
| 738 | 
   | 
  /**********************************************************************  | 
| 739 | 
   | 
   * Wait for a Varnish  | 
| 740 | 
   | 
   */  | 
| 741 | 
   | 
   | 
| 742 | 
   | 
  static void  | 
| 743 | 
  944 | 
  varnish_wait(struct varnish *v)  | 
| 744 | 
   | 
  { | 
| 745 | 
  944 | 
          if (v->cli_fd < 0)  | 
| 746 | 
  0 | 
                  return;  | 
| 747 | 
   | 
   | 
| 748 | 
  944 | 
          vtc_log(v->vl, 2, "Wait");  | 
| 749 | 
   | 
   | 
| 750 | 
  944 | 
          if (!vtc_error) { | 
| 751 | 
   | 
                  /* Do a backend.list to log if child is still running */  | 
| 752 | 
  944 | 
                  (void)varnish_ask_cli(v, "backend.list", NULL);  | 
| 753 | 
  944 | 
          }  | 
| 754 | 
   | 
   | 
| 755 | 
   | 
          /* Then stop it */  | 
| 756 | 
  1888 | 
          varnish_stop(v);  | 
| 757 | 
   | 
   | 
| 758 | 
  1888 | 
          if (varnish_ask_cli(v, "panic.show", NULL) != CLIS_CANT)  | 
| 759 | 
  0 | 
                  varnish_fatal(v, "Unexpected panic");  | 
| 760 | 
   | 
   | 
| 761 | 
  944 | 
          varnish_cleanup(v);  | 
| 762 | 
  944 | 
  }  | 
| 763 | 
   | 
   | 
| 764 | 
   | 
   | 
| 765 | 
   | 
  /**********************************************************************  | 
| 766 | 
   | 
   * Ask a CLI JSON question  | 
| 767 | 
   | 
   */  | 
| 768 | 
   | 
   | 
| 769 | 
   | 
  static void  | 
| 770 | 
  38 | 
  varnish_cli_json(struct varnish *v, const char *cli)  | 
| 771 | 
   | 
  { | 
| 772 | 
   | 
          enum VCLI_status_e u;  | 
| 773 | 
  38 | 
          char *resp = NULL;  | 
| 774 | 
   | 
          const char *errptr;  | 
| 775 | 
   | 
          struct vjsn *vj;  | 
| 776 | 
   | 
   | 
| 777 | 
  38 | 
          VARNISH_LAUNCH(v);  | 
| 778 | 
  38 | 
          u = varnish_ask_cli(v, cli, &resp);  | 
| 779 | 
  38 | 
          vtc_log(v->vl, 2, "CLI %03u <%s>", u, cli);  | 
| 780 | 
  38 | 
          if (u != CLIS_OK)  | 
| 781 | 
  0 | 
                  varnish_fatal(v,  | 
| 782 | 
   | 
                      "FAIL CLI response %u expected %u", u, CLIS_OK);  | 
| 783 | 
  0 | 
          vj = vjsn_parse(resp, &errptr);  | 
| 784 | 
  0 | 
          if (vj == NULL)  | 
| 785 | 
  0 | 
                  varnish_fatal(v, "FAIL CLI, not good JSON: %s", errptr);  | 
| 786 | 
  38 | 
          vjsn_delete(&vj);  | 
| 787 | 
  38 | 
          free(resp);  | 
| 788 | 
  38 | 
  }  | 
| 789 | 
   | 
   | 
| 790 | 
   | 
  /**********************************************************************  | 
| 791 | 
   | 
   * Ask a CLI question  | 
| 792 | 
   | 
   */  | 
| 793 | 
   | 
   | 
| 794 | 
   | 
  static void  | 
| 795 | 
  1239 | 
  varnish_cli(struct varnish *v, const char *cli, unsigned exp, const char *re)  | 
| 796 | 
   | 
  { | 
| 797 | 
   | 
          enum VCLI_status_e u;  | 
| 798 | 
   | 
          struct vsb vsb[1];  | 
| 799 | 
  1239 | 
          vre_t *vre = NULL;  | 
| 800 | 
  1239 | 
          char *resp = NULL, errbuf[VRE_ERROR_LEN];  | 
| 801 | 
   | 
          int err, erroff;  | 
| 802 | 
   | 
   | 
| 803 | 
  1239 | 
          VARNISH_LAUNCH(v);  | 
| 804 | 
  1239 | 
          if (re != NULL) { | 
| 805 | 
  104 | 
                  vre = VRE_compile(re, 0, &err, &erroff, 1);  | 
| 806 | 
  104 | 
                  if (vre == NULL) { | 
| 807 | 
  0 | 
                          AN(VSB_init(vsb, errbuf, sizeof errbuf));  | 
| 808 | 
  0 | 
                          AZ(VRE_error(vsb, err));  | 
| 809 | 
  0 | 
                          AZ(VSB_finish(vsb));  | 
| 810 | 
  0 | 
                          VSB_fini(vsb);  | 
| 811 | 
  0 | 
                          varnish_fatal(v, "Illegal regexp: %s (@%d)",  | 
| 812 | 
   | 
                              errbuf, erroff);  | 
| 813 | 
  0 | 
                  }  | 
| 814 | 
  104 | 
          }  | 
| 815 | 
  1087 | 
          u = varnish_ask_cli(v, cli, &resp);  | 
| 816 | 
  1087 | 
          vtc_log(v->vl, 2, "CLI %03u <%s>", u, cli);  | 
| 817 | 
  1087 | 
          if (exp != 0 && exp != (unsigned)u)  | 
| 818 | 
  0 | 
                  varnish_fatal(v, "FAIL CLI response %u expected %u", u, exp);  | 
| 819 | 
  104 | 
          if (vre != NULL) { | 
| 820 | 
  104 | 
                  err = VRE_match(vre, resp, 0, 0, NULL);  | 
| 821 | 
  104 | 
                  if (err < 1) { | 
| 822 | 
  0 | 
                          AN(VSB_init(vsb, errbuf, sizeof errbuf));  | 
| 823 | 
  0 | 
                          AZ(VRE_error(vsb, err));  | 
| 824 | 
  0 | 
                          AZ(VSB_finish(vsb));  | 
| 825 | 
  0 | 
                          VSB_fini(vsb);  | 
| 826 | 
  0 | 
                          varnish_fatal(v, "Expect failed (%s)", errbuf);  | 
| 827 | 
  0 | 
                  }  | 
| 828 | 
  104 | 
                  VRE_free(&vre);  | 
| 829 | 
  104 | 
          }  | 
| 830 | 
  1239 | 
          free(resp);  | 
| 831 | 
  1239 | 
  }  | 
| 832 | 
   | 
   | 
| 833 | 
   | 
  /**********************************************************************  | 
| 834 | 
   | 
   * Load a VCL program  | 
| 835 | 
   | 
   */  | 
| 836 | 
   | 
   | 
| 837 | 
   | 
  static void  | 
| 838 | 
  598 | 
  varnish_vcl(struct varnish *v, const char *vcl, int fail, char **resp)  | 
| 839 | 
   | 
  { | 
| 840 | 
   | 
          struct vsb *vsb;  | 
| 841 | 
   | 
          enum VCLI_status_e u;  | 
| 842 | 
   | 
   | 
| 843 | 
  598 | 
          VARNISH_LAUNCH(v);  | 
| 844 | 
  598 | 
          vsb = VSB_new_auto();  | 
| 845 | 
  598 | 
          AN(vsb);  | 
| 846 | 
   | 
   | 
| 847 | 
  1196 | 
          VSB_printf(vsb, "vcl.inline vcl%d << %s\nvcl %.1f;\n%s\n%s\n",  | 
| 848 | 
  598 | 
              ++v->vcl_nbr, NONSENSE, v->syntax, vcl, NONSENSE);  | 
| 849 | 
  598 | 
          AZ(VSB_finish(vsb));  | 
| 850 | 
   | 
   | 
| 851 | 
  598 | 
          u = varnish_ask_cli(v, VSB_data(vsb), resp);  | 
| 852 | 
  598 | 
          if (u == CLIS_OK) { | 
| 853 | 
  267 | 
                  VSB_clear(vsb);  | 
| 854 | 
  267 | 
                  VSB_printf(vsb, "vcl.use vcl%d", v->vcl_nbr);  | 
| 855 | 
  267 | 
                  AZ(VSB_finish(vsb));  | 
| 856 | 
  267 | 
                  u = varnish_ask_cli(v, VSB_data(vsb), NULL);  | 
| 857 | 
  267 | 
          }  | 
| 858 | 
  267 | 
          if (u == CLIS_OK && fail) { | 
| 859 | 
  0 | 
                  VSB_destroy(&vsb);  | 
| 860 | 
  0 | 
                  varnish_fatal(v, "VCL compilation succeeded expected failure");  | 
| 861 | 
  331 | 
          } else if (u != CLIS_OK && !fail) { | 
| 862 | 
  0 | 
                  VSB_destroy(&vsb);  | 
| 863 | 
  0 | 
                  varnish_fatal(v, "VCL compilation failed expected success");  | 
| 864 | 
  331 | 
          } else if (fail)  | 
| 865 | 
  331 | 
                  vtc_log(v->vl, 2, "VCL compilation failed (as expected)");  | 
| 866 | 
  598 | 
          VSB_destroy(&vsb);  | 
| 867 | 
  598 | 
  }  | 
| 868 | 
   | 
   | 
| 869 | 
   | 
  /**********************************************************************  | 
| 870 | 
   | 
   * Load a VCL program prefixed by backend decls for our servers  | 
| 871 | 
   | 
   */  | 
| 872 | 
   | 
   | 
| 873 | 
   | 
  static void  | 
| 874 | 
  876 | 
  varnish_vclbackend(struct varnish *v, const char *vcl)  | 
| 875 | 
   | 
  { | 
| 876 | 
   | 
          struct vsb *vsb, *vsb2;  | 
| 877 | 
   | 
          enum VCLI_status_e u;  | 
| 878 | 
   | 
   | 
| 879 | 
  876 | 
          VARNISH_LAUNCH(v);  | 
| 880 | 
  876 | 
          vsb = VSB_new_auto();  | 
| 881 | 
  876 | 
          AN(vsb);  | 
| 882 | 
   | 
   | 
| 883 | 
  876 | 
          vsb2 = VSB_new_auto();  | 
| 884 | 
  876 | 
          AN(vsb2);  | 
| 885 | 
   | 
   | 
| 886 | 
  876 | 
          VSB_printf(vsb2, "vcl %.1f;\n", v->syntax);  | 
| 887 | 
   | 
   | 
| 888 | 
  876 | 
          cmd_server_gen_vcl(vsb2);  | 
| 889 | 
   | 
   | 
| 890 | 
  876 | 
          AZ(VSB_finish(vsb2));  | 
| 891 | 
   | 
   | 
| 892 | 
  1752 | 
          VSB_printf(vsb, "vcl.inline vcl%d << %s\n%s\n%s\n%s\n",  | 
| 893 | 
  876 | 
              ++v->vcl_nbr, NONSENSE, VSB_data(vsb2), vcl, NONSENSE);  | 
| 894 | 
  876 | 
          AZ(VSB_finish(vsb));  | 
| 895 | 
   | 
   | 
| 896 | 
  876 | 
          u = varnish_ask_cli(v, VSB_data(vsb), NULL);  | 
| 897 | 
  876 | 
          if (u != CLIS_OK) { | 
| 898 | 
  0 | 
                  VSB_destroy(&vsb);  | 
| 899 | 
  0 | 
                  VSB_destroy(&vsb2);  | 
| 900 | 
  0 | 
                  varnish_fatal(v, "FAIL VCL does not compile");  | 
| 901 | 
  0 | 
          }  | 
| 902 | 
  876 | 
          VSB_clear(vsb);  | 
| 903 | 
  876 | 
          VSB_printf(vsb, "vcl.use vcl%d", v->vcl_nbr);  | 
| 904 | 
  876 | 
          AZ(VSB_finish(vsb));  | 
| 905 | 
  876 | 
          u = varnish_ask_cli(v, VSB_data(vsb), NULL);  | 
| 906 | 
  876 | 
          assert(u == CLIS_OK);  | 
| 907 | 
  876 | 
          VSB_destroy(&vsb);  | 
| 908 | 
  876 | 
          VSB_destroy(&vsb2);  | 
| 909 | 
  876 | 
  }  | 
| 910 | 
   | 
   | 
| 911 | 
   | 
  /**********************************************************************  | 
| 912 | 
   | 
   */  | 
| 913 | 
   | 
   | 
| 914 | 
   | 
  struct dump_priv { | 
| 915 | 
   | 
          const char *arg;  | 
| 916 | 
   | 
          const struct varnish *v;  | 
| 917 | 
   | 
  };  | 
| 918 | 
   | 
   | 
| 919 | 
   | 
  static int  | 
| 920 | 
  3682 | 
  do_stat_dump_cb(void *priv, const struct VSC_point * const pt)  | 
| 921 | 
   | 
  { | 
| 922 | 
   | 
          const struct varnish *v;  | 
| 923 | 
   | 
          struct dump_priv *dp;  | 
| 924 | 
   | 
          uint64_t u;  | 
| 925 | 
   | 
   | 
| 926 | 
  3682 | 
          if (pt == NULL)  | 
| 927 | 
  0 | 
                  return (0);  | 
| 928 | 
  3682 | 
          dp = priv;  | 
| 929 | 
  3682 | 
          v = dp->v;  | 
| 930 | 
   | 
   | 
| 931 | 
  3682 | 
          if (strcmp(pt->ctype, "uint64_t"))  | 
| 932 | 
  0 | 
                  return (0);  | 
| 933 | 
  3682 | 
          u = VSC_Value(pt);  | 
| 934 | 
   | 
   | 
| 935 | 
  3682 | 
          if (strcmp(dp->arg, "*")) { | 
| 936 | 
  3682 | 
                  if (fnmatch(dp->arg, pt->name, 0))  | 
| 937 | 
  3559 | 
                          return (0);  | 
| 938 | 
  123 | 
          }  | 
| 939 | 
   | 
   | 
| 940 | 
  123 | 
          vtc_log(v->vl, 4, "VSC %s %ju",  pt->name, (uintmax_t)u);  | 
| 941 | 
  123 | 
          return (0);  | 
| 942 | 
  3682 | 
  }  | 
| 943 | 
   | 
   | 
| 944 | 
   | 
  static void  | 
| 945 | 
  10 | 
  varnish_vsc(struct varnish *v, const char *arg)  | 
| 946 | 
   | 
  { | 
| 947 | 
   | 
          struct dump_priv dp;  | 
| 948 | 
   | 
   | 
| 949 | 
  10 | 
          VARNISH_LAUNCH(v);  | 
| 950 | 
  10 | 
          memset(&dp, 0, sizeof dp);  | 
| 951 | 
  10 | 
          dp.v = v;  | 
| 952 | 
  10 | 
          dp.arg = arg;  | 
| 953 | 
  10 | 
          (void)VSM_Status(v->vsm_vsc);  | 
| 954 | 
  10 | 
          (void)VSC_Iter(v->vsc, v->vsm_vsc, do_stat_dump_cb, &dp);  | 
| 955 | 
  10 | 
  }  | 
| 956 | 
   | 
   | 
| 957 | 
   | 
  /**********************************************************************  | 
| 958 | 
   | 
   * Check statistics  | 
| 959 | 
   | 
   */  | 
| 960 | 
   | 
   | 
| 961 | 
   | 
  struct stat_arg { | 
| 962 | 
   | 
          const char      *pattern;  | 
| 963 | 
   | 
          uintmax_t       val;  | 
| 964 | 
   | 
          unsigned        good;  | 
| 965 | 
   | 
  };  | 
| 966 | 
   | 
   | 
| 967 | 
   | 
  struct stat_priv { | 
| 968 | 
   | 
          struct stat_arg lhs;  | 
| 969 | 
   | 
          struct stat_arg rhs;  | 
| 970 | 
   | 
  };  | 
| 971 | 
   | 
   | 
| 972 | 
   | 
  static int  | 
| 973 | 
  257461 | 
  stat_match(const char *pattern, const char *name)  | 
| 974 | 
   | 
  { | 
| 975 | 
   | 
   | 
| 976 | 
  257461 | 
          if (strchr(pattern, '.') == NULL) { | 
| 977 | 
  48917 | 
                  if (fnmatch("MAIN.*", name, 0)) | 
| 978 | 
  3710 | 
                          return (FNM_NOMATCH);  | 
| 979 | 
  45207 | 
                  name += 5;  | 
| 980 | 
  45207 | 
          }  | 
| 981 | 
  253751 | 
          return (fnmatch(pattern, name, 0));  | 
| 982 | 
  257461 | 
  }  | 
| 983 | 
   | 
   | 
| 984 | 
   | 
  static int  | 
| 985 | 
  257316 | 
  do_expect_cb(void *priv, const struct VSC_point * const pt)  | 
| 986 | 
   | 
  { | 
| 987 | 
  257316 | 
          struct stat_priv *sp = priv;  | 
| 988 | 
   | 
   | 
| 989 | 
  257316 | 
          if (pt == NULL)  | 
| 990 | 
  0 | 
                  return (0);  | 
| 991 | 
   | 
   | 
| 992 | 
  257316 | 
          if (!sp->lhs.good && stat_match(sp->lhs.pattern, pt->name) == 0) { | 
| 993 | 
  955 | 
                  AZ(strcmp(pt->ctype, "uint64_t"));  | 
| 994 | 
  955 | 
                  AN(pt->ptr);  | 
| 995 | 
  955 | 
                  sp->lhs.val = VSC_Value(pt);  | 
| 996 | 
  955 | 
                  sp->lhs.good = 1;  | 
| 997 | 
  955 | 
          }  | 
| 998 | 
   | 
   | 
| 999 | 
  257316 | 
          if (sp->rhs.pattern == NULL) { | 
| 1000 | 
  257167 | 
                  sp->rhs.good = 1;  | 
| 1001 | 
  257316 | 
          } else if (!sp->rhs.good &&  | 
| 1002 | 
  149 | 
              stat_match(sp->rhs.pattern, pt->name) == 0) { | 
| 1003 | 
  4 | 
                  AZ(strcmp(pt->ctype, "uint64_t"));  | 
| 1004 | 
  4 | 
                  AN(pt->ptr);  | 
| 1005 | 
  4 | 
                  sp->rhs.val = VSC_Value(pt);  | 
| 1006 | 
  4 | 
                  sp->rhs.good = 1;  | 
| 1007 | 
  4 | 
          }  | 
| 1008 | 
   | 
   | 
| 1009 | 
  257316 | 
          return (sp->lhs.good && sp->rhs.good);  | 
| 1010 | 
  257316 | 
  }  | 
| 1011 | 
   | 
   | 
| 1012 | 
   | 
  /**********************************************************************  | 
| 1013 | 
   | 
   */  | 
| 1014 | 
   | 
   | 
| 1015 | 
   | 
  static void  | 
| 1016 | 
  857 | 
  varnish_expect(struct varnish *v, char * const *av)  | 
| 1017 | 
   | 
  { | 
| 1018 | 
   | 
          struct stat_priv sp;  | 
| 1019 | 
   | 
          int good, i, not;  | 
| 1020 | 
   | 
          uintmax_t u;  | 
| 1021 | 
   | 
          char *l, *p;  | 
| 1022 | 
   | 
   | 
| 1023 | 
  857 | 
          VARNISH_LAUNCH(v);  | 
| 1024 | 
  857 | 
          ZERO_OBJ(&sp, sizeof sp);  | 
| 1025 | 
  857 | 
          l = av[0];  | 
| 1026 | 
  857 | 
          not = (*l == '!');  | 
| 1027 | 
  857 | 
          if (not) { | 
| 1028 | 
  5 | 
                  l++;  | 
| 1029 | 
  5 | 
                  AZ(av[1]);  | 
| 1030 | 
  5 | 
          } else { | 
| 1031 | 
  852 | 
                  AN(av[1]);  | 
| 1032 | 
  852 | 
                  AN(av[2]);  | 
| 1033 | 
  852 | 
                  u = strtoumax(av[2], &p, 0);  | 
| 1034 | 
  852 | 
                  if (u != UINTMAX_MAX && *p == '\0')  | 
| 1035 | 
  848 | 
                          sp.rhs.val = u;  | 
| 1036 | 
   | 
                  else  | 
| 1037 | 
  4 | 
                          sp.rhs.pattern = av[2];  | 
| 1038 | 
   | 
          }  | 
| 1039 | 
   | 
   | 
| 1040 | 
  857 | 
          sp.lhs.pattern = l;  | 
| 1041 | 
   | 
   | 
| 1042 | 
  1210 | 
          for (i = 0; i < 50; i++, (void)usleep(100000)) { | 
| 1043 | 
  1205 | 
                  (void)VSM_Status(v->vsm_vsc);  | 
| 1044 | 
  1205 | 
                  sp.lhs.good = sp.rhs.good = 0;  | 
| 1045 | 
  1205 | 
                  good = VSC_Iter(v->vsc, v->vsm_vsc, do_expect_cb, &sp);  | 
| 1046 | 
  1205 | 
                  if (!good)  | 
| 1047 | 
  250 | 
                          good = -2;  | 
| 1048 | 
  1205 | 
                  if (good < 0)  | 
| 1049 | 
  250 | 
                          continue;  | 
| 1050 | 
   | 
   | 
| 1051 | 
  955 | 
                  if (not)  | 
| 1052 | 
  0 | 
                          varnish_fatal(v, "Found (not expected): %s", l);  | 
| 1053 | 
   | 
   | 
| 1054 | 
  837 | 
                  good = -1;  | 
| 1055 | 
  837 | 
                  if (!strcmp(av[1], "==")) good = (sp.lhs.val == sp.rhs.val);  | 
| 1056 | 
  1672 | 
                  if (!strcmp(av[1], "!=")) good = (sp.lhs.val != sp.rhs.val);  | 
| 1057 | 
  53 | 
                  if (!strcmp(av[1], ">" )) good = (sp.lhs.val >  sp.rhs.val);  | 
| 1058 | 
  72 | 
                  if (!strcmp(av[1], "<" )) good = (sp.lhs.val <  sp.rhs.val);  | 
| 1059 | 
  39 | 
                  if (!strcmp(av[1], ">=")) good = (sp.lhs.val >= sp.rhs.val);  | 
| 1060 | 
  58 | 
                  if (!strcmp(av[1], "<=")) good = (sp.lhs.val <= sp.rhs.val);  | 
| 1061 | 
  0 | 
                  if (good == -1)  | 
| 1062 | 
  0 | 
                          varnish_fatal(v, "comparison %s unknown", av[1]);  | 
| 1063 | 
  955 | 
                  if (good)  | 
| 1064 | 
  852 | 
                          break;  | 
| 1065 | 
  103 | 
          }  | 
| 1066 | 
  1704 | 
          if (good == -1) { | 
| 1067 | 
  0 | 
                  varnish_fatal(v, "VSM error: %s", VSM_Error(v->vsm_vsc));  | 
| 1068 | 
  0 | 
          }  | 
| 1069 | 
  5 | 
          if (good == -2) { | 
| 1070 | 
  5 | 
                  if (not) { | 
| 1071 | 
  5 | 
                          vtc_log(v->vl, 2, "not found (as expected): %s", l);  | 
| 1072 | 
  5 | 
                          return;  | 
| 1073 | 
   | 
                  }  | 
| 1074 | 
  0 | 
                  varnish_fatal(v, "stats field %s unknown",  | 
| 1075 | 
   | 
                      sp.lhs.good ? sp.rhs.pattern : sp.lhs.pattern);  | 
| 1076 | 
  0 | 
          }  | 
| 1077 | 
   | 
   | 
| 1078 | 
  852 | 
          if (good == 1) { | 
| 1079 | 
  1704 | 
                  vtc_log(v->vl, 2, "as expected: %s (%ju) %s %s (%ju)",  | 
| 1080 | 
  852 | 
                      av[0], sp.lhs.val, av[1], av[2], sp.rhs.val);  | 
| 1081 | 
  852 | 
          } else { | 
| 1082 | 
  0 | 
                  varnish_fatal(v, "Not true: %s (%ju) %s %s (%ju)",  | 
| 1083 | 
   | 
                      av[0], sp.lhs.val, av[1], av[2], sp.rhs.val);  | 
| 1084 | 
   | 
          }  | 
| 1085 | 
  857 | 
  }  | 
| 1086 | 
   | 
   | 
| 1087 | 
   | 
  static void  | 
| 1088 | 
  139 | 
  vsl_catchup(struct varnish *v)  | 
| 1089 | 
   | 
  { | 
| 1090 | 
   | 
          int vsl_idle;  | 
| 1091 | 
   | 
   | 
| 1092 | 
  139 | 
          VARNISH_LAUNCH(v);  | 
| 1093 | 
  139 | 
          vsl_idle = v->vsl_idle;  | 
| 1094 | 
  279 | 
          while (!vtc_error && vsl_idle == v->vsl_idle)  | 
| 1095 | 
  140 | 
                  VTIM_sleep(0.1);  | 
| 1096 | 
  139 | 
  }  | 
| 1097 | 
   | 
   | 
| 1098 | 
   | 
  /* SECTION: varnish varnish  | 
| 1099 | 
   | 
   *  | 
| 1100 | 
   | 
   * Define and interact with varnish instances.  | 
| 1101 | 
   | 
   *  | 
| 1102 | 
   | 
   * To define a Varnish server, you'll use this syntax::  | 
| 1103 | 
   | 
   *  | 
| 1104 | 
   | 
   *      varnish vNAME [-arg STRING] [-vcl STRING] [-vcl+backend STRING]  | 
| 1105 | 
   | 
   *              [-errvcl STRING STRING] [-jail STRING] [-proto PROXY]  | 
| 1106 | 
   | 
   *  | 
| 1107 | 
   | 
   * The first ``varnish vNAME`` invocation will start the varnishd master  | 
| 1108 | 
   | 
   * process in the background, waiting for the ``-start`` switch to actually  | 
| 1109 | 
   | 
   * start the child.  | 
| 1110 | 
   | 
   *  | 
| 1111 | 
   | 
   * Types used in the description below:  | 
| 1112 | 
   | 
   *  | 
| 1113 | 
   | 
   * PATTERN  | 
| 1114 | 
   | 
   *         is a 'glob' style pattern (ie: fnmatch(3)) as used in shell filename  | 
| 1115 | 
   | 
   *         expansion.  | 
| 1116 | 
   | 
   *  | 
| 1117 | 
   | 
   * Arguments:  | 
| 1118 | 
   | 
   *  | 
| 1119 | 
   | 
   * vNAME  | 
| 1120 | 
   | 
   *         Identify the Varnish server with a string, it must starts with 'v'.  | 
| 1121 | 
   | 
   *  | 
| 1122 | 
   | 
   * \-arg STRING  | 
| 1123 | 
   | 
   *         Pass an argument to varnishd, for example "-h simple_list".  | 
| 1124 | 
   | 
   *  | 
| 1125 | 
   | 
   *         If the ${varnishd_args_prepend} or ${varnishd_args_append} macros are | 
| 1126 | 
   | 
   *         defined, they are expanded and inserted before / appended to the  | 
| 1127 | 
   | 
   *         varnishd command line as constructed by varnishtest, before the  | 
| 1128 | 
   | 
   *         command line itself is expanded. This enables tweaks to the varnishd  | 
| 1129 | 
   | 
   *         command line without editing test cases. This macros can be defined  | 
| 1130 | 
   | 
   *         using the ``-D`` option for varnishtest.  | 
| 1131 | 
   | 
   *  | 
| 1132 | 
   | 
   * \-vcl STRING  | 
| 1133 | 
   | 
   *         Specify the VCL to load on this Varnish instance. You'll probably  | 
| 1134 | 
   | 
   *         want to use multi-lines strings for this ({...}). | 
| 1135 | 
   | 
   *  | 
| 1136 | 
   | 
   * \-vcl+backend STRING  | 
| 1137 | 
   | 
   *         Do the exact same thing as -vcl, but adds the definition block of  | 
| 1138 | 
   | 
   *         known backends (ie. already defined).  | 
| 1139 | 
   | 
   *  | 
| 1140 | 
   | 
   * \-errvcl STRING1 STRING2  | 
| 1141 | 
   | 
   *         Load STRING2 as VCL, expecting it to fail, and Varnish to send an  | 
| 1142 | 
   | 
   *         error string matching STRING1  | 
| 1143 | 
   | 
   *  | 
| 1144 | 
   | 
   * \-jail STRING  | 
| 1145 | 
   | 
   *         Look at ``man varnishd`` (-j) for more information.  | 
| 1146 | 
   | 
   *  | 
| 1147 | 
   | 
   * \-proto PROXY  | 
| 1148 | 
   | 
   *         Have Varnish use the proxy protocol. Note that PROXY here is the  | 
| 1149 | 
   | 
   *         actual string.  | 
| 1150 | 
   | 
   *  | 
| 1151 | 
   | 
   * You can decide to start the Varnish instance and/or wait for several events::  | 
| 1152 | 
   | 
   *  | 
| 1153 | 
   | 
   *         varnish vNAME [-start] [-wait] [-wait-running] [-wait-stopped]  | 
| 1154 | 
   | 
   *  | 
| 1155 | 
   | 
   * \-start  | 
| 1156 | 
   | 
   *         Start the child process.  | 
| 1157 | 
   | 
   *  | 
| 1158 | 
   | 
   *         Once successfully started, the following macros are available for  | 
| 1159 | 
   | 
   *         the default listen address: ``${vNAME_addr}``, ``${vNAME_port}`` | 
| 1160 | 
   | 
   *         and ``${vNAME_sock}``. Additional macros are available, including | 
| 1161 | 
   | 
   *         the listen address name for each address vNAME listens to, like for  | 
| 1162 | 
   | 
   *         example: ``${vNAME_a0_addr}``. | 
| 1163 | 
   | 
   *  | 
| 1164 | 
   | 
   * \-stop  | 
| 1165 | 
   | 
   *         Stop the child process.  | 
| 1166 | 
   | 
   *  | 
| 1167 | 
   | 
   * \-syntax  | 
| 1168 | 
   | 
   *         Set the VCL syntax level for this command (default: 4.1)  | 
| 1169 | 
   | 
   *  | 
| 1170 | 
   | 
   * \-wait  | 
| 1171 | 
   | 
   *         Wait for that instance to terminate.  | 
| 1172 | 
   | 
   *  | 
| 1173 | 
   | 
   * \-wait-running  | 
| 1174 | 
   | 
   *         Wait for the Varnish child process to be started.  | 
| 1175 | 
   | 
   *  | 
| 1176 | 
   | 
   * \-wait-stopped  | 
| 1177 | 
   | 
   *         Wait for the Varnish child process to stop.  | 
| 1178 | 
   | 
   *  | 
| 1179 | 
   | 
   * \-cleanup  | 
| 1180 | 
   | 
   *         Once Varnish is stopped, clean everything after it. This is only used  | 
| 1181 | 
   | 
   *         in very few tests and you should never need it.  | 
| 1182 | 
   | 
   *  | 
| 1183 | 
   | 
   * \-expectexit NUMBER  | 
| 1184 | 
   | 
   *         Expect varnishd to exit(3) with this value  | 
| 1185 | 
   | 
   *  | 
| 1186 | 
   | 
   * Once Varnish is started, you can talk to it (as you would through  | 
| 1187 | 
   | 
   * ``varnishadm``) with these additional switches::  | 
| 1188 | 
   | 
   *  | 
| 1189 | 
   | 
   *         varnish vNAME [-cli STRING] [-cliok STRING] [-clierr STRING]  | 
| 1190 | 
   | 
   *                       [-clijson STRING]  | 
| 1191 | 
   | 
   *  | 
| 1192 | 
   | 
   * \-cli STRING|-cliok STRING|-clierr STATUS STRING|-cliexpect REGEXP STRING  | 
| 1193 | 
   | 
   *         All four of these will send STRING to the CLI, the only difference  | 
| 1194 | 
   | 
   *         is what they expect the result to be. -cli doesn't expect  | 
| 1195 | 
   | 
   *         anything, -cliok expects 200, -clierr expects STATUS, and  | 
| 1196 | 
   | 
   *         -cliexpect expects the REGEXP to match the returned response.  | 
| 1197 | 
   | 
   *  | 
| 1198 | 
   | 
   * \-clijson STRING  | 
| 1199 | 
   | 
   *         Send STRING to the CLI, expect success (CLIS_OK/200) and check  | 
| 1200 | 
   | 
   *         that the response is parsable JSON.  | 
| 1201 | 
   | 
   *  | 
| 1202 | 
   | 
   * It is also possible to interact with its shared memory (as you would  | 
| 1203 | 
   | 
   * through tools like ``varnishstat``) with additional switches:  | 
| 1204 | 
   | 
   *  | 
| 1205 | 
   | 
   * \-expect \!PATTERN|PATTERN OP NUMBER|PATTERN OP PATTERN  | 
| 1206 | 
   | 
   *         Look into the VSM and make sure the first VSC counter identified by  | 
| 1207 | 
   | 
   *         PATTERN has a correct value. OP can be ==, >, >=, <, <=. For  | 
| 1208 | 
   | 
   *         example::  | 
| 1209 | 
   | 
   *  | 
| 1210 | 
   | 
   *                 varnish v1 -expect SM?.s1.g_space > 1000000  | 
| 1211 | 
   | 
   *                 varnish v1 -expect cache_hit >= cache_hit_grace  | 
| 1212 | 
   | 
   *  | 
| 1213 | 
   | 
   *         In the \! form the test fails if a counter matches PATTERN.  | 
| 1214 | 
   | 
   *  | 
| 1215 | 
   | 
   *         The ``MAIN.`` namespace can be omitted from PATTERN.  | 
| 1216 | 
   | 
   *  | 
| 1217 | 
   | 
   *         The test takes up to 5 seconds before timing out.  | 
| 1218 | 
   | 
   *  | 
| 1219 | 
   | 
   * \-vsc PATTERN  | 
| 1220 | 
   | 
   *         Dump VSC counters matching PATTERN.  | 
| 1221 | 
   | 
   *  | 
| 1222 | 
   | 
   * \-vsl_catchup  | 
| 1223 | 
   | 
   *         Wait until the logging thread has idled to make sure that all  | 
| 1224 | 
   | 
   *         the generated log is flushed  | 
| 1225 | 
   | 
   */  | 
| 1226 | 
   | 
   | 
| 1227 | 
   | 
  void  | 
| 1228 | 
  4882 | 
  cmd_varnish(CMD_ARGS)  | 
| 1229 | 
   | 
  { | 
| 1230 | 
   | 
          struct varnish *v, *v2;  | 
| 1231 | 
   | 
   | 
| 1232 | 
  4882 | 
          (void)priv;  | 
| 1233 | 
   | 
   | 
| 1234 | 
  4882 | 
          if (av == NULL) { | 
| 1235 | 
   | 
                  /* Reset and free */  | 
| 1236 | 
  1970 | 
                  VTAILQ_FOREACH_SAFE(v, &varnishes, list, v2) { | 
| 1237 | 
  947 | 
                          if (v->cli_fd >= 0)  | 
| 1238 | 
  937 | 
                                  varnish_wait(v);  | 
| 1239 | 
  947 | 
                          VTAILQ_REMOVE(&varnishes, v, list);  | 
| 1240 | 
  947 | 
                          varnish_delete(v);  | 
| 1241 | 
  947 | 
                  }  | 
| 1242 | 
  1023 | 
                  return;  | 
| 1243 | 
   | 
          }  | 
| 1244 | 
   | 
   | 
| 1245 | 
  3859 | 
          AZ(strcmp(av[0], "varnish"));  | 
| 1246 | 
  3859 | 
          av++;  | 
| 1247 | 
   | 
   | 
| 1248 | 
  3859 | 
          VTC_CHECK_NAME(vl, av[0], "Varnish", 'v');  | 
| 1249 | 
  3936 | 
          VTAILQ_FOREACH(v, &varnishes, list)  | 
| 1250 | 
  2989 | 
                  if (!strcmp(v->name, av[0]))  | 
| 1251 | 
  2912 | 
                          break;  | 
| 1252 | 
  4877 | 
          if (v == NULL)  | 
| 1253 | 
  947 | 
                  v = varnish_new(av[0]);  | 
| 1254 | 
  3859 | 
          av++;  | 
| 1255 | 
  3859 | 
          v->syntax = 4.1;  | 
| 1256 | 
   | 
   | 
| 1257 | 
  9013 | 
          for (; *av != NULL; av++) { | 
| 1258 | 
  5154 | 
                  if (vtc_error)  | 
| 1259 | 
  0 | 
                          break;  | 
| 1260 | 
  5154 | 
                  if (!strcmp(*av, "-arg")) { | 
| 1261 | 
  305 | 
                          AN(av[1]);  | 
| 1262 | 
  305 | 
                          AZ(v->pid);  | 
| 1263 | 
  305 | 
                          VSB_cat(v->args, " ");  | 
| 1264 | 
  305 | 
                          VSB_cat(v->args, av[1]);  | 
| 1265 | 
  305 | 
                          if (av[1][0] == '-' && av[1][1] == 'a')  | 
| 1266 | 
  29 | 
                                  v->has_a_arg = 1;  | 
| 1267 | 
  305 | 
                          av++;  | 
| 1268 | 
  305 | 
                          continue;  | 
| 1269 | 
   | 
                  }  | 
| 1270 | 
  4849 | 
                  if (!strcmp(*av, "-cleanup")) { | 
| 1271 | 
  3 | 
                          AZ(av[1]);  | 
| 1272 | 
  3 | 
                          varnish_cleanup(v);  | 
| 1273 | 
  3 | 
                          continue;  | 
| 1274 | 
   | 
                  }  | 
| 1275 | 
  4846 | 
                  if (!strcmp(*av, "-cli")) { | 
| 1276 | 
  48 | 
                          AN(av[1]);  | 
| 1277 | 
  48 | 
                          varnish_cli(v, av[1], 0, NULL);  | 
| 1278 | 
  48 | 
                          av++;  | 
| 1279 | 
  48 | 
                          continue;  | 
| 1280 | 
   | 
                  }  | 
| 1281 | 
  4798 | 
                  if (!strcmp(*av, "-clierr")) { | 
| 1282 | 
  122 | 
                          AN(av[1]);  | 
| 1283 | 
  122 | 
                          AN(av[2]);  | 
| 1284 | 
  122 | 
                          varnish_cli(v, av[2], atoi(av[1]), NULL);  | 
| 1285 | 
  122 | 
                          av += 2;  | 
| 1286 | 
  122 | 
                          continue;  | 
| 1287 | 
   | 
                  }  | 
| 1288 | 
  4676 | 
                  if (!strcmp(*av, "-cliexpect")) { | 
| 1289 | 
  104 | 
                          AN(av[1]);  | 
| 1290 | 
  104 | 
                          AN(av[2]);  | 
| 1291 | 
  104 | 
                          varnish_cli(v, av[2], 0, av[1]);  | 
| 1292 | 
  104 | 
                          av += 2;  | 
| 1293 | 
  104 | 
                          continue;  | 
| 1294 | 
   | 
                  }  | 
| 1295 | 
  4572 | 
                  if (!strcmp(*av, "-clijson")) { | 
| 1296 | 
  38 | 
                          AN(av[1]);  | 
| 1297 | 
  38 | 
                          varnish_cli_json(v, av[1]);  | 
| 1298 | 
  38 | 
                          av++;  | 
| 1299 | 
  38 | 
                          continue;  | 
| 1300 | 
   | 
                  }  | 
| 1301 | 
  4534 | 
                  if (!strcmp(*av, "-cliok")) { | 
| 1302 | 
  965 | 
                          AN(av[1]);  | 
| 1303 | 
  965 | 
                          varnish_cli(v, av[1], (unsigned)CLIS_OK, NULL);  | 
| 1304 | 
  965 | 
                          av++;  | 
| 1305 | 
  965 | 
                          continue;  | 
| 1306 | 
   | 
                  }  | 
| 1307 | 
  3569 | 
                  if (!strcmp(*av, "-errvcl")) { | 
| 1308 | 
  331 | 
                          char *r = NULL;  | 
| 1309 | 
  331 | 
                          AN(av[1]);  | 
| 1310 | 
  331 | 
                          AN(av[2]);  | 
| 1311 | 
  331 | 
                          varnish_vcl(v, av[2], 1, &r);  | 
| 1312 | 
  331 | 
                          if (strstr(r, av[1]) == NULL)  | 
| 1313 | 
  0 | 
                                  varnish_fatal(v,  | 
| 1314 | 
   | 
                                      "Did not find expected string: (\"%s\")",  | 
| 1315 | 
   | 
                                      av[1]);  | 
| 1316 | 
   | 
                          else  | 
| 1317 | 
  662 | 
                                  vtc_log(v->vl, 3,  | 
| 1318 | 
   | 
                                      "Found expected string: (\"%s\")",  | 
| 1319 | 
  331 | 
                                      av[1]);  | 
| 1320 | 
  331 | 
                          free(r);  | 
| 1321 | 
  331 | 
                          av += 2;  | 
| 1322 | 
  331 | 
                          continue;  | 
| 1323 | 
   | 
                  }  | 
| 1324 | 
  3238 | 
                  if (!strcmp(*av, "-expect")) { | 
| 1325 | 
  857 | 
                          av++;  | 
| 1326 | 
  857 | 
                          varnish_expect(v, av);  | 
| 1327 | 
  857 | 
                          av += 2;  | 
| 1328 | 
  857 | 
                          continue;  | 
| 1329 | 
   | 
                  }  | 
| 1330 | 
  2381 | 
                  if (!strcmp(*av, "-expectexit")) { | 
| 1331 | 
  10 | 
                          v->expect_exit = strtoul(av[1], NULL, 0);  | 
| 1332 | 
  10 | 
                          av++;  | 
| 1333 | 
  10 | 
                          continue;  | 
| 1334 | 
   | 
                  }  | 
| 1335 | 
  2371 | 
                  if (!strcmp(*av, "-jail")) { | 
| 1336 | 
  4 | 
                          AN(av[1]);  | 
| 1337 | 
  4 | 
                          AZ(v->pid);  | 
| 1338 | 
  4 | 
                          REPLACE(v->jail, av[1]);  | 
| 1339 | 
  4 | 
                          av++;  | 
| 1340 | 
  4 | 
                          continue;  | 
| 1341 | 
   | 
                  }  | 
| 1342 | 
  2367 | 
                  if (!strcmp(*av, "-proto")) { | 
| 1343 | 
  15 | 
                          AN(av[1]);  | 
| 1344 | 
  15 | 
                          AZ(v->pid);  | 
| 1345 | 
  15 | 
                          REPLACE(v->proto, av[1]);  | 
| 1346 | 
  15 | 
                          av++;  | 
| 1347 | 
  15 | 
                          continue;  | 
| 1348 | 
   | 
                  }  | 
| 1349 | 
  2352 | 
                  if (!strcmp(*av, "-start")) { | 
| 1350 | 
  939 | 
                          varnish_start(v);  | 
| 1351 | 
  939 | 
                          continue;  | 
| 1352 | 
   | 
                  }  | 
| 1353 | 
  1413 | 
                  if (!strcmp(*av, "-stop")) { | 
| 1354 | 
  53 | 
                          varnish_stop(v);  | 
| 1355 | 
  53 | 
                          continue;  | 
| 1356 | 
   | 
                  }  | 
| 1357 | 
  1360 | 
                  if (!strcmp(*av, "-syntax")) { | 
| 1358 | 
  55 | 
                          AN(av[1]);  | 
| 1359 | 
  55 | 
                          v->syntax = strtod(av[1], NULL);  | 
| 1360 | 
  55 | 
                          av++;  | 
| 1361 | 
  55 | 
                          continue;  | 
| 1362 | 
   | 
                  }  | 
| 1363 | 
  1305 | 
                  if (!strcmp(*av, "-vcl")) { | 
| 1364 | 
  267 | 
                          AN(av[1]);  | 
| 1365 | 
  267 | 
                          varnish_vcl(v, av[1], 0, NULL);  | 
| 1366 | 
  267 | 
                          av++;  | 
| 1367 | 
  267 | 
                          continue;  | 
| 1368 | 
   | 
                  }  | 
| 1369 | 
  1038 | 
                  if (!strcmp(*av, "-vcl+backend")) { | 
| 1370 | 
  876 | 
                          AN(av[1]);  | 
| 1371 | 
  876 | 
                          varnish_vclbackend(v, av[1]);  | 
| 1372 | 
  876 | 
                          av++;  | 
| 1373 | 
  876 | 
                          continue;  | 
| 1374 | 
   | 
                  }  | 
| 1375 | 
  162 | 
                  if (!strcmp(*av, "-vsc")) { | 
| 1376 | 
  10 | 
                          AN(av[1]);  | 
| 1377 | 
  10 | 
                          varnish_vsc(v, av[1]);  | 
| 1378 | 
  10 | 
                          av++;  | 
| 1379 | 
  10 | 
                          continue;  | 
| 1380 | 
   | 
                  }  | 
| 1381 | 
  152 | 
                  if (!strcmp(*av, "-wait-stopped")) { | 
| 1382 | 
  3 | 
                          wait_stopped(v);  | 
| 1383 | 
  3 | 
                          continue;  | 
| 1384 | 
   | 
                  }  | 
| 1385 | 
  149 | 
                  if (!strcmp(*av, "-wait-running")) { | 
| 1386 | 
  3 | 
                          wait_running(v);  | 
| 1387 | 
  3 | 
                          continue;  | 
| 1388 | 
   | 
                  }  | 
| 1389 | 
  146 | 
                  if (!strcmp(*av, "-wait")) { | 
| 1390 | 
  7 | 
                          varnish_wait(v);  | 
| 1391 | 
  7 | 
                          continue;  | 
| 1392 | 
   | 
                  }  | 
| 1393 | 
  139 | 
                  if (!strcmp(*av, "-vsl_catchup")) { | 
| 1394 | 
  139 | 
                          vsl_catchup(v);  | 
| 1395 | 
  139 | 
                          continue;  | 
| 1396 | 
   | 
                  }  | 
| 1397 | 
  0 | 
                  varnish_fatal(v, "Unknown varnish argument: %s", *av);  | 
| 1398 | 
  0 | 
          }  | 
| 1399 | 
  4882 | 
  }  | 
| 1400 | 
   | 
   | 
| 1401 | 
   | 
  #endif /* VTEST_WITH_VTC_VARNISH */  |