.
[glibc/history.git] / misc / syslog.c
blobba994eeeb8dd8914b2ffa9c704503c24d974fe86
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 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 = NULL; /* string to tag the entry with */
63 static int LogFacility = LOG_USER; /* default facility code */
64 static int LogMask = 0xff; /* mask of priorities to be logged */
65 extern char *__progname; /* Program name, from crt0. */
68 * syslog, vsyslog --
69 * print message on log file; output is intended for syslogd(8).
71 void
72 #if __STDC__
73 syslog(int pri, const char *fmt, ...)
74 #else
75 syslog(pri, fmt, va_alist)
76 int pri;
77 char *fmt;
78 va_dcl
79 #endif
81 va_list ap;
83 #if __STDC__
84 va_start(ap, fmt);
85 #else
86 va_start(ap);
87 #endif
88 vsyslog(pri, fmt, ap);
89 va_end(ap);
92 void
93 vsyslog(pri, fmt, ap)
94 int pri;
95 register const char *fmt;
96 va_list ap;
98 time_t now;
99 int fd;
100 FILE *f;
101 char *buf = 0;
102 size_t bufsize = 0;
103 size_t prioff, msgoff;
105 #define INTERNALLOG LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID
106 /* Check for invalid bits. */
107 if (pri & ~(LOG_PRIMASK|LOG_FACMASK)) {
108 syslog(INTERNALLOG,
109 "syslog: unknown facility/priority: %x", pri);
110 pri &= LOG_PRIMASK|LOG_FACMASK;
113 /* Check priority against setlogmask values. */
114 if (!LOG_MASK(LOG_PRI(pri)) & LogMask)
115 return;
117 /* Set default facility if none specified. */
118 if ((pri & LOG_FACMASK) == 0)
119 pri |= LogFacility;
121 /* Build the message in a memory-buffer stream. */
122 f = open_memstream (&buf, &bufsize);
123 prioff = fprintf (f, "<%d>", pri);
124 (void) time (&now);
125 f->__bufp += strftime (f->__bufp, f->__put_limit - f->__bufp,
126 "%h %e %T ", localtime (&now));
127 msgoff = ftell (f);
128 if (LogTag == NULL)
129 LogTag = __progname;
130 if (LogTag != NULL)
131 fputs (LogTag, f);
132 if (LogStat & LOG_PID)
133 fprintf (f, "[%d]", getpid ());
134 if (LogTag != NULL)
135 putc (':', f), putc (' ', f);
137 /* We have the header. Print the user's format into the buffer. */
138 vfprintf (f, fmt, ap);
140 /* Close the memory stream; this will finalize the data
141 into a malloc'd buffer in BUF. */
142 fclose (f);
144 /* Output to stderr if requested. */
145 if (LogStat & LOG_PERROR) {
146 struct iovec iov[2];
147 register struct iovec *v = iov;
149 v->iov_base = buf + msgoff;
150 v->iov_len = bufsize - msgoff;
151 ++v;
152 v->iov_base = (char *) "\n";
153 v->iov_len = 1;
154 (void)writev(STDERR_FILENO, iov, 2);
157 /* Get connected, output the message to the local logger. */
158 if (!connected)
159 openlog(LogTag, LogStat | LOG_NDELAY, 0);
160 if (send(LogFile, buf, bufsize, 0) < 0)
163 * Output the message to the console; don't worry about blocking,
164 * if console blocks everything will. Make sure the error reported
165 * is the one from the syslogd failure.
167 if (LogStat & LOG_CONS &&
168 (fd = open(_PATH_CONSOLE, O_WRONLY, 0)) >= 0)
170 dprintf (fd, "%s\r\n", buf + msgoff);
171 (void)close(fd);
175 free (buf);
178 static struct sockaddr SyslogAddr; /* AF_UNIX address of local logger */
180 void
181 openlog(ident, logstat, logfac)
182 const char *ident;
183 int logstat, logfac;
185 if (ident != NULL)
186 LogTag = ident;
187 LogStat = logstat;
188 if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0)
189 LogFacility = logfac;
191 if (LogFile == -1) {
192 SyslogAddr.sa_family = AF_UNIX;
193 (void)strncpy(SyslogAddr.sa_data, _PATH_LOG,
194 sizeof(SyslogAddr.sa_data));
195 if (LogStat & LOG_NDELAY) {
196 if ((LogFile = socket(AF_UNIX, SOCK_DGRAM, 0)) == -1)
197 return;
198 (void)fcntl(LogFile, F_SETFD, 1);
201 if (LogFile != -1 && !connected)
202 if (connect(LogFile, &SyslogAddr, sizeof(SyslogAddr)) == -1) {
203 (void)close(LogFile);
204 LogFile = -1;
205 } else
206 connected = 1;
209 void
210 closelog()
212 (void)close(LogFile);
213 LogFile = -1;
214 connected = 0;
217 /* setlogmask -- set the log mask level */
219 setlogmask(pmask)
220 int pmask;
222 int omask;
224 omask = LogMask;
225 if (pmask != 0)
226 LogMask = pmask;
227 return (omask);