]> git.saurik.com Git - apple/libc.git/blame_incremental - gen/syslog.c
Libc-320.1.3.tar.gz
[apple/libc.git] / gen / syslog.c
... / ...
CommitLineData
1/*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23/*
24 * Copyright (c) 1993
25 * The Regents of the University of California. All rights reserved.
26 *
27 * Redistribution and use in source and binary forms, with or without
28 * modification, are permitted provided that the following conditions
29 * are met:
30 * 1. Redistributions of source code must retain the above copyright
31 * notice, this list of conditions and the following disclaimer.
32 * 2. Redistributions in binary form must reproduce the above copyright
33 * notice, this list of conditions and the following disclaimer in the
34 * documentation and/or other materials provided with the distribution.
35 * 3. All advertising materials mentioning features or use of this software
36 * must display the following acknowledgement:
37 * This product includes software developed by the University of
38 * California, Berkeley and its contributors.
39 * 4. Neither the name of the University nor the names of its contributors
40 * may be used to endorse or promote products derived from this software
41 * without specific prior written permission.
42 *
43 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
44 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
47 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * SUCH DAMAGE.
54 */
55
56#include <sys/types.h>
57#include <sys/socket.h>
58#include <sys/syslog.h>
59#include <sys/uio.h>
60#include <sys/un.h>
61#include <netdb.h>
62
63#include <errno.h>
64#include <fcntl.h>
65#include <paths.h>
66#include <stdio.h>
67#include <string.h>
68#include <time.h>
69#include <unistd.h>
70
71#ifdef __STDC__
72#include <stdarg.h>
73#else
74#include <varargs.h>
75#endif
76
77#include <crt_externs.h>
78
79static int LogFile = -1; /* fd for log */
80static int connected; /* have done connect */
81static int LogStat = 0; /* status bits, set by openlog() */
82static const char *LogTag = NULL; /* string to tag the entry with */
83static int LogFacility = LOG_USER; /* default facility code */
84static int LogMask = 0xff; /* mask of priorities to be logged */
85
86/*
87 * syslog, vsyslog --
88 * print message on log file; output is intended for syslogd(8).
89 */
90void
91#ifdef __STDC__
92syslog(int pri, const char *fmt, ...)
93#else
94syslog(pri, fmt, va_alist)
95 int pri;
96 char *fmt;
97 va_dcl
98#endif
99{
100 va_list ap;
101
102#ifdef __STDC__
103 va_start(ap, fmt);
104#else
105 va_start(ap);
106#endif
107 vsyslog(pri, fmt, ap);
108 va_end(ap);
109}
110
111void
112vsyslog(pri, fmt, ap)
113 int pri;
114 register const char *fmt;
115 va_list ap;
116{
117 register int cnt;
118 register char ch, *p, *t;
119 time_t now;
120 int fd, saved_errno;
121#define TBUF_LEN 2048
122#define FMT_LEN 1024
123 char *stdp, tbuf[TBUF_LEN], fmt_cpy[FMT_LEN];
124 int tbuf_left, fmt_left, prlen;
125
126#define INTERNALLOG LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID
127 /* Check for invalid bits. */
128 if (pri & ~(LOG_PRIMASK|LOG_FACMASK)) {
129 syslog(INTERNALLOG,
130 "syslog: unknown facility/priority: %x", pri);
131 pri &= LOG_PRIMASK|LOG_FACMASK;
132 }
133
134 /* Check priority against setlogmask values. */
135 if (!(LOG_MASK(LOG_PRI(pri)) & LogMask))
136 return;
137
138 saved_errno = errno;
139
140 /* Set default facility if none specified. */
141 if ((pri & LOG_FACMASK) == 0)
142 pri |= LogFacility;
143
144 /* Build the message. */
145
146 /*
147 * Although it's tempting, we can't ignore the possibility of
148 * overflowing the buffer when assembling the "fixed" portion
149 * of the message. Strftime's "%h" directive expands to the
150 * locale's abbreviated month name, but if the user has the
151 * ability to construct to his own locale files, it may be
152 * arbitrarily long.
153 */
154 (void)time(&now);
155
156 p = tbuf;
157 tbuf_left = TBUF_LEN;
158
159#define DEC() \
160 do { \
161 if (prlen >= tbuf_left) \
162 prlen = tbuf_left - 1; \
163 p += prlen; \
164 tbuf_left -= prlen; \
165 } while (0)
166
167 prlen = snprintf(p, tbuf_left, "<%d>", pri);
168 DEC();
169
170 prlen = strftime(p, tbuf_left, "%h %e %T ", localtime(&now));
171 DEC();
172
173 if (LogStat & LOG_PERROR)
174 stdp = p;
175 if (LogTag == NULL)
176 LogTag = *(*_NSGetArgv());
177 if (LogTag != NULL) {
178 prlen = snprintf(p, tbuf_left, "%s", LogTag);
179 DEC();
180 }
181 if (LogStat & LOG_PID) {
182 prlen = snprintf(p, tbuf_left, "[%d]", getpid());
183 DEC();
184 }
185 if (LogTag != NULL) {
186 if (tbuf_left > 1) {
187 *p++ = ':';
188 tbuf_left--;
189 }
190 if (tbuf_left > 1) {
191 *p++ = ' ';
192 tbuf_left--;
193 }
194 }
195
196 /*
197 * We wouldn't need this mess if printf handled %m, or if
198 * strerror() had been invented before syslog().
199 */
200 for (t = fmt_cpy, fmt_left = FMT_LEN; (ch = *fmt); ++fmt) {
201 if (ch == '%' && fmt[1] == 'm') {
202 ++fmt;
203 prlen = snprintf(t, fmt_left, "%s",
204 strerror(saved_errno));
205 if (prlen >= fmt_left)
206 prlen = fmt_left - 1;
207 t += prlen;
208 fmt_left -= prlen;
209 } else {
210 if (fmt_left > 1) {
211 *t++ = ch;
212 fmt_left--;
213 }
214 }
215 }
216 *t = '\0';
217
218 prlen = vsnprintf(p, tbuf_left, fmt_cpy, ap);
219 DEC();
220 cnt = p - tbuf;
221
222 /* Output to stderr if requested. */
223 if (LogStat & LOG_PERROR) {
224 struct iovec iov[2];
225
226 iov[0].iov_base = stdp;
227 iov[0].iov_len = cnt - (stdp - tbuf);
228 iov[1].iov_base = "\n";
229 iov[1].iov_len = 1;
230 (void)writev(STDERR_FILENO, iov, 2);
231 }
232
233 /* Get connected, output the message to the local logger. */
234 if (!connected)
235 openlog(LogTag, LogStat | LOG_NDELAY, 0);
236 if (send(LogFile, tbuf, cnt, 0) >= 0)
237 return;
238
239 /*
240 * Output the message to the console; don't worry about blocking,
241 * if console blocks everything will. Make sure the error reported
242 * is the one from the syslogd failure.
243 */
244 if (LogStat & LOG_CONS &&
245 (fd = open(_PATH_CONSOLE, O_WRONLY, 0)) >= 0) {
246 struct iovec iov[2];
247
248 p = strchr(tbuf, '>') + 1;
249 iov[0].iov_base = p;
250 iov[0].iov_len = cnt - (p - tbuf);
251 iov[1].iov_base = "\r\n";
252 iov[1].iov_len = 2;
253 (void)writev(fd, iov, 2);
254 (void)close(fd);
255 }
256}
257
258static struct sockaddr_un SyslogAddr; /* AF_UNIX address of local logger */
259
260void
261openlog(ident, logstat, logfac)
262 const char *ident;
263 int logstat, logfac;
264{
265 if (ident != NULL)
266 LogTag = ident;
267 LogStat = logstat;
268 if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0)
269 LogFacility = logfac;
270
271 if (LogFile == -1) {
272 SyslogAddr.sun_family = AF_UNIX;
273 (void)strncpy(SyslogAddr.sun_path, _PATH_LOG,
274 sizeof(SyslogAddr.sun_path));
275 if (LogStat & LOG_NDELAY) {
276 if ((LogFile = socket(AF_UNIX, SOCK_DGRAM, 0)) == -1)
277 return;
278 (void)fcntl(LogFile, F_SETFD, 1);
279 }
280 }
281 if (LogFile != -1 && !connected)
282 if (connect(LogFile, (struct sockaddr *)&SyslogAddr, sizeof(SyslogAddr)) == -1) {
283 (void)close(LogFile);
284 LogFile = -1;
285 } else
286 connected = 1;
287}
288
289void
290closelog()
291{
292 (void)close(LogFile);
293 LogFile = -1;
294 connected = 0;
295}
296
297/* setlogmask -- set the log mask level */
298int
299setlogmask(pmask)
300 int pmask;
301{
302 int omask;
303
304 omask = LogMask;
305 if (pmask != 0)
306 LogMask = pmask;
307 return (omask);
308}