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