summaryrefslogtreecommitdiff
path: root/tools/xenstore/utils.c
blob: 0d80cb6de8cbbfc190abdcfb78c7aaa905b0881c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#define _GNU_SOURCE
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <signal.h>
#include "utils.h"

static void default_xprintf(const char *fmt, ...)
{
	va_list args;

	va_start(args, fmt);
	vfprintf(stderr, fmt, args);
	va_end(args);
	fflush(stderr);
}

void (*xprintf)(const char *fmt, ...) = default_xprintf;

void barf(const char *fmt, ...)
{
	char *str;
	int bytes;
	va_list arglist;

	xprintf("FATAL: ");

	va_start(arglist, fmt);
	bytes = vasprintf(&str, fmt, arglist);
	va_end(arglist);

 	if (bytes >= 0) {
		syslog(LOG_CRIT, "%s\n", str);
		xprintf("%s\n", str);
		free(str);
	}
	exit(1);
}

void barf_perror(const char *fmt, ...)
{
	char *str;
	int bytes, err = errno;
	va_list arglist;

	xprintf("FATAL: ");

	va_start(arglist, fmt);
	bytes = vasprintf(&str, fmt, arglist);
	va_end(arglist);

 	if (bytes >= 0) {
		syslog(LOG_CRIT, "%s: %s\n", str, strerror(err));
		xprintf("%s: %s\n", str, strerror(err));
		free(str);
	}
	exit(1);
}

const char *dump_state_align(FILE *fp)
{
	long len;
	static char nul[8] = {};

	len = ftell(fp);
	if (len < 0)
		return "Dump state align error";
	len &= 7;
	if (!len)
		return NULL;

	if (fwrite(nul, 8 - len, 1, fp) != 1)
		return "Dump state align error";
	return NULL;
}