]>
git.saurik.com Git - apple/libc.git/blob - gen/syslog.c
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>
79 #include <crt_externs.h>
81 #define LOG_NO_NOTIFY 0x1000
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 */
103 __private_extern__
void _sl_init_notify();
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
111 uint32_t notify_get_state(int token
, int *state
);
112 uint32_t notify_register_plain(const char *name
, int *out_token
);
116 * print message on log file; output is intended for syslogd(8).
120 syslog(int pri
, const char *fmt
, ...)
122 syslog(pri
, fmt
, va_alist
)
135 vsyslog(pri
, fmt
, ap
);
140 vsyslog(pri
, fmt
, ap
)
142 register const char *fmt
;
146 register char ch
, *p
, *t
;
148 int fd
, saved_errno
, filter
, cval
, rc_filter
, primask
;
149 #define TBUF_LEN 2048
151 char *stdp
, tbuf
[TBUF_LEN
], fmt_cpy
[FMT_LEN
];
152 int tbuf_left
, fmt_left
, prlen
;
155 #define INTERNALLOG LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID
156 /* Check for invalid bits. */
157 if (pri
& ~(LOG_PRIMASK
|LOG_FACMASK
))
159 syslog(INTERNALLOG
, "syslog: unknown facility/priority: %x", pri
);
160 pri
&= LOG_PRIMASK
|LOG_FACMASK
;
163 /* Get remote-control priority filter */
164 filter
= _sl_LogMask
;
169 if (_sl_NotifyToken
>= 0)
171 if (notify_get_state(_sl_NotifyToken
, &cval
) == NOTIFY_STATUS_OK
)
181 if ((rc_filter
== 0) && (_sl_NotifyMaster
>= 0))
183 if (notify_get_state(_sl_NotifyMaster
, &cval
) == NOTIFY_STATUS_OK
)
192 primask
= LOG_MASK(LOG_PRI(pri
));
193 if ((primask
& filter
) == 0) return;
197 /* Set default facility if none specified. */
198 if ((pri
& LOG_FACMASK
) == 0) pri
|= _sl_LogFacility
;
200 /* Build the message. */
203 * Although it's tempting, we can't ignore the possibility of
204 * overflowing the buffer when assembling the "fixed" portion
205 * of the message. Strftime's "%h" directive expands to the
206 * locale's abbreviated month name, but if the user has the
207 * ability to construct to his own locale files, it may be
213 tbuf_left
= TBUF_LEN
;
217 if (prlen >= tbuf_left) \
218 prlen = tbuf_left - 1; \
220 tbuf_left -= prlen; \
223 prlen
= snprintf(p
, tbuf_left
, "<%d>", pri
);
226 prlen
= strftime(p
, tbuf_left
, "%h %e %T ", localtime_r(&now
, &tm
));
229 if (_sl_LogStat
& LOG_PERROR
) stdp
= p
;
231 if (_sl_LogTag
== NULL
) _sl_LogTag
= *(*_NSGetArgv());
233 if (_sl_LogTag
!= NULL
)
235 prlen
= snprintf(p
, tbuf_left
, "%s", _sl_LogTag
);
239 if (_sl_LogStat
& LOG_PID
)
241 prlen
= snprintf(p
, tbuf_left
, "[%d]", getpid());
245 if (_sl_LogTag
!= NULL
)
260 * We wouldn't need this mess if printf handled %m, or if
261 * strerror() had been invented before syslog().
263 for (t
= fmt_cpy
, fmt_left
= FMT_LEN
; (ch
= *fmt
); ++fmt
)
265 if (ch
== '%' && fmt
[1] == 'm')
268 prlen
= snprintf(t
, fmt_left
, "%s", strerror(saved_errno
));
269 if (prlen
>= fmt_left
) prlen
= fmt_left
- 1;
285 prlen
= vsnprintf(p
, tbuf_left
, fmt_cpy
, ap
);
289 /* Output to stderr if requested. */
290 if (_sl_LogStat
& LOG_PERROR
)
294 iov
[0].iov_base
= stdp
;
295 iov
[0].iov_len
= cnt
- (stdp
- tbuf
);
296 iov
[1].iov_base
= "\n";
298 (void)writev(STDERR_FILENO
, iov
, 2);
301 /* Get connected, output the message to the local logger. */
302 if (_sl_connected
== 0) openlog(_sl_LogTag
, _sl_LogStat
| LOG_NDELAY
, 0);
303 if (send(_sl_LogFile
, tbuf
, cnt
, 0) >= 0) return;
306 * Output the message to the console; don't worry about blocking,
307 * if console blocks everything will. Make sure the error reported
308 * is the one from the syslogd failure.
310 if (_sl_LogStat
& LOG_CONS
&& (fd
= open(_PATH_CONSOLE
, O_WRONLY
, 0)) >= 0)
314 p
= strchr(tbuf
, '>') + 1;
316 iov
[0].iov_len
= cnt
- (p
- tbuf
);
317 iov
[1].iov_base
= "\r\n";
319 (void)writev(fd
, iov
, 2);
324 #ifndef BUILDING_VARIANT
326 static struct sockaddr_un SyslogAddr
; /* AF_UNIX address of local logger */
328 __private_extern__
void
335 if (_sl_LogStat
& LOG_NO_NOTIFY
)
337 _sl_NotifyMaster
= -2;
338 _sl_NotifyToken
= -2;
342 if (_sl_NotifyMaster
== -1)
344 status
= notify_register_plain(NOTIFY_SYSTEM_MASTER
, &_sl_NotifyMaster
);
345 if (status
!= NOTIFY_STATUS_OK
) _sl_NotifyMaster
= -2;
348 if (_sl_NotifyToken
== -1)
350 _sl_NotifyToken
= -2;
353 prefix
= NOTIFY_PREFIX_USER
;
354 if (getuid() == 0) prefix
= NOTIFY_PREFIX_SYSTEM
;
355 asprintf(¬ify_name
, "%s.%d", prefix
, getpid());
357 if (notify_name
!= NULL
)
359 status
= notify_register_plain(notify_name
, &_sl_NotifyToken
);
361 if (status
!= NOTIFY_STATUS_OK
) _sl_NotifyToken
= -2;
367 openlog(ident
, logstat
, logfac
)
371 if (ident
!= NULL
) _sl_LogTag
= ident
;
373 _sl_LogStat
= logstat
;
375 if (logfac
!= 0 && (logfac
&~ LOG_FACMASK
) == 0) _sl_LogFacility
= logfac
;
377 if (_sl_LogFile
== -1)
379 SyslogAddr
.sun_family
= AF_UNIX
;
380 (void)strncpy(SyslogAddr
.sun_path
, _PATH_LOG
, sizeof(SyslogAddr
.sun_path
));
381 if (_sl_LogStat
& LOG_NDELAY
)
383 if ((_sl_LogFile
= socket(AF_UNIX
, SOCK_DGRAM
, 0)) == -1) return;
384 (void)fcntl(_sl_LogFile
, F_SETFD
, 1);
388 if ((_sl_LogFile
!= -1) && (_sl_connected
== 0))
390 if (connect(_sl_LogFile
, (struct sockaddr
*)&SyslogAddr
, sizeof(SyslogAddr
)) == -1)
392 (void)close(_sl_LogFile
);
407 (void)close(_sl_LogFile
);
412 /* setlogmask -- set the log mask level */
420 if (pmask
!= 0) _sl_LogMask
= pmask
;
424 #endif /* !BUILDING_VARIANT */