| | 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 |
|
#include <syslog.h> |
| 39 |
|
|
| 40 |
|
#include "vdef.h" |
| 41 |
|
#include "vbt.h" |
| 42 |
|
|
| 43 |
|
#include "vas.h" |
| 44 |
|
|
| 45 |
|
const char * |
| 46 |
2018 |
VAS_errtxt(int e) |
| 47 |
|
{ |
| 48 |
|
const char *p; |
| 49 |
2018 |
int oerrno = errno; |
| 50 |
|
|
| 51 |
2018 |
p = strerror(e); |
| 52 |
2018 |
if (p != NULL) |
| 53 |
2018 |
return (p); |
| 54 |
|
|
| 55 |
0 |
errno = oerrno; |
| 56 |
0 |
return ("strerror(3) returned NULL"); |
| 57 |
2018 |
} |
| 58 |
|
|
| 59 |
|
vas_f *VAS_Fail_Func v_noreturn_; |
| 60 |
|
|
| 61 |
|
static void |
| 62 |
0 |
vas_default(const char *func, const char *file, int line, |
| 63 |
|
const char *cond, enum vas_e kind) |
| 64 |
|
{ |
| 65 |
0 |
int err = errno; |
| 66 |
|
char buf[4096]; |
| 67 |
|
|
| 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 |
0 |
} |
| 91 |
|
|
| 92 |
0 |
if (VBT_dump(sizeof buf, buf) < 0) { |
| 93 |
0 |
bprintf(buf, "Failed to print backtrace: %d (%s)", |
| 94 |
|
errno, strerror(errno)); |
| 95 |
0 |
} |
| 96 |
|
|
| 97 |
0 |
syslog(LOG_DEBUG, "%s", buf); |
| 98 |
0 |
} |
| 99 |
|
|
| 100 |
|
void v_noreturn_ |
| 101 |
12 |
VAS_Fail(const char *func, const char *file, int line, |
| 102 |
|
const char *cond, enum vas_e kind) |
| 103 |
|
{ |
| 104 |
|
|
| 105 |
12 |
if (VAS_Fail_Func != NULL) |
| 106 |
12 |
VAS_Fail_Func(func, file, line, cond, kind); |
| 107 |
|
else |
| 108 |
0 |
vas_default(func, file, line, cond, kind); |
| 109 |
0 |
abort(); |
| 110 |
|
} |