varnish-cache/lib/libvarnishapi/vut.c
0
/*-
1
 * Copyright (c) 2006 Verdens Gang AS
2
 * Copyright (c) 2006-2015 Varnish Software AS
3
 * All rights reserved.
4
 *
5
 * Author: Martin Blix Grydeland <martin@varnish-software.com>
6
 *
7
 * SPDX-License-Identifier: BSD-2-Clause
8
 *
9
 * Redistribution and use in source and binary forms, with or without
10
 * modification, are permitted provided that the following conditions
11
 * are met:
12
 * 1. Redistributions of source code must retain the above copyright
13
 *    notice, this list of conditions and the following disclaimer.
14
 * 2. Redistributions in binary form must reproduce the above copyright
15
 *    notice, this list of conditions and the following disclaimer in the
16
 *    documentation and/or other materials provided with the distribution.
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
22
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28
 * SUCH DAMAGE.
29
 *
30
 * Common functions for the utilities
31
 */
32
33
#include "config.h"
34
35
#include <ctype.h>
36
#include <math.h>
37
#include <stdint.h>
38
#include <stdarg.h>
39
#include <stdlib.h>
40
#include <unistd.h>
41
#include <stdio.h>
42
#include <string.h>
43
#include <sys/stat.h> /* for MUSL */
44
45
#include "compat/daemon.h"
46
#include "vdef.h"
47
#include "vpf.h"
48
#include "vapi/vsm.h"
49
#include "vapi/vsl.h"
50
#include "vtim.h"
51
#include "vas.h"
52
#include "miniobj.h"
53
#include "vcs.h"
54
#include "vsb.h"
55
#include "vfil.h"
56
57
#include "vapi/voptget.h"
58
#include "vapi/vsig.h"
59
60
#include "vut.h"
61
62
63
static int vut_synopsis(const struct vopt_spec *);
64
static int vut_options(const struct vopt_spec *);
65
66
static struct vpf_fh    *pfh;
67
static unsigned         daemonized;
68
69
static struct VUT pfh_vut;
70
71
static int
72 6
vut_daemon(struct VUT *vut)
73
{
74 6
        if (daemonized)
75 0
                VUT_Error(vut, 1, "Already running as a daemon");
76 6
        daemonized = 1;
77 6
        return (varnish_daemon(0, 0));
78
}
79
80
static void
81 12
vut_vpf_remove(void)
82
{
83
84 12
        CHECK_OBJ(&pfh_vut, VUT_MAGIC);
85 12
        AN(pfh);
86 12
        AN(pfh_vut.P_arg);
87
88 12
        VPF_Remove(pfh);
89 12
        free(pfh_vut.P_arg);
90 12
        ZERO_OBJ(&pfh_vut, sizeof pfh_vut);
91 12
        pfh = NULL;
92 12
}
93
94
static int v_matchproto_(VSLQ_dispatch_f)
95 2930
vut_dispatch(struct VSL_data *vsl, struct VSL_transaction * const trans[],
96
    void *priv)
