xref: /netbsd-src/lib/libc/gen/syslog.c (revision ce63d6c20fc4ec8ddc95c84bb229e3c4ecf82b69)
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, 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.
20  *
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.
32  */
33 
34 #if defined(LIBC_SCCS) && !defined(lint)
35 static char sccsid[] = "@(#)syslog.c	5.36 (Berkeley) 10/4/92";
36 #endif /* LIBC_SCCS and not lint */
37 
38 #include <sys/types.h>
39 #include <sys/socket.h>
40 #include <sys/syslog.h>
41 #include <sys/uio.h>
42 #include <netdb.h>
43 
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 
52 #if __STDC__
53 #include <stdarg.h>
54 #else
55 #include <varargs.h>
56 #endif
57 
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 = "syslog";	/* 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 
65 /*
66  * syslog, vsyslog --
67  *	print message on log file; output is intended for syslogd(8).
68  */
69 void
70 #if __STDC__
71 syslog(int pri, const char *fmt, ...)
72 #else
73 syslog(pri, fmt, va_alist)
74 	int pri;
75 	char *fmt;
76 	va_dcl
77 #endif
78 {
79 	va_list ap;
80 
81 #if __STDC__
82 	va_start(ap, fmt);
83 #else
84 	va_start(ap);
85 #endif
86 	vsyslog(pri, fmt, ap);
87 	va_end(ap);
88 }
89 
90 void
91 vsyslog(pri, fmt, ap)
92 	int pri;
93 	register const char *fmt;
94 	va_list ap;
95 {
96 	register int cnt;
97 	register char *p;
98 	time_t now;
99 	int fd, saved_errno;
100 	char *stdp, tbuf[2048], fmt_cpy[1024];
101 
102 #define	INTERNALLOG	LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID
103 	/* Check for invalid bits. */
104 	if (pri & ~(LOG_PRIMASK|LOG_FACMASK)) {
105 		syslog(INTERNALLOG,
106 		    "syslog: unknown facility/priority: %x", pri);
107 		pri &= LOG_PRIMASK|LOG_FACMASK;
108 	}
109 
110 	/* Check priority against setlogmask values. */
111 	if (!LOG_MASK(LOG_PRI(pri)) & LogMask)
112 		return;
113 
114 	saved_errno = errno;
115 
116 	/* set default facility if none specified */
117 	if ((pri & LOG_FACMASK) == 0)
118 		pri |= LogFacility;
119 
120 	/* build the message */
121 	(void)time(&now);
122 	(void)sprintf(tbuf, "<%d>%.15s ", pri, ctime(&now) + 4);
123 	for (p = tbuf; *p; ++p);
124 	if (LogStat & LOG_PERROR)
125 		stdp = p;
126 	if (LogTag) {
127 		(void)strcpy(p, LogTag);
128 		for (; *p; ++p);
129 	}
130 	if (LogStat & LOG_PID) {
131 		(void)sprintf(p, "[%d]", getpid());
132 		for (; *p; ++p);
133 	}
134 	if (LogTag) {
135 		*p++ = ':';
136 		*p++ = ' ';
137 	}
138 
139 	/* substitute error message for %m */
140 	{
141 		register char ch, *t1, *t2;
142 		char *strerror();
143 
144 		for (t1 = fmt_cpy; ch = *fmt; ++fmt)
145 			if (ch == '%' && fmt[1] == 'm') {
146 				++fmt;
147 				for (t2 = strerror(saved_errno);
148 				    *t1 = *t2++; ++t1);
149 			}
150 			else
151 				*t1++ = ch;
152 		*t1 = '\0';
153 	}
154 
155 	(void)vsprintf(p, fmt_cpy, ap);
156 
157 	cnt = strlen(tbuf);
158 
159 	/* output to stderr if requested */
160 	if (LogStat & LOG_PERROR) {
161 		struct iovec iov[2];
162 		register struct iovec *v = iov;
163 
164 		v->iov_base = stdp;
165 		v->iov_len = cnt - (stdp - tbuf);
166 		++v;
167 		v->iov_base = "\n";
168 		v->iov_len = 1;
169 		(void)writev(STDERR_FILENO, iov, 2);
170 	}
171 
172 	/* get connected, output the message to the local logger */
173 	if (!connected)
174 		openlog(LogTag, LogStat | LOG_NDELAY, 0);
175 	if (send(LogFile, tbuf, cnt, 0) >= 0)
176 		return;
177 
178 	/* see if should attempt the console */
179 	if (!(LogStat&LOG_CONS))
180 		return;
181 
182 	/*
183 	 * Output the message to the console; don't worry about blocking,
184 	 * if console blocks everything will.  Make sure the error reported
185 	 * is the one from the syslogd failure.
186 	 */
187 	if ((fd = open(_PATH_CONSOLE, O_WRONLY, 0)) >= 0) {
188 		(void)strcat(tbuf, "\r\n");
189 		cnt += 2;
190 		p = index(tbuf, '>') + 1;
191 		(void)write(fd, p, cnt - (p - tbuf));
192 		(void)close(fd);
193 	}
194 }
195 
196 static struct sockaddr SyslogAddr;	/* AF_UNIX address of local logger */
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 
209 	if (LogFile == -1) {
210 		SyslogAddr.sa_family = AF_UNIX;
211 		(void)strncpy(SyslogAddr.sa_data, _PATH_LOG,
212 		    sizeof(SyslogAddr.sa_data));
213 		if (LogStat & LOG_NDELAY) {
214 			if ((LogFile = socket(AF_UNIX, SOCK_DGRAM, 0)) == -1)
215 				return;
216 			(void)fcntl(LogFile, F_SETFD, 1);
217 		}
218 	}
219 	if (LogFile != -1 && !connected)
220 		if (connect(LogFile, &SyslogAddr, sizeof(SyslogAddr)) == -1) {
221 			(void)close(LogFile);
222 			LogFile = -1;
223 		} else
224 			connected = 1;
225 }
226 
227 void
228 closelog()
229 {
230 	(void)close(LogFile);
231 	LogFile = -1;
232 	connected = 0;
233 }
234 
235 /* setlogmask -- set the log mask level */
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