]>
git.saurik.com Git - apple/libc.git/blob - gen/syslog.c
f60709817dab4808721dcc41aaea781bac49f36e
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
25 * The Regents of the University of California. All rights reserved.
27 * Redistribution and use in source and binary forms, with or without
28 * modification, are permitted provided that the following conditions
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.
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
56 #include <sys/types.h>
57 #include <sys/socket.h>
58 #include <sys/syslog.h>
77 #include <crt_externs.h>
79 static int LogFile
= -1; /* fd for log */
80 static int connected
; /* have done connect */
81 static int LogStat
= 0; /* status bits, set by openlog() */
82 static const char *LogTag
= NULL
; /* string to tag the entry with */
83 static int LogFacility
= LOG_USER
; /* default facility code */
84 static int LogMask
= 0xff; /* mask of priorities to be logged */
88 * print message on log file; output is intended for syslogd(8).
92 syslog(int pri
, const char *fmt
, ...)
94 syslog(pri
, fmt
, va_alist
)
107 vsyslog(pri
, fmt
, ap
);
112 vsyslog(pri
, fmt
, ap
)
114 register const char *fmt
;
118 register char ch
, *p
, *t
;
121 #define TBUF_LEN 2048
123 char *stdp
, tbuf
[TBUF_LEN
], fmt_cpy
[FMT_LEN
];
124 int tbuf_left
, fmt_left
, prlen
;
126 #define INTERNALLOG LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID
127 /* Check for invalid bits. */
128 if (pri
& ~(LOG_PRIMASK
|LOG_FACMASK
)) {
130 "syslog: unknown facility/priority: %x", pri
);
131 pri
&= LOG_PRIMASK
|LOG_FACMASK
;
134 /* Check priority against setlogmask values. */
135 if (!(LOG_MASK(LOG_PRI(pri
)) & LogMask
))
140 /* Set default facility if none specified. */
141 if ((pri
& LOG_FACMASK
) == 0)
144 /* Build the message. */
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
157 tbuf_left
= TBUF_LEN
;
161 if (prlen >= tbuf_left) \
162 prlen = tbuf_left - 1; \
164 tbuf_left -= prlen; \
167 prlen
= snprintf(p
, tbuf_left
, "<%d>", pri
);
170 prlen
= strftime(p
, tbuf_left
, "%h %e %T ", localtime(&now
));
173 if (LogStat
& LOG_PERROR
)
176 LogTag
= *(*_NSGetArgv());
177 if (LogTag
!= NULL
) {
178 prlen
= snprintf(p
, tbuf_left
, "%s", LogTag
);
181 if (LogStat
& LOG_PID
) {
182 prlen
= snprintf(p
, tbuf_left
, "[%d]", getpid());
185 if (LogTag
!= NULL
) {
197 * We wouldn't need this mess if printf handled %m, or if
198 * strerror() had been invented before syslog().
200 for (t
= fmt_cpy
, fmt_left
= FMT_LEN
; (ch
= *fmt
); ++fmt
) {
201 if (ch
== '%' && fmt
[1] == 'm') {
203 prlen
= snprintf(t
, fmt_left
, "%s",
204 strerror(saved_errno
));
205 if (prlen
>= fmt_left
)
206 prlen
= fmt_left
- 1;
218 prlen
= vsnprintf(p
, tbuf_left
, fmt_cpy
, ap
);
222 /* Output to stderr if requested. */
223 if (LogStat
& LOG_PERROR
) {
226 iov
[0].iov_base
= stdp
;
227 iov
[0].iov_len
= cnt
- (stdp
- tbuf
);
228 iov
[1].iov_base
= "\n";
230 (void)writev(STDERR_FILENO
, iov
, 2);
233 /* Get connected, output the message to the local logger. */
235 openlog(LogTag
, LogStat
| LOG_NDELAY
, 0);
236 if (send(LogFile
, tbuf
, cnt
, 0) >= 0)
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.
244 if (LogStat
& LOG_CONS
&&
245 (fd
= open(_PATH_CONSOLE
, O_WRONLY
, 0)) >= 0) {
248 p
= strchr(tbuf
, '>') + 1;
250 iov
[0].iov_len
= cnt
- (p
- tbuf
);
251 iov
[1].iov_base
= "\r\n";
253 (void)writev(fd
, iov
, 2);
258 static struct sockaddr_un SyslogAddr
; /* AF_UNIX address of local logger */
261 openlog(ident
, logstat
, logfac
)
268 if (logfac
!= 0 && (logfac
&~ LOG_FACMASK
) == 0)
269 LogFacility
= logfac
;
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)
278 (void)fcntl(LogFile
, F_SETFD
, 1);
281 if (LogFile
!= -1 && !connected
)
282 if (connect(LogFile
, (struct sockaddr
*)&SyslogAddr
, sizeof(SyslogAddr
)) == -1) {
283 (void)close(LogFile
);
292 (void)close(LogFile
);
297 /* setlogmask -- set the log mask level */