Protect pcap_fopen calls
[freeradius.git] / src / lib / log.c
1 /*
2  * log.c        Functions in the library call radlib_log() which
3  *              does internal logging.
4  *
5  * Version:     $Id$
6  *
7  *   This library is free software; you can redistribute it and/or
8  *   modify it under the terms of the GNU Lesser General Public
9  *   License as published by the Free Software Foundation; either
10  *   version 2.1 of the License, or (at your option) any later version.
11  *
12  *   This library is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  *   Lesser General Public License for more details.
16  *
17  *   You should have received a copy of the GNU Lesser General Public
18  *   License along with this library; if not, write to the Free Software
19  *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  *
21  * Copyright 2000,2006  The FreeRADIUS server project
22  */
23
24 #include <freeradius-devel/ident.h>
25 RCSID("$Id$")
26
27 #include <freeradius-devel/libradius.h>
28
29
30 #define FR_STRERROR_BUFSIZE (1024)
31
32 #ifdef HAVE_THREAD_TLS
33 /*
34  *      GCC on most Linux systems
35  */
36 #define THREAD_TLS __thread
37
38 #elif defined(HAVE_DECLSPEC_THREAD)
39 /*
40  *      Visual C++, Borland
41  */
42 #define THREAD_TLS __declspec(thread)
43 #else
44
45 /*
46  *      We don't have thread-local storage.  Ensure we don't
47  *      ask for it.
48  */
49 #define THREAD_TLS
50
51 /*
52  *      Use pthread keys if we have pthreads.  For MAC, which should
53  *      be very fast.
54  */
55 #ifdef HAVE_PTHREAD_H
56 #define USE_PTHREAD_FOR_TLS (1)
57 #endif
58 #endif
59
60 #ifndef USE_PTHREAD_FOR_TLS
61 /*
62  *      Try to create a thread-local-storage version of this buffer.
63  */
64 static THREAD_TLS char fr_strerror_buffer[FR_STRERROR_BUFSIZE];
65
66 #else
67 #include <pthread.h>
68
69 static pthread_key_t  fr_strerror_key;
70 static pthread_once_t fr_strerror_once = PTHREAD_ONCE_INIT;
71
72 /* Create Key */
73 static void fr_strerror_make_key(void)
74 {
75         pthread_key_create(&fr_strerror_key, NULL);
76 }
77 #endif
78
79 /*
80  *      Log to a buffer, trying to be thread-safe.
81  */
82 void fr_strerror_printf(const char *fmt, ...)
83 {
84         va_list ap;
85
86 #ifdef USE_PTHREAD_FOR_TLS
87         char *buffer;
88
89         pthread_once(&fr_strerror_once, fr_strerror_make_key);
90         
91         buffer = pthread_getspecific(fr_strerror_key);
92         if (!buffer) {
93                 buffer = malloc(FR_STRERROR_BUFSIZE);
94                 if (!buffer) return; /* panic and die! */
95
96                 pthread_setspecific(fr_strerror_key, buffer);
97         }
98
99         va_start(ap, fmt);
100         vsnprintf(buffer, FR_STRERROR_BUFSIZE, fmt, ap);
101
102 #else
103         va_start(ap, fmt);
104         vsnprintf(fr_strerror_buffer, sizeof(fr_strerror_buffer), fmt, ap);
105 #endif
106
107         va_end(ap);
108 }
109
110 const char *fr_strerror(void)
111 {
112 #ifndef USE_PTHREAD_FOR_TLS
113         return fr_strerror_buffer;
114
115 #else
116         const char *msg;
117
118         pthread_once(&fr_strerror_once, fr_strerror_make_key);
119
120         msg = pthread_getspecific(fr_strerror_key);
121         if (msg) return msg;
122
123         return "(unknown error)"; /* DON'T return NULL! */
124 #endif
125 }
126
127 void fr_perror(const char *fmt, ...)
128 {
129         va_list ap;
130
131         va_start(ap, fmt);
132         vfprintf(stderr, fmt, ap);
133         if (strchr(fmt, ':') == NULL)
134                 fprintf(stderr, ": ");
135         fprintf(stderr, "%s\n", fr_strerror());
136         va_end(ap);
137 }