Code Coverage: vinyl-cache/lib/libvinylapi/vut.c
 
82217aa4fc7094b3610340078afaf78cd95a8ec9
 
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
vut_daemon(struct VUT *vut)
73 2
{
74 2
        if (daemonized)
75 0
                VUT_Error(vut, 1, "Already running as a daemon");
76 2
        daemonized = 1;
77 2
        return (vinyl_daemon(0, 0));
78 2
}
79
80
static void
81
vut_vpf_remove(void)
82 4
{
83
84 4
        CHECK_OBJ(&pfh_vut, VUT_MAGIC);
85 4
        AN(pfh);
86 4
        AN(pfh_vut.P_arg);
87
88 4
        VPF_Remove(pfh);
89 4
        free(pfh_vut.P_arg);
90 4
        ZERO_OBJ(&pfh_vut, sizeof pfh_vut);
91 4
        pfh = NULL;
92 4
}
93
94
static int v_matchproto_(VSLQ_dispatch_f)
95
vut_dispatch(struct VSL_data *vsl, struct VSL_transaction * const trans[],
96
    void *priv)
97 1009
{
98 1009
        struct VUT *vut;
99 1009
        int i;
100
101 1009
        CAST_OBJ_NOTNULL(vut, priv, VUT_MAGIC);
102
103 1009
        if (vut->k_arg == 0)
104 0
                return (-1);    /* End of file */
105 1009
        AN(vut->dispatch_f);
106 1009
        i = vut->dispatch_f(vsl, trans, vut->dispatch_priv);
107 1009
        if (vut->k_arg > 0)
108 3
                vut->k_arg--;
109 1009
        if (i >= 0 && vut->k_arg == 0)
110 3
                return (-1);    /* End of file */
111 1006
        return (i);
112 1009
}
113
114
void
115
VUT_Error(struct VUT *vut, int status, const char *fmt, ...)
116 97
{
117 97
        va_list ap;
118
119 97
        CHECK_OBJ_NOTNULL(vut, VUT_MAGIC);
120 97
        AN(status);
121
122 97
        va_start(ap, fmt);
123 97
        if (vut->error_f != NULL) {
124 0
                vut->error_f(vut, status, fmt, ap);
125 97
        } else {
126 97
                vfprintf(stderr, fmt, ap);
127 97
                fprintf(stderr, "\n");
128 97
        }
129 97
        va_end(ap);
130 97
        exit(status);
131 97
}
132
133
static void
134
vut_arg_q(struct VUT *vut, const char *arg)
135 59
{
136 59
        struct vsb *vsb;
137
138 59
        AN(arg);
139 59
        if (vut->q_arg == NULL) {
140 56
                REPLACE(vut->q_arg, arg);
141 56
                return;
142 56
        }
143
144 3
        vsb = VSB_new_auto();
145 3
        AN(vsb);
146 3
        AZ(VSB_printf(vsb, "%s\n%s", vut->q_arg, arg));
147 3
        AZ(VSB_finish(vsb));
148
149 3
        REPLACE(vut->q_arg, VSB_data(vsb));
150
151 3
        VSB_destroy(&vsb);
152 3
}
153
154
int
155
VUT_Arg(struct VUT *vut, int opt, const char *arg)
156 429
{
157 429
        int i;
158 429
        char *p;
159
160 429
        CHECK_OBJ_NOTNULL(vut, VUT_MAGIC);
161 429
        AN(opt);
162
163 429
        switch (opt) {
164 0
        case '0':
165 0
                vut->dryrun_opt = 1;
166 0
                return (1);
167 84
        case 'd':
168
                /* Head */
169 84
                vut->d_opt = 1;
170 84
                return (1);
171 6
        case 'D':
172
                /* Daemon mode */
173 6
                vut->D_opt = 1;
174 6
                return (1);
175 14
        case 'g':
176
                /* Grouping */
177 14
                AN(arg);
178 14
                vut->g_arg = VSLQ_Name2Grouping(arg, -1);
179 14
                if (vut->g_arg == -2)
180 1
                        VUT_Error(vut, 1, "Ambiguous grouping type: %s", arg);
181 13
                else if (vut->g_arg < 0)
182 1
                        VUT_Error(vut, 1, "Unknown grouping type: %s", arg);
183 12
                return (1);
184 4
        case 'k':
185
                /* Log transaction limit */
186 4
                AN(arg);
187 4
                vut->k_arg = (int)strtol(arg, &p, 10);
188 4
                if (*p != '\0' || vut->k_arg <= 0)
189 1
                        VUT_Error(vut, 1, "-k: Invalid number '%s'", arg);
190 3
                return (1);
191 96
        case 'n':
192
                /* Vinyl instance name */
193 96
                AN(arg);
194 96
                REPLACE(vut->n_arg, arg);
195 96
                return (1);
196 4
        case 'P':
197
                /* PID file */
198 4
                AN(arg);
199 4
                REPLACE(vut->P_arg, arg);
200 4
                return (1);
201 8
        case 'Q':
202 8
                AN(arg);
203 8
                p = VFIL_readfile(NULL, arg, NULL);
204 8
                if (p == NULL)
205 1
                        VUT_Error(vut, 1, "-Q %s: %s", arg, strerror(errno));
206 7
                vut_arg_q(vut, p);
207 7
                free(p);
208 7
                return (1);
209 52
        case 'q':
210
                /* Query to use */
211 52
                AN(arg);
212 52
                vut_arg_q(vut, arg);
213 52
                return (1);
214 8
        case 'r':
215
                /* Binary file input */
216 8
                AN(arg);
217 8
                REPLACE(vut->r_arg, arg);
218 8
                return (1);
219 4
        case 't':
220
                /* VSM connect timeout */
221 4
                REPLACE(vut->t_arg, arg);
222 4
                return (1);
223 5
        case 'V':
224
                /* Print version number and exit */
225 5
                VCS_Message(vut->progname);
226 5
                exit(0);
227 144
        default:
228 144
                AN(vut->vsl);
229 144
                i = VSL_Arg(vut->vsl, opt, arg);
230 144
                if (i < 0)
231 18
                        VUT_Error(vut, 1, "%s", VSL_Error(vut->vsl));
232 126
                return (i);
233 429
        }
234 429
}
235
236
struct VUT *
237
VUT_Init(const char *progname, int argc, char * const *argv,
238
    const struct vopt_spec *voc)
