]>
git.proxmox.com Git - mirror_frr.git/blob - lib/ferr.h
2 * Copyright (c) 2015-16 David Lamparter, for NetDEF, Inc.
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 /***********************************************************
21 * scroll down to the end of this file for a full example! *
22 ***********************************************************/
28 /* return type when this error indication stuff is used.
30 * guaranteed to have boolean evaluation to "false" when OK, "true" when error
31 * (i.e. can be changed to pointer in the future if neccessary)
33 * For checking, always use "if (value)", nothing else.
34 * Do _NOT_ use any integer constant (!= 0), or sign check (< 0).
38 /* rough category of error indication */
43 /* something isn't the way it's supposed to be.
44 * (things that might otherwise be asserts, really)
48 /* user-supplied parameters don't make sense or is inconsistent
49 * if you can express a rule for it (e.g. "holdtime > 2 * keepalive"),
54 /* user-supplied parameters don't line up with reality
55 * (IP address or interface not available, etc.)
56 * NB: these are really TODOs where the code needs to be fixed to
57 * respond to future changes!
61 /* out of some system resource (probably memory)
62 * aka "you didn't spend enough money error" */
65 /* system error (permission denied, etc.) */
68 /* error return from some external library
69 * (FERR_SYSTEM and FERR_LIBRARY are not strongly distinct) */
81 /* unique_id is calculated as a checksum of source filename and error
82 * message format (*before* calling vsnprintf). Line number and
83 * function name are not used; this keeps the number reasonably static
90 /* valid if != 0. note "errno" might be preprocessor foobar. */
92 /* valid if pathname[0] != '\0' */
93 char pathname
[PATH_MAX
];
98 * NB: errval/ferr_r does NOT carry the full error information. It's only
99 * passed around for future API flexibility. ferr_get_last always returns
100 * the last error set in the current thread.
102 const struct ferr
*ferr_get_last(ferr_r errval
);
104 /* can optionally be called at strategic locations.
105 * always returns 0. */
106 ferr_r
ferr_clear(void);
108 /* do NOT call these functions directly. only for macro use! */
109 ferr_r
ferr_set_internal(const char *file
, int line
, const char *func
,
110 enum ferr_kind kind
, const char *text
, ...);
111 ferr_r
ferr_set_internal_ext(const char *file
, int line
, const char *func
,
112 enum ferr_kind kind
, const char *pathname
,
113 int errno_val
, const char *text
, ...);
119 * If you need to do cleanup (free memory, etc.), save the return value in a
120 * variable of type ferr_r.
122 * Don't put a \n at the end of the error message.
124 #define ferr_code_bug(...) \
125 ferr_set_internal(__FILE__, __LINE__, __func__, FERR_CODE_BUG, \
127 #define ferr_cfg_invalid(...) \
128 ferr_set_internal(__FILE__, __LINE__, __func__, FERR_CONFIG_INVALID, \
130 #define ferr_cfg_reality(...) \
131 ferr_set_internal(__FILE__, __LINE__, __func__, FERR_CONFIG_REALITY, \
133 #define ferr_cfg_resource(...) \
134 ferr_set_internal(__FILE__, __LINE__, __func__, FERR_RESOURCE, \
136 #define ferr_system(...) \
137 ferr_set_internal(__FILE__, __LINE__, __func__, FERR_SYSTEM, \
139 #define ferr_library(...) \
140 ferr_set_internal(__FILE__, __LINE__, __func__, FERR_LIBRARY, \
143 /* extended information variants */
144 #define ferr_system_errno(...) \
145 ferr_set_internal_ext(__FILE__, __LINE__, __func__, FERR_SYSTEM, NULL, \
147 #define ferr_system_path_errno(path, ...) \
148 ferr_set_internal_ext(__FILE__, __LINE__, __func__, FERR_SYSTEM, path, \
152 /* print error message to vty; $ERR is replaced by the error's message */
153 void vty_print_error(struct vty
*vty
, ferr_r err
, const char *msg
, ...);
155 #define CMD_FERR_DO(func, action, ...) \
157 ferr_r cmd_retval = func; \
159 vty_print_error(vty, cmd_retval, __VA_ARGS__); \
164 #define CMD_FERR_RETURN(func, ...) \
165 CMD_FERR_DO(func, return CMD_WARNING_CONFIG_FAILED, __VA_ARGS__)
166 #define CMD_FERR_GOTO(func, label, ...) \
167 CMD_FERR_DO(func, goto label, __VA_ARGS__)
169 /* example: uses bogus #define to keep indent.py happy */
170 #ifdef THIS_IS_AN_EXAMPLE
171 ferr_r
foo_bar_set(struct object
*obj
, int bar
)
173 if (bar
< 1 || bar
>= 100)
174 return ferr_config_invalid("bar setting (%d) must be 0<x<100",
177 if (ioctl(obj
->fd
, bar
))
178 return ferr_system_errno("couldn't set bar to %d", bar
);
185 CMD_FERR_RETURN(foo_bar_set(obj
, atoi(argv
[1])),
186 "command failed: $ERR\n");
190 #endif /* THIS_IS_AN_EXAMPLE */