~ubuntu-branches/ubuntu/maverick/krb5/maverick

« back to all changes in this revision

Viewing changes to src/lib/krb5/posix/syslog.c

  • Committer: Bazaar Package Importer
  • Author(s): Sam Hartman, Russ Allbery, Sam Hartman
  • Date: 2008-08-21 10:41:41 UTC
  • mfrom: (11.1.15 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080821104141-a0f9c4o4cpo8xd0o
Tags: 1.6.dfsg.4~beta1-4
[ Russ Allbery ]
* Translation updates:
  - Swedish, thanks Martin Bagge.  (Closes: #487669, #491774)
  - Italian, thanks Luca Monducci.  (Closes: #493962)

[ Sam Hartman ]
* Translation Updates:
    - Dutch, Thanks Vincent Zweije, Closes: #495733

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright (c) 1983, 1988 Regents of the University of California.
3
 
 * All rights reserved.
4
 
 *
5
 
 * Redistribution and use in source and binary forms are permitted
6
 
 * provided that the above copyright notice and this paragraph are
7
 
 * duplicated in all such forms and that any documentation,
8
 
 * advertising materials, and other materials related to such
9
 
 * distribution and use acknowledge that the software was developed
10
 
 * by the University of California, Berkeley.  The name of the
11
 
 * University may not be used to endorse or promote products derived
12
 
 * from this software without specific prior written permission.
13
 
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14
 
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15
 
 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16
 
 */
17
 
 
18
 
/* based on @(#)syslog.c        5.20 (Berkeley) 1/19/89 */
19
 
 
20
 
/*
21
 
 * SYSLOG -- print message on log file
22
 
 *
23
 
 * This routine looks a lot like printf, except that it outputs to the
24
 
 * log file instead of the standard output.  Also:
25
 
 *      adds a timestamp,
26
 
 *      prints the module name in front of the message,
27
 
 *      has some other formatting types (or will sometime),
28
 
 *      adds a newline on the end of the message.
29
 
 *
30
 
 * The output of this routine is intended to be read by syslogd(8).
31
 
 *
32
 
 * Author: Eric Allman
33
 
 * Modified to use UNIX domain IPC by Ralph Campbell
34
 
 */
35
 
 
36
 
#if !defined(_WIN32)
37
 
 
38
 
#if defined(__STDC__) || defined(_WIN32)
39
 
#include <stdarg.h>
40
 
#else
41
 
#define const
42
 
#include <varargs.h>
43
 
#endif
44
 
#include <errno.h>
45
 
#include <sys/types.h>
46
 
#include <sys/socket.h>
47
 
#include <sys/file.h>
48
 
#include <fcntl.h>
49
 
#include <sys/signal.h>
50
 
#include <syslog.h>
51
 
#include <sys/wait.h>
52
 
#include <netdb.h>
53
 
#include <string.h>
54
 
#include <stdio.h>
55
 
 
56
 
#define LOGNAME "/dev/log"
57
 
#define CONSOLE "/dev/console"
58
 
 
59
 
static int      LogFile = -1;           /* fd for log */
60
 
static int      connected;              /* have done connect */
61
 
static int      LogStat = 0;            /* status bits, set by openlog() */
62
 
static const char *LogTag = "syslog";   /* string to tag the entry with */
63
 
static int      LogFacility = LOG_USER; /* default facility code */
64
 
 
65
 
 
66
 
void
67
 
#if defined(__STDC__) || defined(_WIN32)
68
 
syslog(int pri, const char *fmt, ...)
69
 
#else
70
 
syslog(pri, fmt, va_alist)
71
 
        int pri;
72
 
        char *fmt;
73
 
        va_dcl
74
 
#endif
75
 
{
76
 
    va_list pvar;
77
 
    void vsyslog();
78
 
#if defined(__STDC__) || defined(_WIN32)
79
 
    va_start(pvar, fmt);
80
 
#else
81
 
    va_start(pvar);
82
 
#endif
83
 
    vsyslog(pri, fmt, pvar);
84
 
    va_end(pvar);
85
 
}
86
 
 
87
 
void
88
 
vsyslog(pri, fmt, ap)
89
 
        int pri;
90
 
        const register char *fmt;
91
 
        va_list ap;
92
 
{
93
 
        register int cnt;
94
 
        register char *p;
95
 
        time_t now, time();
96
 
        int pid, saved_errno;
97
 
        char tbuf[2048], fmt_cpy[1024], *ctime();
98
 
        void openlog();
99
 
 
100
 
        saved_errno = errno;
101
 
 
102
 
        /* see if we should just throw out this message */
103
 
        if ((u_int)LOG_FAC(pri) >= LOG_NFACILITIES ||
104
 
            !LOG_MASK(LOG_PRI(pri)) || (pri &~ (LOG_PRIMASK|LOG_FACMASK)))
105
 
                return;
106
 
        if (LogFile < 0 || !connected)
107
 
                openlog(LogTag, LogStat | LOG_NDELAY, 0);
108
 
 
109
 
        /* set default facility if none specified */
110
 
        if ((pri & LOG_FACMASK) == 0)
111
 
                pri |= LogFacility;
112
 
 
113
 
        /* build the message */
114
 
        (void)time(&now);
115
 
        (void)sprintf(tbuf, "<%d>%.15s ", pri, ctime(&now) + 4);
116
 
        for (p = tbuf; *p; ++p);
117
 
        if (LogTag) {
118
 
                (void)strncpy(p, LogTag, sizeof(tbuf) - 1 - (p - tbuf));
119
 
                for (; *p; ++p);
120
 
        }
121
 
        if (LogStat & LOG_PID) {
122
 
                (void)sprintf(p, "[%d]", getpid());
123
 
                for (; *p; ++p);
124
 
        }
125
 
        if (LogTag) {
126
 
                *p++ = ':';
127
 
                *p++ = ' ';
128
 
        }
129
 
 
130
 
        /* substitute error message for %m */
131
 
        {
132
 
                register char ch, *t1, *t2;
133
 
#ifndef strerror
134
 
                extern char *strerror();
135
 
#endif
136
 
                
137
 
                for (t1 = fmt_cpy; ch = *fmt; ++fmt)
138
 
                        if (ch == '%' && fmt[1] == 'm') {
139
 
                                ++fmt;
140
 
                                for (t2 = strerror(saved_errno);
141
 
                                    *t1 = *t2++; ++t1);
142
 
                        }
143
 
                        else
144
 
                                *t1++ = ch;
145
 
                *t1 = '\0';
146
 
        }
147
 
 
148
 
        (void)vsprintf(p, fmt_cpy, ap);
149
 
        /* Bounds checking??  If a system doesn't have syslog, we
150
 
           probably can't rely on it having vsnprintf either.  Try not
151
 
           to let a buffer overrun be exploited.  */
152
 
        if (strlen (tbuf) >= sizeof (tbuf))
153
 
          abort ();
154
 
 
155
 
        /* output the message to the local logger */
156
 
        if (send(LogFile, tbuf, cnt = strlen(tbuf), 0) >= 0 ||
157
 
            !(LogStat&LOG_CONS))
158
 
                return;
159
 
 
160
 
        /* output the message to the console */
161
 
#if defined(SYSV) || defined(_AIX)
162
 
        pid = fork();
163
 
#else
164
 
        pid = vfork();
165
 
#endif
166
 
        if (pid == -1)
167
 
                return;
168
 
        if (pid == 0) {
169
 
                int fd;
170
 
 
171
 
                (void)signal(SIGALRM, SIG_DFL);
172
 
                sigsetmask((long)~sigmask(SIGALRM));
173
 
                (void)alarm((u_int)5);
174
 
                if ((fd = open(CONSOLE, O_WRONLY, 0)) < 0)
175
 
                        return;
176
 
                (void)alarm((u_int)0);
177
 
                tbuf[sizeof(tbuf) - 1] = '\0';
178
 
                (void)strncat(tbuf, "\r", sizeof(tbuf) - 1 - strlen(tbuf));
179
 
                p = strchr(tbuf, '>') + 1;
180
 
                (void)write(fd, p, cnt + 1 - (p - tbuf));
181
 
                (void)close(fd);
182
 
                _exit(0);
183
 
        }
184
 
#if defined(SYSV) || defined(_AIX) || defined(_POSIX_SOURCE)
185
 
#define cast int *
186
 
#else
187
 
#define cast union wait *
188
 
#endif
189
 
        if (!(LogStat & LOG_NOWAIT))
190
 
                while ((cnt = wait((cast)0)) > 0 && cnt != pid);
191
 
#undef cast
192
 
}
193
 
 
194
 
static struct sockaddr SyslogAddr;      /* AF_UNIX address of local logger */
195
 
/*
196
 
 * OPENLOG -- open system log
197
 
 */
198
 
void
199
 
openlog(ident, logstat, logfac)
200
 
        const char *ident;
201
 
        int logstat, logfac;
202
 
{
203
 
        if (ident != NULL)
204
 
                LogTag = ident;
205
 
        LogStat = logstat;
206
 
        if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0)
207
 
                LogFacility = logfac;
208
 
        if (LogFile == -1) {
209
 
                SyslogAddr.sa_family = AF_UNIX;
210
 
                strncpy(SyslogAddr.sa_data, LOGNAME, sizeof SyslogAddr.sa_data);
211
 
                if (LogStat & LOG_NDELAY) {
212
 
                        LogFile = socket(AF_UNIX, SOCK_DGRAM, 0);
213
 
                        fcntl(LogFile, F_SETFD, 1);
214
 
                }
215
 
        }
216
 
        if (LogFile != -1 && !connected &&
217
 
            connect(LogFile, &SyslogAddr, sizeof(SyslogAddr)) != -1)
218
 
                connected = 1;
219
 
}
220
 
 
221
 
/*
222
 
 * CLOSELOG -- close the system log
223
 
 */
224
 
void
225
 
closelog()
226
 
{
227
 
        (void) close(LogFile);
228
 
        LogFile = -1;
229
 
        connected = 0;
230
 
}
231
 
 
232
 
static int      LogMask = 0xff;         /* mask of priorities to be logged */
233
 
/*
234
 
 * SETLOGMASK -- set the log mask level
235
 
 */
236
 
int
237
 
setlogmask(pmask)
238
 
        int pmask;
239
 
{
240
 
        int omask;
241
 
 
242
 
        omask = LogMask;
243
 
        if (pmask != 0)
244
 
                LogMask = pmask;
245
 
        return (omask);
246
 
}
247
 
#else /* Windows or Mac */
248
 
 
 
1
#if defined(_WIN32)
249
2
/* Windows doesn't have the concept of a system log, so just
250
3
** do nothing here.
251
4
*/