| 1 |
#define _GNU_SOURCE |
| 2 |
#include <stdio.h> |
| 3 |
#include <stdarg.h> |
| 4 |
#include <stdlib.h> |
| 5 |
#include <string.h> |
| 6 |
#include <syslog.h> |
| 7 |
#include <errno.h> |
| 8 |
#include <unistd.h> |
| 9 |
#include <fcntl.h> |
| 10 |
#include <sys/types.h> |
| 11 |
#include <signal.h> |
| 12 |
#include "utils.h" |
| 13 |
|
| 14 |
static void default_xprintf(const char *fmt, ...) |
| 15 |
{ |
| 16 |
va_list args; |
| 17 |
|
| 18 |
va_start(args, fmt); |
| 19 |
vfprintf(stderr, fmt, args); |
| 20 |
va_end(args); |
| 21 |
fflush(stderr); |
| 22 |
} |
| 23 |
|
| 24 |
void (*xprintf)(const char *fmt, ...) = default_xprintf; |
| 25 |
|
| 26 |
void barf(const char *fmt, ...) |
| 27 |
{ |
| 28 |
char *str; |
| 29 |
int bytes; |
| 30 |
va_list arglist; |
| 31 |
|
| 32 |
xprintf("FATAL: "); |
| 33 |
|
| 34 |
va_start(arglist, fmt); |
| 35 |
bytes = vasprintf(&str, fmt, arglist); |
| 36 |
va_end(arglist); |
| 37 |
|
| 38 |
if (bytes >= 0) { |
| 39 |
syslog(LOG_CRIT, "%s\n", str); |
| 40 |
xprintf("%s\n", str); |
| 41 |
free(str); |
| 42 |
} |
| 43 |
exit(1); |
| 44 |
} |
| 45 |
|
| 46 |
void barf_perror(const char *fmt, ...) |
| 47 |
{ |
| 48 |
char *str; |
| 49 |
int bytes, err = errno; |
| 50 |
va_list arglist; |
| 51 |
|
| 52 |
xprintf("FATAL: "); |
| 53 |
|
| 54 |
va_start(arglist, fmt); |
| 55 |
bytes = vasprintf(&str, fmt, arglist); |
| 56 |
va_end(arglist); |
| 57 |
|
| 58 |
if (bytes >= 0) { |
| 59 |
syslog(LOG_CRIT, "%s: %s\n", str, strerror(err)); |
| 60 |
xprintf("%s: %s\n", str, strerror(err)); |
| 61 |
free(str); |
| 62 |
} |
| 63 |
exit(1); |
| 64 |
} |
| 65 |
|
| 66 |
const char *dump_state_align(FILE *fp) |
| 67 |
{ |
| 68 |
long len; |
| 69 |
static char nul[8] = {}; |
| 70 |
|
| 71 |
len = ftell(fp); |
| 72 |
if (len < 0) |
| 73 |
return "Dump state align error"; |
| 74 |
len &= 7; |
| 75 |
if (!len) |
| 76 |
return NULL; |
| 77 |
|
| 78 |
if (fwrite(nul, 8 - len, 1, fp) != 1) |
| 79 |
return "Dump state align error"; |
| 80 |
return NULL; |
| 81 |
} |