]> git.proxmox.com Git - mirror_frr.git/blob - lib/ferr.c
lib, zebra: Add type and instance to nexthop update message
[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, int errno_val,
67 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 not */
78 }
79
80 pthread_setspecific(errkey, error);
81 }
82
83 error->file = file;
84 error->line = line;
85 error->func = func;
86 error->kind = kind;
87
88 error->unique_id = jhash(text, strlen(text),
89 jhash(file, strlen(file), 0xd4ed0298));
90
91 error->errno_val = errno_val;
92 if (pathname)
93 snprintf(error->pathname, sizeof(error->pathname),
94 "%s", pathname);
95 else
96 error->pathname[0] = '\0';
97
98 vsnprintf(error->message, sizeof(error->message), text, va);
99 return -1;
100 }
101
102 ferr_r ferr_set_internal(const char *file, int line, const char *func,
103 enum ferr_kind kind, const char *text, ...)
104 {
105 ferr_r rv;
106 va_list va;
107 va_start(va, text);
108 rv = ferr_set_va(file, line, func, kind, NULL, 0, text, va);
109 va_end(va);
110 return rv;
111 }
112
113 ferr_r ferr_set_internal_ext(const char *file, int line, const char *func,
114 enum ferr_kind kind, const char *pathname, int errno_val,
115 const char *text, ...)
116 {
117 ferr_r rv;
118 va_list va;
119 va_start(va, text);
120 rv = ferr_set_va(file, line, func, kind, pathname, errno_val, text, va);
121 va_end(va);
122 return rv;
123 }
124
125 #define REPLACE "$ERR"
126 void vty_print_error(struct vty *vty, ferr_r err, const char *msg, ...)
127 {
128 char tmpmsg[512], *replacepos;
129 const struct ferr *last_error = ferr_get_last(err);
130
131 va_list va;
132 va_start(va, msg);
133 vsnprintf(tmpmsg, sizeof(tmpmsg), msg, va);
134 va_end(va);
135
136 replacepos = strstr(tmpmsg, REPLACE);
137 if (!replacepos)
138 vty_out(vty, "%s\n", tmpmsg);
139 else {
140 replacepos[0] = '\0';
141 replacepos += sizeof(REPLACE) - 1;
142 vty_out(vty, "%s%s%s\n",
143 tmpmsg,
144 last_error ? last_error->message : "(no error?)",
145 replacepos);
146 }
147 }
148