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.
30 DEFINE_MTYPE_STATIC(LIB
, ERRINFO
, "error information")
33 * Thread-specific key for temporary storage of allocated ferr.
35 static pthread_key_t errkey
;
37 static void ferr_free(void *arg
)
39 XFREE(MTYPE_ERRINFO
, arg
);
42 static void err_key_init(void) __attribute__((_CONSTRUCTOR(500)));
43 static void err_key_init(void)
45 pthread_key_create(&errkey
, ferr_free
);
48 static void err_key_fini(void) __attribute__((_DESTRUCTOR(500)));
49 static void err_key_fini(void)
51 pthread_key_delete(errkey
);
55 * Global shared hash table holding reference text for all defined errors.
57 pthread_mutex_t refs_mtx
= PTHREAD_MUTEX_INITIALIZER
;
60 static int ferr_hash_cmp(const void *a
, const void *b
)
62 const struct ferr_ref
*f_a
= a
;
63 const struct ferr_ref
*f_b
= b
;
65 return f_a
->code
== f_b
->code
;
68 static inline unsigned int ferr_hash_key(void *a
)
70 struct ferr_ref
*f
= a
;
75 void ferr_ref_add(struct ferr_ref
*ref
)
79 pthread_mutex_lock(&refs_mtx
);
81 while (ref
[i
].code
!= END_FERR
) {
82 hash_get(refs
, &ref
[i
], hash_alloc_intern
);
86 pthread_mutex_unlock(&refs_mtx
);
89 struct ferr_ref
*ferr_ref_get(uint32_t code
)
91 struct ferr_ref holder
;
95 pthread_mutex_lock(&refs_mtx
);
97 ref
= hash_lookup(refs
, &holder
);
99 pthread_mutex_unlock(&refs_mtx
);
104 void ferr_ref_display(struct vty
*vty
, uint32_t code
)
106 struct ferr_ref
*ref
= ferr_ref_get(code
);
109 vty_out(vty
, "Code %d - Unknown\n", code
);
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
);
119 DEFUN_NOSH(show_error_code
,
121 "show error (0-4294967296)",
123 "Information on errors\n"
124 "Error code to get info about\n")
126 uint32_t arg
= strtoul(argv
[2]->arg
, NULL
, 10);
128 ferr_ref_display(vty
, arg
);
132 void ferr_ref_init(void)
134 pthread_mutex_lock(&refs_mtx
);
136 refs
= hash_create(ferr_hash_key
, ferr_hash_cmp
,
137 "Error Reference Texts");
139 pthread_mutex_unlock(&refs_mtx
);
141 install_element(VIEW_NODE
, &show_error_code_cmd
);
144 void ferr_ref_fini(void)
146 pthread_mutex_lock(&refs_mtx
);
151 pthread_mutex_unlock(&refs_mtx
);
154 const struct ferr
*ferr_get_last(ferr_r errval
)
156 struct ferr
*last_error
= pthread_getspecific(errkey
);
157 if (!last_error
|| last_error
->kind
== 0)
162 ferr_r
ferr_clear(void)
164 struct ferr
*last_error
= pthread_getspecific(errkey
);
166 last_error
->kind
= 0;
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
)
174 struct ferr
*error
= pthread_getspecific(errkey
);
177 error
= XCALLOC(MTYPE_ERRINFO
, sizeof(*error
));
179 pthread_setspecific(errkey
, error
);
187 error
->unique_id
= jhash(text
, strlen(text
),
188 jhash(file
, strlen(file
), 0xd4ed0298));
190 error
->errno_val
= errno_val
;
192 snprintf(error
->pathname
, sizeof(error
->pathname
), "%s",
195 error
->pathname
[0] = '\0';
197 vsnprintf(error
->message
, sizeof(error
->message
), text
, va
);
201 ferr_r
ferr_set_internal(const char *file
, int line
, const char *func
,
202 enum ferr_kind kind
, const char *text
, ...)
207 rv
= ferr_set_va(file
, line
, func
, kind
, NULL
, 0, text
, va
);
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
, ...)
219 rv
= ferr_set_va(file
, line
, func
, kind
, pathname
, errno_val
, text
, va
);
224 #define REPLACE "$ERR"
225 void vty_print_error(struct vty
*vty
, ferr_r err
, const char *msg
, ...)
227 char tmpmsg
[512], *replacepos
;
228 const struct ferr
*last_error
= ferr_get_last(err
);
232 vsnprintf(tmpmsg
, sizeof(tmpmsg
), msg
, va
);
235 replacepos
= strstr(tmpmsg
, REPLACE
);
237 vty_out(vty
, "%s\n", tmpmsg
);
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?)",