1 /*
2  *
3   ***** BEGIN LICENSE BLOCK *****
4 
5   Copyright (C) 2009-2016 Olof Hagsand and Benny Holmgren
6   Copyright (C) 2017-2019 Olof Hagsand
7   Copyright (C) 2020 Olof Hagsand and Rubicon Communications, LLC(Netgate)
8 
9   This file is part of CLIXON.
10 
11   Licensed under the Apache License, Version 2.0 (the "License");
12   you may not use this file except in compliance with the License.
13   You may obtain a copy of the License at
14 
15     http://www.apache.org/licenses/LICENSE-2.0
16 
17   Unless required by applicable law or agreed to in writing, software
18   distributed under the License is distributed on an "AS IS" BASIS,
19   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20   See the License for the specific language governing permissions and
21   limitations under the License.
22 
23   Alternatively, the contents of this file may be used under the terms of
24   the GNU General Public License Version 3 or later (the "GPL"),
25   in which case the provisions of the GPL are applicable instead
26   of those above. If you wish to allow use of your version of this file only
27   under the terms of the GPL, and not to allow others to
28   use your version of this file under the terms of Apache License version 2,
29   indicate your decision by deleting the provisions above and replace them with
30   the  notice and other provisions required by the GPL. If you do not delete
31   the provisions above, a recipient may use your version of this file under
32   the terms of any one of the Apache License version 2 or the GPL.
33 
34   ***** END LICENSE BLOCK *****
35 
36  *
37  * Errors may be syslogged using LOG_ERR, and printed to stderr, as controlled by
38  * clicon_log_init
39  * global error variables are set:
40  *  clicon_errno, clicon_suberrno, clicon_err_reason.
41  */
42 
43 #ifndef _CLIXON_ERR_H_
44 #define _CLIXON_ERR_H_
45 
46 /*
47  * Constants
48  */
49 #define ERR_STRLEN 256
50 
51 /* Special error number for clicon_suberrno
52  * For catching xml parse errors as exceptions
53  */
54 #define XMLPARSE_ERRNO 898943
55 
56 /*
57  * Types
58  * Add error category here, but must also add an entry in EV variable.
59  */
60 enum clicon_err{
61     /* 0 means error not set) */
62     OE_DB = 1,   /* database registries */
63     OE_DAEMON,   /* demons: pidfiles, etc */
64     OE_EVENTS,   /* events, filedescriptors, timeouts */
65     OE_CFG,      /* configuration */
66     OE_NETCONF,  /* Netconf error */
67     OE_PROTO,    /* config/client communication */
68     OE_REGEX,    /* Regexp error */
69     OE_UNIX,     /* unix/linux syscall error */
70     OE_SYSLOG,   /* syslog error */
71     OE_ROUTING,  /* routing daemon error (eg quagga) */
72     OE_XML,      /* xml parsing etc */
73     OE_PLUGIN,   /* plugin loading, etc */
74     OE_YANG ,    /* Yang error */
75     OE_FATAL,    /* Fatal error */
76     OE_UNDEF,
77 };
78 
79 /*
80  * Variables
81  * XXX: should not be global
82  */
83 extern int  clicon_errno;    /* CLICON errors (see clicon_err) */
84 extern int  clicon_suberrno; /* Eg orig errno */
85 extern char clicon_err_reason[ERR_STRLEN];
86 
87 /*
88  * Macros
89  */
90 #define clicon_err(e,s,_fmt, args...)  clicon_err_fn(__FUNCTION__, __LINE__, (e), (s), _fmt , ##args)
91 
92 /*
93  * Prototypes
94  */
95 int   clicon_err_reset(void);
96 #if defined(__GNUC__) && __GNUC__ >= 3
97 int   clicon_err_fn(const char *fn, const int line, int category, int err, const char *format, ...) __attribute__ ((format (printf, 5, 6)));
98 #else
99 int   clicon_err_fn(const char *fn, const int line, int category, int err, const char *format, ...);
100 #endif
101 char *clicon_strerror(int err);
102 void *clicon_err_save(void);
103 int   clicon_err_restore(void *handle);
104 
105 #endif  /* _CLIXON_ERR_H_ */
106