]> git.proxmox.com Git - mirror_frr.git/blob - lib/ferr.c
zebra, lib: fix the ZEBRA_INTERFACE_VRF_UPDATE zapi message
[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 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(void *a)
76 {
77 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 vty_out(vty, "Code %"PRIu32" - Unknown\n", code);
131 return;
132 }
133 listnode_add(errlist, ref);
134 }
135
136 for (ALL_LIST_ELEMENTS_RO(errlist, ln, ref)) {
137 if (json) {
138 char key[11];
139
140 snprintf(key, sizeof(key), "%"PRIu32, ref->code);
141 obj = json_object_new_object();
142 json_object_string_add(obj, "title", ref->title);
143 json_object_string_add(obj, "description",
144 ref->description);
145 json_object_string_add(obj, "suggestion",
146 ref->suggestion);
147 json_object_object_add(top, key, obj);
148 } else {
149 char pbuf[256];
150 char ubuf[256];
151
152 snprintf(pbuf, sizeof(pbuf), "\nError %"PRIu32" - %s",
153 ref->code, ref->title);
154 memset(ubuf, '=', strlen(pbuf));
155 ubuf[strlen(pbuf)] = '\0';
156
157 vty_out(vty, "%s\n%s\n", pbuf, ubuf);
158 vty_out(vty, "Description:\n%s\n\n", ref->description);
159 vty_out(vty, "Recommendation:\n%s\n", ref->suggestion);
160 }
161 }
162
163 if (json) {
164 const char *str = json_object_to_json_string_ext(
165 top, JSON_C_TO_STRING_PRETTY);
166 vty_out(vty, "%s\n", str);
167 json_object_free(top);
168 }
169
170 list_delete(&errlist);
171 }
172
173 DEFUN_NOSH(show_error_code,
174 show_error_code_cmd,
175 "show error <(1-4294967296)|all> [json]",
176 SHOW_STR
177 "Information on errors\n"
178 "Error code to get info about\n"
179 "Information on all errors\n"
180 JSON_STR)
181 {
182 bool json = strmatch(argv[argc-1]->text, "json");
183 uint32_t arg = 0;
184
185 if (!strmatch(argv[2]->text, "all"))
186 arg = strtoul(argv[2]->arg, NULL, 10);
187
188 log_ref_display(vty, arg, json);
189 return CMD_SUCCESS;
190 }
191
192 void log_ref_init(void)
193 {
194 pthread_mutex_lock(&refs_mtx);
195 {
196 refs = hash_create(ferr_hash_key, ferr_hash_cmp,
197 "Error Reference Texts");
198 }
199 pthread_mutex_unlock(&refs_mtx);
200
201 install_element(VIEW_NODE, &show_error_code_cmd);
202 }
203
204 void log_ref_fini(void)
205 {
206 pthread_mutex_lock(&refs_mtx);
207 {
208 hash_clean(refs, NULL);
209 hash_free(refs);
210 refs = NULL;
211 }
212 pthread_mutex_unlock(&refs_mtx);
213 }
214
215 const struct ferr *ferr_get_last(ferr_r errval)
216 {
217 struct ferr *last_error = pthread_getspecific(errkey);
218 if (!last_error || last_error->kind == 0)
219 return NULL;
220 return last_error;
221 }
222
223 ferr_r ferr_clear(void)
224 {
225 struct ferr *last_error = pthread_getspecific(errkey);
226 if (last_error)
227 last_error->kind = 0;
228 return ferr_ok();
229 }
230
231 static ferr_r ferr_set_va(const char *file, int line, const char *func,
232 enum ferr_kind kind, const char *pathname,
233 int errno_val, const char *text, va_list va)
234 {
235 struct ferr *error = pthread_getspecific(errkey);
236
237 if (!error) {
238 error = XCALLOC(MTYPE_ERRINFO, sizeof(*error));
239
240 pthread_setspecific(errkey, error);
241 }
242
243 error->file = file;
244 error->line = line;
245 error->func = func;
246 error->kind = kind;
247
248 error->unique_id = jhash(text, strlen(text),
249 jhash(file, strlen(file), 0xd4ed0298));
250
251 error->errno_val = errno_val;
252 if (pathname)
253 snprintf(error->pathname, sizeof(error->pathname), "%s",
254 pathname);
255 else
256 error->pathname[0] = '\0';
257
258 vsnprintf(error->message, sizeof(error->message), text, va);
259 return -1;
260 }
261
262 ferr_r ferr_set_internal(const char *file, int line, const char *func,
263 enum ferr_kind kind, const char *text, ...)
264 {
265 ferr_r rv;
266 va_list va;
267 va_start(va, text);
268 rv = ferr_set_va(file, line, func, kind, NULL, 0, text, va);
269 va_end(va);
270 return rv;
271 }
272
273 ferr_r ferr_set_internal_ext(const char *file, int line, const char *func,
274 enum ferr_kind kind, const char *pathname,
275 int errno_val, const char *text, ...)
276 {
277 ferr_r rv;
278 va_list va;
279 va_start(va, text);
280 rv = ferr_set_va(file, line, func, kind, pathname, errno_val, text, va);
281 va_end(va);
282 return rv;
283 }
284
285 #define REPLACE "$ERR"
286 void vty_print_error(struct vty *vty, ferr_r err, const char *msg, ...)
287 {
288 char tmpmsg[512], *replacepos;
289 const struct ferr *last_error = ferr_get_last(err);
290
291 va_list va;
292 va_start(va, msg);
293 vsnprintf(tmpmsg, sizeof(tmpmsg), msg, va);
294 va_end(va);
295
296 replacepos = strstr(tmpmsg, REPLACE);
297 if (!replacepos)
298 vty_out(vty, "%s\n", tmpmsg);
299 else {
300 replacepos[0] = '\0';
301 replacepos += sizeof(REPLACE) - 1;
302 vty_out(vty, "%s%s%s\n", tmpmsg,
303 last_error ? last_error->message : "(no error?)",
304 replacepos);
305 }
306 }