/
log.c
189 lines (161 loc) · 4.39 KB
/
log.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
* log.c Functions in the library call radlib_log() which
* does internal logging.
*
* Version: $Id$
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*
* Copyright 2000,2006 The FreeRADIUS server project
*/
RCSID("$Id$")
#include <freeradius-devel/libradius.h>
/*
* Are we using glibc or a close relative?
*/
#ifdef HAVE_FEATURES_H
# include <features.h>
#endif
#define FR_STRERROR_BUFSIZE (2048)
fr_thread_local_setup(char *, fr_strerror_buffer) /* macro */
fr_thread_local_setup(char *, fr_syserror_buffer) /* macro */
/*
* Explicitly cleanup the memory allocated to the error buffer,
* just in case valgrind complains about it.
*/
static void fr_logging_free(void *arg)
{
free(arg);
}
/*
* Log to thread local error buffer
*/
void fr_strerror_printf(char const *fmt, ...)
{
va_list ap;
char *buffer;
buffer = fr_thread_local_init(fr_strerror_buffer, fr_logging_free);
if (!buffer) {
int ret;
/*
* malloc is thread safe, talloc is not
*/
buffer = malloc(sizeof(char) * (FR_STRERROR_BUFSIZE + 1)); /* One byte extra for status */
if (!buffer) {
fr_perror("Failed allocating memory for libradius error buffer");
return;
}
ret = fr_thread_local_set(fr_strerror_buffer, buffer);
if (ret != 0) {
fr_perror("Failed setting up TLS for libradius error buffer: %s", fr_syserror(ret));
free(buffer);
return;
}
}
va_start(ap, fmt);
vsnprintf(buffer, FR_STRERROR_BUFSIZE, fmt, ap);
buffer[FR_STRERROR_BUFSIZE] = '\1'; /* Flip the 'new' byte to true */
va_end(ap);
}
/** Get the last library error
*
* Will only return the last library error once, after which it will return a zero length string.
*
* @return library error or zero length string
*/
char const *fr_strerror(void)
{
char *buffer;
buffer = fr_thread_local_get(fr_strerror_buffer);
if (buffer && buffer[FR_STRERROR_BUFSIZE]) {
buffer[FR_STRERROR_BUFSIZE] = '\0'; /* Flip the 'new' byte to false */
return buffer;
}
return "";
}
/** Guaranteed to be thread-safe version of strerror
*
* @param num errno as returned by function or from global errno.
* @return local specific error string relating to errno.
*/
char const *fr_syserror(int num)
{
char *buffer;
buffer = fr_thread_local_init(fr_syserror_buffer, fr_logging_free);
if (!buffer) {
int ret;
/*
* malloc is thread safe, talloc is not
*/
buffer = malloc(sizeof(char) * FR_STRERROR_BUFSIZE);
if (!buffer) {
fr_perror("Failed allocating memory for system error buffer");
return NULL;
}
ret = fr_thread_local_set(fr_syserror_buffer, buffer);
if (ret != 0) {
fr_perror("Failed setting up TLS for system error buffer: %s", fr_syserror(ret));
free(buffer);
return NULL;
}
}
/*
* XSI-Compliant version
*/
#if !defined(HAVE_FEATURES_H) || ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 500) && ! _GNU_SOURCE)
if (!num || (strerror_r(num, buffer, FR_STRERROR_BUFSIZE) != 0)) {
buffer[0] = '\0';
}
return buffer;
/*
* GNU Specific version
*
* The GNU Specific version returns a char pointer. That pointer may point
* the buffer you just passed in, or to an immutable static string.
*/
#else
{
char const *p;
p = strerror_r(num, buffer, FR_STRERROR_BUFSIZE);
if (!num || !p) {
buffer[0] = '\0';
return buffer;
}
return p;
}
#endif
}
void fr_perror(char const *fmt, ...)
{
char const *error;
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
if (strchr(fmt, ':') == NULL)
fprintf(stderr, ": ");
error = fr_strerror();
if (error) {
fprintf(stderr, "%s\n", error);
}
va_end(ap);
}
bool fr_assert_cond(char const *file, int line, char const *expr, bool cond)
{
if (!cond) {
fr_perror("SOFT ASSERT FAILED %s[%u]: %s\n", file, line, expr);
return false;
}
return cond;
}