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