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