1 /*
2  *  error -- OpenVPN compatible error reporting API
3  *           https://community.openvpn.net/openvpn/wiki/Tapctl
4  *
5  *  Copyright (C) 2002-2022 OpenVPN Inc <sales@openvpn.net>
6  *  Copyright (C) 2018-2022 Simon Rozman <simon@rozman.si>
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License version 2
10  *  as published by the Free Software Foundation.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License along
18  *  with this program; if not, write to the Free Software Foundation, Inc.,
19  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21 
22 #ifndef ERROR_H
23 #define ERROR_H
24 
25 #include <stdarg.h>
26 #include <stdbool.h>
27 #include <stdlib.h>
28 
29 /*
30  * These globals should not be accessed directly,
31  * but rather through macros or inline functions defined below.
32  */
33 extern unsigned int x_debug_level;
34 extern int x_msg_line_num;
35 
36 /* msg() flags */
37 
38 #define M_DEBUG_LEVEL     (0x0F)         /* debug level mask */
39 
40 #define M_FATAL           (1<<4)         /* exit program */
41 #define M_NONFATAL        (1<<5)         /* non-fatal error */
42 #define M_WARN            (1<<6)         /* call syslog with LOG_WARNING */
43 #define M_DEBUG           (1<<7)
44 
45 #define M_ERRNO           (1<<8)         /* show errno description */
46 
47 #define M_NOMUTE          (1<<11)        /* don't do mute processing */
48 #define M_NOPREFIX        (1<<12)        /* don't show date/time prefix */
49 #define M_USAGE_SMALL     (1<<13)        /* fatal options error, call usage_small */
50 #define M_MSG_VIRT_OUT    (1<<14)        /* output message through msg_status_output callback */
51 #define M_OPTERR          (1<<15)        /* print "Options error:" prefix */
52 #define M_NOLF            (1<<16)        /* don't print new line */
53 #define M_NOIPREFIX       (1<<17)        /* don't print instance prefix */
54 
55 /* flag combinations which are frequently used */
56 #define M_ERR     (M_FATAL | M_ERRNO)
57 #define M_USAGE   (M_USAGE_SMALL | M_NOPREFIX | M_OPTERR)
58 #define M_CLIENT  (M_MSG_VIRT_OUT | M_NOMUTE | M_NOIPREFIX)
59 
60 
61 /** Check muting filter */
62 bool dont_mute(unsigned int flags);
63 
64 /* Macro to ensure (and teach static analysis tools) we exit on fatal errors */
65 #ifdef _MSC_VER
66 #pragma warning(disable: 4127) /* EXIT_FATAL(flags) macro raises "warning C4127: conditional expression is constant" on each non M_FATAL invocation. */
67 #endif
68 #define EXIT_FATAL(flags) do { if ((flags) & M_FATAL) {_exit(1);}} while (false)
69 
70 #define HAVE_VARARG_MACROS
71 #define msg(flags, ...) do { if (msg_test(flags)) {x_msg((flags), __VA_ARGS__);} EXIT_FATAL(flags); } while (false)
72 #ifdef ENABLE_DEBUG
73 #define dmsg(flags, ...) do { if (msg_test(flags)) {x_msg((flags), __VA_ARGS__);} EXIT_FATAL(flags); } while (false)
74 #else
75 #define dmsg(flags, ...)
76 #endif
77 
78 void x_msg(const unsigned int flags, const char *format, ...);     /* should be called via msg above */
79 
80 void x_msg_va(const unsigned int flags, const char *format, va_list arglist);
81 
82 /* Inline functions */
83 
84 static inline bool
check_debug_level(unsigned int level)85 check_debug_level(unsigned int level)
86 {
87     return (level & M_DEBUG_LEVEL) <= x_debug_level;
88 }
89 
90 /** Return true if flags represent and enabled, not muted log level */
91 static inline bool
msg_test(unsigned int flags)92 msg_test(unsigned int flags)
93 {
94     return check_debug_level(flags) && dont_mute(flags);
95 }
96 
97 #endif /* ifndef ERROR_H */
98