]> git.proxmox.com Git - mirror_frr.git/blob - lib/ferr.c
*: use C99 standard fixed-width integer types
[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
28 DEFINE_MTYPE_STATIC(LIB, ERRINFO, "error information")
29
30 static pthread_key_t errkey;
31
32 static void ferr_free(void *arg)
33 {
34 XFREE(MTYPE_ERRINFO, arg);
35 }
36
37 static void err_key_init(void) __attribute__((_CONSTRUCTOR(500)));
38 static void err_key_init(void)
39 {
40 pthread_key_create(&errkey, ferr_free);
41 }
42
43 static void err_key_fini(void) __attribute__((_DESTRUCTOR(500)));
44 static void err_key_fini(void)
45 {
46 pthread_key_delete(errkey);
47 }
48
49 const struct ferr *ferr_get_last(ferr_r errval)
50 {
51 struct ferr *last_error = pthread_getspecific(errkey);
52 if (!last_error || last_error->kind == 0)
53 return NULL;
54 return last_error;
55 }
56
57 ferr_r ferr_clear(void)
58 {
59 struct ferr *last_error = pthread_getspecific(errkey);
60 if (last_error)
61 last_error->kind = 0;
62 return ferr_ok();
63 }
64
65 static ferr_r ferr_set_va(const char *file, int line, const char *func,
66 enum ferr_kind kind, const char *pathname,
67 int errno_val, const char *text, va_list va)
68 {
69 struct ferr *error = pthread_getspecific(errkey);
70
71 if (!error) {
72 error = XCALLOC(MTYPE_ERRINFO, sizeof(*error));
73 if (!error) {
74 /* we're screwed */
75 zlog_err("out of memory while allocating error info");
76 raise(SIGSEGV);
77 abort(); /* raise() can return, but raise(SIGSEGV) shall
78 not */
79 }
80
81 pthread_setspecific(errkey, error);
82 }
83
84 error->file = file;
85 error->line = line;
86 error->func = func;
87 error->kind = kind;
88
89 error->unique_id = jhash(text, strlen(text),
90 jhash(file, strlen(file), 0xd4ed0298));
91
92 error->errno_val = errno_val;
93 if (pathname)
94 snprintf(error->pathname, sizeof(error->pathname), "%s",
95 pathname);
96 else
97 error->pathname[0] = '\0';
98
99 vsnprintf(error->message, sizeof(error->message), text, va);
100 return -1;
101 }
102
103 ferr_r ferr_set_internal(const char *file, int line, const char *func,
104 enum ferr_kind kind, const char *text, ...)
105 {
106 ferr_r rv;
107 va_list va;
108 va_start(va, text);
109 rv = ferr_set_va(file, line, func, kind, NULL, 0, text, va);
110 va_end(va);
111 return rv;
112 }
113
114 ferr_r ferr_set_internal_ext(const char *file, int line, const char *func,
115 enum ferr_kind kind, const char *pathname,
116 int errno_val, const char *text, ...)
117 {
118 ferr_r rv;
119 va_list va;
120 va_start(va, text);
121 rv = ferr_set_va(file, line, func, kind, pathname, errno_val, text, va);
122 va_end(va);
123 return rv;
124 }
125
126 #define REPLACE "$ERR"
127 void vty_print_error(struct vty *vty, ferr_r err, const char *msg, ...)
128 {
129 char tmpmsg[512], *replacepos;
130 const struct ferr *last_error = ferr_get_last(err);
131
132 va_list va;
133 va_start(va, msg);
134 vsnprintf(tmpmsg, sizeof(tmpmsg), msg, va);
135 va_end(va);
136
137 replacepos = strstr(tmpmsg, REPLACE);
138 if (!replacepos)
139 vty_out(vty, "%s\n", tmpmsg);
140 else {
141 replacepos[0] = '\0';
142 replacepos += sizeof(REPLACE) - 1;
143 vty_out(vty, "%s%s%s\n", tmpmsg,
144 last_error ? last_error->message : "(no error?)",
145 replacepos);
146 }
147 }