varnish-cache/bin/varnishncsa/varnishncsa.c
0
/*-
1
 * Copyright (c) 2006 Verdens Gang AS
2
 * Copyright (c) 2006-2016 Varnish Software AS
3
 * All rights reserved.
4
 *
5
 * Author: Anders Berg <andersb@vgnett.no>
6
 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
7
 * Author: Tollef Fog Heen <tfheen@varnish-software.com>
8
 * Author: Martin Blix Grydeland <mbgrydeland@varnish-software.com>
9
 *
10
 * SPDX-License-Identifier: BSD-2-Clause
11
 *
12
 * Redistribution and use in source and binary forms, with or without
13
 * modification, are permitted provided that the following conditions
14
 * are met:
15
 * 1. Redistributions of source code must retain the above copyright
16
 *    notice, this list of conditions and the following disclaimer.
17
 * 2. Redistributions in binary form must reproduce the above copyright
18
 *    notice, this list of conditions and the following disclaimer in the
19
 *    documentation and/or other materials provided with the distribution.
20
 *
21
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
25
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31
 * SUCH DAMAGE.
32
 *
33
 * Obtain log data from the shared memory log, order it by session ID, and
34
 * display it in Apache / NCSA combined log format.
35
 *
36
 * See doc/sphinx/reference/varnishncsa.rst for the supported format
37
 * specifiers.
38
 *
39
 */
