1 | /***************************************
2 | $Revision: 1.21 $
3 |
4 | Error reporting (er) erroutines.h - header file for error reporting.
5 |
6 | Status: NOT REVUED, TESTED,
7 |
8 | Design and implementation by: Marek Bukowy
9 |
10 | ******************/ /******************
11 | Copyright (c) 1999,2000,2001,2002 RIPE NCC
12 |
13 | All Rights Reserved
14 |
15 | Permission to use, copy, modify, and distribute this software and its
16 | documentation for any purpose and without fee is hereby granted,
17 | provided that the above copyright notice appear in all copies and that
18 | both that copyright notice and this permission notice appear in
19 | supporting documentation, and that the name of the author not be
20 | used in advertising or publicity pertaining to distribution of the
21 | software without specific, written prior permission.
22 |
23 | THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
24 | ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS; IN NO EVENT SHALL
25 | AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
26 | DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
27 | AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
28 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
29 | ***************************************/
30 |
31 | #ifndef ER_H
32 | #define ER_H
33 |
34 |
35 | typedef unsigned int er_mask_t;
36 | typedef int er_ret_t;
37 |
38 | #include <stdio.h>
39 | #include <unistd.h>
40 | #include <stdlib.h>
41 | #include <assert.h>
42 | #include <time.h>
43 | #include <stdarg.h>
44 | #include <strings.h>
45 |
46 | #include <sys/param.h>
47 | #include <glib.h>
48 | #include <pthread.h>
49 |
50 | #include <bitmask.h>
51 | #include <stubs.h>
52 |
53 | #include "thread.h"
54 |
55 | #ifdef HAVE_PTHREAD_H
56 | #include <pthread.h>
57 | #endif
58 |
59 | #ifdef ER_IMPL
60 | #define EXTDEF
61 | #define EXTINI(a,b) a = b;
62 | #else
63 | #define EXTDEF extern
64 | #define EXTINI(a,b) extern a;
65 | #endif
66 |
67 | #ifdef __cplusplus
68 | extern "C" {
69 | #endif
70 |
71 | typedef enum {
72 | ER_PATH_SOCK, /* unbuffered file/socket access via a file descriptor */
73 | ER_PATH_BUFPTR, /* buffered file access via a FILE structure */
74 | ER_PATH_NAME, /* buffered file access via a file name
75 | (file reopened for every message) */
76 | ER_PATH_EXEC, /* message constructed, send to stdin of the command
77 | at the end or after one message depending on options */
78 | ER_PATH_SYSLOG, /* syslog msg sent at every message */
79 | ER_PATH_CIRC
80 | } er_path_mt;
81 |
82 | EXTDEF char *er_pathtypes[]
83 | #ifdef ER_IMPL
84 | = {
85 | "SOCK", /* MUST BE IN SYNC WITH THE ABOVE */
86 | "BUFPTR",
87 | "NAME",
88 | "EXEC",
89 | "SYSLOG",
90 | "CIRC",
91 | NULL
92 | }
93 | #endif
94 | ;
95 |
96 | typedef union {
97 | struct {
98 | int fd; /* int filedescr */
99 | } sock;
100 | struct {
101 | FILE *fp; /* FILE* fp for FILEBUFPTR */
102 | } bufptr;
103 | struct {
104 | char filename[80]; /* filename for FILEBUFNAM */
105 | int date; /* 'DATE' option - construct a filename */
106 | } name;
107 | struct {
108 | int usepath;
109 | char **argv; /* parameters for exec - XXX DYNAMIC!!!! */
110 | } exec;
111 | struct {
112 | int facility; /* openlog(3) parameters for SYSLOG */
113 | int logopt;
114 | char ident[32];
115 | } syslog;
116 | } er_path_descr_t;
117 |
118 | typedef struct {
119 | char name[32];
120 | char active;
121 | int format;
122 | pthread_mutex_t mutex;
123 | er_path_mt type;
124 | er_path_descr_t descr;
125 | GList *filters;
126 | } er_path_t;
127 |
128 | typedef struct {
129 | mask_t fac_mask;
130 | er_mask_t asp_mask;
131 | int sev_min;
132 | int sev_max;
133 | pthread_t thr_id;
134 | /* unsigned err; -- a specific error code - or 0 to mean all errors */
135 | } er_filter_t;
136 |
137 | typedef struct {
138 | char errtxt[1024];
139 | int errpos;
140 | char *token;
141 | er_path_t path;
142 | er_filter_t curfilt;
143 | int sock;
144 | } lexerr_t;
145 |
146 |
147 |
148 | #define MNELEN 16
149 | typedef struct {
150 | er_ret_t code;
151 | char mnem[MNELEN];
152 | char text[80];
153 | } er_list_t;
154 |
155 |
156 | typedef struct {
157 | er_ret_t code;
158 | char name[4];
159 | char desc[80];
160 | er_list_t *errs;
161 | } er_fac_t;
162 |
163 |
164 | #define ER_SEV_F 0x20000000 /*+ fatal error +*/
165 | #define ER_SEV_E 0x10000000 /*+ error +*/
166 | #define ER_SEV_W 0x08000000 /*+ warning +*/
167 | #define ER_SEV_I 0x04000000 /*+ information +*/
168 | #define ER_SEV_D 0x02000000 /*+ debug message +*/
169 | #define ER_SEV_L 0x01000000 /*+ library error +*/
170 |
171 |
172 | /* macro to see if the code is OK -- masks out the facility and compares,
173 | assuming all OK codes within the facilities are 0
174 | */
175 |
176 |
177 |
178 | #define ER_SEV_TXT 20
179 |
180 | #define ER_MSGLEN 384
181 | #define ER_ERRLEN 2048
182 |
183 | typedef struct {
184 | int sev;
185 | char chr[2];
186 | char txt[ER_SEV_TXT];
187 | } er_level_t;
188 |
189 | #define DEFFAC(a,b) { FAC_##a, #a, b, a##_mod_err }
190 | #define ER_LASTTXT {-1} /* macro for use in error text arrays */
191 | #define ERDUP(a) a, #a
192 | #include "er_facilities.h"
193 | /* the macro expects two arguments:
194 | capital letters symbol of the facility
195 | short (<80 chars) description
196 | which then are expanded, eg. DEFFAC(TT, "test facility") expands to:
197 | { FAC_TT , "TT", "test facility" , NULL} ,
198 | Therefore, the FAC_TT must be defined in the enum below.
199 | The er_fac_code_t enum must begin with FAC_NONE=0
200 | and must end with FAC_LAST.
201 | The er_fac_err array must end with FAC_NONE.
202 |
203 | The user code must contain INITFAC(a) call early in the code that
204 | sets the pointer to the respective ??_mod_err array. There is nothing
205 | wrong in calling it twice, so don't hesitate if you must do it.
206 |
207 | After a facility number changes (eg. because another one was added or
208 | deleted before yours) ALL your code must be recompiled before linking.
209 | */
210 |
211 |
212 | #include "er_aspects.h"
213 | #include "er_formats.h"
214 |
215 | #ifndef ER_IMPL /* for client modules */
216 | extern er_level_t er_level_a[];
217 | #else /* full definition */
218 | er_level_t er_level_a[] = {
219 | { ER_SEV_F, "F" , "fatal error" },
220 | { ER_SEV_E, "E" , "error" },
221 | { ER_SEV_W, "W" , "warning" },
222 | { ER_SEV_I, "I" , "information" },
223 | { ER_SEV_D, "D" , "debug msg" },
224 | { ER_SEV_L, "L" , "library err" },
225 | { 0, "-" , "BUG! no such sev 0" }
226 | };
227 | #endif /* ER_IMPL */
228 |
229 |
230 | /*************************************************************************/
231 |
232 | EXTINI(GList *er_pathlist , NULL)
233 | EXTDEF er_mask_t er_asparray[FAC_LAST];
234 | EXTDEF rw_lock_t er_paths_lock;
235 | EXTINI(GHashTable *er_macro_hash, NULL)
236 |
237 | #ifdef ER_IMPL
238 |
239 | /* global vars !!!!! must be set for reporting purposes.
240 | Must be initialised in main() by ER_init().
241 | */
242 | char er_progname[32];
243 | char er_pid[16];
244 |
245 | /* those are private variables */
246 | pthread_mutex_t er_pathlist_mutex = PTHREAD_MUTEX_INITIALIZER;
247 | #endif
248 |
249 |
250 |
251 |
252 | void ER_init(char *progname, int processdefs);
253 |
254 | #define ER_dbg_eq(mod, asp, typ, expr) \
255 | ER_dbg_va (mod, asp, #expr " = " typ, expr)
256 |
257 | void ER_perror(er_fac_code_t facwhere, int errcode, char *format,...)
258 | #ifdef __GNUC__ /* let gcc check the format string for problems */
259 | __attribute__ ((format (printf, 3, 4)))
260 | #endif
261 | ;
262 | void ER_dbg_va(er_fac_code_t facwhere, er_mask_t asp, char *txt, ...);
263 | void ER_inf_va(er_fac_code_t facwhere, er_mask_t asp, char *txt, ...);
264 | int ER_anybody_wants(er_fac_code_t facwhere, int errcode, er_mask_t asp );
265 | int ER_is_traced(er_fac_code_t facwhere, er_mask_t asp);
266 |
267 | void ER_setpath(er_path_t *newset);
268 |
269 | int NOERR(er_ret_t a);
270 | #define ERR(a) (!NOERR(a))
271 |
272 | char *er_getsevsym( int sev, int mode );
273 | char *er_getfacsym(er_fac_code_t faccode);
274 | er_mask_t er_getfacval(char *key);
275 | unsigned int er_getaspval(char *key);
276 | er_path_mt er_getpathval(char *key);
277 |
278 | er_ret_t er_add_filter( er_path_t *pathptr, er_filter_t *filter );
279 | er_ret_t er_add_path( er_path_t *pathptr, char *key );
280 |
281 | #ifdef __cplusplus
282 | }
283 | #endif
284 |
285 | #undef EXTDEF
286 | #undef EXTINI
287 | #endif /* ER_H */