97
{
98
        struct VUT *vut;
99
        int i;
100
101 2930
        CAST_OBJ_NOTNULL(vut, priv, VUT_MAGIC);
102
103 2930
        if (vut->k_arg == 0)
104 0
                return (-1);    /* End of file */
105 2930
        AN(vut->dispatch_f);
106 2930
        i = vut->dispatch_f(vsl, trans, vut->dispatch_priv);
107 2930
        if (vut->k_arg > 0)
108 9
                vut->k_arg--;
109 2930
        if (i >= 0 && vut->k_arg == 0)
110 9
                return (-1);    /* End of file */
111 2921
        return (i);
112 2930
}
113
114
void
115 291
VUT_Error(struct VUT *vut, int status, const char *fmt, ...)
116
{
117
        va_list ap;
118
119 291
        CHECK_OBJ_NOTNULL(vut, VUT_MAGIC);
120 291
        AN(status);
121
122 291
        va_start(ap, fmt);
123 291
        if (vut->error_f != NULL) {
124 0
                vut->error_f(vut, status, fmt, ap);
125 0
        } else {
126 291
                vfprintf(stderr, fmt, ap);
127 291
                fprintf(stderr, "\n");
128
        }
129 291
        va_end(ap);
130 291
        exit(status);
131
}
132
133
static void
134 177
vut_arg_q(struct VUT *vut, const char *arg)
135
{
136
        struct vsb *vsb;
137
138 177
        AN(arg);
139 177
        if (vut->q_arg == NULL) {
140 168
                REPLACE(vut->q_arg, arg);
141 168
                return;
142
        }
143
144 9
        vsb = VSB_new_auto();
145 9
        AN(vsb);
146 9
        AZ(VSB_printf(vsb, "%s\n%s", vut->q_arg, arg));
147 9
        AZ(VSB_finish(vsb));
148
149 9
        REPLACE(vut->q_arg, VSB_data(vsb));
150
151 9
        VSB_destroy(&vsb);
152 177
}
153
154
int
155 1242
VUT_Arg(struct VUT *vut, int opt, const char *arg)
156
{
157
        int i;
158
        char *p;
159
160 1242
        CHECK_OBJ_NOTNULL(vut, VUT_MAGIC);
161 1242
        AN(opt);
162
163 1242
        switch (opt) {
164
        case 'd':
165
                /* Head */
166 237
                vut->d_opt = 1;
167 237
                return (1);
168
        case 'D':
169
                /* Daemon mode */
170 18
                vut->D_opt = 1;
171 18
                return (1);
172
        case 'g':
173
                /* Grouping */
174 42
                AN(arg);
175 42
                vut->g_arg = VSLQ_Name2Grouping(arg, -1);
176 42
                if (vut->g_arg == -2)
177 3
                        VUT_Error(vut, 1, "Ambiguous grouping type: %s", arg);
178 39
                else if (vut->g_arg < 0)
179 3
                        VUT_Error(vut, 1, "Unknown grouping type: %s", arg);
180 36
                return (1);
181
        case 'k':
182
                /* Log transaction limit */
183 12
                AN(arg);
184 12
                vut->k_arg = (int)strtol(arg, &p, 10);
185 12
                if (*p != '\0' || vut->k_arg <= 0)
186 3
                        VUT_Error(vut, 1, "-k: Invalid number '%s'", arg);
187 9
                return (1);
188
        case 'n':
189
                /* Varnish instance name */
190 273
                AN(arg);
191 273
                REPLACE(vut->n_arg, arg);
192 273
                return (1);
193
        case 'P':
194
                /* PID file */
195 12
                AN(arg);
196 12
                REPLACE(vut->P_arg, arg);
197 12
                return (1);
198
        case 'Q':
199 24
                AN(arg);
200 24
                p = VFIL_readfile(NULL, arg, NULL);
201 24
                if (p == NULL)
202 3
                        VUT_Error(vut, 1, "-Q %s: %s", arg, strerror(errno));
203 21
                vut_arg_q(vut, p);
204 21
                free(p);
205 21
                return (1);
206
        case 'q':
207
                /* Query to use */
208 156
                AN(arg);
209 156
                vut_arg_q(vut, arg);
210 156
                return (1);
211
        case 'r':
212
                /* Binary file input */
213 24
                AN(arg);
214 24
                REPLACE(vut->r_arg, arg);
215 24
                return (1);
216
        case 't':
217
                /* VSM connect timeout */
218 12
                REPLACE(vut->t_arg, arg);
219 12
                return (1);
220
        case 'V':
221
                /* Print version number and exit */
222 15
                VCS_Message(vut->progname);
223 15
                exit(0);
224
        default:
225 417
                AN(vut->vsl);
226 417
                i = VSL_Arg(vut->vsl, opt, arg);
227 417
                if (i < 0)
228 54
                        VUT_Error(vut, 1, "%s", VSL_Error(vut->vsl));
229 363
                return (i);
230
        }
231 1161
}
232
233
struct VUT *
234 705
VUT_Init(const char *progname, int argc, char * const *argv,
235
    const struct vopt_spec *voc)
