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