GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: lib/libc/gen/syslog_r.c Lines: 0 77 0.0 %
Date: 2017-11-13 Branches: 0 54 0.0 %

Line Branch Exec Source
1
/*	$OpenBSD: syslog_r.c,v 1.19 2017/08/08 14:23:23 bluhm Exp $ */
2
/*
3
 * Copyright (c) 1983, 1988, 1993
4
 *	The Regents of the University of California.  All rights reserved.
5
 *
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions
8
 * are met:
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.
17
 *
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
28
 * SUCH DAMAGE.
29
 */
30
31
#include <sys/types.h>
32
#include <sys/uio.h>
33
#include <netdb.h>
34
35
#include <errno.h>
36
#include <fcntl.h>
37
#include <paths.h>
38
#include <stdio.h>
39
#include <stdlib.h>
40
#include <string.h>
41
#include <syslog.h>
42
#include <time.h>
43
#include <unistd.h>
44
#include <limits.h>
45
#include <stdarg.h>
46
47
/* Reentrant version of syslog, i.e. syslog_r() */
48
void
49
syslog_r(int pri, struct syslog_data *data, const char *fmt, ...)
50
{
51
	va_list ap;
52
53
	va_start(ap, fmt);
54
	vsyslog_r(pri, data, fmt, ap);
55
	va_end(ap);
56
}
57
DEF_WEAK(syslog_r);
58
59
void
60
vsyslog_r(int pri, struct syslog_data *data, const char *fmt, va_list ap)
61
{
62
	__vsyslog_r(pri, data, 1, fmt, ap);
63
}
64
DEF_WEAK(vsyslog_r);
65
66
/*
67
 * This is used by both syslog_r and syslog.
68
 */
69
void
70
__vsyslog_r(int pri, struct syslog_data *data,
71
    int reentrant, const char *fmt, va_list ap)
72
{
73
	int cnt;
74
	char ch, *p, *t;
75
	int saved_errno;
76
#define	TBUF_SIZE	(LOG_MAXLINE+1)
77
#define	FMT_SIZE	(1024+1)
78
	char *stdp = NULL, tbuf[TBUF_SIZE], fmt_cpy[FMT_SIZE];
79
	int tbuf_left, fmt_left, prlen;
80
81
#define	INTERNALLOG	LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID
82
	/* Check for invalid bits. */
83
	if (pri & ~(LOG_PRIMASK|LOG_FACMASK)) {
84
		syslog_r(INTERNALLOG, data,
85
		    "syslog%s: unknown facility/priority: %x",
86
		    reentrant ? "_r" : "", pri);
87
		pri &= LOG_PRIMASK|LOG_FACMASK;
88
	}
89
90
	/* Check priority against setlogmask values. */
91
	if (!(LOG_MASK(LOG_PRI(pri)) & data->log_mask))
92
		return;
93
94
	saved_errno = errno;
95
96
	/* Set default facility if none specified. */
97
	if ((pri & LOG_FACMASK) == 0)
98
		pri |= data->log_fac;
99
100
	p = tbuf;
101
	tbuf_left = TBUF_SIZE;
102
103
#define	DEC()	\
104
	do {					\
105
		if (prlen < 0)			\
106
			prlen = 0;		\
107
		if (prlen >= tbuf_left)		\
108
			prlen = tbuf_left - 1;	\
109
		p += prlen;			\
110
		tbuf_left -= prlen;		\
111
	} while (0)
112
113
	prlen = snprintf(p, tbuf_left, "<%d>", pri);
114
	DEC();
115
116
	if (data->log_stat & LOG_PERROR)
117
		stdp = p;
118
	if (data->log_tag == NULL)
119
		data->log_tag = __progname;
120
	if (data->log_tag != NULL) {
121
		prlen = snprintf(p, tbuf_left, "%.*s", NAME_MAX, data->log_tag);
122
		DEC();
123
	}
124
	if (data->log_stat & LOG_PID) {
125
		prlen = snprintf(p, tbuf_left, "[%ld]", (long)getpid());
126
		DEC();
127
	}
128
	if (data->log_tag != NULL) {
129
		if (tbuf_left > 1) {
130
			*p++ = ':';
131
			tbuf_left--;
132
		}
133
		if (tbuf_left > 1) {
134
			*p++ = ' ';
135
			tbuf_left--;
136
		}
137
	}
138
139
	for (t = fmt_cpy, fmt_left = FMT_SIZE;
140
	    (ch = *fmt) != '\0' && fmt_left > 1;
141
	    ++fmt) {
142
		if (ch == '%' && fmt[1] == 'm') {
143
			char ebuf[NL_TEXTMAX];
144
145
			++fmt;
146
			(void)strerror_r(saved_errno, ebuf, sizeof(ebuf));
147
			prlen = snprintf(t, fmt_left, "%s", ebuf);
148
			if (prlen < 0)
149
				prlen = 0;
150
			if (prlen >= fmt_left)
151
				prlen = fmt_left - 1;
152
			t += prlen;
153
			fmt_left -= prlen;
154
		} else if (ch == '%' && fmt[1] == '%' && fmt_left > 2) {
155
			++fmt;
156
			*t++ = '%';
157
			*t++ = '%';
158
			fmt_left -= 2;
159
		} else {
160
			*t++ = ch;
161
			fmt_left--;
162
		}
163
	}
164
	*t = '\0';
165
166
	prlen = vsnprintf(p, tbuf_left, fmt_cpy, ap);
167
	DEC();
168
	cnt = p - tbuf;
169
	while (cnt > 0 && p[-1] == '\n') {
170
		*(--p) = '\0';
171
		--cnt;
172
	}
173
174
	/* Output to stderr if requested. */
175
	if (data->log_stat & LOG_PERROR) {
176
		struct iovec iov[2];
177
178
		iov[0].iov_base = stdp;
179
		iov[0].iov_len = cnt > stdp - tbuf ? cnt - (stdp - tbuf) : 0;
180
		iov[1].iov_base = "\n";
181
		iov[1].iov_len = 1;
182
		(void)writev(STDERR_FILENO, iov, 2);
183
	}
184
185
	/*
186
	 * If the sendsyslog() fails, it means that syslogd
187
	 * is not running or the kernel ran out of buffers.
188
	 */
189
	sendsyslog(tbuf, cnt, data->log_stat & LOG_CONS);
190
}
191
192
void
193
openlog_r(const char *ident, int logstat, int logfac, struct syslog_data *data)
194
{
195
	if (ident != NULL)
196
		data->log_tag = ident;
197
	data->log_stat = logstat;
198
	if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0)
199
		data->log_fac = logfac;
200
}
201
DEF_WEAK(openlog_r);
202
203
void
204
closelog_r(struct syslog_data *data)
205
{
206
	data->log_tag = NULL;
207
}
208
DEF_WEAK(closelog_r);