update from main archive 961127
[glibc.git] / misc / syslog.c
blob9db16ca352f7335918f9c983c97502366c14debb
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>
51 #include <stdlib.h>
53 #if __STDC__
54 #include <stdarg.h>
55 #else
56 #include <varargs.h>
57 #endif
59 static int LogType = SOCK_DGRAM; /* type of socket connection */
60 static int LogFile = -1; /* fd for log */
61 static int connected; /* have done connect */
62 static int LogStat = 0; /* status bits, set by openlog() */
63 static const char *LogTag = NULL; /* string to tag the entry with */
64 static int LogFacility = LOG_USER; /* default facility code */
65 static int LogMask = 0xff; /* mask of priorities to be logged */
66 extern char *__progname; /* Program name, from crt0. */
69 * syslog, vsyslog --
70 * print message on log file; output is intended for syslogd(8).
72 void
73 #if __STDC__
74 syslog(int pri, const char *fmt, ...)
75 #else
76 syslog(pri, fmt, va_alist)
77 int pri;
78 char *fmt;
79 va_dcl
80 #endif
82 va_list ap;
84 #if __STDC__
85 va_start(ap, fmt);
86 #else
87 va_start(ap);
88 #endif
89 vsyslog(pri, fmt, ap);
90 va_end(ap);
93 void
94 vsyslog(pri, fmt, ap)
95 int pri;
96 register const char *fmt;
97 va_list ap;
99 struct tm now_tm;
100 time_t now;
101 int fd;
102 FILE *f;
103 char *buf = 0;
104 size_t bufsize = 0;
105 size_t prioff, msgoff;
107 #define INTERNALLOG LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID
108 /* Check for invalid bits. */
109 if (pri & ~(LOG_PRIMASK|LOG_FACMASK)) {
110 syslog(INTERNALLOG,
111 "syslog: unknown facility/priority: %x", pri);
112 pri &= LOG_PRIMASK|LOG_FACMASK;
115 /* Check priority against setlogmask values. */
116 if (!LOG_MASK(LOG_PRI(pri)) & LogMask)
117 return;
119 /* Set default facility if none specified. */
120 if ((pri & LOG_FACMASK) == 0)
121 pri |= LogFacility;
123 /* Build the message in a memory-buffer stream. */
124 f = open_memstream (&buf, &bufsize);
125 prioff = fprintf (f, "<%d>", pri);
126 (void) time (&now);
127 #ifdef USE_IN_LIBIO
128 f->_IO_write_ptr += strftime (f->_IO_write_ptr,
129 f->_IO_write_end - f->_IO_write_ptr,
130 "%h %e %T ",
131 __localtime_r (&now, &now_tm));
132 #else
133 f->__bufp += strftime (f->__bufp, f->__put_limit - f->__bufp,
134 "%h %e %T ", __localtime_r (&now, &now_tm));
135 #endif
136 msgoff = ftell (f);
137 if (LogTag == NULL)
138 LogTag = __progname;
139 if (LogTag != NULL)
140 fputs (LogTag, f);
141 if (LogStat & LOG_PID)
142 fprintf (f, "[%d]", getpid ());
143 if (LogTag != NULL)
144 putc (':', f), putc (' ', f);
146 /* We have the header. Print the user's format into the buffer. */
147 vfprintf (f, fmt, ap);
149 /* Close the memory stream; this will finalize the data
150 into a malloc'd buffer in BUF. */
151 fclose (f);
153 /* Output to stderr if requested. */
154 if (LogStat & LOG_PERROR) {
155 struct iovec iov[2];
156 register struct iovec *v = iov;
158 v->iov_base = buf + msgoff;
159 v->iov_len = bufsize - msgoff;
160 ++v;
161 v->iov_base = (char *) "\n";
162 v->iov_len = 1;
163 (void)writev(STDERR_FILENO, iov, 2);
166 /* Get connected, output the message to the local logger. */
167 if (!connected)
168 openlog(LogTag, LogStat | LOG_NDELAY, 0);
170 /* If we have a SOCK_STREAM connection, also send ASCII NUL as
171 a record terminator. */
172 if (LogType == SOCK_STREAM)
173 ++bufsize;
175 if (__send(LogFile, buf, bufsize, 0) < 0)
177 closelog (); /* attempt re-open next time */
179 * Output the message to the console; don't worry about blocking,
180 * if console blocks everything will. Make sure the error reported
181 * is the one from the syslogd failure.
183 if (LogStat & LOG_CONS &&
184 (fd = open(_PATH_CONSOLE, O_WRONLY, 0)) >= 0)
186 dprintf (fd, "%s\r\n", buf + msgoff);
187 (void)close(fd);
191 free (buf);
194 static struct sockaddr SyslogAddr; /* AF_UNIX address of local logger */
196 void
197 openlog(ident, logstat, logfac)
198 const char *ident;
199 int logstat, logfac;
201 if (ident != NULL)
202 LogTag = ident;
203 LogStat = logstat;
204 if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0)
205 LogFacility = logfac;
207 while (1) {
208 if (LogFile == -1) {
209 SyslogAddr.sa_family = AF_UNIX;
210 (void)strncpy(SyslogAddr.sa_data, _PATH_LOG,
211 sizeof(SyslogAddr.sa_data));
212 if (LogStat & LOG_NDELAY) {
213 if ((LogFile = socket(AF_UNIX, LogType, 0))
214 == -1)
215 return;
216 (void)fcntl(LogFile, F_SETFD, 1);
219 if (LogFile != -1 && !connected)
220 if (__connect(LogFile, &SyslogAddr, sizeof(SyslogAddr))
221 == -1)
223 int saved_errno = errno;
224 (void)close(LogFile);
225 LogFile = -1;
226 if (LogType == SOCK_DGRAM
227 && saved_errno == EPROTOTYPE)
229 /* retry with next SOCK_STREAM: */
230 LogType = SOCK_STREAM;
231 continue;
233 } else
234 connected = 1;
235 break;
239 void
240 closelog()
242 (void)close(LogFile);
243 LogFile = -1;
244 connected = 0;
247 /* setlogmask -- set the log mask level */
249 setlogmask(pmask)
250 int pmask;
252 int omask;
254 omask = LogMask;
255 if (pmask != 0)
256 LogMask = pmask;
257 return (omask);