]> git.proxmox.com Git - mirror_frr.git/blame - lib/ferr.h
Merge pull request #1188 from opensourcerouting/foreach_indentation
[mirror_frr.git] / lib / ferr.h
CommitLineData
3155489a
DL
1/*
2 * Copyright (c) 2015-16 David Lamparter, for NetDEF, Inc.
3 *
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.
7 *
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.
15 */
16
17#ifndef _FRR_FERR_H
18#define _FRR_FERR_H
19
20/***********************************************************
21 * scroll down to the end of this file for a full example! *
22 ***********************************************************/
23
24#include <stdint.h>
25#include <limits.h>
26#include <errno.h>
27
28/* return type when this error indication stuff is used.
29 *
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)
32 *
33 * For checking, always use "if (value)", nothing else.
34 * Do _NOT_ use any integer constant (!= 0), or sign check (< 0).
35 */
36typedef int ferr_r;
37
38/* rough category of error indication */
39enum ferr_kind {
40 /* no error */
41 FERR_OK = 0,
42
43 /* something isn't the way it's supposed to be.
44 * (things that might otherwise be asserts, really)
45 */
46 FERR_CODE_BUG,
47
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"),
50 * it's this category.
51 */
52 FERR_CONFIG_INVALID,
53
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!
58 */
59 FERR_CONFIG_REALITY,
60
61 /* out of some system resource (probably memory)
62 * aka "you didn't spend enough money error" */
63 FERR_RESOURCE,
64
65 /* system error (permission denied, etc.) */
66 FERR_SYSTEM,
67
68 /* error return from some external library
69 * (FERR_SYSTEM and FERR_LIBRARY are not strongly distinct) */
70 FERR_LIBRARY,
71};
72
73struct ferr {
74 /* code location */
75 const char *file;
76 const char *func;
77 int line;
78
79 enum ferr_kind kind;
80
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
84 * across changes.
85 */
86 uint32_t unique_id;
87
88 char message[384];
89
90 /* valid if != 0. note "errno" might be preprocessor foobar. */
91 int errno_val;
92 /* valid if pathname[0] != '\0' */
93 char pathname[PATH_MAX];
94};
95
96/* get error details.
97 *
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.
101 */
102const struct ferr *ferr_get_last(ferr_r errval);
103
104/* can optionally be called at strategic locations.
105 * always returns 0. */
106ferr_r ferr_clear(void);
107
108/* do NOT call these functions directly. only for macro use! */
109ferr_r ferr_set_internal(const char *file, int line, const char *func,
110 enum ferr_kind kind, const char *text, ...);
111ferr_r ferr_set_internal_ext(const char *file, int line, const char *func,
112 enum ferr_kind kind, const char *pathname, int errno_val,
113 const char *text, ...);
114
115#define ferr_ok() \
116 0
117
118/* Report an error.
119 *
120 * If you need to do cleanup (free memory, etc.), save the return value in a
121 * variable of type ferr_r.
122 *
123 * Don't put a \n at the end of the error message.
124 */
125#define ferr_code_bug(...) \
126 ferr_set_internal(__FILE__, __LINE__, __func__, FERR_CODE_BUG, \
127 __VA_ARGS__)
128#define ferr_cfg_invalid(...) \
129 ferr_set_internal(__FILE__, __LINE__, __func__, FERR_CONFIG_INVALID, \
130 __VA_ARGS__)
131#define ferr_cfg_reality(...) \
132 ferr_set_internal(__FILE__, __LINE__, __func__, FERR_CONFIG_REALITY, \
133 __VA_ARGS__)
134#define ferr_cfg_resource(...) \
135 ferr_set_internal(__FILE__, __LINE__, __func__, FERR_RESOURCE, \
136 __VA_ARGS__)
137#define ferr_system(...) \
138 ferr_set_internal(__FILE__, __LINE__, __func__, FERR_SYSTEM, \
139 __VA_ARGS__)
140#define ferr_library(...) \
141 ferr_set_internal(__FILE__, __LINE__, __func__, FERR_LIBRARY, \
142 __VA_ARGS__)
143
144/* extended information variants */
145#define ferr_system_errno(...) \
146 ferr_set_internal_ext(__FILE__, __LINE__, __func__, FERR_SYSTEM, \
147 NULL, errno, __VA_ARGS__)
148#define ferr_system_path_errno(path, ...) \
149 ferr_set_internal_ext(__FILE__, __LINE__, __func__, FERR_SYSTEM, \
150 path, errno, __VA_ARGS__)
151
152#include "vty.h"
153/* print error message to vty; $ERR is replaced by the error's message */
154void vty_print_error(struct vty *vty, ferr_r err, const char *msg, ...);
155
156#define CMD_FERR_DO(func, action, ...) \
157 do { ferr_r cmd_retval = func; \
158 if (cmd_retval) { \
159 vty_print_error(vty, cmd_retval, __VA_ARGS__); \
160 action; \
161 } \
162 } while (0)
163
164#define CMD_FERR_RETURN(func, ...) \
165 CMD_FERR_DO(func, return CMD_WARNING, __VA_ARGS__)
166#define CMD_FERR_GOTO(func, label, ...) \
167 CMD_FERR_DO(func, goto label, __VA_ARGS__)
168
169/* example:
170
171ferr_r foo_bar_set(struct object *obj, int bar)
172{
173 if (bar < 1 || bar >= 100)
174 return ferr_config_invalid("bar setting (%d) must be 0<x<100", bar);
175 obj->bar = bar;
176 if (ioctl (obj->fd, bar))
177 return ferr_system_errno("couldn't set bar to %d", bar);
178
179 return ferr_ok();
180}
181
182DEFUN("bla")
183{
184 CMD_FERR_RETURN(foo_bar_set(obj, atoi(argv[1])),
185 "command failed: $ERR\n");
186 return CMD_SUCCESS;
187}
188
189*/
190
191#endif /* _FERR_H */