]> git.proxmox.com Git - mirror_frr.git/blame - lib/ferr.c
zebra, lib: fix the ZEBRA_INTERFACE_VRF_UPDATE zapi message
[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
b45ac5f5
DL
17#ifdef HAVE_CONFIG_H
18#include "config.h"
19#endif
20
3155489a
DL
21#include <stdlib.h>
22#include <stdarg.h>
23#include <string.h>
24#include <pthread.h>
25#include <signal.h>
ed8841d3 26#include <inttypes.h>
3155489a
DL
27
28#include "ferr.h"
29#include "vty.h"
30#include "jhash.h"
31#include "memory.h"
7b526b61
QY
32#include "hash.h"
33#include "command.h"
ed8841d3
QY
34#include "json.h"
35#include "linklist.h"
3155489a
DL
36
37DEFINE_MTYPE_STATIC(LIB, ERRINFO, "error information")
38
7b526b61
QY
39/*
40 * Thread-specific key for temporary storage of allocated ferr.
41 */
3155489a
DL
42static pthread_key_t errkey;
43
44static void ferr_free(void *arg)
45{
46 XFREE(MTYPE_ERRINFO, arg);
47}
48
49static void err_key_init(void) __attribute__((_CONSTRUCTOR(500)));
50static void err_key_init(void)
51{
52 pthread_key_create(&errkey, ferr_free);
53}
54
55static void err_key_fini(void) __attribute__((_DESTRUCTOR(500)));
56static void err_key_fini(void)
57{
58 pthread_key_delete(errkey);
59}
60
7b526b61
QY
61/*
62 * Global shared hash table holding reference text for all defined errors.
63 */
64pthread_mutex_t refs_mtx = PTHREAD_MUTEX_INITIALIZER;
65struct hash *refs;
66
74df8d6d 67static bool ferr_hash_cmp(const void *a, const void *b)
7b526b61 68{
85cd2f9f
QY
69 const struct log_ref *f_a = a;
70 const struct log_ref *f_b = b;
7b526b61
QY
71
72 return f_a->code == f_b->code;
73}
74
75static inline unsigned int ferr_hash_key(void *a)
76{
85cd2f9f 77 struct log_ref *f = a;
7b526b61
QY
78
79 return f->code;
80}
81
85cd2f9f 82void log_ref_add(struct log_ref *ref)
7b526b61 83{
90234540
DS
84 uint32_t i = 0;
85
7b526b61
QY
86 pthread_mutex_lock(&refs_mtx);
87 {
90234540
DS
88 while (ref[i].code != END_FERR) {
89 hash_get(refs, &ref[i], hash_alloc_intern);
90 i++;
91 }
7b526b61
QY
92 }
93 pthread_mutex_unlock(&refs_mtx);
94}
95
85cd2f9f 96struct log_ref *log_ref_get(uint32_t code)
7b526b61 97{
85cd2f9f
QY
98 struct log_ref holder;
99 struct log_ref *ref;
7b526b61
QY
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
85cd2f9f 111void log_ref_display(struct vty *vty, uint32_t code, bool json)
7b526b61 112{
85cd2f9f 113 struct log_ref *ref;
184ce1c5 114 struct json_object *top = NULL, *obj = NULL;
ed8841d3
QY
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) {
85cd2f9f 128 ref = log_ref_get(code);
ed8841d3
QY
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];
af4d3437 139
ed8841d3
QY
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];
af4d3437 151
ed8841d3 152 snprintf(pbuf, sizeof(pbuf), "\nError %"PRIu32" - %s",
b2111f08 153 ref->code, ref->title);
ed8841d3 154 memset(ubuf, '=', strlen(pbuf));
aa1bac30 155 ubuf[strlen(pbuf)] = '\0';
ed8841d3
QY
156
157 vty_out(vty, "%s\n%s\n", pbuf, ubuf);
af4d3437
QY
158 vty_out(vty, "Description:\n%s\n\n", ref->description);
159 vty_out(vty, "Recommendation:\n%s\n", ref->suggestion);
ed8841d3
QY
160 }
161 }
7b526b61 162
ed8841d3
QY
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);
7b526b61
QY
168 }
169
6a154c88 170 list_delete(&errlist);
7b526b61
QY
171}
172
173DEFUN_NOSH(show_error_code,
174 show_error_code_cmd,
ed8841d3 175 "show error <(1-4294967296)|all> [json]",
7b526b61
QY
176 SHOW_STR
177 "Information on errors\n"
ed8841d3
QY
178 "Error code to get info about\n"
179 "Information on all errors\n"
180 JSON_STR)
7b526b61 181{
ed8841d3
QY
182 bool json = strmatch(argv[argc-1]->text, "json");
183 uint32_t arg = 0;
af4d3437 184
ed8841d3
QY
185 if (!strmatch(argv[2]->text, "all"))
186 arg = strtoul(argv[2]->arg, NULL, 10);
7b526b61 187
85cd2f9f 188 log_ref_display(vty, arg, json);
7b526b61
QY
189 return CMD_SUCCESS;
190}
191
85cd2f9f 192void log_ref_init(void)
7b526b61
QY
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
85cd2f9f 204void log_ref_fini(void)
7b526b61
QY
205{
206 pthread_mutex_lock(&refs_mtx);
207 {
fa8b3ca6 208 hash_clean(refs, NULL);
7b526b61
QY
209 hash_free(refs);
210 refs = NULL;
211 }
212 pthread_mutex_unlock(&refs_mtx);
213}
214
3155489a
DL
215const 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
223ferr_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
231static ferr_r ferr_set_va(const char *file, int line, const char *func,
996c9314
LB
232 enum ferr_kind kind, const char *pathname,
233 int errno_val, const char *text, va_list va)
3155489a
DL
234{
235 struct ferr *error = pthread_getspecific(errkey);
236
237 if (!error) {
238 error = XCALLOC(MTYPE_ERRINFO, sizeof(*error));
3155489a
DL
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),
996c9314 249 jhash(file, strlen(file), 0xd4ed0298));
3155489a
DL
250
251 error->errno_val = errno_val;
252 if (pathname)
996c9314
LB
253 snprintf(error->pathname, sizeof(error->pathname), "%s",
254 pathname);
3155489a
DL
255 else
256 error->pathname[0] = '\0';
257
258 vsnprintf(error->message, sizeof(error->message), text, va);
259 return -1;
260}
261
262ferr_r ferr_set_internal(const char *file, int line, const char *func,
996c9314 263 enum ferr_kind kind, const char *text, ...)
3155489a
DL
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
273ferr_r ferr_set_internal_ext(const char *file, int line, const char *func,
996c9314
LB
274 enum ferr_kind kind, const char *pathname,
275 int errno_val, const char *text, ...)
3155489a
DL
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"
286void 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;
996c9314 302 vty_out(vty, "%s%s%s\n", tmpmsg,
3155489a
DL
303 last_error ? last_error->message : "(no error?)",
304 replacepos);
305 }
306}