]>
Commit | Line | Data |
---|---|---|
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" | |
00dffa8c | 36 | #include "frr_pthread.h" |
3155489a DL |
37 | |
38 | DEFINE_MTYPE_STATIC(LIB, ERRINFO, "error information") | |
39 | ||
7b526b61 QY |
40 | /* |
41 | * Thread-specific key for temporary storage of allocated ferr. | |
42 | */ | |
3155489a DL |
43 | static pthread_key_t errkey; |
44 | ||
45 | static void ferr_free(void *arg) | |
46 | { | |
47 | XFREE(MTYPE_ERRINFO, arg); | |
48 | } | |
49 | ||
50 | static void err_key_init(void) __attribute__((_CONSTRUCTOR(500))); | |
51 | static void err_key_init(void) | |
52 | { | |
53 | pthread_key_create(&errkey, ferr_free); | |
54 | } | |
55 | ||
56 | static void err_key_fini(void) __attribute__((_DESTRUCTOR(500))); | |
57 | static void err_key_fini(void) | |
58 | { | |
59 | pthread_key_delete(errkey); | |
60 | } | |
61 | ||
7b526b61 QY |
62 | /* |
63 | * Global shared hash table holding reference text for all defined errors. | |
64 | */ | |
c17faa4b | 65 | static pthread_mutex_t refs_mtx = PTHREAD_MUTEX_INITIALIZER; |
7b526b61 QY |
66 | struct hash *refs; |
67 | ||
74df8d6d | 68 | static bool ferr_hash_cmp(const void *a, const void *b) |
7b526b61 | 69 | { |
85cd2f9f QY |
70 | const struct log_ref *f_a = a; |
71 | const struct log_ref *f_b = b; | |
7b526b61 QY |
72 | |
73 | return f_a->code == f_b->code; | |
74 | } | |
75 | ||
d8b87afe | 76 | static inline unsigned int ferr_hash_key(const void *a) |
7b526b61 | 77 | { |
d8b87afe | 78 | const struct log_ref *f = a; |
7b526b61 QY |
79 | |
80 | return f->code; | |
81 | } | |
82 | ||
85cd2f9f | 83 | void log_ref_add(struct log_ref *ref) |
7b526b61 | 84 | { |
90234540 DS |
85 | uint32_t i = 0; |
86 | ||
00dffa8c | 87 | frr_with_mutex(&refs_mtx) { |
90234540 DS |
88 | while (ref[i].code != END_FERR) { |
89 | hash_get(refs, &ref[i], hash_alloc_intern); | |
90 | i++; | |
91 | } | |
7b526b61 | 92 | } |
7b526b61 QY |
93 | } |
94 | ||
85cd2f9f | 95 | struct log_ref *log_ref_get(uint32_t code) |
7b526b61 | 96 | { |
85cd2f9f QY |
97 | struct log_ref holder; |
98 | struct log_ref *ref; | |
7b526b61 QY |
99 | |
100 | holder.code = code; | |
00dffa8c | 101 | frr_with_mutex(&refs_mtx) { |
7b526b61 QY |
102 | ref = hash_lookup(refs, &holder); |
103 | } | |
7b526b61 QY |
104 | |
105 | return ref; | |
106 | } | |
107 | ||
85cd2f9f | 108 | void log_ref_display(struct vty *vty, uint32_t code, bool json) |
7b526b61 | 109 | { |
85cd2f9f | 110 | struct log_ref *ref; |
184ce1c5 | 111 | struct json_object *top = NULL, *obj = NULL; |
ed8841d3 QY |
112 | struct list *errlist; |
113 | struct listnode *ln; | |
114 | ||
115 | if (json) | |
116 | top = json_object_new_object(); | |
117 | ||
00dffa8c | 118 | frr_with_mutex(&refs_mtx) { |
ed8841d3 QY |
119 | errlist = code ? list_new() : hash_to_list(refs); |
120 | } | |
ed8841d3 QY |
121 | |
122 | if (code) { | |
85cd2f9f | 123 | ref = log_ref_get(code); |
1f9128d6 | 124 | if (!ref) |
ed8841d3 | 125 | return; |
ed8841d3 QY |
126 | listnode_add(errlist, ref); |
127 | } | |
128 | ||
129 | for (ALL_LIST_ELEMENTS_RO(errlist, ln, ref)) { | |
130 | if (json) { | |
131 | char key[11]; | |
af4d3437 | 132 | |
ed8841d3 QY |
133 | snprintf(key, sizeof(key), "%"PRIu32, ref->code); |
134 | obj = json_object_new_object(); | |
135 | json_object_string_add(obj, "title", ref->title); | |
136 | json_object_string_add(obj, "description", | |
137 | ref->description); | |
138 | json_object_string_add(obj, "suggestion", | |
139 | ref->suggestion); | |
140 | json_object_object_add(top, key, obj); | |
141 | } else { | |
142 | char pbuf[256]; | |
143 | char ubuf[256]; | |
af4d3437 | 144 | |
ed8841d3 | 145 | snprintf(pbuf, sizeof(pbuf), "\nError %"PRIu32" - %s", |
b2111f08 | 146 | ref->code, ref->title); |
ed8841d3 | 147 | memset(ubuf, '=', strlen(pbuf)); |
aa1bac30 | 148 | ubuf[strlen(pbuf)] = '\0'; |
ed8841d3 QY |
149 | |
150 | vty_out(vty, "%s\n%s\n", pbuf, ubuf); | |
af4d3437 QY |
151 | vty_out(vty, "Description:\n%s\n\n", ref->description); |
152 | vty_out(vty, "Recommendation:\n%s\n", ref->suggestion); | |
ed8841d3 QY |
153 | } |
154 | } | |
7b526b61 | 155 | |
ed8841d3 QY |
156 | if (json) { |
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); | |
7b526b61 QY |
161 | } |
162 | ||
6a154c88 | 163 | list_delete(&errlist); |
7b526b61 QY |
164 | } |
165 | ||
166 | DEFUN_NOSH(show_error_code, | |
167 | show_error_code_cmd, | |
20054cb4 | 168 | "show error <(1-4294967295)|all> [json]", |
7b526b61 QY |
169 | SHOW_STR |
170 | "Information on errors\n" | |
ed8841d3 QY |
171 | "Error code to get info about\n" |
172 | "Information on all errors\n" | |
173 | JSON_STR) | |
7b526b61 | 174 | { |
ed8841d3 QY |
175 | bool json = strmatch(argv[argc-1]->text, "json"); |
176 | uint32_t arg = 0; | |
af4d3437 | 177 | |
ed8841d3 QY |
178 | if (!strmatch(argv[2]->text, "all")) |
179 | arg = strtoul(argv[2]->arg, NULL, 10); | |
7b526b61 | 180 | |
85cd2f9f | 181 | log_ref_display(vty, arg, json); |
7b526b61 QY |
182 | return CMD_SUCCESS; |
183 | } | |
184 | ||
85cd2f9f | 185 | void log_ref_init(void) |
7b526b61 | 186 | { |
00dffa8c | 187 | frr_with_mutex(&refs_mtx) { |
7b526b61 QY |
188 | refs = hash_create(ferr_hash_key, ferr_hash_cmp, |
189 | "Error Reference Texts"); | |
190 | } | |
7b526b61 QY |
191 | } |
192 | ||
85cd2f9f | 193 | void log_ref_fini(void) |
7b526b61 | 194 | { |
00dffa8c | 195 | frr_with_mutex(&refs_mtx) { |
fa8b3ca6 | 196 | hash_clean(refs, NULL); |
7b526b61 QY |
197 | hash_free(refs); |
198 | refs = NULL; | |
199 | } | |
7b526b61 QY |
200 | } |
201 | ||
1f9128d6 QY |
202 | void log_ref_vty_init(void) |
203 | { | |
204 | install_element(VIEW_NODE, &show_error_code_cmd); | |
205 | } | |
206 | ||
207 | ||
3155489a DL |
208 | const struct ferr *ferr_get_last(ferr_r errval) |
209 | { | |
210 | struct ferr *last_error = pthread_getspecific(errkey); | |
211 | if (!last_error || last_error->kind == 0) | |
212 | return NULL; | |
213 | return last_error; | |
214 | } | |
215 | ||
216 | ferr_r ferr_clear(void) | |
217 | { | |
218 | struct ferr *last_error = pthread_getspecific(errkey); | |
219 | if (last_error) | |
220 | last_error->kind = 0; | |
221 | return ferr_ok(); | |
222 | } | |
223 | ||
224 | static ferr_r ferr_set_va(const char *file, int line, const char *func, | |
996c9314 LB |
225 | enum ferr_kind kind, const char *pathname, |
226 | int errno_val, const char *text, va_list va) | |
3155489a DL |
227 | { |
228 | struct ferr *error = pthread_getspecific(errkey); | |
229 | ||
230 | if (!error) { | |
231 | error = XCALLOC(MTYPE_ERRINFO, sizeof(*error)); | |
3155489a DL |
232 | |
233 | pthread_setspecific(errkey, error); | |
234 | } | |
235 | ||
236 | error->file = file; | |
237 | error->line = line; | |
238 | error->func = func; | |
239 | error->kind = kind; | |
240 | ||
241 | error->unique_id = jhash(text, strlen(text), | |
996c9314 | 242 | jhash(file, strlen(file), 0xd4ed0298)); |
3155489a DL |
243 | |
244 | error->errno_val = errno_val; | |
245 | if (pathname) | |
996c9314 LB |
246 | snprintf(error->pathname, sizeof(error->pathname), "%s", |
247 | pathname); | |
3155489a DL |
248 | else |
249 | error->pathname[0] = '\0'; | |
250 | ||
251 | vsnprintf(error->message, sizeof(error->message), text, va); | |
252 | return -1; | |
253 | } | |
254 | ||
255 | ferr_r ferr_set_internal(const char *file, int line, const char *func, | |
996c9314 | 256 | enum ferr_kind kind, const char *text, ...) |
3155489a DL |
257 | { |
258 | ferr_r rv; | |
259 | va_list va; | |
260 | va_start(va, text); | |
261 | rv = ferr_set_va(file, line, func, kind, NULL, 0, text, va); | |
262 | va_end(va); | |
263 | return rv; | |
264 | } | |
265 | ||
266 | ferr_r ferr_set_internal_ext(const char *file, int line, const char *func, | |
996c9314 LB |
267 | enum ferr_kind kind, const char *pathname, |
268 | int errno_val, const char *text, ...) | |
3155489a DL |
269 | { |
270 | ferr_r rv; | |
271 | va_list va; | |
272 | va_start(va, text); | |
273 | rv = ferr_set_va(file, line, func, kind, pathname, errno_val, text, va); | |
274 | va_end(va); | |
275 | return rv; | |
276 | } | |
277 | ||
278 | #define REPLACE "$ERR" | |
279 | void vty_print_error(struct vty *vty, ferr_r err, const char *msg, ...) | |
280 | { | |
281 | char tmpmsg[512], *replacepos; | |
282 | const struct ferr *last_error = ferr_get_last(err); | |
283 | ||
284 | va_list va; | |
285 | va_start(va, msg); | |
286 | vsnprintf(tmpmsg, sizeof(tmpmsg), msg, va); | |
287 | va_end(va); | |
288 | ||
289 | replacepos = strstr(tmpmsg, REPLACE); | |
290 | if (!replacepos) | |
291 | vty_out(vty, "%s\n", tmpmsg); | |
292 | else { | |
293 | replacepos[0] = '\0'; | |
294 | replacepos += sizeof(REPLACE) - 1; | |
996c9314 | 295 | vty_out(vty, "%s%s%s\n", tmpmsg, |
3155489a DL |
296 | last_error ? last_error->message : "(no error?)", |
297 | replacepos); | |
298 | } | |
299 | } |