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