1
/* $OpenBSD: syslog.c,v 1.28 2005/08/08 08:05:34 espie Exp $ */
3
* Copyright (c) 1983, 1988, 1993
4
* The Regents of the University of California. All rights reserved.
6
* Redistribution and use in source and binary forms, with or without
7
* modification, are permitted provided that the following conditions
9
* 1. Redistributions of source code must retain the above copyright
10
* notice, this list of conditions and the following disclaimer.
11
* 2. Redistributions in binary form must reproduce the above copyright
12
* notice, this list of conditions and the following disclaimer in the
13
* documentation and/or other materials provided with the distribution.
14
* 3. Neither the name of the University nor the names of its contributors
15
* may be used to endorse or promote products derived from this software
16
* without specific prior written permission.
18
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31
#include <sys/types.h>
32
#include <sys/socket.h>
47
static struct syslog_data sdata = SYSLOG_DATA_INIT;
49
extern char *__progname; /* Program name, from crt0. */
51
static void disconnectlog_r(struct syslog_data *); /* disconnect from syslogd */
52
static void connectlog_r(struct syslog_data *); /* (re)connect to syslogd */
56
* print message on log file; output is intended for syslogd(8).
59
syslog(int pri, const char *fmt, ...)
64
vsyslog(pri, fmt, ap);
69
vsyslog(int pri, const char *fmt, va_list ap)
71
vsyslog_r(pri, &sdata, fmt, ap);
75
openlog(const char *ident, int logstat, int logfac)
77
openlog_r(ident, logstat, logfac, &sdata);
86
/* setlogmask -- set the log mask level */
90
return setlogmask_r(pmask, &sdata);
93
/* Reentrant version of syslog, i.e. syslog_r() */
96
syslog_r(int pri, struct syslog_data *data, const char *fmt, ...)
101
vsyslog_r(pri, data, fmt, ap);
106
vsyslog_r(int pri, struct syslog_data *data, const char *fmt, va_list ap)
111
int fd, saved_errno, error;
112
#define TBUF_LEN 2048
114
char *stdp = NULL, tbuf[TBUF_LEN], fmt_cpy[FMT_LEN];
115
int tbuf_left, fmt_left, prlen;
117
#define INTERNALLOG LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID
118
/* Check for invalid bits. */
119
if (pri & ~(LOG_PRIMASK|LOG_FACMASK)) {
120
if (data == &sdata) {
122
"syslog: unknown facility/priority: %x", pri);
124
syslog_r(INTERNALLOG, data,
125
"syslog_r: unknown facility/priority: %x", pri);
127
pri &= LOG_PRIMASK|LOG_FACMASK;
130
/* Check priority against setlogmask values. */
131
if (!(LOG_MASK(LOG_PRI(pri)) & data->log_mask))
136
/* Set default facility if none specified. */
137
if ((pri & LOG_FACMASK) == 0)
138
pri |= data->log_fac;
140
/* If we have been called through syslog(), no need for reentrancy. */
145
tbuf_left = TBUF_LEN;
151
if (prlen >= tbuf_left) \
152
prlen = tbuf_left - 1; \
154
tbuf_left -= prlen; \
157
prlen = snprintf(p, tbuf_left, "<%d>", pri);
161
* syslogd will expand time automagically for reentrant case, and
162
* for normal case, just do like before
164
if (data == &sdata) {
165
prlen = strftime(p, tbuf_left, "%h %e %T ", localtime(&now));
169
if (data->log_stat & LOG_PERROR)
171
if (data->log_tag == NULL)
172
data->log_tag = __progname;
173
if (data->log_tag != NULL) {
174
prlen = snprintf(p, tbuf_left, "%s", data->log_tag);
177
if (data->log_stat & LOG_PID) {
178
prlen = snprintf(p, tbuf_left, "[%ld]", (long)getpid());
181
if (data->log_tag != NULL) {
192
/* strerror() is not reentrant */
194
for (t = fmt_cpy, fmt_left = FMT_LEN; (ch = *fmt); ++fmt) {
195
if (ch == '%' && fmt[1] == 'm') {
197
if (data == &sdata) {
198
prlen = snprintf(t, fmt_left, "%s",
199
strerror(saved_errno));
201
prlen = snprintf(t, fmt_left, "Error %d",
206
if (prlen >= fmt_left)
207
prlen = fmt_left - 1;
210
} else if (ch == '%' && fmt[1] == '%' && fmt_left > 2) {
224
prlen = vsnprintf(p, tbuf_left, fmt_cpy, ap);
228
/* Output to stderr if requested. */
229
if (data->log_stat & LOG_PERROR) {
232
iov[0].iov_base = stdp;
233
iov[0].iov_len = cnt - (stdp - tbuf);
234
iov[1].iov_base = "\n";
236
(void)writev(STDERR_FILENO, iov, 2);
239
/* Get connected, output the message to the local logger. */
241
openlog_r(data->log_tag, data->log_stat, 0, data);
245
* If the send() failed, there are two likely scenarios:
246
* 1) syslogd was restarted
247
* 2) /dev/log is out of socket buffer space
248
* We attempt to reconnect to /dev/log to take care of
249
* case #1 and keep send()ing data to cover case #2
250
* to give syslogd a chance to empty its socket buffer.
252
if ((error = send(data->log_file, tbuf, cnt, 0)) < 0) {
253
if (errno != ENOBUFS) {
254
disconnectlog_r(data);
259
if ((error = send(data->log_file, tbuf, cnt, 0)) >= 0)
261
} while (errno == ENOBUFS);
265
* Output the message to the console; try not to block
266
* as a blocking console should not stop other processes.
267
* Make sure the error reported is the one from the syslogd failure.
269
if (error == -1 && (data->log_stat & LOG_CONS) &&
270
(fd = open(_PATH_CONSOLE, O_WRONLY|O_NONBLOCK, 0)) >= 0) {
273
p = strchr(tbuf, '>') + 1;
275
iov[0].iov_len = cnt - (p - tbuf);
276
iov[1].iov_base = "\r\n";
278
(void)writev(fd, iov, 2);
287
disconnectlog_r(struct syslog_data *data)
290
* If the user closed the FD and opened another in the same slot,
291
* that's their problem. They should close it before calling on
294
if (data->log_file != -1) {
295
close(data->log_file);
298
data->connected = 0; /* retry connect */
302
connectlog_r(struct syslog_data *data)
305
struct sockaddr syslogAddr;
306
struct sockaddr_un syslogAddrUn;
309
#define SyslogAddr u.syslogAddrUn
311
if (data->log_file == -1) {
312
if ((data->log_file = socket(AF_UNIX, SOCK_DGRAM, 0)) == -1)
314
(void)fcntl(data->log_file, F_SETFD, 1);
316
if (data->log_file != -1 && !data->connected) {
317
memset(&SyslogAddr, '\0', sizeof(SyslogAddr));
319
/* BIONIC: no sun_len field to fill on Linux */
320
SyslogAddr.sun_len = sizeof(SyslogAddr);
322
SyslogAddr.sun_family = AF_UNIX;
323
strlcpy(SyslogAddr.sun_path, _PATH_LOG,
324
sizeof(SyslogAddr.sun_path));
325
if (connect(data->log_file, &u.syslogAddr,
326
sizeof(SyslogAddr)) == -1) {
327
(void)close(data->log_file);
335
openlog_r(const char *ident, int logstat, int logfac, struct syslog_data *data)
338
data->log_tag = ident;
339
data->log_stat = logstat;
340
if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0)
341
data->log_fac = logfac;
343
if (data->log_stat & LOG_NDELAY) /* open immediately */
346
data->opened = 1; /* ident and facility has been set */
350
closelog_r(struct syslog_data *data)
352
(void)close(data->log_file);
355
data->log_tag = NULL;
358
/* setlogmask -- set the log mask level */
360
setlogmask_r(int pmask, struct syslog_data *data)
364
omask = data->log_mask;
366
data->log_mask = pmask;