40
41
#include "config.h"
42
43
#include <stdlib.h>
44
#include <stdio.h>
45
#include <unistd.h>
46
#include <string.h>
47
#include <signal.h>
48
#include <stdarg.h>
49
#include <inttypes.h>
50
#include <limits.h>
51
#include <ctype.h>
52
#include <time.h>
53
#include <math.h>
54
55
#define VOPT_DEFINITION
56
#define VOPT_INC "varnishncsa_options.h"
57
58
#include "vdef.h"
59
60
#include "vapi/vsl.h"
61
#include "vapi/voptget.h"
62
#include "vas.h"
63
#include "venc.h"
64
#include "vsb.h"
65
#include "vut.h"
66
#include "vqueue.h"
67
#include "miniobj.h"
68
69
#define TIME_FMT "[%d/%b/%Y:%T %z]"
70
#define FORMAT "%h %l %u %t \"%r\" %s %b \"%{Referer}i\" \"%{User-agent}i\""
71
72
static struct VUT *vut;
73
74
struct format;
75
76
enum e_frag {
77
        F_H,                    /* %H Proto */
78
        F_U,                    /* %U URL path */
79
        F_q,                    /* %q Query string */
80
        F_b,                    /* %b Body bytes sent */
81
        F_h,                    /* %h Host name / IP Address */
82
        F_m,                    /* %m Method */
83
        F_s,                    /* %s Status */
84
        F_I,                    /* %I Bytes received */
85
        F_O,                    /* %O Bytes sent */
86
        F_tstart,               /* Time start */
87
        F_tend,                 /* Time end */
88
        F_ttfb,                 /* %{Varnish:time_firstbyte}x */
89
        F_host,                 /* Host header */
90
        F_auth,                 /* Authorization header */
91
        F__MAX,
92
};
93
94
enum format_policy {
95
        FMTPOL_INTERNAL = 1,
96
        FMTPOL_REQ,
97
        FMTPOL_RESP,
98
        FMTPOL_FIRST,
99
        FMTPOL_LAST
100
};
101
102
struct fragment {
103
        uint64_t                gen;
104
        const char              *b, *e;
105
};
106
107
typedef int format_f(const struct format *format);
108
109
struct format {
110
        unsigned                magic;
111
#define FORMAT_MAGIC            0xC3119CDA
112
113
        char                    time_type;
114
        VTAILQ_ENTRY(format)    list;
115
        format_f                *func;
116
        struct fragment         *frag;
117
        char                    *string;
118
        const char *const       *strptr;
119
        char                    *time_fmt;
120
        int64_t                 *int64;
121
};
122
123
struct watch {
124
        unsigned                magic;
125
#define WATCH_MAGIC             0xA7D4005C
126
127
        VTAILQ_ENTRY(watch)     list;
128
        char                    *key;
129
        int                     keylen;
130
        struct fragment         frag;
131
        enum format_policy      match;
132
};
133
VTAILQ_HEAD(watch_head, watch);
134
135
struct vsl_watch {
136
        unsigned                magic;
137
#define VSL_WATCH_MAGIC         0xE3E27D23
138
139
        VTAILQ_ENTRY(vsl_watch) list;
140
        enum VSL_tag_e          tag;
141
        int                     idx;
142
        char                    *prefix;
143
        int                     prefixlen;
144
        struct fragment         frag;
145
};
146
VTAILQ_HEAD(vsl_watch_head, vsl_watch);
147
148
static struct ctx {
149
        /* Options */
150
        int                     a_opt;
151
        char                    *w_arg;
152
153
        FILE                    *fo;
154
        struct vsb              *vsb;
155
        uint64_t                gen;
156
        VTAILQ_HEAD(,format)    format;
157
        int                     quote_how;
158
        char                    *missing_string;
159
        char                    *missing_int;
160
161
        /* State */
162
        struct watch_head       watch_vcl_log;
163
        struct watch_head       watch_reqhdr; /* also bereqhdr */
164
        struct watch_head       watch_resphdr; /* also beresphdr */
165
        struct vsl_watch_head   watch_vsl;
166
        struct fragment         frag[F__MAX];
167
        const char              *hitmiss;
168
        const char              *handling;
169
        const char              *side;
170
        int64_t                 vxid;
171
        int                     recv_compl;
172
} CTX;
173
174
static void parse_format(const char *format);
175
176
static unsigned
177 7505
frag_needed(const struct fragment *frag, enum format_policy fp)
178
{
179
        unsigned is_first, want_first, want_frag;
180
181 7505
        is_first = CTX.gen != frag->gen;
182
183 7505
        switch (fp) {
184
        case FMTPOL_LAST:
185 100
                want_frag = 1;
186 100
                want_first = 0;
187 100
                break;
188
        case FMTPOL_FIRST:
189
        case FMTPOL_INTERNAL:
190 3795
                want_first = 1;
191 3795
                want_frag = 1;
192 3795
                break;
193
        case FMTPOL_REQ:
194 3070
                want_first = *CTX.side == 'c';
195 3070
                want_frag = !CTX.recv_compl;
196 3070
                break;
197
        case FMTPOL_RESP:
198 540
                want_first = *CTX.side == 'b';
199 540
                want_frag = (*CTX.side == 'c') || !CTX.recv_compl;
200 540
                break;
201
        default:
202 0
                WRONG("Invalid format policy");
203 0
        }
204
205 7505
        if (!want_frag)
206 290
                return (0);
207 7215
        if (want_first && !is_first)
208 250
                return (0);
209 6965
        return (1);
210 7505
}
211
212
static void
213 25
openout(int append)
214
{
215
216 25
        AN(CTX.w_arg);
217 25
        if (!strcmp(CTX.w_arg, "-"))
218 0
                CTX.fo = stdout;
219
        else
220 25
                CTX.fo = fopen(CTX.w_arg, append ? "a" : "w");
221 25
        if (CTX.fo == NULL)
222 10
                VUT_Error(vut, 1, "Can't open output file (%s)",
223 5
                    strerror(errno));
224 20
}
225
226
static int v_matchproto_(VUT_cb_f)
227 5
rotateout(struct VUT *v)
228
{
229
230 5
        assert(v == vut);
231 5
        AN(CTX.w_arg);
232 5
        AN(CTX.fo);
233 5
        (void)fclose(CTX.fo);
234 5
        openout(1);
235 5
        AN(CTX.fo);
236 5
        return (0);
237
}
238
239
static int v_matchproto_(VUT_cb_f)
240 4245
flushout(struct VUT *v)
241
{
242
243 4245
        assert(v == vut);
244 4245
        AN(CTX.fo);
245 4245
        if (fflush(CTX.fo))
246 0
                return (-5);
247 4245
        return (0);
248 4245
}
249
250
static inline int
251 1270
vsb_fcat(struct vsb *vsb, const struct fragment *f, const char *dflt)
252
{
253 1270
        if (f->gen == CTX.gen) {
254 1250
                assert(f->b <= f->e);
255 1250
                VSB_quote(vsb, f->b, f->e - f->b, CTX.quote_how);
256 1270
        } else if (dflt)
257 20
                VSB_quote(vsb, dflt, -1, CTX.quote_how);
258
        else
259 0
                return (-1);
260 1270
        return (0);
261 1270
}
262
263
static int v_matchproto_(format_f)
264 1585
format_string(const struct format *format)
265
{
266
267 1585
        CHECK_OBJ_NOTNULL(format, FORMAT_MAGIC);
268 1585
        AN(format->string);
269 1585
        AZ(VSB_cat(CTX.vsb, format->string));
270 1585
        return (1);
271
}
272
273
static int v_matchproto_(format_f)
274 185
format_strptr(const struct format *format)
275
{
276
277 185
        CHECK_OBJ_NOTNULL(format, FORMAT_MAGIC);
278 185
        AN(format->strptr);
279 185
        AN(*format->strptr);
280 185
        AZ(VSB_cat(CTX.vsb, *format->strptr));
281 185
        return (1);
282
}
283
284
static int v_matchproto_(format_f)
285 105
format_int64(const struct format *format)
286
{
287
288 105
        CHECK_OBJ_NOTNULL(format, FORMAT_MAGIC);
289 105
        VSB_printf(CTX.vsb, "%jd", (intmax_t)*format->int64);
290 105
        return (1);
291
}
292
293
static int v_matchproto_(format_f)
294 1145
format_fragment(const struct format *format)
295
{
296
297 1145
        CHECK_OBJ_NOTNULL(format, FORMAT_MAGIC);
298 1145
        AN(format->frag);
299
300 1145
        if (format->frag->gen != CTX.gen) {
301 240
                if (format->string == NULL)
302 0
                        return (-1);
303 240
                VSB_quote(CTX.vsb, format->string, -1, CTX.quote_how);
304 240
                return (0);
305
        }
306 905
        AZ(vsb_fcat(CTX.vsb, format->frag, NULL));
307 905
        return (1);
308 1145
}
309
310
static int v_matchproto_(format_f)
311 300
format_time(const struct format *format)
312
{
313
        double t_start, t_end, d;
314
        char *p;
315
        char buf[64];
316
        time_t t;
317
        intmax_t l;
318
        struct tm tm;
319
320 300
        CHECK_OBJ_NOTNULL(format, FORMAT_MAGIC);
321 300
        if (CTX.frag[F_tstart].gen == CTX.gen) {
322 300
                t_start = strtod(CTX.frag[F_tstart].b, &p);
323 300
                if (p != CTX.frag[F_tstart].e)
324 0
                        t_start = NAN;
325 300
        } else
326 0
                t_start = NAN;
327 300
        if (isnan(t_start)) {
328
                /* Missing t_start is a no go */
329 0
                if (format->string == NULL)
330 0
                        return (-1);
331 0
                AZ(VSB_cat(CTX.vsb, format->string));
332 0
                return (0);
333
        }
334
335
        /* Missing t_end defaults to t_start */
336 300
        if (CTX.frag[F_tend].gen == CTX.gen) {
337 295
                t_end = strtod(CTX.frag[F_tend].b, &p);
338 295
                if (p != CTX.frag[F_tend].e)
339 0
                        t_end = t_start;
340 295
        } else
341 5
                t_end = t_start;
342
343 300
        AN(format->time_fmt);
344
345 300
        switch (format->time_type) {
346
        case 't':
347 150
                t = (intmax_t)floor(t_start);
348 150
                (void)localtime_r(&t, &tm);
349 150
                AN(strftime(buf, sizeof buf, format->time_fmt, &tm));
350 150
                AZ(VSB_cat(CTX.vsb, buf));
351 150
                return (1);
352
        case '3':
353 15
                l = (intmax_t)(modf(t_start, &d) * 1e3);
354 15
                break;
355
        case '6':
356 15
                l = (intmax_t)(modf(t_start, &d) * 1e6);
357 15
                break;
358
        case 'S':
359 15
                l = (intmax_t)t_start;
360 15
                break;
361
        case 'M':
362 15
                l = (intmax_t)(t_start * 1e3);
363 15
                break;
364
        case 'U':
365 15
                l = (intmax_t)(t_start * 1e6);
366 15
                break;
367
        case 's':
368 30
                l = (intmax_t)(t_end - t_start);
369 30
                break;
370
        case 'm':
371 15
                l = (intmax_t)((t_end - t_start) * 1e3);
372 15
                break;
373
        case 'u':
374 30
                l = (intmax_t)((t_end - t_start) * 1e6);
375 30
                break;
376
        default:
377 0
                WRONG("Time format specifier");
378 0
        }
379
380
#ifdef __FreeBSD__
381 150
        assert(fmtcheck(format->time_fmt, "%jd") == format->time_fmt);
382
#endif
383 150
        AZ(VSB_printf(CTX.vsb, format->time_fmt, l));
384
385 150
        return (1);
386 300
}
387
388
static int v_matchproto_(format_f)
389 75
format_requestline(const struct format *format)
390
{
391
392 75
        (void)format;
393 75
        AZ(vsb_fcat(CTX.vsb, &CTX.frag[F_m], "-"));
394 75
        AZ(VSB_putc(CTX.vsb, ' '));
395 75
        if (CTX.frag[F_host].gen == CTX.gen) {
396 65
                if (strncmp(CTX.frag[F_host].b, "http://", 7))
397 65
                        AZ(VSB_cat(CTX.vsb, "http://"));
398 65
                AZ(vsb_fcat(CTX.vsb, &CTX.frag[F_host], NULL));
399 65
        } else
400 10
                AZ(VSB_cat(CTX.vsb, "http://localhost"));
401 75
        AZ(vsb_fcat(CTX.vsb, &CTX.frag[F_U], ""));
402 75
        AZ(vsb_fcat(CTX.vsb, &CTX.frag[F_q], ""));
403 75
        AZ(VSB_putc(CTX.vsb, ' '));
404 75
        AZ(vsb_fcat(CTX.vsb, &CTX.frag[F_H], "HTTP/1.0"));
405 75
        return (1);
406
}
407
408
static int v_matchproto_(format_f)
409 90
format_auth(const struct format *format)
410
{
411 90
        struct vsb *vsb = VSB_new_auto();
412 90
        AN(vsb);
413
        char *q;
414
415 90
        if (CTX.frag[F_auth].gen != CTX.gen ||
416 40
            VENC_Decode_Base64(vsb, CTX.frag[F_auth].b, CTX.frag[F_auth].e)) {
417 50
                VSB_destroy(&vsb);
418 50
                if (format->string == NULL)
419 0
                        return (-1);
420 50
                VSB_quote(CTX.vsb, format->string, -1, CTX.quote_how);
421 50
                return (0);
422
        }
423 40
        AZ(VSB_finish(vsb));
424 40
        q = strchr(VSB_data(vsb), ':');
425 40
        if (q != NULL)
426 40
                *q = '\0';
427 40
        VSB_quote(CTX.vsb, VSB_data(vsb), -1, CTX.quote_how);
428 40
        VSB_destroy(&vsb);
429 40
        return (1);
430 90
}
431
432
static int
433 500
print(void)
434
{
435
        const struct format *f;
436 500
        int i, r = 1;
437
438 500
        VSB_clear(CTX.vsb);
439 3985
        VTAILQ_FOREACH(f, &CTX.format, list) {
440 3485
                CHECK_OBJ_NOTNULL(f, FORMAT_MAGIC);
441 3485
                i = (f->func)(f);
442 3485
                AZ(VSB_error(CTX.vsb));
443 3485
                if (r > i)
444 130
                        r = i;
445 3485
        }
446 500
        AZ(VSB_putc(CTX.vsb, '\n'));
447 500
        AZ(VSB_finish(CTX.vsb));
448 500
        if (r >= 0) {
449 500
                i = fwrite(VSB_data(CTX.vsb), 1, VSB_len(CTX.vsb), CTX.fo);
450 500
                if (i != VSB_len(CTX.vsb))
451 0
                        return (-5);
452 500
        }
453 500
        return (0);
454 500
}
455
456
static void
457 1190
addf_string(const char *str)
458
{
459
        struct format *f;
460
461 1190
        AN(str);
462 1190
        ALLOC_OBJ(f, FORMAT_MAGIC);
463 1190
        AN(f);
464 1190
        f->func = format_string;
465 1190
        f->string = strdup(str);
466 1190
        AN(f->string);
467 1190
        VTAILQ_INSERT_TAIL(&CTX.format, f, list);
468 1190
}
469
470
static void
471 40
addf_strptr(const char *const *strptr)
472
{
473
        struct format *f;
474
475 40
        AN(strptr);
476 40
        ALLOC_OBJ(f, FORMAT_MAGIC);
477 40
        AN(f);
478 40
        f->func = format_strptr;
479 40
        f->strptr = strptr;
480 40
        VTAILQ_INSERT_TAIL(&CTX.format, f, list);
481 40
}
482
483
static void
484 495
addf_fragment(struct fragment *frag, const char *str)
485
{
486
        struct format *f;
487
488 495
        AN(frag);
489 495
        ALLOC_OBJ(f, FORMAT_MAGIC);
490 495
        AN(f);
491 495
        f->func = format_fragment;
492 495
        f->frag = frag;
493 495
        if (str != NULL) {
494 495
                f->string = strdup(str);
495 495
                AN(f->string);
496 495
        }
497 495
        VTAILQ_INSERT_TAIL(&CTX.format, f, list);
498 495
}
499
500
static void
501 25
addf_int64(int64_t *i)
502
{
503
        struct format *f;
504
505 25
        AN(i);
506 25
        ALLOC_OBJ(f, FORMAT_MAGIC);
507 25
        AN(f);
508 25
        f->func = format_int64;
509 25
        f->int64 = i;
510 25
        VTAILQ_INSERT_TAIL(&CTX.format, f, list);
511 25
}
512
513
static void
514 160
addf_time(char type, const char *fmt)
515
{
516
        struct format *f;
517
518 160
        ALLOC_OBJ(f, FORMAT_MAGIC);
519 160
        AN(f);
520 160
        AN(fmt);
521 160
        f->func = format_time;
522 160
        f->time_type = type;
523 160
        f->time_fmt = strdup(fmt);
524
525 160
        if (f->time_type == 'T') {
526 25
                if (!strcmp(fmt, "s"))
527 10
                        f->time_type = 's';
528 15
                else if (!strcmp(fmt, "ms"))
529 5
                        f->time_type = 'm';
530 10
                else if (!strcmp(fmt, "us"))
531 10
                        f->time_type = 'u';
532
                else
533 0
                        VUT_Error(vut, 1, "Unknown specifier: %%{%s}T",
534 0
                            fmt);
535 25
                REPLACE(f->time_fmt, "%jd");
536 160
        } else if (f->time_type == 't') {
537 135
                if (!strcmp(fmt, "sec")) {
538 5
                        f->time_type = 'S';
539 5
                        REPLACE(f->time_fmt, "%jd");
540 135
                } else if (!strncmp(fmt, "msec", 4)) {
541 15
                        fmt += 4;
542 15
                        if (!strcmp(fmt, "_frac")) {
543 5
                                f->time_type = '3';
544 5
                                REPLACE(f->time_fmt, "%03jd");
545 15
                        } else if (*fmt == '\0') {
546 5
                                f->time_type = 'M';
547 5
                                REPLACE(f->time_fmt, "%jd");
548 5
                        }
549 130
                } else if (!strncmp(fmt, "usec", 4)) {
550 15
                        fmt += 4;
551 15
                        if (!strcmp(fmt, "_frac")) {
552 5
                                f->time_type = '6';
553 5
                                REPLACE(f->time_fmt, "%06jd");
554 15
                        } else if (*fmt == '\0') {
555 5
                                f->time_type = 'U';
556 5
                                REPLACE(f->time_fmt, "%jd");
557 5
                        }
558 15
                }
559 135
        }
560
561 160
        AN(f->time_fmt);
562 160
        VTAILQ_INSERT_TAIL(&CTX.format, f, list);
563 160
}
564
565
static void
566 95
addf_requestline(void)
567
{
568
        struct format *f;
569
570 95
        ALLOC_OBJ(f, FORMAT_MAGIC);
571 95
        AN(f);
572 95
        f->func = format_requestline;
573 95
        VTAILQ_INSERT_TAIL(&CTX.format, f, list);
574 95
}
575
576
static void
577 10
addf_vcl_log(const char *key)
578
{
579
        struct watch *w;
580
        struct format *f;
581
582 10
        AN(key);
583 10
        ALLOC_OBJ(w, WATCH_MAGIC);
584 10
        AN(w);
585 10
        w->keylen = asprintf(&w->key, "%s:", key);
586 10
        assert(w->keylen > 0);
587 10
        VTAILQ_INSERT_TAIL(&CTX.watch_vcl_log, w, list);
588
589 10
        ALLOC_OBJ(f, FORMAT_MAGIC);
590 10
        AN(f);
591 10
        f->func = format_fragment;
592 10
        f->frag = &w->frag;
593 10
        f->string = strdup("");
594 10
        AN(f->string);
595 10
        VTAILQ_INSERT_TAIL(&CTX.format, f, list);
596 10
}
597
598
static void
599 355
addf_hdr(struct watch_head *head, char *key)
600
{
601
        struct watch *w;
602
        struct format *f;
603
        char *match;
604
605 355
        AN(head);
606 355
        AN(key);
607 355
        ALLOC_OBJ(w, WATCH_MAGIC);
608 355
        AN(w);
609
610 355
        match = strchr(key, ':');
611 355
        if (match != NULL) {
612 80
                match++;
613 80
                if (!strncmp(match, "first", 5))
614 40
                        w->match = FMTPOL_FIRST;
615 40
                else if (!strncmp(match, "last", 4))
616 40
                        w->match = FMTPOL_LAST;
617
                else
618 0
                        VUT_Error(vut, 1, "Unknown match rule :%s", match);
619 80
                match[-1] = '\0';
620 80
        }
621
622 355
        w->keylen = asprintf(&w->key, "%s:", key);
623 355
        assert(w->keylen > 0);
624 355
        VTAILQ_INSERT_TAIL(head, w, list);
625
626 355
        ALLOC_OBJ(f, FORMAT_MAGIC);
627 355
        AN(f);
628 355
        f->func = format_fragment;
629 355
        f->frag = &w->frag;
630 355
        f->string = strdup(CTX.missing_string);
631 355
        AN(f->string);
632 355
        VTAILQ_INSERT_TAIL(&CTX.format, f, list);
633 355
}
634
635
static void
636 65
addf_vsl(enum VSL_tag_e tag, long i, const char *prefix)
637
{
638
        struct vsl_watch *w;
639
640 65
        ALLOC_OBJ(w, VSL_WATCH_MAGIC);
641 65
        AN(w);
642 65
        if (VSL_tagflags[tag] && CTX.quote_how != VSB_QUOTE_JSON)
643 10
                VUT_Error(vut, 1, "Tag %s can contain control characters",
644 5
                    VSL_tags[tag]);
645 60
        w->tag = tag;
646 60
        assert(i <= INT_MAX);
647 60
        w->idx = i;
648 60
        if (prefix != NULL) {
649 15
                w->prefixlen = asprintf(&w->prefix, "%s:", prefix);
650 15
                assert(w->prefixlen > 0);
651 15
        }
652 60
        VTAILQ_INSERT_TAIL(&CTX.watch_vsl, w, list);
653 60
        addf_fragment(&w->frag, CTX.missing_string);
654 60
}
655
656
static void
657 100
addf_auth(void)
658
{
659
        struct format *f;
660
661 100
        ALLOC_OBJ(f, FORMAT_MAGIC);
662 100
        AN(f);
663 100
        f->func = format_auth;
664 100
        f->string = strdup("-");
665 100
        AN(f->string);
666 100
        VTAILQ_INSERT_TAIL(&CTX.format, f, list);
667 100
}
668
669
static void
670 200
parse_x_format(char *buf)
671
{
672
        char *e, *r, *s;
673
        long lval;
674
        int slt;
675
676 200
        if (!strcmp(buf, "Varnish:time_firstbyte")) {
677 5
                addf_fragment(&CTX.frag[F_ttfb], CTX.missing_int);
678 5
                return;
679
        }
680 195
        if (!strcmp(buf, "Varnish:hitmiss")) {
681 5
                addf_strptr(&CTX.hitmiss);
682 5
                return;
683
        }
684 190
        if (!strcmp(buf, "Varnish:handling")) {
685 10
                addf_strptr(&CTX.handling);
686 10
                return;
687
        }
688 180
        if (!strcmp(buf, "Varnish:side")) {
689 25
                addf_strptr(&CTX.side);
690 25
                return;
691
        }
692 155
        if (!strcmp(buf, "Varnish:vxid")) {
693 25
                addf_int64(&CTX.vxid);
694 25
                return;
695
        }
696 130
        if (!strncmp(buf, "VCL_Log:", 8)) {
697 10
                addf_vcl_log(buf + 8);
698 10
                return;
699
        }
700 120
        if (!strncmp(buf, "VSL:", 4)) {
701 110
                buf += 4;
702 110
                e = buf;
703 1030
                while (*e != '\0')
704 920
                        e++;
705 110
                if (e == buf)
706 5
                        VUT_Error(vut, 1, "Missing tag in VSL:");
707 105
                if (e[-1] == ']') {
708 60
                        r = e - 1;
709 250
                        while (r > buf && *r != '[')
710 190
                                r--;
711 60
                        if (r == buf || r[1] == ']')
712 5
                                VUT_Error(vut, 1, "Syntax error: VSL:%s", buf);
713 55
                        e[-1] = '\0';
714 55
                        lval = strtol(r + 1, &s, 10);
715 55
                        if (s != e - 1)
716 5
                                VUT_Error(vut, 1, "Syntax error: VSL:%s]", buf);
717 50
                        if (lval <= 0 || lval > 255) {
718 20
                                VUT_Error(vut, 1,
719
                                    "Syntax error. Field specifier must be"
720
                                    " between 1 and 255: %s]",
721 10
                                    buf);
722
                        }
723 40
                        *r = '\0';
724 40
                } else
725 45
                        lval = 0;
726 85
                r = buf;
727 715
                while (r < e && *r != ':')
728 630
                        r++;
729 85
                if (r != e) {
730 15
                        slt = VSL_Name2Tag(buf, r - buf);
731 15
                        r++;
732 15
                } else {
733 70
                        slt = VSL_Name2Tag(buf, -1);
734 70
                        r = NULL;
735
                }
736 85
                if (slt == -2)
737 5
                        VUT_Error(vut, 1, "Tag not unique: %s", buf);
738 80
                if (slt == -1)
739 15
                        VUT_Error(vut, 1, "Unknown log tag: %s", buf);
740 65
                assert(slt >= 0);
741
742 65
                addf_vsl((enum VSL_tag_e)slt, lval, r);
743 65
                return;
744
        }
745 10
        if (!strcmp(buf, "Varnish:default_format")) {
746 5
                parse_format(FORMAT);
747 5
                return;
748
        }
749 5
        VUT_Error(vut, 1, "Unknown formatting extension: %s", buf);
750 150
}
751
752
static void
753 380
parse_format(const char *format)
754
{
755
        const char *p, *q;
756
        struct vsb *vsb;
757
        char buf[256];
758
        int b;
759
760 380
        if (format == NULL)
761 75
                format = FORMAT;
762
763 380
        vsb = VSB_new_auto();
764 380
        AN(vsb);
765
766 3645
        for (p = format; *p != '\0'; p++) {
767
768
                /* Allow the most essential escape sequences in format */
769 3280
                if (*p == '\\' && p[1] != '\0') {
770 5
                        if (*++p == 't')
771 0
                                AZ(VSB_putc(vsb, '\t'));
772 5
                        else if (*p == 'n')
773 5
                                AZ(VSB_putc(vsb, '\n'));
774
                        else
775 0
                                AZ(VSB_putc(vsb, *p));
776 5
                        continue;
777
                }
778
779 3275
                if (*p != '%') {
780 1835
                        AZ(VSB_putc(vsb, *p));
781 1835
                        continue;
782
                }
783
784 1440
                if (VSB_len(vsb) > 0) {
785 1085
                        AZ(VSB_finish(vsb));
786 1085
                        addf_string(VSB_data(vsb));
787 1085
                        VSB_clear(vsb);
788 1085
                }
789
790 1440
                p++;
791 1440
                switch (*p) {
792
                case 'b':       /* Body bytes sent */
793 100
                        addf_fragment(&CTX.frag[F_b], CTX.missing_int);
794 100
                        break;
795
                case 'D':       /* Float request time */
796 5
                        addf_time('T', "us");
797 5
                        break;
798
                case 'h':       /* Client host name / IP Address */
799 100
                        addf_fragment(&CTX.frag[F_h], CTX.missing_string);
800 100
                        break;
801
                case 'H':       /* Protocol */
802 15
                        addf_fragment(&CTX.frag[F_H], "HTTP/1.0");
803 15
                        break;
804
                case 'I':       /* Bytes received */
805 5
                        addf_fragment(&CTX.frag[F_I], CTX.missing_int);
806 5
                        break;
807
                case 'l':       /* Client user ID (identd) always '-' */
808 85
                        AZ(VSB_putc(vsb, '-'));
809 85
                        break;
810
                case 'm':       /* Method */
811 15
                        addf_fragment(&CTX.frag[F_m], CTX.missing_string);
812 15
                        break;
813
                case 'O':       /* Bytes sent */
814 5
                        addf_fragment(&CTX.frag[F_O], CTX.missing_int);
815 5
                        break;
816
                case 'q':       /* Query string */
817 15
                        addf_fragment(&CTX.frag[F_q], "");
818 15
                        break;
819
                case 'r':       /* Request line */
820 95
                        addf_requestline();
821 95
                        break;
822
                case 's':       /* Status code */
823 155
                        addf_fragment(&CTX.frag[F_s], CTX.missing_int);
824 155
                        break;
825
                case 't':       /* strftime */
826 85
                        addf_time(*p, TIME_FMT);
827 85
                        break;
828
                case 'T':       /* Int request time */
829 5
                        addf_time(*p, "s");
830 5
                        break;
831
                case 'u':       /* Remote user from auth */
832 100
                        addf_auth();
833 100
                        break;
834
                case 'U':       /* URL */
835 20
                        addf_fragment(&CTX.frag[F_U], CTX.missing_string);
836 20
                        break;
837
                case '{':
838 630
                        p++;
839 630
                        q = p;
840 630
                        b = 1;
841 6630
                        while (*q) {
842 6625
                                if (*q == '{')
843 20
                                        b++;
844 6605
                                else if (*q == '}')
845 645
                                        if (--b == 0)
846 625
                                                break;
847 6000
                                q++;
848
                        }
849 630
                        if (b > 0)
850 10
                                VUT_Error(vut, 1, "Unmatched bracket at: %s",
851 5
                                    p - 2);
852 625
                        assert((unsigned)(q - p) < sizeof buf - 1);
853 625
                        strncpy(buf, p, q - p);
854 625
                        buf[q - p] = '\0';
855 625
                        q++;
856 625
                        switch (*q) {
857
                        case 'i':
858 285
                                addf_hdr(&CTX.watch_reqhdr, buf);
859 285
                                break;
860
                        case 'o':
861 70
                                addf_hdr(&CTX.watch_resphdr, buf);
862 70
                                break;
863
                        case 't':
864 50
                                addf_time(*q, buf);
865 50
                                break;
866
                        case 'T':
867 15
                                addf_time(*q, buf);
868 15
                                break;
869
                        case 'x':
870 200
                                parse_x_format(buf);
871 200
                                break;
872
                        default:
873 10
                                VUT_Error(vut, 1,
874
                                    "Unknown format specifier at: %s",
875 5
                                    p - 2);
876
                        }
877 620
                        p = q;
878 620
                        break;
879
                default:
880 10
                        VUT_Error(vut, 1, "Unknown format specifier at: %s",
881 5
                            p - 1);
882
                }
883 1425
        }
884
885 365
        if (VSB_len(vsb) > 0) {
886
                /* Add any remaining static */
887 105
                AZ(VSB_finish(vsb));
888 105
                addf_string(VSB_data(vsb));
889 105
                VSB_clear(vsb);
890 105
        }
891
892 365
        VSB_destroy(&vsb);
893 365
}
894
895
static int
896 18720
isprefix(const char *prefix, size_t len, const char *b,
897
    const char *e, const char **next)
