]> git.proxmox.com Git - mirror_frr.git/blob - lib/ferr.c
Merge pull request #4778 from mjstapp/dplane_macs
[mirror_frr.git] / lib / ferr.c
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 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
20
21 #include <stdlib.h>
22 #include <stdarg.h>
23 #include <string.h>
24 #include <pthread.h>
25 #include <signal.h>
26 #include <inttypes.h>
27
28 #include "ferr.h"
29 #include "vty.h"
30 #include "jhash.h"
31 #include "memory.h"
32 #include "hash.h"
33 #include "command.h"
34 #include "json.h"
35 #include "linklist.h"
36
37 DEFINE_MTYPE_STATIC(LIB, ERRINFO, "error information")
38
39 /*
40 * Thread-specific key for temporary storage of allocated ferr.
41 */
42 static pthread_key_t errkey;
43
44 static void ferr_free(void *arg)
45 {
46 XFREE(MTYPE_ERRINFO, arg);
47 }
48
49 static void err_key_init(void) __attribute__((_CONSTRUCTOR(500)));
50 static void err_key_init(void)
51 {
52 pthread_key_create(&errkey, ferr_free);
53 }
54
55 static void err_key_fini(void) __attribute__((_DESTRUCTOR(500)));
56 static void err_key_fini(void)
57 {
58 pthread_key_delete(errkey);
59 }
60
61 /*
62 * Global shared hash table holding reference text for all defined errors.
63 */
64 static pthread_mutex_t refs_mtx = PTHREAD_MUTEX_INITIALIZER;
65 struct hash *refs;
66
67 static bool ferr_hash_cmp(const void *a, const void *b)
68 {
69 const struct log_ref *f_a = a;
70 const struct log_ref *f_b = b;
71
72 return f_a->code == f_b->code;
73 }
74
75 static inline unsigned int ferr_hash_key(const void *a)
76 {
77 const struct log_ref *f = a;
78
79 return f->code;
80 }
81
82 void log_ref_add(struct log_ref *ref)
83 {
84 uint32_t i = 0;
85
86 pthread_mutex_lock(&refs_mtx);
87 {
88 while (ref[i].code != END_FERR) {
89 hash_get(refs, &ref[i], hash_alloc_intern);
90 i++;
91 }
92 }
93 pthread_mutex_unlock(&refs_mtx);
94 }
95
96 struct log_ref *log_ref_get(uint32_t code)
97 {
98 struct log_ref holder;
99 struct log_ref *ref;
100
101 holder.code = code;
102 pthread_mutex_lock(&refs_mtx);
103 {
104 ref = hash_lookup(refs, &holder);
105 }
106 pthread_mutex_unlock(&refs_mtx);
107
108 return ref;
109 }
110
111 void log_ref_display(struct vty *vty, uint32_t code, bool json)
112 {
113 struct log_ref *ref;
114 struct json_object *top = NULL, *obj = NULL;
115 struct list *errlist;
116 struct listnode *ln;
117
118 if (json)
119 top = json_object_new_object();
120
121 pthread_mutex_lock(&refs_mtx);
122 {
123 errlist = code ? list_new() : hash_to_list(refs);
124 }
125 pthread_mutex_unlock(&refs_mtx);
126
127 if (code) {
128 ref = log_ref_get(code);
129 if (!ref)
130 return;
131 listnode_add(errlist, ref);
132 }
133
134 for (ALL_LIST_ELEMENTS_RO(errlist, ln, ref)) {
135 if (json) {
136 char key[11];
137
138 snprintf(key, sizeof(key), "%"PRIu32, ref->code);
139 obj = json_object_new_object();
140 json_object_string_add(obj, "title", ref->title);
141 json_object_string_add(obj, "description",
142 ref->description);
143 json_object_string_add(obj, "suggestion",
144 ref->suggestion);
145 json_object_object_add(top, key, obj);
146 } else {
147 char pbuf[256];
148 char ubuf[256];
149
150 snprintf(pbuf, sizeof(pbuf), "\nError %"PRIu32" - %s",
151 ref->code, ref->title);
152 memset(ubuf, '=', strlen(pbuf));
153 ubuf[strlen(pbuf)] = '\0';
154
155 vty_out(vty, "%s\n%s\n", pbuf, ubuf);
156 vty_out(vty, "Description:\n%s\n\n", ref->description);
157 vty_out(vty, "Recommendation:\n%s\n", ref->suggestion);
158 }
159 }
160
161 if (json) {
162 const char *str = json_object_to_json_string_ext(
163 top, JSON_C_TO_STRING_PRETTY);
164 vty_out(vty, "%s\n", str);
165 json_object_free(top);
166 }
167
168 list_delete(&errlist);
169 }
170
171 DEFUN_NOSH(show_error_code,
172 show_error_code_cmd,
173 "show error <(1-4294967296)|all> [json]",
174 SHOW_STR
175 "Information on errors\n"
176 "Error code to get info about\n"
177 "Information on all errors\n"
178 JSON_STR)
179 {
180 bool json = strmatch(argv[argc-1]->text, "json");
181 uint32_t arg = 0;
182
183 if (!strmatch(argv[2]->text, "all"))
184 arg = strtoul(argv[2]->arg, NULL, 10);
185
186 log_ref_display(vty, arg, json);
187 return CMD_SUCCESS;
188 }
189
190 void log_ref_init(void)
191 {
192 pthread_mutex_lock(&refs_mtx);
193 {
194 refs = hash_create(ferr_hash_key, ferr_hash_cmp,
195 "Error Reference Texts");
196 }
197 pthread_mutex_unlock(&refs_mtx);
198 }
199
200 void log_ref_fini(void)
201 {
202 pthread_mutex_lock(&refs_mtx);
203 {
204 hash_clean(refs, NULL);
205 hash_free(refs);
206 refs = NULL;
207 }
208 pthread_mutex_unlock(&refs_mtx);
209 }
210
211 void log_ref_vty_init(void)
212 {
213 install_element(VIEW_NODE, &show_error_code_cmd);
214 }
215
216
217 const struct ferr *ferr_get_last(ferr_r errval)
218 {
219 struct ferr *last_error = pthread_getspecific(errkey);
220 if (!last_error || last_error->kind == 0)
221 return NULL;
222 return last_error;
223 }
224
225 ferr_r ferr_clear(void)
226 {
227 struct ferr *last_error = pthread_getspecific(errkey);
228 if (last_error)
229 last_error->kind = 0;
230 return ferr_ok();
231 }
232
233 static ferr_r ferr_set_va(const char *file, int line, const char *func,
234 enum ferr_kind kind, const char *pathname,
235 int errno_val, const char *text, va_list va)
236 {
237 struct ferr *error = pthread_getspecific(errkey);
238
239 if (!error) {
240 error = XCALLOC(MTYPE_ERRINFO, sizeof(*error));
241
242 pthread_setspecific(errkey, error);
243 }
244
245 error->file = file;
246 error->line = line;
247 error->func = func;
248 error->kind = kind;
249
250 error->unique_id = jhash(text, strlen(text),
251 jhash(file, strlen(file), 0xd4ed0298));
252
253 error->errno_val = errno_val;
254 if (pathname)
255 snprintf(error->pathname, sizeof(error->pathname), "%s",
256 pathname);
257 else
258 error->pathname[0] = '\0';
259
260 vsnprintf(error->message, sizeof(error->message), text, va);
261 return -1;
262 }
263
264 ferr_r ferr_set_internal(const char *file, int line, const char *func,
265 enum ferr_kind kind, const char *text, ...)
266 {
267 ferr_r rv;
268 va_list va;
269 va_start(va, text);
270 rv = ferr_set_va(file, line, func, kind, NULL, 0, text, va);
271 va_end(va);
272 return rv;
273 }
274
275 ferr_r ferr_set_internal_ext(const char *file, int line, const char *func,
276 enum ferr_kind kind, const char *pathname,
277 int errno_val, const char *text, ...)
278 {
279 ferr_r rv;
280 va_list va;
281 va_start(va, text);
282 rv = ferr_set_va(file, line, func, kind, pathname, errno_val, text, va);
283 va_end(va);
284 return rv;
285 }
286
287 #define REPLACE "$ERR"
288 void vty_print_error(struct vty *vty, ferr_r err, const char *msg, ...)
289 {
290 char tmpmsg[512], *replacepos;
291 const struct ferr *last_error = ferr_get_last(err);
292
293 va_list va;
294 va_start(va, msg);
295 vsnprintf(tmpmsg, sizeof(tmpmsg), msg, va);
296 va_end(va);
297
298 replacepos = strstr(tmpmsg, REPLACE);
299 if (!replacepos)
300 vty_out(vty, "%s\n", tmpmsg);
301 else {
302 replacepos[0] = '\0';
303 replacepos += sizeof(REPLACE) - 1;
304 vty_out(vty, "%s%s%s\n", tmpmsg,
305 last_error ? last_error->message : "(no error?)",
306 replacepos);
307 }
308 }