236
{
237
        struct VUT *vut;
238
239 705
        AN(progname);
240 705
        AN(argv);
241 705
        AN(voc);
242
243 705
        VSIG_Arm_hup();
244 705
        VSIG_Arm_int();
245 705
        VSIG_Arm_term();
246 705
        VSIG_Arm_usr1();
247
248 705
        if (argc == 2 && !strcmp(argv[1], "--synopsis"))
249 15
                exit(vut_synopsis(voc));
250 690
        if (argc == 2 && !strcmp(argv[1], "--options"))
251 15
                exit(vut_options(voc));
252 675
        if (argc == 2 && !strcmp(argv[1], "--optstring")) {
253 0
                (void)printf("%s\n", voc->vopt_optstring);
254 0
                exit(0);
255
        }
256
257 675
        ALLOC_OBJ(vut, VUT_MAGIC);
258 675
        AN(vut);
259 675
        vut->progname = progname;
260 675
        vut->g_arg = VSL_g_vxid;
261 675
        vut->k_arg = -1;
262 675
        AZ(vut->vsl);
263 675
        vut->vsl = VSL_New();
264 675
        AN(vut->vsl);
265 675
        return (vut);
266
}
267
268
void
269 0
VUT_Signal(VUT_sighandler_f sig_cb)
270
{
271
272 0
        AN(sig_cb);
273 0
        (void)signal(SIGHUP, sig_cb);
274 0
        (void)signal(SIGINT, sig_cb);
275 0
        (void)signal(SIGTERM, sig_cb);
276 0
        (void)signal(SIGUSR1, sig_cb);
277 0
}
278
279
void
280 0
VUT_Signaled(struct VUT *vut, int sig)
281
{
282
283 0
        CHECK_OBJ_NOTNULL(vut, VUT_MAGIC);
284
#define VSIG_SIGNAL(UPPER, lower) \
285
        VSIG_##lower += (int)(sig == SIG##UPPER);
286
#include "tbl/vsig_list.h"
287
}
288
289
void
290 366
VUT_Setup(struct VUT *vut)
291
{
292
        struct VSL_cursor *c;
293
294 366
        CHECK_OBJ_NOTNULL(vut, VUT_MAGIC);
295 366
        AN(vut->vsl);
296 366
        AZ(vut->vsm);
297 366
        AZ(vut->vslq);
298
299
        /* Check input arguments (2 used for bug in FlexeLint) */
300 1098
        if ((vut->n_arg == NULL ? 0 : 2) +
301 732
            (vut->r_arg == NULL ? 0 : 2) > 2)
302 3
                VUT_Error(vut, 1, "Only one of -n and -r options may be used");
303
304 363
        if (vut->r_arg != NULL && !strcmp(vut->r_arg, "-") && vut->D_opt)
305 0
                VUT_Error(vut, 1, "Daemon cannot read from stdin");
306
307
        /* Create and validate the query expression */
308 726
        vut->vslq = VSLQ_New(vut->vsl, NULL,
309 363
            (enum VSL_grouping_e)vut->g_arg, vut->q_arg);
310 363
        if (vut->vslq == NULL)
311 192
                VUT_Error(vut, 1, "Query expression error:\n%s",
312 96
                    VSL_Error(vut->vsl));
313
314
        /* Setup input */
315 267
        if (vut->r_arg) {
316 21
                c = VSL_CursorFile(vut->vsl, vut->r_arg, 0);
317 21
                if (c == NULL)
318 6
                        VUT_Error(vut, 1, "%s", VSL_Error(vut->vsl));
319 15
                VSLQ_SetCursor(vut->vslq, &c);
320 15
                AZ(c);
321 15
        } else {
322 246
                vut->vsm = VSM_New();
323 246
                AN(vut->vsm);
324 246
                if (vut->n_arg && VSM_Arg(vut->vsm, 'n', vut->n_arg) <= 0)
325 0
                        VUT_Error(vut, 1, "%s", VSM_Error(vut->vsm));
326 246
                if (vut->t_arg && VSM_Arg(vut->vsm, 't', vut->t_arg) <= 0)
327 9
                        VUT_Error(vut, 1, "%s", VSM_Error(vut->vsm));
328 237
                if (VSM_Attach(vut->vsm, STDERR_FILENO))
329 3
                        VUT_Error(vut, 1, "VSM: %s", VSM_Error(vut->vsm));
330
                // Cursor is handled in VUT_Main()
331
        }
332
333
        /* Open PID file */
334 249
        if (vut->P_arg) {
335 12
                if (pfh != NULL)
336 0
                        VUT_Error(vut, 1, "PID file already created");
337 12
                pfh = VPF_Open(vut->P_arg, 0644, NULL);
338 12
                if (pfh == NULL)
339 0
                        VUT_Error(vut, 1,
340 0
                            "%s: %s", vut->P_arg, strerror(errno));
341 12
        }
342
343
        /* Daemon mode */
344 249
        if (vut->D_opt && vut_daemon(vut) == -1)
345 0
                VUT_Error(vut, 1, "Daemon mode: %s", strerror(errno));
346
347
        /* Write PID and setup exit handler */
348 249
        if (vut->P_arg) {
349 12
                AN(pfh);
350 12
                VPF_Write(pfh);
351
352
                /* NB: move ownership to a global pseudo-VUT. */
353 12
                INIT_OBJ(&pfh_vut, VUT_MAGIC);
354 12
                pfh_vut.P_arg = vut->P_arg;
355 12
                pfh_vut.error_f = vut->error_f;
356 12
                vut->P_arg = NULL;
357
358 12
                AZ(atexit(vut_vpf_remove));
359 12
        }
360 249
}
361
362
void
363 249
VUT_Fini(struct VUT **vutp)
364
{
365
        struct VUT *vut;
366
367 249
        TAKE_OBJ_NOTNULL(vut, vutp, VUT_MAGIC);
368 249
        AN(vut->progname);
369
370 249
        free(vut->n_arg);
371 249
        free(vut->q_arg);
372 249
        free(vut->r_arg);
373 249
        free(vut->t_arg);
374 249
        AZ(vut->P_arg);
375
376 249
        if (vut->vslq)
377 249
                VSLQ_Delete(&vut->vslq);
378 249
        if (vut->vsl)
379 249
                VSL_Delete(vut->vsl);
380 249
        if (vut->vsm)
381 234
                VSM_Destroy(&vut->vsm);
382
383 249
        FREE_OBJ(vut);
384 249
}
385
386
static void
387 0
vut_CursorError(struct VUT *vut, vtim_mono *last)
388
{
389
        const char *diag;
390
        vtim_mono now;
391
392 0
        CHECK_OBJ_NOTNULL(vut, VUT_MAGIC);
393 0
        AN(vut->vsl);
394 0
        AN(last);
395
396 0
        diag = VSL_Error(vut->vsl);
397 0
        if (diag == NULL)
398 0
                diag = "Missing diagnostic";
399
400 0
        now = VTIM_mono();
401 0
        if (isnan(*last) || *last + 1 < now) {
402 0
                fprintf(stderr, "Failed to acquire log: %s\n", diag);
403 0
                *last = now;
404 0
        }
405 0
}
406
407
int
408 249
VUT_Main(struct VUT *vut)
409
{
410
        struct VSL_cursor *c;
411 249
        int i = -1;
412 249
        int hascursor = -1;
413 249
        vtim_mono t_failcursor = NAN;
414
415 249
        CHECK_OBJ_NOTNULL(vut, VUT_MAGIC);
416 249
        AN(vut->vslq);
417
418 20810
        while (!VSIG_int && !VSIG_term) {
419 20774
                if (VSIG_hup != vut->last_sighup) {
420
                        /* sighup callback */
421 12
                        vut->last_sighup = VSIG_hup;
422 12
                        if (vut->sighup_f != NULL)
423 6
                                i = vut->sighup_f(vut);
424
                        else
425 6
                                i = 1;
426 12
                        if (i)
427 6
                                break;
428 6
                }
429
430 20768
                if (VSIG_usr1 != vut->last_sigusr1) {
431
                        /* Flush and report any incomplete records */
432 0
                        vut->last_sigusr1 = VSIG_usr1;
433 0
                        (void)VSLQ_Flush(vut->vslq, vut_dispatch, vut);
434 0
                }
435
436
                /* We must repeatedly call VSM_Status() when !hascursor
437
                 * to make VSM discover our segment.
438
                 *
439
                 * XXX consider moving the error handling to VSLQ_Dispatch.
440
                 * or some other VSL utility function
441
                 * Reasons:
442
                 *
443
                 * - it does not seem to make much sense to call VSM_StillValid
444
                 *   in vsl if that can only detect invalid segments after
445
                 *   VSM_Status has run, so it appears both should be
446
                 *   consolidated
447
                 *
448
                 * - not all VSL Clients will use VUT, yet the log abandoned/
449
                 *   overrun situation will be occur for all of them.
450
                 */
451
452 20768
                if (vut->vsm != NULL &&
453 20753
                    (VSM_Status(vut->vsm) & VSM_WRK_RESTARTED)) {
454 0
                        if (hascursor < 1) {
455 0
                                fprintf(stderr, "Log abandoned (vsm)\n");
456 0
                                VSLQ_SetCursor(vut->vslq, NULL);
457 0
                                hascursor = 0;
458 0
                        }
459 0
                }
460 20768
                if (vut->vsm != NULL && hascursor < 1) {
461
                        /* Reconnect VSM */
462 234
                        AZ(vut->r_arg);
463 234
                        VTIM_sleep(0.1);
464 468
                        c = VSL_CursorVSM(vut->vsl, vut->vsm,
465 234
                            (vut->d_opt ? VSL_COPT_TAILSTOP : VSL_COPT_TAIL)
466 234
                            | VSL_COPT_BATCH);
467 234
                        if (c == NULL) {
468 0
                                vut_CursorError(vut, &t_failcursor);
469 0
                                VSL_ResetError(vut->vsl);
470 0
                                continue;
471
                        }
472 234
                        if (hascursor >= 0)
473 0
                                fprintf(stderr, "Log reacquired\n");
474 234
                        hascursor = 1;
475 234
                        VSLQ_SetCursor(vut->vslq, &c);
476 234
                        AZ(c);
477 234
                }
478
479 20768
                do
480 53336
                        i = VSLQ_Dispatch(vut->vslq, vut_dispatch, vut);
481 32568
                while (i == vsl_more &&
482 11800
                    VSIG_usr1 == vut->last_sigusr1 &&
483 11800
                    VSIG_hup == vut->last_sighup);
484
485 20768
                if (i == vsl_more)
486 0
                        continue;
487 20768
                else if (i == vsl_end) {
488 20561
                        if (vut->idle_f) {
489 9025
                                i = vut->idle_f(vut);
490 9025
                                if (i)
491 0
                                        break;
492 9025
                        }
493 20561
                        VTIM_sleep(0.01);
494 20561
                        continue;
495 207
                } else if (i == vsl_e_eof)
496 207
                        break;
497
498 0
                if (vut->vsm == NULL)
499 0
                        break;
500
501
                /* XXX: Make continuation optional */
502
503 0
                (void)VSLQ_Flush(vut->vslq, vut_dispatch, vut);
504
505 0
                if (i == vsl_e_abandon) {
506 0
                        fprintf(stderr, "Log abandoned (vsl)\n");
507 0
                        VSLQ_SetCursor(vut->vslq, NULL);
508 0
                        hascursor = 0;
509 0
                } else if (i == vsl_e_overrun) {
510 0
                        fprintf(stderr, "Log overrun\n");
511 0
                        VSLQ_SetCursor(vut->vslq, NULL);
512 0
                        hascursor = 0;
513 0
                } else
514 0
                        fprintf(stderr, "Error %d from VSLQ_Dispatch()", i);
515
        }
516
517 249
        return (i);
518
}
519
520
/**********************************************************************/
521
522
void v_noreturn_
523 30
VUT_Usage(const struct VUT *vut, const struct vopt_spec *voc, int status)
524
{
525
        const char **opt;
526
527 30
        fprintf(stderr, "Usage: %s <options>\n\n", vut->progname);
528 30
        fprintf(stderr, "Options:\n");
529 732
        for (opt = voc->vopt_usage; *opt != NULL; opt += 2)
530 702
                fprintf(stderr, " %-25s %s\n", *opt, *(opt + 1));
531 30
        exit(status);
532
}
533
534
/**********************************************************************/
535
536
537
static void
538 321
print_nobrackets(const char *s)
539
{
540
        const char *e;
541
542
        /* Remove whitespace */
543 321
        while (isspace(*s))
544 0
                s++;
545 321
        e = s + strlen(s);
546 321
        while (e > s && isspace(e[-1]))
547 0
                e--;
548
549
        /* Remove outer layer brackets if present */
550 321
        if (e > s && *s == '[' && e[-1] == ']') {
551 321
                s++;
552 321
                e--;
553 321
        }
554
555 321
        printf("%.*s", (int)(e - s), s);
556 321
}
557
558
static void
559 321
print_tabbed(const char *string, int tabs)
560
{
561
        int i;
562
        const char *c;
563
564 50211
        for (c = string; *c; c++) {
565 49890
                if (c == string || *(c - 1) == '\n')
566 690
                        for (i = 0; i < tabs; i++)
567 690
                                printf("\t");
568 49890
                printf("%c", *c);
569 49890
        }
570 321
}
571
572
static void
573 321
print_opt(const struct vopt_list *opt)
574
{
575 321
        print_nobrackets(opt->synopsis);
576 321
        printf("\n\n");
577 321
        print_tabbed(opt->ldesc, 1);
578 321
        printf("\n\n");
579 321
}
580
581
static int
582 15
vut_synopsis(const struct vopt_spec *voc)
583
{
584 15
        printf(".. |synopsis| replace:: %s\n", voc->vopt_synopsis);
585 15
        return (0);
586
}
587
588
static int
589 15
vut_options(const struct vopt_spec *voc)
590
{
591
        int i;
592
593 336
        for (i = 0; i < voc->vopt_list_n; i++)
594 321
                print_opt(&voc->vopt_list[i]);
595 15
        printf("--optstring\n"
596
            "\tPrint the optstring parameter to ``getopt(3)`` to help"
597
            " writing wrapper scripts.\n\n");
598 15
        return (0);
599
}