varnish-cache/lib/libvarnish/vas.c
0
/*-
1
 * Copyright (c) 2006 Verdens Gang AS
2
 * Copyright (c) 2006-2016 Varnish Software AS
3
 * All rights reserved.
4
 *
5
 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
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
 * This is the default backend function for libvarnish' assert facilities.
31
 */
32
33
#include "config.h"
34
35
#include <stdio.h>
36
#include <stdlib.h>
37
#include <string.h>
38
39
#include "vdef.h"
40
41
#include "vas.h"
42
43
const char *
44 78827
VAS_errtxt(int e)
45
{
46
        const char *p;
47 78827
        int oerrno = errno;
48
49 78827
        p = strerror(e);
50 78827
        if (p != NULL)
51 78827
                return (p);
52
53 0
        errno = oerrno;
54 0
        return ("strerror(3) returned NULL");
55 78827
}
56
57
vas_f *VAS_Fail_Func v_noreturn_;
58
59
void v_noreturn_
60 516
VAS_Fail(const char *func, const char *file, int line,
61
    const char *cond, enum vas_e kind)
62
{
63 516
        int err = errno;
64
65 516
        if (VAS_Fail_Func != NULL) {
66 516
                VAS_Fail_Func(func, file, line, cond, kind);
67
        } else {
68 0
                if (kind == VAS_MISSING) {
69 0
                        fprintf(stderr,
70
                            "Missing error handling code in %s(), %s line %d:\n"
71
                            "  Condition(%s) not true.\n",
72 0
                            func, file, line, cond);
73 0
                } else if (kind == VAS_INCOMPLETE) {
74 0
                        fprintf(stderr,
75
                            "Incomplete code in %s(), %s line %d:\n",
76 0
                            func, file, line);
77 0
                } else if (kind == VAS_WRONG) {
78 0
                        fprintf(stderr,
79
                            "Wrong turn in %s(), %s line %d: %s\n",
80 0
                            func, file, line, cond);
81 0
                } else {
82 0
                        fprintf(stderr,
83
                            "Assert error in %s(), %s line %d:\n"
84
                            "  Condition(%s) not true.\n",
85 0
                            func, file, line, cond);
86
                }
87 0
                if (err)
88 0
                        fprintf(stderr,
89 0
                            "  errno = %d (%s)\n", err, strerror(err));
90
        }
91 0
        abort();
92
}