~ubuntu-branches/ubuntu/natty/freeradius/natty-updates

« back to all changes in this revision

Viewing changes to src/lib/log.c

  • Committer: Bazaar Package Importer
  • Author(s): Josip Rodin
  • Date: 2009-11-23 03:57:37 UTC
  • mfrom: (1.2.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 28.
  • Revision ID: james.westby@ubuntu.com-20091123035737-zsgtzhfych8hir68
Tags: 2.1.7+dfsg-1
* Adopting the package, closes: #536623.
* New upstream version, closes: #513484.
  + Fixes the blooper in unlang evaluation logic, closes: #526175.
* Used quilt (and added README.source), and moved upstream file patching
  into debian/patches/. The source is no longer in collab-maint git
  (to make it simpler for me to finally get this out the door), but
  kept the .gitignore should we need that again.
* Dropped the dialup_admin/bin/backup_radacct patch (integrated upstream).
* Dropped the raddb/Makefile patch (problem no longer exists upstream).
* Dropped the lib/packet.c lib/radius.c main/listen.c patches (was from
  upstream 2.0.5 anyway).
* Dropped references to otp.conf, it no longer exists upstream.
  Keep removing the conffile statoverride in prerm.
* Dropped references to snmp.conf, it no longer exists upstream.
  Keep removing the conffile statoverride in prerm.
* Ship /etc/freeradius/modules/* in the freeradius package.
* Stop shipping sites-enabled symlinks in the package and instead create
  them only on initial install, thanks to Matej Vela, closes: #533396.
* Add export PATH="${PATH:+$PATH:}/usr/sbin:/sbin" to the init script
  at the request of John Morrissey, closes: #550143.
* Stop installing /var/run/freeradius in the package to silence Lintian.
  The init script already recreates it at will.
* Remove executable bit from example.pl to silence Lintian.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
 * log.c        Functions in the library call radlib_log() which
3
 
 *              sets a global error string "char *librad_errstr".
 
3
 *              does internal logging.
4
4
 *
5
 
 * Version:     $Id: log.c,v 1.15 2007/12/25 08:19:38 aland Exp $
 
5
 * Version:     $Id$
6
6
 *
7
7
 *   This library is free software; you can redistribute it and/or
8
8
 *   modify it under the terms of the GNU Lesser General Public
22
22
 */
23
23
 
24
24
#include <freeradius-devel/ident.h>
25
 
RCSID("$Id: log.c,v 1.15 2007/12/25 08:19:38 aland Exp $")
 
25
RCSID("$Id$")
26
26
 
27
27
#include <freeradius-devel/libradius.h>
28
28
 
29
 
char librad_errstr[1024];
30
 
 
31
 
/*
32
 
 *  Do logging to a static buffer.  Note that we MIGHT be asked
33
 
 *  to write a previous log message to librad_errstr.
34
 
 *
35
 
 *  This also isn't multithreaded-safe, so it'll have to be changed
36
 
 *  in the future.
37
 
 */
38
 
void librad_log(const char *fmt, ...)
 
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, ...)
39
83
{
40
84
        va_list ap;
41
 
        char my_errstr[sizeof(librad_errstr)];
42
 
 
43
 
        va_start(ap, fmt);
44
 
        vsnprintf(my_errstr, sizeof(my_errstr), fmt, ap);
45
 
        strcpy(librad_errstr, my_errstr);
 
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
 
46
107
        va_end(ap);
47
108
}
48
109
 
49
 
void librad_perror(const char *fmt, ...)
 
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, ...)
50
128
{
51
129
        va_list ap;
52
130
 
54
132
        vfprintf(stderr, fmt, ap);
55
133
        if (strchr(fmt, ':') == NULL)
56
134
                fprintf(stderr, ": ");
57
 
        fprintf(stderr, "%s\n", librad_errstr);
 
135
        fprintf(stderr, "%s\n", fr_strerror());
58
136
        va_end(ap);
59
137
}