]> git.saurik.com Git - apple/libc.git/blame - gen/syslog.c
Libc-391.4.3.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 *
734aad71
A
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
e9ce8d39
A
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
734aad71
A
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.
e9ce8d39
A
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>
3d9156a7 67#include <stdlib.h>
e9ce8d39
A
68#include <string.h>
69#include <time.h>
70#include <unistd.h>
3d9156a7 71#include <notify.h>
e9ce8d39
A
72
73#ifdef __STDC__
74#include <stdarg.h>
75#else
76#include <varargs.h>
77#endif
78
79#include <crt_externs.h>
3d9156a7
A
80
81#define LOG_NO_NOTIFY 0x1000
82
83#ifdef BUILDING_VARIANT
84__private_extern__ int _sl_LogFile; /* fd for log */
85__private_extern__ int _sl_connected; /* have done connect */
86__private_extern__ int _sl_LogStat; /* status bits, set by openlog() */
87__private_extern__ const char *_sl_LogTag; /* string to tag the entry with */
88__private_extern__ int _sl_LogFacility; /* default facility code */
89__private_extern__ int _sl_LogMask; /* mask of priorities to be logged */
90__private_extern__ int _sl_NotifyToken; /* for remote control of priority filter */
91__private_extern__ int _sl_NotifyMaster; /* for remote control of priority filter */
92#else /* !BUILDING_VARIANT */
93__private_extern__ int _sl_LogFile = -1; /* fd for log */
94__private_extern__ int _sl_connected = 0; /* have done connect */
95__private_extern__ int _sl_LogStat = 0; /* status bits, set by openlog() */
96__private_extern__ const char *_sl_LogTag = NULL; /* string to tag the entry with */
97__private_extern__ int _sl_LogFacility = LOG_USER; /* default facility code */
98__private_extern__ int _sl_LogMask = 0xff; /* mask of priorities to be logged */
99__private_extern__ int _sl_NotifyToken = -1; /* for remote control of max logged priority */
100__private_extern__ int _sl_NotifyMaster = -1; /* for remote control of max logged priority */
101#endif /* BUILDING_VARIANT */
102
103__private_extern__ void _sl_init_notify();
104
105#define NOTIFY_SYSTEM_MASTER "com.apple.system.syslog.master"
106#define NOTIFY_PREFIX_SYSTEM "com.apple.system.syslog"
107#define NOTIFY_PREFIX_USER "user.syslog"
108#define NOTIFY_STATE_OFFSET 1000
109
110/* notify SPI */
111uint32_t notify_get_state(int token, int *state);
112uint32_t notify_register_plain(const char *name, int *out_token);
e9ce8d39
A
113
114/*
115 * syslog, vsyslog --
116 * print message on log file; output is intended for syslogd(8).
117 */
118void
119#ifdef __STDC__
120syslog(int pri, const char *fmt, ...)
121#else
122syslog(pri, fmt, va_alist)
123 int pri;
124 char *fmt;
125 va_dcl
126#endif
127{
128 va_list ap;
129
130#ifdef __STDC__
131 va_start(ap, fmt);
132#else
133 va_start(ap);
134#endif
135 vsyslog(pri, fmt, ap);
136 va_end(ap);
137}
138
139void
140vsyslog(pri, fmt, ap)
141 int pri;
142 register const char *fmt;
143 va_list ap;
144{
145 register int cnt;
146 register char ch, *p, *t;
147 time_t now;
3d9156a7 148 int fd, saved_errno, filter, cval, rc_filter, primask;
e9ce8d39
A
149#define TBUF_LEN 2048
150#define FMT_LEN 1024
151 char *stdp, tbuf[TBUF_LEN], fmt_cpy[FMT_LEN];
152 int tbuf_left, fmt_left, prlen;
3d9156a7 153
e9ce8d39
A
154#define INTERNALLOG LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID
155 /* Check for invalid bits. */
3d9156a7
A
156 if (pri & ~(LOG_PRIMASK|LOG_FACMASK))
157 {
158 syslog(INTERNALLOG, "syslog: unknown facility/priority: %x", pri);
e9ce8d39
A
159 pri &= LOG_PRIMASK|LOG_FACMASK;
160 }
161
3d9156a7
A
162 /* Get remote-control priority filter */
163 filter = _sl_LogMask;
164 rc_filter = 0;
165
166 _sl_init_notify();
167
168 if (_sl_NotifyToken >= 0)
169 {
170 if (notify_get_state(_sl_NotifyToken, &cval) == NOTIFY_STATUS_OK)
171 {
172 if (cval != 0)
173 {
174 filter = cval;
175 rc_filter = 1;
176 }
177 }
178 }
179
180 if ((rc_filter == 0) && (_sl_NotifyMaster >= 0))
181 {
182 if (notify_get_state(_sl_NotifyMaster, &cval) == NOTIFY_STATUS_OK)
183 {
184 if (cval != 0)
185 {
186 filter = cval;
187 }
188 }
189 }
190
191 primask = LOG_MASK(LOG_PRI(pri));
192 if ((primask & filter) == 0) return;
e9ce8d39
A
193
194 saved_errno = errno;
195
196 /* Set default facility if none specified. */
3d9156a7 197 if ((pri & LOG_FACMASK) == 0) pri |= _sl_LogFacility;
e9ce8d39
A
198
199 /* Build the message. */
200
201 /*
202 * Although it's tempting, we can't ignore the possibility of
203 * overflowing the buffer when assembling the "fixed" portion
204 * of the message. Strftime's "%h" directive expands to the
205 * locale's abbreviated month name, but if the user has the
206 * ability to construct to his own locale files, it may be
207 * arbitrarily long.
208 */
209 (void)time(&now);
210
211 p = tbuf;
212 tbuf_left = TBUF_LEN;
213
214#define DEC() \
215 do { \
216 if (prlen >= tbuf_left) \
217 prlen = tbuf_left - 1; \
218 p += prlen; \
219 tbuf_left -= prlen; \
220 } while (0)
221
222 prlen = snprintf(p, tbuf_left, "<%d>", pri);
223 DEC();
224
225 prlen = strftime(p, tbuf_left, "%h %e %T ", localtime(&now));
226 DEC();
227
3d9156a7
A
228 if (_sl_LogStat & LOG_PERROR) stdp = p;
229
230 if (_sl_LogTag == NULL) _sl_LogTag = *(*_NSGetArgv());
231
232 if (_sl_LogTag != NULL)
233 {
234 prlen = snprintf(p, tbuf_left, "%s", _sl_LogTag);
e9ce8d39
A
235 DEC();
236 }
3d9156a7
A
237
238 if (_sl_LogStat & LOG_PID)
239 {
e9ce8d39
A
240 prlen = snprintf(p, tbuf_left, "[%d]", getpid());
241 DEC();
242 }
3d9156a7
A
243
244 if (_sl_LogTag != NULL)
245 {
246 if (tbuf_left > 1)
247 {
e9ce8d39
A
248 *p++ = ':';
249 tbuf_left--;
250 }
3d9156a7
A
251 if (tbuf_left > 1)
252 {
e9ce8d39
A
253 *p++ = ' ';
254 tbuf_left--;
255 }
256 }
257
258 /*
259 * We wouldn't need this mess if printf handled %m, or if
260 * strerror() had been invented before syslog().
261 */
3d9156a7
A
262 for (t = fmt_cpy, fmt_left = FMT_LEN; (ch = *fmt); ++fmt)
263 {
264 if (ch == '%' && fmt[1] == 'm')
265 {
e9ce8d39 266 ++fmt;
3d9156a7
A
267 prlen = snprintf(t, fmt_left, "%s", strerror(saved_errno));
268 if (prlen >= fmt_left) prlen = fmt_left - 1;
e9ce8d39
A
269 t += prlen;
270 fmt_left -= prlen;
3d9156a7
A
271 }
272 else
273 {
274 if (fmt_left > 1)
275 {
e9ce8d39
A
276 *t++ = ch;
277 fmt_left--;
278 }
279 }
280 }
3d9156a7 281
e9ce8d39
A
282 *t = '\0';
283
284 prlen = vsnprintf(p, tbuf_left, fmt_cpy, ap);
285 DEC();
286 cnt = p - tbuf;
287
288 /* Output to stderr if requested. */
3d9156a7
A
289 if (_sl_LogStat & LOG_PERROR)
290 {
e9ce8d39
A
291 struct iovec iov[2];
292
293 iov[0].iov_base = stdp;
294 iov[0].iov_len = cnt - (stdp - tbuf);
295 iov[1].iov_base = "\n";
296 iov[1].iov_len = 1;
297 (void)writev(STDERR_FILENO, iov, 2);
298 }
299
300 /* Get connected, output the message to the local logger. */
3d9156a7
A
301 if (_sl_connected == 0) openlog(_sl_LogTag, _sl_LogStat | LOG_NDELAY, 0);
302 if (send(_sl_LogFile, tbuf, cnt, 0) >= 0) return;
e9ce8d39
A
303
304 /*
305 * Output the message to the console; don't worry about blocking,
306 * if console blocks everything will. Make sure the error reported
307 * is the one from the syslogd failure.
308 */
3d9156a7
A
309 if (_sl_LogStat & LOG_CONS && (fd = open(_PATH_CONSOLE, O_WRONLY, 0)) >= 0)
310 {
e9ce8d39
A
311 struct iovec iov[2];
312
313 p = strchr(tbuf, '>') + 1;
314 iov[0].iov_base = p;
315 iov[0].iov_len = cnt - (p - tbuf);
316 iov[1].iov_base = "\r\n";
317 iov[1].iov_len = 2;
318 (void)writev(fd, iov, 2);
319 (void)close(fd);
320 }
321}
322
3d9156a7
A
323#ifndef BUILDING_VARIANT
324
e9ce8d39
A
325static struct sockaddr_un SyslogAddr; /* AF_UNIX address of local logger */
326
3d9156a7
A
327__private_extern__ void
328_sl_init_notify()
329{
330 int status;
331 char *notify_name;
332 const char *prefix;
333
334 if (_sl_LogStat & LOG_NO_NOTIFY)
335 {
336 _sl_NotifyMaster = -2;
337 _sl_NotifyToken = -2;
338 return;
339 }
340
341 if (_sl_NotifyMaster == -1)
342 {
343 status = notify_register_plain(NOTIFY_SYSTEM_MASTER, &_sl_NotifyMaster);
344 if (status != NOTIFY_STATUS_OK) _sl_NotifyMaster = -2;
345 }
346
347 if (_sl_NotifyToken == -1)
348 {
349 _sl_NotifyToken = -2;
350
351 notify_name = NULL;
352 prefix = NOTIFY_PREFIX_USER;
353 if (getuid() == 0) prefix = NOTIFY_PREFIX_SYSTEM;
354 asprintf(&notify_name, "%s.%d", prefix, getpid());
355
356 if (notify_name != NULL)
357 {
358 status = notify_register_plain(notify_name, &_sl_NotifyToken);
359 free(notify_name);
360 if (status != NOTIFY_STATUS_OK) _sl_NotifyToken = -2;
361 }
362 }
363}
364
e9ce8d39
A
365void
366openlog(ident, logstat, logfac)
367 const char *ident;
368 int logstat, logfac;
369{
3d9156a7
A
370 if (ident != NULL) _sl_LogTag = ident;
371
372 _sl_LogStat = logstat;
373
374 if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0) _sl_LogFacility = logfac;
e9ce8d39 375
3d9156a7
A
376 if (_sl_LogFile == -1)
377 {
e9ce8d39 378 SyslogAddr.sun_family = AF_UNIX;
3d9156a7
A
379 (void)strncpy(SyslogAddr.sun_path, _PATH_LOG, sizeof(SyslogAddr.sun_path));
380 if (_sl_LogStat & LOG_NDELAY)
381 {
382 if ((_sl_LogFile = socket(AF_UNIX, SOCK_DGRAM, 0)) == -1) return;
383 (void)fcntl(_sl_LogFile, F_SETFD, 1);
e9ce8d39
A
384 }
385 }
3d9156a7
A
386
387 if ((_sl_LogFile != -1) && (_sl_connected == 0))
388 {
389 if (connect(_sl_LogFile, (struct sockaddr *)&SyslogAddr, sizeof(SyslogAddr)) == -1)
390 {
391 (void)close(_sl_LogFile);
392 _sl_LogFile = -1;
393 }
394 else
395 {
396 _sl_connected = 1;
397 }
398 }
399
400 _sl_init_notify();
e9ce8d39
A
401}
402
403void
404closelog()
405{
3d9156a7
A
406 (void)close(_sl_LogFile);
407 _sl_LogFile = -1;
408 _sl_connected = 0;
e9ce8d39
A
409}
410
411/* setlogmask -- set the log mask level */
412int
413setlogmask(pmask)
414 int pmask;
415{
416 int omask;
417
3d9156a7
A
418 omask = _sl_LogMask;
419 if (pmask != 0) _sl_LogMask = pmask;
e9ce8d39
A
420 return (omask);
421}
3d9156a7
A
422
423#endif /* !BUILDING_VARIANT */