239 235
{
240 235
        struct VUT *vut;
241
242 235
        AN(progname);
243 235
        AN(argv);
244 235
        AN(voc);
245
246 235
        VSIG_Arm_hup();
247 235
        VSIG_Arm_int();
248 235
        VSIG_Arm_term();
249 235
        VSIG_Arm_usr1();
250
251 235
        if (argc == 2 && !vstrcmp(argv[1], "--synopsis"))
252 0
                exit(vut_synopsis(voc));
253 235
        if (argc == 2 && !vstrcmp(argv[1], "--options"))
254 0
                exit(vut_options(voc));
255 235
        if (argc == 2 && !vstrcmp(argv[1], "--optstring")) {
256 0
                (void)printf("%s\n", voc->vopt_optstring);
257 0
                exit(0);
258 0
        }
259
260 235
        ALLOC_OBJ(vut, VUT_MAGIC);
261 235
        AN(vut);
262 235
        vut->progname = progname;
263 235
        vut->g_arg = VSL_g_vxid;
264 235
        vut->k_arg = -1;
265 235
        AZ(vut->vsl);
266 235
        vut->vsl = VSL_New();
267 235
        AN(vut->vsl);
268 235
        return (vut);
269 235
}
270
271
void
272
VUT_Signal(VUT_sighandler_f sig_cb)
273 0
{
274
275 0
        AN(sig_cb);
276 0
        (void)signal(SIGHUP, sig_cb);
277 0
        (void)signal(SIGINT, sig_cb);
278 0
        (void)signal(SIGTERM, sig_cb);
279 0
        (void)signal(SIGUSR1, sig_cb);
280 0
}
281
282
void
283
VUT_Signaled(struct VUT *vut, int sig)
284 0
{
285
286 0
        CHECK_OBJ_NOTNULL(vut, VUT_MAGIC);
287 0
#define VSIG_SIGNAL(UPPER, lower) \
288 0
        VSIG_##lower += (int)(sig == SIG##UPPER);
289 0
#include "tbl/vsig_list.h"
290 0
}
291
292
void
293
VUT_Setup(struct VUT *vut)
294 127
{
295 127
        struct VSL_cursor *c;
296
297 127
        CHECK_OBJ_NOTNULL(vut, VUT_MAGIC);
298 127
        AN(vut->vsl);
299 127
        AZ(vut->vsm);
300 127
        AZ(vut->vslq);
301
302
        /* Check input arguments (2 used for bug in FlexeLint) */
303 127
        if ((vut->n_arg == NULL ? 0 : 2) +
304 127
            (vut->r_arg == NULL ? 0 : 2) > 2)
305 1
                VUT_Error(vut, 1, "Only one of -n and -r options may be used");
306
307 126
        if (vut->r_arg != NULL && !vstrcmp(vut->r_arg, "-") && vut->D_opt)
308 0
                VUT_Error(vut, 1, "Daemon cannot read from stdin");
309
310
        /* Create and validate the query expression */
311 126
        vut->vslq = VSLQ_New(vut->vsl, NULL,
312 126
            (enum VSL_grouping_e)vut->g_arg, vut->q_arg);
313 126
        if (vut->vslq == NULL)
314 32
                VUT_Error(vut, 1, "Query expression error:\n%s",
315 32
                    VSL_Error(vut->vsl));
316
317 94
        if (vut->dryrun_opt)
318 0
                return;
319
320
        /* Setup input */
321 94
        if (vut->r_arg) {
322 7
                c = VSL_CursorFile(vut->vsl, vut->r_arg, 0);
323 7
                if (c == NULL)
324 2
                        VUT_Error(vut, 1, "%s", VSL_Error(vut->vsl));
325 5
                VSLQ_SetCursor(vut->vslq, &c);
326 5
                AZ(c);
327 87
        } else {
328 87
                vut->vsm = VSM_New();
329 87
                AN(vut->vsm);
330 87
                if (vut->n_arg && VSM_Arg(vut->vsm, 'n', vut->n_arg) <= 0)
331 0
                        VUT_Error(vut, 1, "%s", VSM_Error(vut->vsm));
332 87
                if (vut->t_arg && VSM_Arg(vut->vsm, 't', vut->t_arg) <= 0)
333 3
                        VUT_Error(vut, 1, "%s", VSM_Error(vut->vsm));
334 84
                if (VSM_Attach(vut->vsm, STDERR_FILENO))
335 1
                        VUT_Error(vut, 1, "VSM: %s", VSM_Error(vut->vsm));
336
                // Cursor is handled in VUT_Main()
337 84
        }
338
339
        /* Open PID file */
340 88
        if (vut->P_arg) {
341 4
                if (pfh != NULL)
342 0
                        VUT_Error(vut, 1, "PID file already created");
343 4
                pfh = VPF_Open(vut->P_arg, 0644, NULL);
344 4
                if (pfh == NULL)
345 0
                        VUT_Error(vut, 1,
346 0
                            "%s: %s", vut->P_arg, strerror(errno));
347 4
        }
348
349
        /* Daemon mode */
350 88
        if (vut->D_opt && vut_daemon(vut) == -1)
351 0
                VUT_Error(vut, 1, "Daemon mode: %s", strerror(errno));
352
353
        /* Write PID and setup exit handler */
354 88
        if (vut->P_arg) {
355 4
                AN(pfh);
356 4
                VPF_Write(pfh);
357
358
                /* NB: move ownership to a global pseudo-VUT. */
359 4
                INIT_OBJ(&pfh_vut, VUT_MAGIC);
360 4
                pfh_vut.P_arg = vut->P_arg;
361 4
                pfh_vut.error_f = vut->error_f;
362 4
                vut->P_arg = NULL;
363
364 4
                AZ(atexit(vut_vpf_remove));
365 4
        }
366 88
}
367
368
void
369
VUT_Fini(struct VUT **vutp)
370 88
{
371 88
        struct VUT *vut;
372
373 88
        TAKE_OBJ_NOTNULL(vut, vutp, VUT_MAGIC);
374 88
        AN(vut->progname);
375
376 88
        free(vut->n_arg);
377 88
        free(vut->q_arg);
378 88
        free(vut->r_arg);
379 88
        free(vut->t_arg);
380 88
        AZ(vut->P_arg);
381
382 88
        if (vut->vslq)
383 88
                VSLQ_Delete(&vut->vslq);
384 88
        if (vut->vsl)
385 88
                VSL_Delete(vut->vsl);
386 88
        if (vut->vsm)
387 83
                VSM_Destroy(&vut->vsm);
388
389 88
        FREE_OBJ(vut);
390 88
}
391
392
static void
393
vut_CursorError(struct VUT *vut, vtim_mono *last)
394 0
{
395 0
        const char *diag;
396 0
        vtim_mono now;
397
398 0
        CHECK_OBJ_NOTNULL(vut, VUT_MAGIC);
399 0
        AN(vut->vsl);
400 0
        AN(last);
401
402 0
        diag = VSL_Error(vut->vsl);
403 0
        if (diag == NULL)
404 0
                diag = "Missing diagnostic";
405
406 0
        now = VTIM_mono();
407 0
        if (isnan(*last) || *last + 1 < now) {
408 0
                fprintf(stderr, "Failed to acquire log: %s\n", diag);
409 0
                *last = now;
410 0
        }
411 0
}
412
413
int
414
VUT_Main(struct VUT *vut)
415 88
{
416 88
        struct VSL_cursor *c;
417 88
        int i = -1;
418 88
        int hascursor = -1;
419 88
        vtim_mono t_failcursor = NAN;
420
421 88
        CHECK_OBJ_NOTNULL(vut, VUT_MAGIC);
422 88
        AN(vut->vslq);
423
424 6898
        while (!VSIG_int && !VSIG_term && !vut->dryrun_opt) {
425 6886
                if (VSIG_hup != vut->last_sighup) {
426
                        /* sighup callback */
427 4
                        vut->last_sighup = VSIG_hup;
428 4
                        if (vut->sighup_f != NULL)
429 2
                                i = vut->sighup_f(vut);
430 2
                        else
431 2
                                i = 1;
432 4
                        if (i)
433 2
                                break;
434 4
                }
435
436 6884
                if (VSIG_usr1 != vut->last_sigusr1) {
437
                        /* Flush and report any incomplete records */
438 0
                        vut->last_sigusr1 = VSIG_usr1;
439 0
                        (void)VSLQ_Flush(vut->vslq, vut_dispatch, vut);
440 0
                }
441
442
                /* We must repeatedly call VSM_Status() when !hascursor
443
                 * to make VSM discover our segment.
444
                 *
445
                 * XXX consider moving the error handling to VSLQ_Dispatch.
446
                 * or some other VSL utility function
447
                 * Reasons:
448
                 *
449
                 * - it does not seem to make much sense to call VSM_StillValid
450
                 *   in vsl if that can only detect invalid segments after
451
                 *   VSM_Status has run, so it appears both should be
452
                 *   consolidated
453
                 *
454
                 * - not all VSL Clients will use VUT, yet the log abandoned/
455
                 *   overrun situation will be occur for all of them.
456
                 */
457
458 6884
                if (vut->vsm != NULL &&
459 6884
                    (VSM_Status(vut->vsm) & VSM_WRK_RESTARTED)) {
460 0
                        if (hascursor < 1) {
461 0
                                fprintf(stderr, "Log abandoned (vsm)\n");
462 0
                                VSLQ_SetCursor(vut->vslq, NULL);
463 0
                                hascursor = 0;
464 0
                        }
465 0
                }
466 6884
                if (vut->vsm != NULL && hascursor < 1) {
467
                        /* Reconnect VSM */
468 83
                        AZ(vut->r_arg);
469 83
                        VTIM_sleep(0.1);
470 83
                        c = VSL_CursorVSM(vut->vsl, vut->vsm,
471 83
                            (vut->d_opt ? VSL_COPT_TAILSTOP : VSL_COPT_TAIL)
472 83
                            | VSL_COPT_BATCH);
473 83
                        if (c == NULL) {
474 0
                                vut_CursorError(vut, &t_failcursor);
475 0
                                VSL_ResetError(vut->vsl);
476 0
                                continue;
477 0
                        }
478 83
                        if (hascursor >= 0)
479 0
                                fprintf(stderr, "Log reacquired\n");
480 83
                        hascursor = 1;
481 83
                        VSLQ_SetCursor(vut->vslq, &c);
482 83
                        AZ(c);
483 83
                }
484
485 6884
                do
486 11056
                        i = VSLQ_Dispatch(vut->vslq, vut_dispatch, vut);
487 11056
                while (i == vsl_more &&
488 11056
                    VSIG_usr1 == vut->last_sigusr1 &&
489 11056
                    VSIG_hup == vut->last_sighup);
490
491 6884
                if (i == vsl_more)
492 0
                        continue;
493 6884
                else if (i == vsl_end) {
494 6810
                        if (vut->idle_f) {
495 3221
                                i = vut->idle_f(vut);
496 3221
                                if (i)
497 0
                                        break;
498 3221
                        }
499 6810
                        VTIM_sleep(0.01);
500 6810
                        continue;
501 6810
                } else if (i == vsl_e_eof)
502 74
                        break;
503
504 0
                if (vut->vsm == NULL)
505 0
                        break;
506
507
                /* XXX: Make continuation optional */
508
509 0
                (void)VSLQ_Flush(vut->vslq, vut_dispatch, vut);
510
511 0
                if (i == vsl_e_abandon) {
512 0
                        fprintf(stderr, "Log abandoned (vsl)\n");
513 0
                        VSLQ_SetCursor(vut->vslq, NULL);
514 0
                        hascursor = 0;
515 0
                } else if (i == vsl_e_overrun) {
516 0
                        fprintf(stderr, "Log overrun\n");
517 0
                        VSLQ_SetCursor(vut->vslq, NULL);
518 0
                        hascursor = 0;
519 0
                } else
520 0
                        fprintf(stderr, "Error %d from VSLQ_Dispatch()", i);
521 0
        }
522
523 88
        return (i);
524 88
}
525
526
/**********************************************************************/
527
528
void v_noreturn_
529
VUT_Usage(const struct VUT *vut, const struct vopt_spec *voc, int status)
530 10
{
531 10
        const char **opt;
532
533 10
        fprintf(stderr, "Usage: %s <options>\n\n", vut->progname);
534 10
        fprintf(stderr, "Options:\n");
535 254
        for (opt = voc->vopt_usage; *opt != NULL; opt += 2)
536 244
                fprintf(stderr, " %-25s %s\n", *opt, *(opt + 1));
537 10
        exit(status);
538 10
}
539
540
/**********************************************************************/
541
542
543
static void
544
print_nobrackets(const char *s)
545 0
{
546 0
        const char *e;
547
548
        /* Remove whitespace */
549 0
        while (isspace(*s))
550 0
                s++;
551 0
        e = s + vstrlen(s);
552 0
        while (e > s && isspace(e[-1]))
553 0
                e--;
554
555
        /* Remove outer layer brackets if present */
556 0
        if (e > s && *s == '[' && e[-1] == ']') {
557 0
                s++;
558 0
                e--;
559 0
        }
560
561 0
        printf("%.*s", (int)(e - s), s);
562 0
}
563
564
static void
565
print_tabbed(const char *string, int tabs)
566 0
{
567 0
        int i;
568 0
        const char *c;
569
570 0
        for (c = string; *c; c++) {
571 0
                if (c == string || *(c - 1) == '\n')
572 0
                        for (i = 0; i < tabs; i++)
573 0
                                printf("\t");
574 0
                printf("%c", *c);
575 0
        }
576 0
}
577
578
static void
579
print_opt(const struct vopt_list *opt)
580 0
{
581 0
        print_nobrackets(opt->synopsis);
582 0
        printf("\n\n");
583 0
        print_tabbed(opt->ldesc, 1);
584 0
        printf("\n\n");
585 0
}
586
587
static int
588
vut_synopsis(const struct vopt_spec *voc)
589 0
{
590 0
        printf(".. |synopsis| replace:: %s\n", voc->vopt_synopsis);
591 0
        return (0);
592 0
}
593
594
static int
595
vut_options(const struct vopt_spec *voc)
596 0
{
597 0
        int i;
598
599 0
        for (i = 0; i < voc->vopt_list_n; i++)
600 0
                print_opt(&voc->vopt_list[i]);
601 0
        printf("--optstring\n"
602 0
            "\tPrint the optstring parameter to ``getopt(3)`` to help"
603 0
            " writing wrapper scripts.\n\n");
604 0
        return (0);
605 0
}