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