]> git.proxmox.com Git - mirror_frr.git/blame - lib/ferr.c
zebra: Allow json output to give a bit more data
[mirror_frr.git] / lib / ferr.c
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#include <stdlib.h>
18#include <stdarg.h>
19#include <string.h>
20#include <pthread.h>
21#include <signal.h>
ed8841d3 22#include <inttypes.h>
3155489a
DL
23
24#include "ferr.h"
25#include "vty.h"
26#include "jhash.h"
27#include "memory.h"
7b526b61
QY
28#include "hash.h"
29#include "command.h"
ed8841d3
QY
30#include "json.h"
31#include "linklist.h"
3155489a
DL
32
33DEFINE_MTYPE_STATIC(LIB, ERRINFO, "error information")
34
7b526b61
QY
35/*
36 * Thread-specific key for temporary storage of allocated ferr.
37 */
3155489a
DL
38static pthread_key_t errkey;
39
40static void ferr_free(void *arg)
41{
42 XFREE(MTYPE_ERRINFO, arg);
43}
44
45static void err_key_init(void) __attribute__((_CONSTRUCTOR(500)));
46static void err_key_init(void)
47{
48 pthread_key_create(&errkey, ferr_free);
49}
50
51static void err_key_fini(void) __attribute__((_DESTRUCTOR(500)));
52static void err_key_fini(void)
53{
54 pthread_key_delete(errkey);
55}
56
7b526b61
QY
57/*
58 * Global shared hash table holding reference text for all defined errors.
59 */
60pthread_mutex_t refs_mtx = PTHREAD_MUTEX_INITIALIZER;
61struct hash *refs;
62
63static int ferr_hash_cmp(const void *a, const void *b)
64{
85cd2f9f
QY
65 const struct log_ref *f_a = a;
66 const struct log_ref *f_b = b;
7b526b61
QY
67
68 return f_a->code == f_b->code;
69}
70
71static inline unsigned int ferr_hash_key(void *a)
72{
85cd2f9f 73 struct log_ref *f = a;
7b526b61
QY
74
75 return f->code;
76}
77
85cd2f9f 78void log_ref_add(struct log_ref *ref)
7b526b61 79{
90234540
DS
80 uint32_t i = 0;
81
7b526b61
QY
82 pthread_mutex_lock(&refs_mtx);
83 {
90234540
DS
84 while (ref[i].code != END_FERR) {
85 hash_get(refs, &ref[i], hash_alloc_intern);
86 i++;
87 }
7b526b61
QY
88 }
89 pthread_mutex_unlock(&refs_mtx);
90}
91
85cd2f9f 92struct log_ref *log_ref_get(uint32_t code)
7b526b61 93{
85cd2f9f
QY
94 struct log_ref holder;
95 struct log_ref *ref;
7b526b61
QY
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
85cd2f9f 107void log_ref_display(struct vty *vty, uint32_t code, bool json)
7b526b61 108{
85cd2f9f 109 struct log_ref *ref;
184ce1c5 110 struct json_object *top = NULL, *obj = NULL;
ed8841d3
QY
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) {
85cd2f9f 124 ref = log_ref_get(code);
ed8841d3
QY
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];
af4d3437 135
ed8841d3
QY
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];
af4d3437 147
ed8841d3 148 snprintf(pbuf, sizeof(pbuf), "\nError %"PRIu32" - %s",
b2111f08 149 ref->code, ref->title);
ed8841d3
QY
150 memset(ubuf, '=', strlen(pbuf));
151 ubuf[sizeof(ubuf) - 1] = '\0';
152
153 vty_out(vty, "%s\n%s\n", pbuf, ubuf);
af4d3437
QY
154 vty_out(vty, "Description:\n%s\n\n", ref->description);
155 vty_out(vty, "Recommendation:\n%s\n", ref->suggestion);
ed8841d3
QY
156 }
157 }
7b526b61 158
ed8841d3
QY
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);
7b526b61
QY
164 }
165
ed8841d3 166 list_delete_and_null(&errlist);
7b526b61
QY
167}
168
169DEFUN_NOSH(show_error_code,
170 show_error_code_cmd,
ed8841d3 171 "show error <(1-4294967296)|all> [json]",
7b526b61
QY
172 SHOW_STR
173 "Information on errors\n"
ed8841d3
QY
174 "Error code to get info about\n"
175 "Information on all errors\n"
176 JSON_STR)
7b526b61 177{
ed8841d3
QY
178 bool json = strmatch(argv[argc-1]->text, "json");
179 uint32_t arg = 0;
af4d3437 180
ed8841d3
QY
181 if (!strmatch(argv[2]->text, "all"))
182 arg = strtoul(argv[2]->arg, NULL, 10);
7b526b61 183
85cd2f9f 184 log_ref_display(vty, arg, json);
7b526b61
QY
185 return CMD_SUCCESS;
186}
187
85cd2f9f 188void log_ref_init(void)
7b526b61
QY
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
85cd2f9f 200void log_ref_fini(void)
7b526b61
QY
201{
202 pthread_mutex_lock(&refs_mtx);
203 {
fa8b3ca6 204 hash_clean(refs, NULL);
7b526b61
QY
205 hash_free(refs);
206 refs = NULL;
207 }
208 pthread_mutex_unlock(&refs_mtx);
209}
210
3155489a
DL
211const 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
219ferr_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
227static ferr_r ferr_set_va(const char *file, int line, const char *func,
996c9314
LB
228 enum ferr_kind kind, const char *pathname,
229 int errno_val, const char *text, va_list va)
3155489a
DL
230{
231 struct ferr *error = pthread_getspecific(errkey);
232
233 if (!error) {
234 error = XCALLOC(MTYPE_ERRINFO, sizeof(*error));
3155489a
DL
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),
996c9314 245 jhash(file, strlen(file), 0xd4ed0298));
3155489a
DL
246
247 error->errno_val = errno_val;
248 if (pathname)
996c9314
LB
249 snprintf(error->pathname, sizeof(error->pathname), "%s",
250 pathname);
3155489a
DL
251 else
252 error->pathname[0] = '\0';
253
254 vsnprintf(error->message, sizeof(error->message), text, va);
255 return -1;
256}
257
258ferr_r ferr_set_internal(const char *file, int line, const char *func,
996c9314 259 enum ferr_kind kind, const char *text, ...)
3155489a
DL
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
269ferr_r ferr_set_internal_ext(const char *file, int line, const char *func,
996c9314
LB
270 enum ferr_kind kind, const char *pathname,
271 int errno_val, const char *text, ...)
3155489a
DL
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"
282void 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;
996c9314 298 vty_out(vty, "%s%s%s\n", tmpmsg,
3155489a
DL
299 last_error ? last_error->message : "(no error?)",
300 replacepos);
301 }
302}