File: error.h

package info (click to toggle)
fwupdate 12-7
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 588 kB
  • sloc: ansic: 4,329; makefile: 358; sh: 49
file content (166 lines) | stat: -rw-r--r-- 3,704 bytes parent folder | download | duplicates (7)
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
  error.h - some error functions to work with efivars error logger.

  Copyright 2016 Red Hat, Inc.  <pjones@redhat.com>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
#ifndef EFIBOOTMGR_ERROR_H__
#define EFIBOOTMGR_ERROR_H__ 1

extern int verbose;

static inline void
__attribute__((__unused__))
error_reporter(void)
{
	int rc = 1;
	int saved_errno = errno;

        for (int i = 0; rc > 0; i++) {
                char *filename = NULL;
                char *function = NULL;
                int line = 0;
                char *message = NULL;
                int error = 0;

                rc = efi_error_get(i, &filename, &function, &line, &message,
                                   &error);
                if (rc < 0) {
			fprintf(stderr, "error fetching trace value");
			exit(1);
		}
                if (rc == 0)
                        break;
                fprintf(stderr, " %s:%d %s(): %s: %s\n",
			filename, line, function, message, strerror(error));
        }
	errno = saved_errno;
}

static inline void
__attribute__((__unused__))
conditional_error_reporter(int show, int clear)
{
	int saved_errno = errno;
	fflush(NULL);

	if (show) {
		fprintf(stderr, "error trace:\n");
		error_reporter();
	}
	if (clear) {
		errno = 0;
		efi_error_clear();
	}
	errno = saved_errno;
}

static inline void
__attribute__((__unused__))
cond_error(int test, int eval, const char *fmt, ...)
{
	int saved_errno = errno;
	if (!test)
		return;
	fflush(NULL);

	va_list ap;
	va_start(ap, fmt);
	vfprintf(stderr, fmt, ap);
	errno = saved_errno;
	fprintf(stderr, ": %m\n");
	conditional_error_reporter(verbose >= 2, 0);
	va_end(ap);
	exit(eval);
}

static inline void
__attribute__((__unused__))
error(int eval, const char *fmt, ...)
{
	int saved_errno = errno;
	fflush(NULL);

	va_list ap;
	va_start(ap, fmt);
	vfprintf(stderr, fmt, ap);
	errno = saved_errno;
	fprintf(stderr, ": %m\n");
	conditional_error_reporter(verbose >= 2, 0);
	va_end(ap);
	exit(eval);
}

static inline void
__attribute__((__unused__))
errorx(int eval, const char *fmt, ...)
{
	fflush(NULL);

	va_list ap;
	va_start(ap, fmt);
	vfprintf(stderr, fmt, ap);
	fprintf(stderr, "\n");
	conditional_error_reporter(verbose >= 2, 1);
	va_end(ap);
	exit(eval);
}

static inline void
__attribute__((__unused__))
cond_warning(int test, const char *fmt, ...)
{
	int saved_errno = errno;
	if (!test)
		return;

	va_list ap;
	va_start(ap, fmt);
	vprintf(fmt, ap);
	errno = saved_errno;
	printf(": %m\n");
	conditional_error_reporter(verbose >= 2, 1);
	va_end(ap);
}

static inline void
__attribute__((__unused__))
warning(const char *fmt, ...)
{
	int saved_errno = errno;
	va_list ap;

	va_start(ap, fmt);
	vprintf(fmt, ap);
	errno = saved_errno;
	printf(": %m\n");
	conditional_error_reporter(verbose >= 2, 1);
	va_end(ap);
}

static inline void
__attribute__((__unused__))
warningx(const char *fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);
	vprintf(fmt, ap);
	printf("\n");
	conditional_error_reporter(verbose >= 2, 1);
	va_end(ap);
}
#endif /* EFIBOOTMGR_ERROR_H__ */