1 // SPDX-License-Identifier: ISC
3 * Copyright (c) 2015-16 David Lamparter, for NetDEF, Inc.
25 #include "frr_pthread.h"
27 DEFINE_MTYPE_STATIC(LIB
, ERRINFO
, "error information");
30 * Thread-specific key for temporary storage of allocated ferr.
32 static pthread_key_t errkey
;
34 static void ferr_free(void *arg
)
36 XFREE(MTYPE_ERRINFO
, arg
);
39 static void err_key_init(void) __attribute__((_CONSTRUCTOR(500)));
40 static void err_key_init(void)
42 pthread_key_create(&errkey
, ferr_free
);
45 static void err_key_fini(void) __attribute__((_DESTRUCTOR(500)));
46 static void err_key_fini(void)
48 pthread_key_delete(errkey
);
52 * Global shared hash table holding reference text for all defined errors.
54 static pthread_mutex_t refs_mtx
= PTHREAD_MUTEX_INITIALIZER
;
57 static bool ferr_hash_cmp(const void *a
, const void *b
)
59 const struct log_ref
*f_a
= a
;
60 const struct log_ref
*f_b
= b
;
62 return f_a
->code
== f_b
->code
;
65 static inline unsigned int ferr_hash_key(const void *a
)
67 const struct log_ref
*f
= a
;
72 void log_ref_add(struct log_ref
*ref
)
76 frr_with_mutex (&refs_mtx
) {
77 while (ref
[i
].code
!= END_FERR
) {
78 (void)hash_get(refs
, &ref
[i
], hash_alloc_intern
);
84 struct log_ref
*log_ref_get(uint32_t code
)
86 struct log_ref holder
;
90 frr_with_mutex (&refs_mtx
) {
91 ref
= hash_lookup(refs
, &holder
);
97 void log_ref_display(struct vty
*vty
, uint32_t code
, bool json
)
100 struct json_object
*top
= NULL
, *obj
= NULL
;
101 struct list
*errlist
;
105 top
= json_object_new_object();
107 frr_with_mutex (&refs_mtx
) {
108 errlist
= code
? list_new() : hash_to_list(refs
);
112 ref
= log_ref_get(code
);
115 json_object_free(top
);
116 list_delete(&errlist
);
119 listnode_add(errlist
, ref
);
122 for (ALL_LIST_ELEMENTS_RO(errlist
, ln
, ref
)) {
126 snprintf(key
, sizeof(key
), "%u", ref
->code
);
127 obj
= json_object_new_object();
128 json_object_string_add(obj
, "title", ref
->title
);
129 json_object_string_add(obj
, "description",
131 json_object_string_add(obj
, "suggestion",
133 json_object_object_add(top
, key
, obj
);
138 snprintf(pbuf
, sizeof(pbuf
), "\nError %u - %s",
139 ref
->code
, ref
->title
);
140 memset(ubuf
, '=', strlen(pbuf
));
141 ubuf
[strlen(pbuf
)] = '\0';
143 vty_out(vty
, "%s\n%s\n", pbuf
, ubuf
);
144 vty_out(vty
, "Description:\n%s\n\n", ref
->description
);
145 vty_out(vty
, "Recommendation:\n%s\n", ref
->suggestion
);
150 list_delete(&errlist
);
153 DEFUN_NOSH(show_error_code
,
155 "show error <(1-4294967295)|all> [json]",
157 "Information on errors\n"
158 "Error code to get info about\n"
159 "Information on all errors\n"
162 bool json
= strmatch(argv
[argc
-1]->text
, "json");
165 if (!strmatch(argv
[2]->text
, "all"))
166 arg
= strtoul(argv
[2]->arg
, NULL
, 10);
168 log_ref_display(vty
, arg
, json
);
172 void log_ref_init(void)
174 frr_with_mutex (&refs_mtx
) {
175 refs
= hash_create(ferr_hash_key
, ferr_hash_cmp
,
176 "Error Reference Texts");
180 void log_ref_fini(void)
182 frr_with_mutex (&refs_mtx
) {
183 hash_clean(refs
, NULL
);
189 void log_ref_vty_init(void)
191 install_element(VIEW_NODE
, &show_error_code_cmd
);
195 const struct ferr
*ferr_get_last(ferr_r errval
)
197 struct ferr
*last_error
= pthread_getspecific(errkey
);
198 if (!last_error
|| last_error
->kind
== 0)
203 ferr_r
ferr_clear(void)
205 struct ferr
*last_error
= pthread_getspecific(errkey
);
207 last_error
->kind
= 0;
212 static ferr_r
ferr_set_va(const char *file
, int line
, const char *func
,
213 enum ferr_kind kind
, const char *pathname
,
214 int errno_val
, const char *text
, va_list va
)
216 struct ferr
*error
= pthread_getspecific(errkey
);
219 error
= XCALLOC(MTYPE_ERRINFO
, sizeof(*error
));
221 pthread_setspecific(errkey
, error
);
229 error
->unique_id
= jhash(text
, strlen(text
),
230 jhash(file
, strlen(file
), 0xd4ed0298));
232 error
->errno_val
= errno_val
;
234 snprintf(error
->pathname
, sizeof(error
->pathname
), "%s",
237 error
->pathname
[0] = '\0';
239 vsnprintf(error
->message
, sizeof(error
->message
), text
, va
);
243 ferr_r
ferr_set_internal(const char *file
, int line
, const char *func
,
244 enum ferr_kind kind
, const char *text
, ...)
249 rv
= ferr_set_va(file
, line
, func
, kind
, NULL
, 0, text
, va
);
254 ferr_r
ferr_set_internal_ext(const char *file
, int line
, const char *func
,
255 enum ferr_kind kind
, const char *pathname
,
256 int errno_val
, const char *text
, ...)
261 rv
= ferr_set_va(file
, line
, func
, kind
, pathname
, errno_val
, text
, va
);
266 #define REPLACE "$ERR"
267 void vty_print_error(struct vty
*vty
, ferr_r err
, const char *msg
, ...)
269 char tmpmsg
[512], *replacepos
;
270 const struct ferr
*last_error
= ferr_get_last(err
);
274 vsnprintf(tmpmsg
, sizeof(tmpmsg
), msg
, va
);
277 replacepos
= strstr(tmpmsg
, REPLACE
);
279 vty_out(vty
, "%s\n", tmpmsg
);
281 replacepos
[0] = '\0';
282 replacepos
+= sizeof(REPLACE
) - 1;
283 vty_out(vty
, "%s%s%s\n", tmpmsg
,
284 last_error
? last_error
->message
: "(no error?)",