898
{
899 18720
        assert(len > 0);
900 18720
        if (e < b + len || strncasecmp(b, prefix, len))
901 15950
                return (0);
902 2770
        b += len;
903 2770
        if (next) {
904 5365
                while (b < e && *b == ' ')
905 2595
                        b++;
906 2770
                *next = b;
907 2770
        }
908 2770
        return (1);
909 18720
}
910
911
static void
912 2710
frag_fields(enum format_policy fp, const char *b, const char *e, ...)
913
{
914
        va_list ap;
915
        const char *p, *q;
916
        int n, field;
917
        struct fragment *frag;
918
919 2710
        AN(b);
920 2710
        AN(e);
921 2710
        va_start(ap, e);
922
923 2710
        n = 0;
924 6370
        while (1) {
925 6385
                field = va_arg(ap, int);
926 6385
                frag = va_arg(ap, struct fragment *);
927 6385
                if (field == 0) {
928 2710
                        AZ(frag);
929 2710
                        break;
930
                }
931 3675
                p = q = NULL;
932 9770
                while (n < field) {
933 9465
                        while (b < e && isspace(*b))
934 3370
                                b++;
935 6095
                        p = b;
936 52205
                        while (b < e && !isspace(*b))
937 46110
                                b++;
938 6095
                        q = b;
939 6095
                        n++;
940
                }
941 3675
                assert(p != NULL && q != NULL);
942 3675
                if (p >= e || q <= p)
943 15
                        continue;
944 3660
                if (frag_needed(frag, fp)) {
945
                        /* We only grab the same matching field once */
946 3565
                        frag->gen = CTX.gen;
947 3565
                        frag->b = p;
948 3565
                        frag->e = q;
949 3565
                }
950
        }
951 2710
        va_end(ap);
952 2710
}
953
954
static void
955 3845
frag_line(enum format_policy fp, const char *b, const char *e,
956
    struct fragment *f)
