OpenVPN
error.h
Go to the documentation of this file.
1 /*
2  * OpenVPN -- An application to securely tunnel IP networks
3  * over a single TCP/UDP port, with support for SSL/TLS-based
4  * session authentication and key exchange,
5  * packet encryption, packet authentication, and
6  * packet compression.
7  *
8  * Copyright (C) 2002-2018 OpenVPN Inc <sales@openvpn.net>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License version 2
12  * as published by the Free Software Foundation.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23 
24 #ifndef ERROR_H
25 #define ERROR_H
26 
27 #include "basic.h"
28 
29 #include <errno.h>
30 #include <stdbool.h>
31 
32 #include <assert.h>
33 
34 #if _WIN32
35 #include <windows.h>
36 #endif
37 
38 /* #define ABORT_ON_ERROR */
39 
40 #ifdef ENABLE_PKCS11
41 #define ERR_BUF_SIZE 8192
42 #else
43 #define ERR_BUF_SIZE 1280
44 #endif
45 
46 struct gc_arena;
47 
48 /*
49  * Where should messages be printed before syslog is opened?
50  * Not used if OPENVPN_DEBUG_COMMAND_LINE is defined.
51  */
52 #define OPENVPN_MSG_FP stdout
53 #define OPENVPN_ERROR_FP stderr
54 
55 /*
56  * Exit status codes
57  */
58 
59 #define OPENVPN_EXIT_STATUS_GOOD 0
60 #define OPENVPN_EXIT_STATUS_ERROR 1
61 #define OPENVPN_EXIT_STATUS_USAGE 1
62 #define OPENVPN_EXIT_STATUS_CANNOT_OPEN_DEBUG_FILE 1
63 
64 /*
65  * Special command line debugging mode.
66  * If OPENVPN_DEBUG_COMMAND_LINE
67  * is defined, contents of argc/argv will
68  * be dumped to OPENVPN_DEBUG_FILE as well
69  * as all other OpenVPN messages.
70  */
71 
72 /* #define OPENVPN_DEBUG_COMMAND_LINE */
73 #define OPENVPN_DEBUG_FILE PACKAGE ".log"
74 
75 /* String and Error functions */
76 
77 #ifdef _WIN32
78 #define openvpn_errno() GetLastError()
79 #define openvpn_strerror(e, gc) strerror_win32(e, gc)
80 const char *strerror_win32(DWORD errnum, struct gc_arena *gc);
81 
82 #else
83 #define openvpn_errno() errno
84 #define openvpn_strerror(x, gc) strerror(x)
85 #endif
86 
87 /*
88  * These globals should not be accessed directly,
89  * but rather through macros or inline functions defined below.
90  */
91 extern unsigned int x_debug_level;
92 extern int x_msg_line_num;
93 
94 /* msg() flags */
95 
96 #define M_DEBUG_LEVEL (0x0F) /* debug level mask */
97 
98 #define M_FATAL (1<<4) /* exit program */
99 #define M_NONFATAL (1<<5) /* non-fatal error */
100 #define M_WARN (1<<6) /* call syslog with LOG_WARNING */
101 #define M_DEBUG (1<<7)
102 
103 #define M_ERRNO (1<<8) /* show errno description */
104 
105 #define M_NOMUTE (1<<11) /* don't do mute processing */
106 #define M_NOPREFIX (1<<12) /* don't show date/time prefix */
107 #define M_USAGE_SMALL (1<<13) /* fatal options error, call usage_small */
108 #define M_MSG_VIRT_OUT (1<<14) /* output message through msg_status_output callback */
109 #define M_OPTERR (1<<15) /* print "Options error:" prefix */
110 #define M_NOLF (1<<16) /* don't print new line */
111 #define M_NOIPREFIX (1<<17) /* don't print instance prefix */
112 
113 /* flag combinations which are frequently used */
114 #define M_ERR (M_FATAL | M_ERRNO)
115 #define M_USAGE (M_USAGE_SMALL | M_NOPREFIX | M_OPTERR)
116 #define M_CLIENT (M_MSG_VIRT_OUT | M_NOMUTE | M_NOIPREFIX)
117 
118 /*
119  * Mute levels are designed to avoid large numbers of
120  * mostly similar messages clogging the log file.
121  *
122  * A mute level of 0 is always printed.
123  */
124 #define MUTE_LEVEL_SHIFT 24
125 #define MUTE_LEVEL_MASK 0xFF
126 
127 #define ENCODE_MUTE_LEVEL(mute_level) (((mute_level) & MUTE_LEVEL_MASK) << MUTE_LEVEL_SHIFT)
128 #define DECODE_MUTE_LEVEL(flags) (((flags) >> MUTE_LEVEL_SHIFT) & MUTE_LEVEL_MASK)
129 
130 /*
131  * log_level: verbosity level n (--verb n) must be >= log_level to print.
132  * mute_level: don't print more than n (--mute n) consecutive messages at
133  * a given mute level, or if 0 disable muting and print everything.
134  *
135  * Mask map:
136  * Bits 0-3: log level
137  * Bits 4-23: M_x flags
138  * Bits 24-31: mute level
139  */
140 #define LOGLEV(log_level, mute_level, other) ((log_level) | ENCODE_MUTE_LEVEL(mute_level) | other)
141 
142 /*
143  * If compiler supports variable arguments in macros, define
144  * msg() as a macro for optimization win.
145  */
146 
148 bool dont_mute(unsigned int flags);
149 
150 /* Macro to ensure (and teach static analysis tools) we exit on fatal errors */
151 #define EXIT_FATAL(flags) do { if ((flags) & M_FATAL) {_exit(1);}} while (false)
152 
153 #define msg(flags, ...) do { if (msg_test(flags)) {x_msg((flags), __VA_ARGS__);} EXIT_FATAL(flags); } while (false)
154 #ifdef ENABLE_DEBUG
155 #define dmsg(flags, ...) do { if (msg_test(flags)) {x_msg((flags), __VA_ARGS__);} EXIT_FATAL(flags); } while (false)
156 #else
157 #define dmsg(flags, ...)
158 #endif
159 
160 void x_msg(const unsigned int flags, const char *format, ...)
161 #ifdef __GNUC__
162 #if __USE_MINGW_ANSI_STDIO
163 __attribute__ ((format(gnu_printf, 2, 3)))
164 #else
165 __attribute__ ((format(__printf__, 2, 3)))
166 #endif
167 #endif
168 ; /* should be called via msg above */
169 
170 void x_msg_va(const unsigned int flags, const char *format, va_list arglist);
171 
172 /*
173  * Function prototypes
174  */
175 
176 void error_reset(void);
177 
178 /* route errors to stderr that would normally go to stdout */
179 void errors_to_stderr(void);
180 
181 void set_suppress_timestamps(bool suppressed);
182 
183 void set_machine_readable_output(bool parsable);
184 
185 
186 #define SDL_CONSTRAIN (1<<0)
187 bool set_debug_level(const int level, const unsigned int flags);
188 
189 bool set_mute_cutoff(const int cutoff);
190 
191 int get_debug_level(void);
192 
193 int get_mute_cutoff(void);
194 
195 const char *msg_flags_string(const unsigned int flags, struct gc_arena *gc);
196 
197 /*
198  * File to print messages to before syslog is opened.
199  */
200 FILE *msg_fp(const unsigned int flags);
201 
202 /* Fatal logic errors */
203 #ifndef ENABLE_SMALL
204 #define ASSERT(x) do { if (!(x)) {assert_failed(__FILE__, __LINE__, #x);}} while (false)
205 #else
206 #define ASSERT(x) do { if (!(x)) {assert_failed(__FILE__, __LINE__, NULL);}} while (false)
207 #endif
208 
209 #ifdef _MSC_VER
210 __declspec(noreturn)
211 #endif
212 void assert_failed(const char *filename, int line, const char *condition)
213 #ifndef _MSC_VER
214 __attribute__((__noreturn__))
215 #endif
216 ;
217 
218 /* Poor-man's static_assert() for when not supplied by assert.h, taken from
219  * Linux's sys/cdefs.h under GPLv2 */
220 #ifndef static_assert
221 #define static_assert(expr, diagnostic) \
222  extern int (*__OpenVPN_static_assert_function(void)) \
223  [!!sizeof(struct { int __error_if_negative : (expr) ? 2 : -1; })]
224 #endif
225 
226 #ifdef ENABLE_DEBUG
227 void crash(void); /* force a segfault (debugging only) */
228 
229 #endif
230 
231 /* Inline functions */
232 
233 static inline bool
234 check_debug_level(unsigned int level)
235 {
236  return (level & M_DEBUG_LEVEL) <= x_debug_level;
237 }
238 
240 static inline bool
241 msg_test(unsigned int flags)
242 {
243  return check_debug_level(flags) && dont_mute(flags);
244 }
245 
246 /* Call if we forked */
247 void msg_forked(void);
248 
249 /* syslog output */
250 
251 void open_syslog(const char *pgmname, bool stdio_to_null);
252 
253 void close_syslog(void);
254 
255 /* log file output */
256 void redirect_stdout_stderr(const char *file, bool append);
257 
258 #ifdef _WIN32
259 /* get original stderr handle, even if redirected by --log/--log-append */
260 HANDLE get_orig_stderr(void);
261 
262 #endif
263 
264 /* exit program */
265 void openvpn_exit(const int status);
266 
267 /* exit program on out of memory error */
268 void out_of_memory(void);
269 
270 /*
271  * Check the return status of read/write routines.
272  */
273 
274 struct link_socket;
275 struct tuntap;
276 
277 extern unsigned int x_cs_info_level;
278 extern unsigned int x_cs_verbose_level;
279 extern unsigned int x_cs_err_delay_ms;
280 
281 void reset_check_status(void);
282 
283 void set_check_status(unsigned int info_level, unsigned int verbose_level);
284 
285 void x_check_status(int status,
286  const char *description,
287  struct link_socket *sock,
288  struct tuntap *tt);
289 
290 static inline void
291 check_status(int status, const char *description, struct link_socket *sock, struct tuntap *tt)
292 {
293  if (status < 0 || check_debug_level(x_cs_verbose_level))
294  {
295  x_check_status(status, description, sock, tt);
296  }
297 }
298 
299 static inline void
300 set_check_status_error_delay(unsigned int milliseconds)
301 {
302  x_cs_err_delay_ms = milliseconds;
303 }
304 
305 /*
306  * In multiclient mode, put a client-specific prefix
307  * before each message.
308  *
309  * TODO: x_msg_prefix should be thread-local
310  */
311 
312 extern const char *x_msg_prefix;
313 
314 void msg_thread_init(void);
315 
316 void msg_thread_uninit(void);
317 
318 static inline void
319 msg_set_prefix(const char *prefix)
320 {
321  x_msg_prefix = prefix;
322 }
323 
324 static inline const char *
326 {
327  return x_msg_prefix;
328 }
329 
330 /*
331  * Allow MSG to be redirected through a virtual_output object
332  */
333 
334 struct virtual_output;
335 
336 extern const struct virtual_output *x_msg_virtual_output;
337 
338 static inline void
340 {
341  x_msg_virtual_output = vo;
342 }
343 
344 static inline const struct virtual_output *
346 {
347  return x_msg_virtual_output;
348 }
349 
350 /*
351  * Return true if this is a system error
352  * which can be safely ignored.
353  */
354 static inline bool
355 ignore_sys_error(const int err)
356 {
357  /* I/O operation pending */
358 #ifdef _WIN32
359  if (err == WSAEWOULDBLOCK || err == WSAEINVAL)
360  {
361  return true;
362  }
363 #else
364  if (err == EAGAIN)
365  {
366  return true;
367  }
368 #endif
369 
370 #if 0 /* if enabled, suppress ENOBUFS errors */
371 #ifdef ENOBUFS
372  /* No buffer space available */
373  if (err == ENOBUFS)
374  {
375  return true;
376  }
377 #endif
378 #endif
379 
380  return false;
381 }
382 
384 static inline unsigned int
385 nonfatal(const unsigned int err)
386 {
387  return err & M_FATAL ? (err ^ M_FATAL) | M_NONFATAL : err;
388 }
389 
390 #include "errlevel.h"
391 
392 #endif /* ifndef ERROR_H */
#define M_NONFATAL
Definition: error.h:99
int x_msg_line_num
Definition: error.c:212
void openvpn_exit(const int status)
Definition: error.c:730
void msg_thread_init(void)
Definition: tun.h:155
static void msg_set_virtual_output(const struct virtual_output *vo)
Definition: error.h:339
void msg_thread_uninit(void)
void redirect_stdout_stderr(const char *file, bool append)
Definition: error.c:513
void open_syslog(const char *pgmname, bool stdio_to_null)
Definition: error.c:456
void x_check_status(int status, const char *description, struct link_socket *sock, struct tuntap *tt)
Definition: error.c:654
static const char * msg_get_prefix(void)
Definition: error.h:325
const char * msg_flags_string(const unsigned int flags, struct gc_arena *gc)
Definition: error.c:778
unsigned int x_cs_verbose_level
Definition: error.c:628
void errors_to_stderr(void)
Definition: error.c:187
void error_reset(void)
Definition: error.c:163
static bool check_debug_level(unsigned int level)
Definition: error.h:234
list flags
#define M_DEBUG_LEVEL
Definition: error.h:96
static const struct virtual_output * msg_get_virtual_output(void)
Definition: error.h:345
HANDLE get_orig_stderr(void)
Definition: error.c:498
void out_of_memory(void)
Definition: error.c:449
__attribute__((unused))
Definition: test.c:41
void x_msg(const unsigned int flags, const char *format,...)
Definition: error.c:215
const char * strerror_win32(DWORD errnum, struct gc_arena *gc)
Definition: error.c:816
bool set_debug_level(const int level, const unsigned int flags)
Definition: error.c:107
static bool msg_test(unsigned int flags)
Return true if flags represent an enabled, not muted log level.
Definition: error.h:241
const struct virtual_output * x_msg_virtual_output
Definition: error.c:723
bool dont_mute(unsigned int flags)
Check muting filter.
Definition: error.c:396
void x_msg_va(const unsigned int flags, const char *format, va_list arglist)
Definition: error.c:224
void close_syslog(void)
Definition: error.c:480
void reset_check_status(void)
Definition: error.c:632
unsigned int x_debug_level
Definition: error.c:54
#define M_FATAL
Definition: error.h:98
static void check_status(int status, const char *description, struct link_socket *sock, struct tuntap *tt)
Definition: error.h:291
unsigned int x_cs_info_level
Definition: error.c:627
void set_check_status(unsigned int info_level, unsigned int verbose_level)
Definition: error.c:639
FILE * msg_fp(const unsigned int flags)
Definition: error.c:196
static bool ignore_sys_error(const int err)
Definition: error.h:355
static void set_check_status_error_delay(unsigned int milliseconds)
Definition: error.h:300
static unsigned int nonfatal(const unsigned int err)
Convert fatal errors to nonfatal, don&#39;t touch other errors.
Definition: error.h:385
Garbage collection arena used to keep track of dynamically allocated memory.
Definition: buffer.h:116
const char * x_msg_prefix
Definition: error.c:717
void set_machine_readable_output(bool parsable)
Definition: error.c:157
int get_debug_level(void)
Definition: error.c:139
void set_suppress_timestamps(bool suppressed)
Definition: error.c:151
unsigned int x_cs_err_delay_ms
Definition: error.c:629
bool set_mute_cutoff(const int cutoff)
Definition: error.c:125
static SERVICE_STATUS status
Definition: interactive.c:56
void assert_failed(const char *filename, int line, const char *condition) __attribute__((__noreturn__))
Definition: error.c:431
int get_mute_cutoff(void)
Definition: error.c:145
static void msg_set_prefix(const char *prefix)
Definition: error.h:319
void msg_forked(void)
Definition: error.c:101