]>
git.saurik.com Git - apple/libc.git/blob - gen.subproj/syslog.c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
24 * The Regents of the University of California. All rights reserved.
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that the following conditions
29 * 1. Redistributions of source code must retain the above copyright
30 * notice, this list of conditions and the following disclaimer.
31 * 2. Redistributions in binary form must reproduce the above copyright
32 * notice, this list of conditions and the following disclaimer in the
33 * documentation and/or other materials provided with the distribution.
34 * 3. All advertising materials mentioning features or use of this software
35 * must display the following acknowledgement:
36 * This product includes software developed by the University of
37 * California, Berkeley and its contributors.
38 * 4. Neither the name of the University nor the names of its contributors
39 * may be used to endorse or promote products derived from this software
40 * without specific prior written permission.
42 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
43 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
46 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 #include <sys/types.h>
56 #include <sys/socket.h>
57 #include <sys/syslog.h>
76 #include <crt_externs.h>
78 static int LogFile
= -1; /* fd for log */
79 static int connected
; /* have done connect */
80 static int LogStat
= 0; /* status bits, set by openlog() */
81 static const char *LogTag
= NULL
; /* string to tag the entry with */
82 static int LogFacility
= LOG_USER
; /* default facility code */
83 static int LogMask
= 0xff; /* mask of priorities to be logged */
87 * print message on log file; output is intended for syslogd(8).
91 syslog(int pri
, const char *fmt
, ...)
93 syslog(pri
, fmt
, va_alist
)
106 vsyslog(pri
, fmt
, ap
);
111 vsyslog(pri
, fmt
, ap
)
113 register const char *fmt
;
117 register char ch
, *p
, *t
;
120 #define TBUF_LEN 2048
122 char *stdp
, tbuf
[TBUF_LEN
], fmt_cpy
[FMT_LEN
];
123 int tbuf_left
, fmt_left
, prlen
;
125 #define INTERNALLOG LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID
126 /* Check for invalid bits. */
127 if (pri
& ~(LOG_PRIMASK
|LOG_FACMASK
)) {
129 "syslog: unknown facility/priority: %x", pri
);
130 pri
&= LOG_PRIMASK
|LOG_FACMASK
;
133 /* Check priority against setlogmask values. */
134 if (!(LOG_MASK(LOG_PRI(pri
)) & LogMask
))
139 /* Set default facility if none specified. */
140 if ((pri
& LOG_FACMASK
) == 0)
143 /* Build the message. */
146 * Although it's tempting, we can't ignore the possibility of
147 * overflowing the buffer when assembling the "fixed" portion
148 * of the message. Strftime's "%h" directive expands to the
149 * locale's abbreviated month name, but if the user has the
150 * ability to construct to his own locale files, it may be
156 tbuf_left
= TBUF_LEN
;
160 if (prlen >= tbuf_left) \
161 prlen = tbuf_left - 1; \
163 tbuf_left -= prlen; \
166 prlen
= snprintf(p
, tbuf_left
, "<%d>", pri
);
169 prlen
= strftime(p
, tbuf_left
, "%h %e %T ", localtime(&now
));
172 if (LogStat
& LOG_PERROR
)
175 LogTag
= *(*_NSGetArgv());
176 if (LogTag
!= NULL
) {
177 prlen
= snprintf(p
, tbuf_left
, "%s", LogTag
);
180 if (LogStat
& LOG_PID
) {
181 prlen
= snprintf(p
, tbuf_left
, "[%d]", getpid());
184 if (LogTag
!= NULL
) {
196 * We wouldn't need this mess if printf handled %m, or if
197 * strerror() had been invented before syslog().
199 for (t
= fmt_cpy
, fmt_left
= FMT_LEN
; (ch
= *fmt
); ++fmt
) {
200 if (ch
== '%' && fmt
[1] == 'm') {
202 prlen
= snprintf(t
, fmt_left
, "%s",
203 strerror(saved_errno
));
204 if (prlen
>= fmt_left
)
205 prlen
= fmt_left
- 1;
217 prlen
= vsnprintf(p
, tbuf_left
, fmt_cpy
, ap
);
221 /* Output to stderr if requested. */
222 if (LogStat
& LOG_PERROR
) {
225 iov
[0].iov_base
= stdp
;
226 iov
[0].iov_len
= cnt
- (stdp
- tbuf
);
227 iov
[1].iov_base
= "\n";
229 (void)writev(STDERR_FILENO
, iov
, 2);
232 /* Get connected, output the message to the local logger. */
234 openlog(LogTag
, LogStat
| LOG_NDELAY
, 0);
235 if (send(LogFile
, tbuf
, cnt
, 0) >= 0)
239 * Output the message to the console; don't worry about blocking,
240 * if console blocks everything will. Make sure the error reported
241 * is the one from the syslogd failure.
243 if (LogStat
& LOG_CONS
&&
244 (fd
= open(_PATH_CONSOLE
, O_WRONLY
, 0)) >= 0) {
247 p
= strchr(tbuf
, '>') + 1;
249 iov
[0].iov_len
= cnt
- (p
- tbuf
);
250 iov
[1].iov_base
= "\r\n";
252 (void)writev(fd
, iov
, 2);
257 static struct sockaddr_un SyslogAddr
; /* AF_UNIX address of local logger */
260 openlog(ident
, logstat
, logfac
)
267 if (logfac
!= 0 && (logfac
&~ LOG_FACMASK
) == 0)
268 LogFacility
= logfac
;
271 SyslogAddr
.sun_family
= AF_UNIX
;
272 (void)strncpy(SyslogAddr
.sun_path
, _PATH_LOG
,
273 sizeof(SyslogAddr
.sun_path
));
274 if (LogStat
& LOG_NDELAY
) {
275 if ((LogFile
= socket(AF_UNIX
, SOCK_DGRAM
, 0)) == -1)
277 (void)fcntl(LogFile
, F_SETFD
, 1);
280 if (LogFile
!= -1 && !connected
)
281 if (connect(LogFile
, &SyslogAddr
, sizeof(SyslogAddr
)) == -1) {
282 (void)close(LogFile
);
291 (void)close(LogFile
);
296 /* setlogmask -- set the log mask level */