Source File modules/er/erroutines.h

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