initial import
[glibc.git] / misc / syslog.c
blobba82e2301959ff79142082729b3cf7a19ff7e532
1 /*
2 * Copyright (c) 1983, 1988, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
34 #if defined(LIBC_SCCS) && !defined(lint)
35 static char sccsid[] = "@(#)syslog.c 8.4 (Berkeley) 3/18/94";
36 #endif /* LIBC_SCCS and not lint */
38 #include <sys/types.h>
39 #include <sys/socket.h>
40 #include <sys/syslog.h>
41 #include <sys/uio.h>
42 #include <netdb.h>
44 #include <errno.h>
45 #include <fcntl.h>
46 #include <paths.h>
47 #include <stdio.h>
48 #include <string.h>
49 #include <time.h>
50 #include <unistd.h>
52 #if __STDC__
53 #include <stdarg.h>
54 #else
55 #include <varargs.h>
56 #endif
58 static int LogFile = -1; /* fd for log */
59 static int connected; /* have done connect */
60 static int LogStat = 0; /* status bits, set by openlog() */
61 static const char *LogTag = NULL; /* string to tag the entry with */
62 static int LogFacility = LOG_USER; /* default facility code */
63 static int LogMask = 0xff; /* mask of priorities to be logged */
64 extern char *__progname; /* Program name, from crt0. */
67 * syslog, vsyslog --
68 * print message on log file; output is intended for syslogd(8).
70 void
71 #if __STDC__
72 syslog(int pri, const char *fmt, ...)
73 #else
74 syslog(pri, fmt, va_alist)
75 int pri;
76 char *fmt;
77 va_dcl
78 #endif
80 va_list ap;
82 #if __STDC__
83 va_start(ap, fmt);
84 #else
85 va_start(ap);
86 #endif
87 vsyslog(pri, fmt, ap);
88 va_end(ap);
91 void
92 vsyslog(pri, fmt, ap)
93 int pri;
94 register const char *fmt;
95 va_list ap;
97 register int cnt;
98 register char ch, *p, *t;
99 time_t now;
100 int fd, saved_errno;
101 char *stdp, tbuf[2048], fmt_cpy[1024];
103 #define INTERNALLOG LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID
104 /* Check for invalid bits. */
105 if (pri & ~(LOG_PRIMASK|LOG_FACMASK)) {
106 syslog(INTERNALLOG,
107 "syslog: unknown facility/priority: %x", pri);
108 pri &= LOG_PRIMASK|LOG_FACMASK;
111 /* Check priority against setlogmask values. */
112 if (!LOG_MASK(LOG_PRI(pri)) & LogMask)
113 return;
115 saved_errno = errno;
117 /* Set default facility if none specified. */
118 if ((pri & LOG_FACMASK) == 0)
119 pri |= LogFacility;
121 /* Build the message. */
122 (void)time(&now);
123 p = tbuf + sprintf(tbuf, "<%d>", pri);
124 p += strftime(p, sizeof (tbuf) - (p - tbuf), "%h %e %T ",
125 localtime(&now));
126 if (LogStat & LOG_PERROR)
127 stdp = p;
128 if (LogTag == NULL)
129 LogTag = __progname;
130 if (LogTag != NULL)
131 p += sprintf(p, "%s", LogTag);
132 if (LogStat & LOG_PID)
133 p += sprintf(p, "[%d]", getpid());
134 if (LogTag != NULL) {
135 *p++ = ':';
136 *p++ = ' ';
139 /* Substitute error message for %m. */
140 for (t = fmt_cpy; ch = *fmt; ++fmt)
141 if (ch == '%' && fmt[1] == 'm') {
142 ++fmt;
143 t += sprintf(t, "%s", strerror(saved_errno));
144 } else
145 *t++ = ch;
146 *t = '\0';
148 p += vsprintf(p, fmt_cpy, ap);
149 cnt = p - tbuf;
151 /* Output to stderr if requested. */
152 if (LogStat & LOG_PERROR) {
153 struct iovec iov[2];
154 register struct iovec *v = iov;
156 v->iov_base = stdp;
157 v->iov_len = cnt - (stdp - tbuf);
158 ++v;
159 v->iov_base = "\n";
160 v->iov_len = 1;
161 (void)writev(STDERR_FILENO, iov, 2);
164 /* Get connected, output the message to the local logger. */
165 if (!connected)
166 openlog(LogTag, LogStat | LOG_NDELAY, 0);
167 if (send(LogFile, tbuf, cnt, 0) >= 0)
168 return;
171 * Output the message to the console; don't worry about blocking,
172 * if console blocks everything will. Make sure the error reported
173 * is the one from the syslogd failure.
175 if (LogStat & LOG_CONS &&
176 (fd = open(_PATH_CONSOLE, O_WRONLY, 0)) >= 0) {
177 (void)strcat(tbuf, "\r\n");
178 cnt += 2;
179 p = index(tbuf, '>') + 1;
180 (void)write(fd, p, cnt - (p - tbuf));
181 (void)close(fd);
185 static struct sockaddr SyslogAddr; /* AF_UNIX address of local logger */
187 void
188 openlog(ident, logstat, logfac)
189 const char *ident;
190 int logstat, logfac;
192 if (ident != NULL)
193 LogTag = ident;
194 LogStat = logstat;
195 if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0)
196 LogFacility = logfac;
198 if (LogFile == -1) {
199 SyslogAddr.sa_family = AF_UNIX;
200 (void)strncpy(SyslogAddr.sa_data, _PATH_LOG,
201 sizeof(SyslogAddr.sa_data));
202 if (LogStat & LOG_NDELAY) {
203 if ((LogFile = socket(AF_UNIX, SOCK_DGRAM, 0)) == -1)
204 return;
205 (void)fcntl(LogFile, F_SETFD, 1);
208 if (LogFile != -1 && !connected)
209 if (connect(LogFile, &SyslogAddr, sizeof(SyslogAddr)) == -1) {
210 (void)close(LogFile);
211 LogFile = -1;
212 } else
213 connected = 1;
216 void
217 closelog()
219 (void)close(LogFile);
220 LogFile = -1;
221 connected = 0;
224 /* setlogmask -- set the log mask level */
226 setlogmask(pmask)
227 int pmask;
229 int omask;
231 omask = LogMask;
232 if (pmask != 0)
233 LogMask = pmask;
234 return (omask);