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.
33 DEFINE_MTYPE_STATIC(LIB
, ERRINFO
, "error information")
36 * Thread-specific key for temporary storage of allocated ferr.
38 static pthread_key_t errkey
;
40 static void ferr_free(void *arg
)
42 XFREE(MTYPE_ERRINFO
, arg
);
45 static void err_key_init(void) __attribute__((_CONSTRUCTOR(500)));
46 static void err_key_init(void)
48 pthread_key_create(&errkey
, ferr_free
);
51 static void err_key_fini(void) __attribute__((_DESTRUCTOR(500)));
52 static void err_key_fini(void)
54 pthread_key_delete(errkey
);
58 * Global shared hash table holding reference text for all defined errors.
60 pthread_mutex_t refs_mtx
= PTHREAD_MUTEX_INITIALIZER
;
63 static int ferr_hash_cmp(const void *a
, const void *b
)
65 const struct log_ref
*f_a
= a
;
66 const struct log_ref
*f_b
= b
;
68 return f_a
->code
== f_b
->code
;
71 static inline unsigned int ferr_hash_key(void *a
)
73 struct log_ref
*f
= a
;
78 void log_ref_add(struct log_ref
*ref
)
82 pthread_mutex_lock(&refs_mtx
);
84 while (ref
[i
].code
!= END_FERR
) {
85 hash_get(refs
, &ref
[i
], hash_alloc_intern
);
89 pthread_mutex_unlock(&refs_mtx
);
92 struct log_ref
*log_ref_get(uint32_t code
)
94 struct log_ref holder
;
98 pthread_mutex_lock(&refs_mtx
);
100 ref
= hash_lookup(refs
, &holder
);
102 pthread_mutex_unlock(&refs_mtx
);
107 void log_ref_display(struct vty
*vty
, uint32_t code
, bool json
)
110 struct json_object
*top
= NULL
, *obj
= NULL
;
111 struct list
*errlist
;
115 top
= json_object_new_object();
117 pthread_mutex_lock(&refs_mtx
);
119 errlist
= code
? list_new() : hash_to_list(refs
);
121 pthread_mutex_unlock(&refs_mtx
);
124 ref
= log_ref_get(code
);
126 vty_out(vty
, "Code %"PRIu32
" - Unknown\n", code
);
129 listnode_add(errlist
, ref
);
132 for (ALL_LIST_ELEMENTS_RO(errlist
, ln
, ref
)) {
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",
141 json_object_string_add(obj
, "suggestion",
143 json_object_object_add(top
, key
, obj
);
148 snprintf(pbuf
, sizeof(pbuf
), "\nError %"PRIu32
" - %s",
149 ref
->code
, ref
->title
);
150 memset(ubuf
, '=', strlen(pbuf
));
151 ubuf
[strlen(pbuf
)] = '\0';
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
);
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
);
166 list_delete_and_null(&errlist
);
169 DEFUN_NOSH(show_error_code
,
171 "show error <(1-4294967296)|all> [json]",
173 "Information on errors\n"
174 "Error code to get info about\n"
175 "Information on all errors\n"
178 bool json
= strmatch(argv
[argc
-1]->text
, "json");
181 if (!strmatch(argv
[2]->text
, "all"))
182 arg
= strtoul(argv
[2]->arg
, NULL
, 10);
184 log_ref_display(vty
, arg
, json
);
188 void log_ref_init(void)
190 pthread_mutex_lock(&refs_mtx
);
192 refs
= hash_create(ferr_hash_key
, ferr_hash_cmp
,
193 "Error Reference Texts");
195 pthread_mutex_unlock(&refs_mtx
);
197 install_element(VIEW_NODE
, &show_error_code_cmd
);
200 void log_ref_fini(void)
202 pthread_mutex_lock(&refs_mtx
);
204 hash_clean(refs
, NULL
);
208 pthread_mutex_unlock(&refs_mtx
);
211 const struct ferr
*ferr_get_last(ferr_r errval
)
213 struct ferr
*last_error
= pthread_getspecific(errkey
);
214 if (!last_error
|| last_error
->kind
== 0)
219 ferr_r
ferr_clear(void)
221 struct ferr
*last_error
= pthread_getspecific(errkey
);
223 last_error
->kind
= 0;
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
)
231 struct ferr
*error
= pthread_getspecific(errkey
);
234 error
= XCALLOC(MTYPE_ERRINFO
, sizeof(*error
));
236 pthread_setspecific(errkey
, error
);
244 error
->unique_id
= jhash(text
, strlen(text
),
245 jhash(file
, strlen(file
), 0xd4ed0298));
247 error
->errno_val
= errno_val
;
249 snprintf(error
->pathname
, sizeof(error
->pathname
), "%s",
252 error
->pathname
[0] = '\0';
254 vsnprintf(error
->message
, sizeof(error
->message
), text
, va
);
258 ferr_r
ferr_set_internal(const char *file
, int line
, const char *func
,
259 enum ferr_kind kind
, const char *text
, ...)
264 rv
= ferr_set_va(file
, line
, func
, kind
, NULL
, 0, text
, va
);
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
, ...)
276 rv
= ferr_set_va(file
, line
, func
, kind
, pathname
, errno_val
, text
, va
);
281 #define REPLACE "$ERR"
282 void vty_print_error(struct vty
*vty
, ferr_r err
, const char *msg
, ...)
284 char tmpmsg
[512], *replacepos
;
285 const struct ferr
*last_error
= ferr_get_last(err
);
289 vsnprintf(tmpmsg
, sizeof(tmpmsg
), msg
, va
);
292 replacepos
= strstr(tmpmsg
, REPLACE
);
294 vty_out(vty
, "%s\n", tmpmsg
);
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?)",