957
{
958
959 3845
        if (!frag_needed(f, fp)) {
960
                /* We only grab the same matching record once */
961 445
                return;
962
        }
963
964 3400
        if (e == NULL)
965 20
                e = b + strlen(b);
966
967
        /* Skip leading space */
968 3400
        while (b < e && isspace(*b))
969 0
                ++b;
970
971
        /* Skip trailing space */
972 3400
        while (e > b && isspace(e[-1]))
973 0
                --e;
974
975 3400
        f->gen = CTX.gen;
976 3400
        f->b = b;
977 3400
        f->e = e;
978 3845
}
979
980
static void
981 5730
process_hdr(enum format_policy fp, const struct watch_head *head, const char *b,
982
    const char *e, int unset)
983
{
984
        struct watch *w;
985
        const char *p;
986
987 8740
        VTAILQ_FOREACH(w, head, list) {
988 3010
                CHECK_OBJ_NOTNULL(w, WATCH_MAGIC);
989 3010
                if (!isprefix(w->key, w->keylen, b, e, &p))
990 2575
                        continue;
991
992 435
                if (w->match) {
993 200
                        assert(w->match == FMTPOL_FIRST ||
994
                            w->match == FMTPOL_LAST);
995 200
                        fp = w->match;
996 200
                }
997
998 435
                if (unset && !w->match) {
999 110
                        frag_line(fp, CTX.missing_string,
1000
                            NULL,
1001 55
                            &w->frag);
1002 55
                } else
1003 380
                        frag_line(fp, p, e, &w->frag);
1004 435
        }
1005 5730
}
1006
1007
static void
1008 18605
process_vsl(const struct vsl_watch_head *head, enum VSL_tag_e tag,
1009
    const char *b, const char *e)
