2 * Copyright (c) 2015-16 David Lamparter, for NetDEF, Inc.
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.
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.
36 #include "frr_pthread.h"
38 DEFINE_MTYPE_STATIC(LIB
, ERRINFO
, "error information")
41 * Thread-specific key for temporary storage of allocated ferr.
43 static pthread_key_t errkey
;
45 static void ferr_free(void *arg
)
47 XFREE(MTYPE_ERRINFO
, arg
);
50 static void err_key_init(void) __attribute__((_CONSTRUCTOR(500)));
51 static void err_key_init(void)
53 pthread_key_create(&errkey
, ferr_free
);
56 static void err_key_fini(void) __attribute__((_DESTRUCTOR(500)));
57 static void err_key_fini(void)
59 pthread_key_delete(errkey
);
63 * Global shared hash table holding reference text for all defined errors.
65 static pthread_mutex_t refs_mtx
= PTHREAD_MUTEX_INITIALIZER
;
68 static bool ferr_hash_cmp(const void *a
, const void *b
)
70 const struct log_ref
*f_a
= a
;
71 const struct log_ref
*f_b
= b
;
73 return f_a
->code
== f_b
->code
;
76 static inline unsigned int ferr_hash_key(const void *a
)
78 const struct log_ref
*f
= a
;
83 void log_ref_add(struct log_ref
*ref
)
87 frr_with_mutex(&refs_mtx
) {
88 while (ref
[i
].code
!= END_FERR
) {
89 hash_get(refs
, &ref
[i
], hash_alloc_intern
);
95 struct log_ref
*log_ref_get(uint32_t code
)
97 struct log_ref holder
;
101 frr_with_mutex(&refs_mtx
) {
102 ref
= hash_lookup(refs
, &holder
);
108 void log_ref_display(struct vty
*vty
, uint32_t code
, bool json
)
111 struct json_object
*top
= NULL
, *obj
= NULL
;
112 struct list
*errlist
;
116 top
= json_object_new_object();
118 frr_with_mutex(&refs_mtx
) {
119 errlist
= code
? list_new() : hash_to_list(refs
);
123 ref
= log_ref_get(code
);
126 listnode_add(errlist
, ref
);
129 for (ALL_LIST_ELEMENTS_RO(errlist
, ln
, ref
)) {
133 snprintf(key
, sizeof(key
), "%u", ref
->code
);
134 obj
= json_object_new_object();
135 json_object_string_add(obj
, "title", ref
->title
);
136 json_object_string_add(obj
, "description",
138 json_object_string_add(obj
, "suggestion",
140 json_object_object_add(top
, key
, obj
);
145 snprintf(pbuf
, sizeof(pbuf
), "\nError %u - %s",
146 ref
->code
, ref
->title
);
147 memset(ubuf
, '=', strlen(pbuf
));
148 ubuf
[strlen(pbuf
)] = '\0';
150 vty_out(vty
, "%s\n%s\n", pbuf
, ubuf
);
151 vty_out(vty
, "Description:\n%s\n\n", ref
->description
);
152 vty_out(vty
, "Recommendation:\n%s\n", ref
->suggestion
);
157 const char *str
= json_object_to_json_string_ext(
158 top
, JSON_C_TO_STRING_PRETTY
);
159 vty_out(vty
, "%s\n", str
);
160 json_object_free(top
);
163 list_delete(&errlist
);
166 DEFUN_NOSH(show_error_code
,
168 "show error <(1-4294967295)|all> [json]",
170 "Information on errors\n"
171 "Error code to get info about\n"
172 "Information on all errors\n"
175 bool json
= strmatch(argv
[argc
-1]->text
, "json");
178 if (!strmatch(argv
[2]->text
, "all"))
179 arg
= strtoul(argv
[2]->arg
, NULL
, 10);
181 log_ref_display(vty
, arg
, json
);
185 void log_ref_init(void)
187 frr_with_mutex(&refs_mtx
) {
188 refs
= hash_create(ferr_hash_key
, ferr_hash_cmp
,
189 "Error Reference Texts");
193 void log_ref_fini(void)
195 frr_with_mutex(&refs_mtx
) {
196 hash_clean(refs
, NULL
);
202 void log_ref_vty_init(void)
204 install_element(VIEW_NODE
, &show_error_code_cmd
);
208 const struct ferr
*ferr_get_last(ferr_r errval
)
210 struct ferr
*last_error
= pthread_getspecific(errkey
);
211 if (!last_error
|| last_error
->kind
== 0)
216 ferr_r
ferr_clear(void)
218 struct ferr
*last_error
= pthread_getspecific(errkey
);
220 last_error
->kind
= 0;
224 static ferr_r
ferr_set_va(const char *file
, int line
, const char *func
,
225 enum ferr_kind kind
, const char *pathname
,
226 int errno_val
, const char *text
, va_list va
)
228 struct ferr
*error
= pthread_getspecific(errkey
);
231 error
= XCALLOC(MTYPE_ERRINFO
, sizeof(*error
));
233 pthread_setspecific(errkey
, error
);
241 error
->unique_id
= jhash(text
, strlen(text
),
242 jhash(file
, strlen(file
), 0xd4ed0298));
244 error
->errno_val
= errno_val
;
246 snprintf(error
->pathname
, sizeof(error
->pathname
), "%s",
249 error
->pathname
[0] = '\0';
251 vsnprintf(error
->message
, sizeof(error
->message
), text
, va
);
255 ferr_r
ferr_set_internal(const char *file
, int line
, const char *func
,
256 enum ferr_kind kind
, const char *text
, ...)
261 rv
= ferr_set_va(file
, line
, func
, kind
, NULL
, 0, text
, va
);
266 ferr_r
ferr_set_internal_ext(const char *file
, int line
, const char *func
,
267 enum ferr_kind kind
, const char *pathname
,
268 int errno_val
, const char *text
, ...)
273 rv
= ferr_set_va(file
, line
, func
, kind
, pathname
, errno_val
, text
, va
);
278 #define REPLACE "$ERR"
279 void vty_print_error(struct vty
*vty
, ferr_r err
, const char *msg
, ...)
281 char tmpmsg
[512], *replacepos
;
282 const struct ferr
*last_error
= ferr_get_last(err
);
286 vsnprintf(tmpmsg
, sizeof(tmpmsg
), msg
, va
);
289 replacepos
= strstr(tmpmsg
, REPLACE
);
291 vty_out(vty
, "%s\n", tmpmsg
);
293 replacepos
[0] = '\0';
294 replacepos
+= sizeof(REPLACE
) - 1;
295 vty_out(vty
, "%s%s%s\n", tmpmsg
,
296 last_error
? last_error
->message
: "(no error?)",