2
* Copyright (c) 1983, 1988 Regents of the University of California.
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.
18
/* based on @(#)syslog.c 5.20 (Berkeley) 1/19/89 */
21
* SYSLOG -- print message on log file
23
* This routine looks a lot like printf, except that it outputs to the
24
* log file instead of the standard output. Also:
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.
30
* The output of this routine is intended to be read by syslogd(8).
33
* Modified to use UNIX domain IPC by Ralph Campbell
38
#if defined(__STDC__) || defined(_WIN32)
45
#include <sys/types.h>
46
#include <sys/socket.h>
49
#include <sys/signal.h>
56
#define LOGNAME "/dev/log"
57
#define CONSOLE "/dev/console"
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 */
67
#if defined(__STDC__) || defined(_WIN32)
68
syslog(int pri, const char *fmt, ...)
70
syslog(pri, fmt, va_alist)
78
#if defined(__STDC__) || defined(_WIN32)
83
vsyslog(pri, fmt, pvar);
90
const register char *fmt;
97
char tbuf[2048], fmt_cpy[1024], *ctime();
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)))
106
if (LogFile < 0 || !connected)
107
openlog(LogTag, LogStat | LOG_NDELAY, 0);
109
/* set default facility if none specified */
110
if ((pri & LOG_FACMASK) == 0)
113
/* build the message */
115
(void)sprintf(tbuf, "<%d>%.15s ", pri, ctime(&now) + 4);
116
for (p = tbuf; *p; ++p);
118
(void)strncpy(p, LogTag, sizeof(tbuf) - 1 - (p - tbuf));
121
if (LogStat & LOG_PID) {
122
(void)sprintf(p, "[%d]", getpid());
130
/* substitute error message for %m */
132
register char ch, *t1, *t2;
134
extern char *strerror();
137
for (t1 = fmt_cpy; ch = *fmt; ++fmt)
138
if (ch == '%' && fmt[1] == 'm') {
140
for (t2 = strerror(saved_errno);
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))
155
/* output the message to the local logger */
156
if (send(LogFile, tbuf, cnt = strlen(tbuf), 0) >= 0 ||
160
/* output the message to the console */
161
#if defined(SYSV) || defined(_AIX)
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)
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));
184
#if defined(SYSV) || defined(_AIX) || defined(_POSIX_SOURCE)
187
#define cast union wait *
189
if (!(LogStat & LOG_NOWAIT))
190
while ((cnt = wait((cast)0)) > 0 && cnt != pid);
194
static struct sockaddr SyslogAddr; /* AF_UNIX address of local logger */
196
* OPENLOG -- open system log
199
openlog(ident, logstat, logfac)
206
if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0)
207
LogFacility = logfac;
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);
216
if (LogFile != -1 && !connected &&
217
connect(LogFile, &SyslogAddr, sizeof(SyslogAddr)) != -1)
222
* CLOSELOG -- close the system log
227
(void) close(LogFile);
232
static int LogMask = 0xff; /* mask of priorities to be logged */
234
* SETLOGMASK -- set the log mask level
247
#else /* Windows or Mac */
249
2
/* Windows doesn't have the concept of a system log, so just
250
3
** do nothing here.