1010
{
1011
        struct vsl_watch *w;
1012
        const char *p;
1013 25385
        VTAILQ_FOREACH(w, head, list) {
1014 6780
                CHECK_OBJ_NOTNULL(w, VSL_WATCH_MAGIC);
1015 6780
                if (tag != w->tag)
1016 6425
                        continue;
1017 355
                p = b;
1018 355
                if (w->prefixlen > 0 &&
1019 210
                    !isprefix(w->prefix, w->prefixlen, b, e, &p))
1020 190
                        continue;
1021 165
                if (w->idx == 0)
1022 30
                        frag_line(FMTPOL_INTERNAL, p, e, &w->frag);
1023
                else
1024 135
                        frag_fields(FMTPOL_INTERNAL, p, e, w->idx, &w->frag, 0, NULL);
1025 165
        }
1026 18605
}
1027
1028
static int v_matchproto_(VSLQ_dispatch_f)
1029 640
dispatch_f(struct VSL_data *vsl, struct VSL_transaction * const pt[],
1030
    void *priv)
1031
{
1032
        struct VSL_transaction *t;
1033
        enum VSL_tag_e tag;
1034
        const char *b, *e, *p;
1035
        struct watch *w;
1036
        int i, skip;
1037
1038 640
        (void)vsl;
1039 640
        (void)priv;
1040
1041 1280
        for (t = pt[0]; t != NULL; t = *++pt) {
1042 640
                CTX.gen++;
1043
1044 640
                if (t->type == VSL_t_req) {
1045 430
                        CTX.side = "c";
1046 640
                } else if (t->type == VSL_t_bereq) {
1047 70
                        CTX.side = "b";
1048 70
                } else
1049 140
                        continue;
1050
1051 500
                CTX.recv_compl = 0;
1052 500
                CTX.hitmiss = "-";
1053 500
                CTX.handling = "-";
1054 500
                CTX.vxid = t->vxid;
1055 500
                skip = 0;
1056 19180
                while (skip == 0 && 1 == VSL_Next(t->c)) {
1057 18680
                        tag = VSL_TAG(t->c->rec.ptr);
1058 18680
                        if (VSL_tagflags[tag] &&
1059 80
                            CTX.quote_how != VSB_QUOTE_JSON)
1060 75
                                continue;
1061
1062 18605
                        b = VSL_CDATA(t->c->rec.ptr);
1063 18605
                        e = b + VSL_LEN(t->c->rec.ptr);
1064 18605
                        if (!VSL_tagflags[tag]) {
1065 37200
                                while (e > b && e[-1] == '\0')
1066 18600
                                        e--;
1067 18600
                        }
1068
1069 18605
                        switch (tag) {
1070
                        case SLT_HttpGarbage:
1071 0
                                skip = 1;
1072 0
                                break;
1073
                        case SLT_PipeAcct:
1074 35
                                frag_fields(FMTPOL_INTERNAL, b, e,
1075
                                    3, &CTX.frag[F_I],
1076
                                    4, &CTX.frag[F_O],
1077
                                    0, NULL);
1078 35
                                break;
1079
                        case SLT_BackendOpen:
1080 70
                                frag_fields(FMTPOL_INTERNAL, b, e,
1081
                                    3, &CTX.frag[F_h],
1082
                                    0, NULL);
1083 70
                                break;
1084
                        case SLT_ReqStart:
1085 430
                                frag_fields(FMTPOL_INTERNAL, b, e,
1086
                                    1, &CTX.frag[F_h],
1087
                                    0, NULL);
1088 430
                                break;
1089
                        case SLT_BereqMethod:
1090
                        case SLT_ReqMethod:
1091 535
                                frag_line(FMTPOL_REQ, b, e, &CTX.frag[F_m]);
1092 535
                                break;
1093
                        case SLT_BereqURL:
1094
                        case SLT_ReqURL:
1095 620
                                p = memchr(b, '?', e - b);
1096 620
                                if (p == NULL)
1097 465
                                        p = e;
1098 620
                                frag_line(FMTPOL_REQ, b, p, &CTX.frag[F_U]);
1099 620
                                frag_line(FMTPOL_REQ, p, e, &CTX.frag[F_q]);
1100 620
                                break;
1101
                        case SLT_BereqProtocol:
1102
                        case SLT_ReqProtocol:
1103 510
                                frag_line(FMTPOL_REQ, b, e, &CTX.frag[F_H]);
1104 510
                                break;
1105
                        case SLT_BerespStatus:
1106
                        case SLT_RespStatus:
1107 480
                                frag_line(FMTPOL_RESP, b, e, &CTX.frag[F_s]);
1108 480
                                break;
1109
                        case SLT_BereqAcct:
1110
                        case SLT_ReqAcct:
1111 465
                                frag_fields(FMTPOL_INTERNAL, b, e,
1112
                                    3, &CTX.frag[F_I],
1113
                                    5, &CTX.frag[F_b],
1114
                                    6, &CTX.frag[F_O],
1115
                                    0, NULL);
1116 465
                                break;
1117
                        case SLT_Timestamp:
1118
#define ISPREFIX(a, b, c, d)    isprefix(a, strlen(a), b, c, d)
1119 2445
                                if (ISPREFIX("Start:", b, e, &p)) {
1120 500
                                        frag_fields(FMTPOL_INTERNAL, p, e, 1,
1121
                                            &CTX.frag[F_tstart], 0, NULL);
1122
1123 3965
                                } else if (ISPREFIX("Resp:", b, e, &p) ||
1124 1555
                                    ISPREFIX("PipeSess:", b, e, &p) ||
1125 1520
                                    ISPREFIX("BerespBody:", b, e, &p)) {
1126 485
                                        frag_fields(FMTPOL_INTERNAL, p, e, 1,
1127
                                            &CTX.frag[F_tend], 0, NULL);
1128
1129 2875
                                } else if (ISPREFIX("Process:", b, e, &p) ||
1130 965
                                    ISPREFIX("Pipe:", b, e, &p) ||
1131 930
                                    ISPREFIX("Beresp:", b, e, &p)) {
1132 590
                                        frag_fields(FMTPOL_INTERNAL, p, e, 2,
1133
                                            &CTX.frag[F_ttfb], 0, NULL);
1134 590
                                }
1135 2445
                                break;
1136
                        case SLT_BereqHeader:
1137
                        case SLT_ReqHeader:
1138 2340
                                process_hdr(FMTPOL_REQ, &CTX.watch_reqhdr, b, e, 0);
1139 2340
                                if (ISPREFIX("Authorization:", b, e, &p) &&
1140 130
                                    ISPREFIX("basic ", p, e, &p))
1141 130
                                        frag_line(FMTPOL_REQ, p, e,
1142
                                            &CTX.frag[F_auth]);
1143 2210
                                else if (ISPREFIX("Host:", b, e, &p))
1144 480
                                        frag_line(FMTPOL_REQ, p, e,
1145
                                            &CTX.frag[F_host]);
1146
#undef ISPREFIX
1147 2340
                                break;
1148
                        case SLT_BerespHeader:
1149
                        case SLT_RespHeader:
1150 3155
                                process_hdr(FMTPOL_RESP, &CTX.watch_resphdr, b, e, 0);
1151 3155
                                break;
1152
                        case SLT_BereqUnset:
1153
                        case SLT_ReqUnset:
1154 110
                                process_hdr(FMTPOL_REQ, &CTX.watch_reqhdr, b, e, 1);
1155 110
                                break;
1156
                        case SLT_BerespUnset:
1157
                        case SLT_RespUnset:
1158 125
                                process_hdr(FMTPOL_RESP, &CTX.watch_resphdr, b, e, 1);
1159 125
                                break;
1160
                        case SLT_HitMiss:
1161 55
                                CTX.hitmiss = "miss";
1162 55
                                CTX.handling = "hitmiss";
1163 55
                                break;
1164
                        case SLT_HitPass:
1165 5
                                CTX.hitmiss = "miss";
1166 5
                                CTX.handling = "hitpass";
1167 5
                                break;
1168
                        case SLT_VCL_call:
1169 1750
                                if (!strcasecmp(b, "recv")) {
1170 425
                                        CTX.recv_compl = 1;
1171 425
                                        CTX.hitmiss = "-";
1172 425
                                        CTX.handling = "-";
1173 1750
                                } else if (!strcasecmp(b, "hit")) {
1174 85
                                        CTX.hitmiss = "hit";
1175 85
                                        CTX.handling = "hit";
1176 1325
                                } else if (!strcasecmp(b, "miss") && strcmp(CTX.handling, "hitmiss")) {
1177 185
                                        CTX.hitmiss = "miss";
1178 185
                                        CTX.handling = "miss";
1179 1240
                                } else if (!strcasecmp(b, "pass") && strcmp(CTX.handling, "hitpass")) {
1180 35
                                        CTX.hitmiss = "miss";
1181 35
                                        CTX.handling = "pass";
1182 1055
                                } else if (!strcasecmp(b, "synth")) {
1183
                                        /* Arguably, synth isn't a hit or
1184
                                           a miss, but miss is less
1185
                                           wrong */
1186 60
                                        CTX.hitmiss = "miss";
1187 60
                                        CTX.handling = "synth";
1188 1020
                                } else if (!strcasecmp(b, "backend_response")) {
1189 60
                                        CTX.recv_compl = 1;
1190 60
                                }
1191 1750
                                break;
1192
                        case SLT_VCL_return:
1193 1750
                                if (!strcasecmp(b, "pipe")) {
1194 70
                                        CTX.hitmiss = "miss";
1195 70
                                        CTX.handling = "pipe";
1196 1750
                                } else if (!strcasecmp(b, "restart"))
1197 0
                                        skip = 1;
1198 1750
                                break;
1199
                        case SLT_VCL_Log:
1200 55
                                VTAILQ_FOREACH(w, &CTX.watch_vcl_log, list) {
1201 5
                                        CHECK_OBJ_NOTNULL(w, WATCH_MAGIC);
1202 5
                                        if (e - b < w->keylen ||
1203 5
                                            strncmp(b, w->key, w->keylen))
1204 0
                                                continue;
1205 5
                                        p = b + w->keylen;
1206 5
                                        frag_line(FMTPOL_INTERNAL, p, e, &w->frag);
1207 5
                                }
1208 50
                                break;
1209
                        default:
1210 3675
                                break;
1211
                        }
1212
1213 18605
                        process_vsl(&CTX.watch_vsl, tag, b, e);
1214
                }
1215 500
                if (skip)
1216 0
                        continue;
1217 500
                i = print();
1218 500
                if (i)
1219 0
                        return (i);
1220 500
        }
1221
1222 640
        return (0);
1223 640
}
1224
1225
static char *
1226 15
read_format(const char *formatfile)
1227
{
1228
        FILE *fmtfile;
1229 15
        size_t len = 0;
1230
        int fmtlen;
1231 15
        char *fmt = NULL;
1232
1233 15
        fmtfile = fopen(formatfile, "r");
1234 15
        if (fmtfile == NULL)
1235 10
                VUT_Error(vut, 1, "Can't open format file (%s)",
1236 5
                    strerror(errno));
1237 10
        AN(fmtfile);
1238 10
        fmtlen = getline(&fmt, &len, fmtfile);
1239 10
        if (fmtlen == -1) {
1240 5
                free(fmt);
1241 5
                if (feof(fmtfile))
1242 5
                        VUT_Error(vut, 1, "Empty format file");
1243 0
                VUT_Error(vut, 1, "Can't read format from file (%s)",
1244 0
                    strerror(errno));
1245
        }
1246 5
        AZ(fclose(fmtfile));
1247 5
        if (fmt[fmtlen - 1] == '\n')
1248 5
                fmt[fmtlen - 1] = '\0';
1249 5
        return (fmt);
1250
}
1251
1252
int
1253 510
main(int argc, char * const *argv)
1254
{
1255
        signed char opt;
1256 510
        char *format = NULL;
1257 510
        int mode_opt = 0;
1258
1259 510
        vut = VUT_InitProg(argc, argv, &vopt_spec);
1260 510
        AN(vut);
1261 510
        memset(&CTX, 0, sizeof CTX);
1262 510
        VTAILQ_INIT(&CTX.format);
1263 510
        VTAILQ_INIT(&CTX.watch_vcl_log);
1264 510
        VTAILQ_INIT(&CTX.watch_reqhdr);
1265 510
        VTAILQ_INIT(&CTX.watch_resphdr);
1266 510
        VTAILQ_INIT(&CTX.watch_vsl);
1267 510
        CTX.vsb = VSB_new_auto();
1268 510
        AN(CTX.vsb);
1269 510
        CTX.quote_how = VSB_QUOTE_ESCHEX;
1270 510
        REPLACE(CTX.missing_string, "-");
1271 510
        REPLACE(CTX.missing_int, "-");
1272
1273 320
        tzset();                // We use localtime_r(3)
1274
1275 1545
        while ((opt = getopt(argc, argv, vopt_spec.vopt_optstring)) != -1) {
1276 1230
                switch (opt) {
1277
                case 'a':
1278
                        /* Append to file */
1279 0
                        CTX.a_opt = 1;
1280 0
                        break;
1281
                case 'b': /* backend mode */
1282
                case 'c': /* client mode */
1283
                case 'E': /* show ESI */
1284 100
                        AN(VUT_Arg(vut, opt, NULL));
1285 100
                        mode_opt = 1;
1286 100
                        break;
1287
                case 'F':
1288 295
                        if (format != NULL)
1289 0
                                VUT_Error(vut, 1, "Format already set");
1290 295
                        REPLACE(format, optarg);
1291 295
                        break;
1292
                case 'f':
1293 5
                        if (format != NULL)
1294 0
                                VUT_Error(vut, 1, "Format already set");
1295
                        /* Format string from file */
1296 5
                        format = read_format(optarg);
1297 5
                        AN(format);
1298 5
                        break;
1299
                case 'h':
1300
                        /* Usage help */
1301 5
                        VUT_Usage(vut, &vopt_spec, 0);
1302
                        break;
1303
                case 'j':
1304 25
                        REPLACE(CTX.missing_string, "");
1305 25
                        REPLACE(CTX.missing_int, "0");
1306 25
                        CTX.quote_how = VSB_QUOTE_JSON;
1307 25
                        break;
1308
                case 'w':
1309
                        /* Write to file */
1310 20
                        REPLACE(CTX.w_arg, optarg);
1311 20
                        break;
1312
                default:
1313 780
                        if (!VUT_Arg(vut, opt, optarg))
1314 0
                                VUT_Usage(vut, &vopt_spec, 1);
1315 780
                }
1316
        }
1317
1318
        /* default is client mode: */
1319 315
        if (!mode_opt)
1320 295
                AN(VUT_Arg(vut, 'c', NULL));
1321
1322 315
        if (optind != argc)
1323 5
                VUT_Usage(vut, &vopt_spec, 1);
1324
1325 310
        if (vut->D_opt && !CTX.w_arg)
1326 5
                VUT_Error(vut, 1, "Missing -w option");
1327
1328 305
        if (vut->D_opt && !strcmp(CTX.w_arg, "-"))
1329 0
                VUT_Error(vut, 1, "Daemon cannot write to stdout");
1330
1331
        /* Check for valid grouping mode */
1332 305
        assert(vut->g_arg < VSL_g__MAX);
1333 305
        if (vut->g_arg != VSL_g_vxid && vut->g_arg != VSL_g_request)
1334 10
                VUT_Error(vut, 1, "Invalid grouping mode: %s",
1335 5
                    VSLQ_grouping[vut->g_arg]);
1336
1337
        /* Prepare output format */
1338 300
        parse_format(format);
1339 300
        REPLACE(format, NULL);
1340
1341
        /* Setup output */
1342 300
        vut->dispatch_f = dispatch_f;
1343 300
        vut->dispatch_priv = NULL;
1344 300
        if (CTX.w_arg) {
1345 15
                openout(CTX.a_opt);
1346 15
                AN(CTX.fo);
1347 15
                if (vut->D_opt)
1348 5
                        vut->sighup_f = rotateout;
1349 15
        } else
1350 285
                CTX.fo = stdout;
1351 300
        vut->idle_f = flushout;
1352
1353 300
        VUT_Setup(vut);
1354 300
        (void)VUT_Main(vut);
1355 300
        VUT_Fini(&vut);
1356
1357 300
        exit(0);
1358
}