]> git.proxmox.com Git - mirror_frr.git/blob - lib/ferr.c
lib: Add LIB_ERR_ZMQ
[mirror_frr.git] / lib / ferr.c
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
17 #include <stdlib.h>
18 #include <stdarg.h>
19 #include <string.h>
20 #include <pthread.h>
21 #include <signal.h>
22
23 #include "ferr.h"
24 #include "vty.h"
25 #include "jhash.h"
26 #include "memory.h"
27 #include "hash.h"
28 #include "command.h"
29
30 DEFINE_MTYPE_STATIC(LIB, ERRINFO, "error information")
31
32 /*
33 * Thread-specific key for temporary storage of allocated ferr.
34 */
35 static pthread_key_t errkey;
36
37 static void ferr_free(void *arg)
38 {
39 XFREE(MTYPE_ERRINFO, arg);
40 }
41
42 static void err_key_init(void) __attribute__((_CONSTRUCTOR(500)));
43 static void err_key_init(void)
44 {
45 pthread_key_create(&errkey, ferr_free);
46 }
47
48 static void err_key_fini(void) __attribute__((_DESTRUCTOR(500)));
49 static void err_key_fini(void)
50 {
51 pthread_key_delete(errkey);
52 }
53
54 /*
55 * Global shared hash table holding reference text for all defined errors.
56 */
57 pthread_mutex_t refs_mtx = PTHREAD_MUTEX_INITIALIZER;
58 struct hash *refs;
59
60 static int ferr_hash_cmp(const void *a, const void *b)
61 {
62 const struct ferr_ref *f_a = a;
63 const struct ferr_ref *f_b = b;
64
65 return f_a->code == f_b->code;
66 }
67
68 static inline unsigned int ferr_hash_key(void *a)
69 {
70 struct ferr_ref *f = a;
71
72 return f->code;
73 }
74
75 void ferr_ref_add(struct ferr_ref *ref)
76 {
77 uint32_t i = 0;
78
79 pthread_mutex_lock(&refs_mtx);
80 {
81 while (ref[i].code != END_FERR) {
82 hash_get(refs, &ref[i], hash_alloc_intern);
83 i++;
84 }
85 }
86 pthread_mutex_unlock(&refs_mtx);
87 }
88
89 struct ferr_ref *ferr_ref_get(uint32_t code)
90 {
91 struct ferr_ref holder;
92 struct ferr_ref *ref;
93
94 holder.code = code;
95 pthread_mutex_lock(&refs_mtx);
96 {
97 ref = hash_lookup(refs, &holder);
98 }
99 pthread_mutex_unlock(&refs_mtx);
100
101 return ref;
102 }
103
104 void ferr_ref_display(struct vty *vty, uint32_t code)
105 {
106 struct ferr_ref *ref = ferr_ref_get(code);
107
108 if (!ref) {
109 vty_out(vty, "Code %d - Unknown\n", code);
110 return;
111 }
112
113 vty_out(vty, "Error Code %d - %s\n", code, ref->title);
114 vty_out(vty, "--------------------------------------\n");
115 vty_out(vty, "\nDescription:\n%s\n\nRecommendation:\n%s\n\n",
116 ref->description, ref->suggestion);
117 }
118
119 DEFUN_NOSH(show_error_code,
120 show_error_code_cmd,
121 "show error (0-4294967296)",
122 SHOW_STR
123 "Information on errors\n"
124 "Error code to get info about\n")
125 {
126 uint32_t arg = strtoul(argv[2]->arg, NULL, 10);
127
128 ferr_ref_display(vty, arg);
129 return CMD_SUCCESS;
130 }
131
132 void ferr_ref_init(void)
133 {
134 pthread_mutex_lock(&refs_mtx);
135 {
136 refs = hash_create(ferr_hash_key, ferr_hash_cmp,
137 "Error Reference Texts");
138 }
139 pthread_mutex_unlock(&refs_mtx);
140
141 install_element(VIEW_NODE, &show_error_code_cmd);
142 }
143
144 void ferr_ref_fini(void)
145 {
146 pthread_mutex_lock(&refs_mtx);
147 {
148 hash_free(refs);
149 refs = NULL;
150 }
151 pthread_mutex_unlock(&refs_mtx);
152 }
153
154 const struct ferr *ferr_get_last(ferr_r errval)
155 {
156 struct ferr *last_error = pthread_getspecific(errkey);
157 if (!last_error || last_error->kind == 0)
158 return NULL;
159 return last_error;
160 }
161
162 ferr_r ferr_clear(void)
163 {
164 struct ferr *last_error = pthread_getspecific(errkey);
165 if (last_error)
166 last_error->kind = 0;
167 return ferr_ok();
168 }
169
170 static ferr_r ferr_set_va(const char *file, int line, const char *func,
171 enum ferr_kind kind, const char *pathname,
172 int errno_val, const char *text, va_list va)
173 {
174 struct ferr *error = pthread_getspecific(errkey);
175
176 if (!error) {
177 error = XCALLOC(MTYPE_ERRINFO, sizeof(*error));
178
179 pthread_setspecific(errkey, error);
180 }
181
182 error->file = file;
183 error->line = line;
184 error->func = func;
185 error->kind = kind;
186
187 error->unique_id = jhash(text, strlen(text),
188 jhash(file, strlen(file), 0xd4ed0298));
189
190 error->errno_val = errno_val;
191 if (pathname)
192 snprintf(error->pathname, sizeof(error->pathname), "%s",
193 pathname);
194 else
195 error->pathname[0] = '\0';
196
197 vsnprintf(error->message, sizeof(error->message), text, va);
198 return -1;
199 }
200
201 ferr_r ferr_set_internal(const char *file, int line, const char *func,
202 enum ferr_kind kind, const char *text, ...)
203 {
204 ferr_r rv;
205 va_list va;
206 va_start(va, text);
207 rv = ferr_set_va(file, line, func, kind, NULL, 0, text, va);
208 va_end(va);
209 return rv;
210 }
211
212 ferr_r ferr_set_internal_ext(const char *file, int line, const char *func,
213 enum ferr_kind kind, const char *pathname,
214 int errno_val, const char *text, ...)
215 {
216 ferr_r rv;
217 va_list va;
218 va_start(va, text);
219 rv = ferr_set_va(file, line, func, kind, pathname, errno_val, text, va);
220 va_end(va);
221 return rv;
222 }
223
224 #define REPLACE "$ERR"
225 void vty_print_error(struct vty *vty, ferr_r err, const char *msg, ...)
226 {
227 char tmpmsg[512], *replacepos;
228 const struct ferr *last_error = ferr_get_last(err);
229
230 va_list va;
231 va_start(va, msg);
232 vsnprintf(tmpmsg, sizeof(tmpmsg), msg, va);
233 va_end(va);
234
235 replacepos = strstr(tmpmsg, REPLACE);
236 if (!replacepos)
237 vty_out(vty, "%s\n", tmpmsg);
238 else {
239 replacepos[0] = '\0';
240 replacepos += sizeof(REPLACE) - 1;
241 vty_out(vty, "%s%s%s\n", tmpmsg,
242 last_error ? last_error->message : "(no error?)",
243 replacepos);
244 }
245 }