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