syslog.c revision 1.3 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[] = "from: @(#)syslog.c 5.36 (Berkeley) 10/4/92";*/
36 static char rcsid[] = "$Id: syslog.c,v 1.3 1993/07/30 08:23:34 mycroft Exp $";
37 #endif /* LIBC_SCCS and not lint */
38
39 #include <sys/types.h>
40 #include <sys/socket.h>
41 #include <sys/syslog.h>
42 #include <sys/uio.h>
43 #include <netdb.h>
44
45 #include <errno.h>
46 #include <fcntl.h>
47 #include <paths.h>
48 #include <stdio.h>
49 #include <string.h>
50 #include <time.h>
51 #include <unistd.h>
52
53 #if __STDC__
54 #include <stdarg.h>
55 #else
56 #include <varargs.h>
57 #endif
58
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 */
64 static int LogMask = 0xff; /* mask of priorities to be logged */
65
66 /*
67 * syslog, vsyslog --
68 * print message on log file; output is intended for syslogd(8).
69 */
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
79 {
80 va_list ap;
81
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);
89 }
90
91 void
92 vsyslog(pri, fmt, ap)
93 int pri;
94 register const char *fmt;
95 va_list ap;
96 {
97 register int cnt;
98 register char *p;
99 time_t now;
100 int fd, saved_errno;
101 char *stdp, tbuf[2048], fmt_cpy[1024];
102
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;
109 }
110
111 /* Check priority against setlogmask values. */
112 if (!LOG_MASK(LOG_PRI(pri)) & LogMask)
113 return;
114
115 saved_errno = errno;
116
117 /* set default facility if none specified */
118 if ((pri & LOG_FACMASK) == 0)
119 pri |= LogFacility;
120
121 /* build the message */
122 (void)time(&now);
123 (void)sprintf(tbuf, "<%d>%.15s ", pri, ctime(&now) + 4);
124 for (p = tbuf; *p; ++p);
125 if (LogStat & LOG_PERROR)
126 stdp = p;
127 if (LogTag) {
128 (void)strcpy(p, LogTag);
129 for (; *p; ++p);
130 }
131 if (LogStat & LOG_PID) {
132 (void)sprintf(p, "[%d]", getpid());
133 for (; *p; ++p);
134 }
135 if (LogTag) {
136 *p++ = ':';
137 *p++ = ' ';
138 }
139
140 /* substitute error message for %m */
141 {
142 register char ch, *t1, *t2;
143 char *strerror();
144
145 for (t1 = fmt_cpy; ch = *fmt; ++fmt)
146 if (ch == '%' && fmt[1] == 'm') {
147 ++fmt;
148 for (t2 = strerror(saved_errno);
149 *t1 = *t2++; ++t1);
150 }
151 else
152 *t1++ = ch;
153 *t1 = '\0';
154 }
155
156 (void)vsprintf(p, fmt_cpy, ap);
157
158 cnt = strlen(tbuf);
159
160 /* output to stderr if requested */
161 if (LogStat & LOG_PERROR) {
162 struct iovec iov[2];
163 register struct iovec *v = iov;
164
165 v->iov_base = stdp;
166 v->iov_len = cnt - (stdp - tbuf);
167 ++v;
168 v->iov_base = "\n";
169 v->iov_len = 1;
170 (void)writev(STDERR_FILENO, iov, 2);
171 }
172
173 /* get connected, output the message to the local logger */
174 if (!connected)
175 openlog(LogTag, LogStat | LOG_NDELAY, 0);
176 if (send(LogFile, tbuf, cnt, 0) >= 0)
177 return;
178
179 /* see if should attempt the console */
180 if (!(LogStat&LOG_CONS))
181 return;
182
183 /*
184 * Output the message to the console; don't worry about blocking,
185 * if console blocks everything will. Make sure the error reported
186 * is the one from the syslogd failure.
187 */
188 if ((fd = open(_PATH_CONSOLE, O_WRONLY, 0)) >= 0) {
189 (void)strcat(tbuf, "\r\n");
190 cnt += 2;
191 p = index(tbuf, '>') + 1;
192 (void)write(fd, p, cnt - (p - tbuf));
193 (void)close(fd);
194 }
195 }
196
197 static struct sockaddr SyslogAddr; /* AF_UNIX address of local logger */
198
199 void
200 openlog(ident, logstat, logfac)
201 const char *ident;
202 int logstat, logfac;
203 {
204 if (ident != NULL)
205 LogTag = ident;
206 LogStat = logstat;
207 if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0)
208 LogFacility = logfac;
209
210 if (LogFile == -1) {
211 SyslogAddr.sa_family = AF_UNIX;
212 (void)strncpy(SyslogAddr.sa_data, _PATH_LOG,
213 sizeof(SyslogAddr.sa_data));
214 if (LogStat & LOG_NDELAY) {
215 if ((LogFile = socket(AF_UNIX, SOCK_DGRAM, 0)) == -1)
216 return;
217 (void)fcntl(LogFile, F_SETFD, 1);
218 }
219 }
220 if (LogFile != -1 && !connected)
221 if (connect(LogFile, &SyslogAddr, sizeof(SyslogAddr)) == -1) {
222 (void)close(LogFile);
223 LogFile = -1;
224 } else
225 connected = 1;
226 }
227
228 void
229 closelog()
230 {
231 (void)close(LogFile);
232 LogFile = -1;
233 connected = 0;
234 }
235
236 /* setlogmask -- set the log mask level */
237 int
238 setlogmask(pmask)
239 int pmask;
240 {
241 int omask;
242
243 omask = LogMask;
244 if (pmask != 0)
245 LogMask = pmask;
246 return (omask);
247 }
248