1*6dec6488Schristos /* $NetBSD: xsyslog.c,v 1.7 2020/03/02 15:30:25 christos Exp $ */
2d5c1e04aSchristos
3d5c1e04aSchristos /*
4d5c1e04aSchristos * Copyright (c) 1983, 1988, 1993
5d5c1e04aSchristos * The Regents of the University of California. All rights reserved.
6d5c1e04aSchristos *
7d5c1e04aSchristos * Redistribution and use in source and binary forms, with or without
8d5c1e04aSchristos * modification, are permitted provided that the following conditions
9d5c1e04aSchristos * are met:
10d5c1e04aSchristos * 1. Redistributions of source code must retain the above copyright
11d5c1e04aSchristos * notice, this list of conditions and the following disclaimer.
12d5c1e04aSchristos * 2. Redistributions in binary form must reproduce the above copyright
13d5c1e04aSchristos * notice, this list of conditions and the following disclaimer in the
14d5c1e04aSchristos * documentation and/or other materials provided with the distribution.
15d5c1e04aSchristos * 3. Neither the name of the University nor the names of its contributors
16d5c1e04aSchristos * may be used to endorse or promote products derived from this software
17d5c1e04aSchristos * without specific prior written permission.
18d5c1e04aSchristos *
19d5c1e04aSchristos * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20d5c1e04aSchristos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21d5c1e04aSchristos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22d5c1e04aSchristos * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23d5c1e04aSchristos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24d5c1e04aSchristos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25d5c1e04aSchristos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26d5c1e04aSchristos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27d5c1e04aSchristos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28d5c1e04aSchristos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29d5c1e04aSchristos * SUCH DAMAGE.
30d5c1e04aSchristos */
31d5c1e04aSchristos
32d5c1e04aSchristos #include <sys/cdefs.h>
33d5c1e04aSchristos #if defined(LIBC_SCCS) && !defined(lint)
34d5c1e04aSchristos #if 0
35d5c1e04aSchristos static char sccsid[] = "@(#)syslog.c 8.5 (Berkeley) 4/29/95";
36d5c1e04aSchristos #else
37*6dec6488Schristos __RCSID("$NetBSD: xsyslog.c,v 1.7 2020/03/02 15:30:25 christos Exp $");
38d5c1e04aSchristos #endif
39d5c1e04aSchristos #endif /* LIBC_SCCS and not lint */
40d5c1e04aSchristos
41d5c1e04aSchristos #include "namespace.h"
42d5c1e04aSchristos #include <sys/types.h>
43d5c1e04aSchristos #include <sys/param.h>
44d5c1e04aSchristos #include <sys/socket.h>
45d5c1e04aSchristos #include <sys/syslog.h>
46d5c1e04aSchristos #include <sys/uio.h>
47d5c1e04aSchristos #include <sys/un.h>
48d5c1e04aSchristos
49d5c1e04aSchristos #include <errno.h>
50d5c1e04aSchristos #include <stdio.h>
51d5c1e04aSchristos #include <stdarg.h>
52d5c1e04aSchristos #include <string.h>
53d5c1e04aSchristos #include <fcntl.h>
54d5c1e04aSchristos #include <unistd.h>
55d5c1e04aSchristos #include <stdlib.h>
56d5c1e04aSchristos #include <paths.h>
57d5c1e04aSchristos #include "syslog_private.h"
58d5c1e04aSchristos #include "reentrant.h"
59d5c1e04aSchristos #include "extern.h"
60d5c1e04aSchristos
61af7b41adSchristos static void
disconnectlog_r(struct syslog_data * data)62af7b41adSchristos disconnectlog_r(struct syslog_data *data)
63d5c1e04aSchristos {
64af7b41adSchristos /*
65af7b41adSchristos * If the user closed the FD and opened another in the same slot,
66af7b41adSchristos * that's their problem. They should close it before calling on
67af7b41adSchristos * system services.
68af7b41adSchristos */
69af7b41adSchristos if (data->log_file != -1) {
70af7b41adSchristos (void)close(data->log_file);
71af7b41adSchristos data->log_file = -1;
72af7b41adSchristos }
73af7b41adSchristos data->log_connected = 0; /* retry connect */
74af7b41adSchristos }
75af7b41adSchristos
76af7b41adSchristos static void
connectlog_r(struct syslog_data * data)77af7b41adSchristos connectlog_r(struct syslog_data *data)
78af7b41adSchristos {
79af7b41adSchristos /* AF_UNIX address of local logger */
80af7b41adSchristos static const struct sockaddr_un sun = {
81af7b41adSchristos .sun_family = AF_LOCAL,
82af7b41adSchristos .sun_len = sizeof(sun),
83af7b41adSchristos .sun_path = _PATH_LOG,
84af7b41adSchristos };
85af7b41adSchristos
86af7b41adSchristos if (data->log_file == -1 || fcntl(data->log_file, F_GETFL, 0) == -1) {
87af7b41adSchristos if ((data->log_file = socket(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC,
88af7b41adSchristos 0)) == -1)
89af7b41adSchristos return;
90af7b41adSchristos data->log_connected = 0;
91af7b41adSchristos }
92af7b41adSchristos if (!data->log_connected) {
93af7b41adSchristos if (connect(data->log_file,
94af7b41adSchristos (const struct sockaddr *)(const void *)&sun,
95af7b41adSchristos (socklen_t)sizeof(sun)) == -1) {
96af7b41adSchristos (void)close(data->log_file);
97af7b41adSchristos data->log_file = -1;
98af7b41adSchristos } else
99af7b41adSchristos data->log_connected = 1;
100af7b41adSchristos }
101d5c1e04aSchristos }
102d5c1e04aSchristos
103d5c1e04aSchristos void
_openlog_unlocked_r(const char * ident,int logstat,int logfac,struct syslog_data * data)104af7b41adSchristos _openlog_unlocked_r(const char *ident, int logstat, int logfac,
105af7b41adSchristos struct syslog_data *data)
106d5c1e04aSchristos {
107af7b41adSchristos if (ident != NULL)
108af7b41adSchristos data->log_tag = ident;
109af7b41adSchristos data->log_stat = logstat;
110af7b41adSchristos if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0)
111af7b41adSchristos data->log_fac = logfac;
112af7b41adSchristos
113af7b41adSchristos if (data->log_stat & LOG_NDELAY) /* open immediately */
114af7b41adSchristos connectlog_r(data);
115af7b41adSchristos
116af7b41adSchristos data->log_opened = 1;
117d5c1e04aSchristos }
118d5c1e04aSchristos
119af7b41adSchristos void
_closelog_unlocked_r(struct syslog_data * data)120af7b41adSchristos _closelog_unlocked_r(struct syslog_data *data)
121d5c1e04aSchristos {
122af7b41adSchristos (void)close(data->log_file);
123af7b41adSchristos data->log_file = -1;
124af7b41adSchristos data->log_connected = 0;
125d5c1e04aSchristos }
126d5c1e04aSchristos
127*6dec6488Schristos static __sysloglike(6, 7) void
_xsyslogp_r(int pri,struct syslog_fun * fun,struct syslog_data * data,const char * msgid,const char * sdfmt,const char * msgfmt,...)128d5c1e04aSchristos _xsyslogp_r(int pri, struct syslog_fun *fun,
129d5c1e04aSchristos struct syslog_data *data, const char *msgid,
130d5c1e04aSchristos const char *sdfmt, const char *msgfmt, ...)
131d5c1e04aSchristos {
132d5c1e04aSchristos va_list ap;
133d5c1e04aSchristos va_start(ap, msgfmt);
134d5c1e04aSchristos _vxsyslogp_r(pri, fun, data, msgid, sdfmt, msgfmt, ap);
135d5c1e04aSchristos va_end(ap);
136d5c1e04aSchristos }
137d5c1e04aSchristos
138d5c1e04aSchristos void
_vxsyslogp_r(int pri,struct syslog_fun * fun,struct syslog_data * data,const char * msgid,const char * sdfmt,const char * msgfmt,va_list ap)139d5c1e04aSchristos _vxsyslogp_r(int pri, struct syslog_fun *fun,
140d5c1e04aSchristos struct syslog_data *data, const char *msgid,
141d5c1e04aSchristos const char *sdfmt, const char *msgfmt, va_list ap)
142d5c1e04aSchristos {
143d5c1e04aSchristos static const char BRCOSP[] = "]: ";
144d5c1e04aSchristos static const char CRLF[] = "\r\n";
145d5c1e04aSchristos size_t cnt, prlen, tries;
146d5c1e04aSchristos char ch, *p, *t;
147d5c1e04aSchristos int fd, saved_errno;
148d5c1e04aSchristos #define TBUF_LEN 2048
149d5c1e04aSchristos #define FMT_LEN 1024
150d5c1e04aSchristos #define MAXTRIES 10
151d5c1e04aSchristos char tbuf[TBUF_LEN], fmt_cpy[FMT_LEN], fmt_cat[FMT_LEN] = "";
152d5c1e04aSchristos size_t tbuf_left, fmt_left, msgsdlen;
153d5c1e04aSchristos char *fmt = fmt_cat;
154d5c1e04aSchristos struct iovec iov[7]; /* prog + [ + pid + ]: + fmt + crlf */
155d5c1e04aSchristos int opened, iovcnt;
156d5c1e04aSchristos
15726ea30aeSmaya iovcnt = opened = 0;
158cbf276ffSkre
159d5c1e04aSchristos #define INTERNALLOG LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID
160d5c1e04aSchristos /* Check for invalid bits. */
161d5c1e04aSchristos if (pri & ~(LOG_PRIMASK|LOG_FACMASK)) {
162d5c1e04aSchristos _xsyslogp_r(INTERNALLOG, &_syslog_ss_fun, data, NULL, NULL,
163c298ef6fSchristos "Unknown facility/priority: %#x", pri);
164d5c1e04aSchristos pri &= LOG_PRIMASK|LOG_FACMASK;
165d5c1e04aSchristos }
166d5c1e04aSchristos
167d5c1e04aSchristos /* Check priority against setlogmask values. */
168d5c1e04aSchristos if (!(LOG_MASK(LOG_PRI(pri)) & data->log_mask))
169d5c1e04aSchristos return;
170d5c1e04aSchristos
171d5c1e04aSchristos saved_errno = errno;
172d5c1e04aSchristos
173d5c1e04aSchristos /* Set default facility if none specified. */
174d5c1e04aSchristos if ((pri & LOG_FACMASK) == 0)
175d5c1e04aSchristos pri |= data->log_fac;
176d5c1e04aSchristos
177d5c1e04aSchristos /* Build the message. */
178d5c1e04aSchristos p = tbuf;
179d5c1e04aSchristos tbuf_left = TBUF_LEN;
180d5c1e04aSchristos
181d5c1e04aSchristos prlen = snprintf_ss(p, tbuf_left, "<%d>1 ", pri);
182d5c1e04aSchristos DEC();
183d5c1e04aSchristos
184d5c1e04aSchristos prlen = (*fun->timefun)(p, tbuf_left);
185d5c1e04aSchristos
186af7b41adSchristos (*fun->lock)(data);
187d5c1e04aSchristos
188d5c1e04aSchristos if (data->log_hostname[0] == '\0' && gethostname(data->log_hostname,
189d5c1e04aSchristos sizeof(data->log_hostname)) == -1) {
190d5c1e04aSchristos /* can this really happen? */
191d5c1e04aSchristos data->log_hostname[0] = '-';
192d5c1e04aSchristos data->log_hostname[1] = '\0';
193d5c1e04aSchristos }
194d5c1e04aSchristos
195d5c1e04aSchristos DEC();
196d5c1e04aSchristos prlen = snprintf_ss(p, tbuf_left, " %s ", data->log_hostname);
197d5c1e04aSchristos
198d5c1e04aSchristos if (data->log_tag == NULL)
199d5c1e04aSchristos data->log_tag = getprogname();
200d5c1e04aSchristos
201d5c1e04aSchristos DEC();
202d5c1e04aSchristos prlen = snprintf_ss(p, tbuf_left, "%s ",
203d5c1e04aSchristos data->log_tag ? data->log_tag : "-");
204d5c1e04aSchristos
205af7b41adSchristos (*fun->unlock)(data);
206d5c1e04aSchristos
207d5c1e04aSchristos if (data->log_stat & (LOG_PERROR|LOG_CONS)) {
208d5c1e04aSchristos iov[iovcnt].iov_base = p;
209d5c1e04aSchristos iov[iovcnt].iov_len = prlen - 1;
210d5c1e04aSchristos iovcnt++;
211d5c1e04aSchristos }
212d5c1e04aSchristos DEC();
213d5c1e04aSchristos
214d5c1e04aSchristos if (data->log_stat & LOG_PID) {
215d5c1e04aSchristos prlen = snprintf_ss(p, tbuf_left, "%d ", getpid());
216d5c1e04aSchristos if (data->log_stat & (LOG_PERROR|LOG_CONS)) {
217d5c1e04aSchristos iov[iovcnt].iov_base = __UNCONST("[");
218d5c1e04aSchristos iov[iovcnt].iov_len = 1;
219d5c1e04aSchristos iovcnt++;
220d5c1e04aSchristos iov[iovcnt].iov_base = p;
221d5c1e04aSchristos iov[iovcnt].iov_len = prlen - 1;
222d5c1e04aSchristos iovcnt++;
223d5c1e04aSchristos iov[iovcnt].iov_base = __UNCONST(BRCOSP);
224d5c1e04aSchristos iov[iovcnt].iov_len = 3;
225d5c1e04aSchristos iovcnt++;
226d5c1e04aSchristos }
227d5c1e04aSchristos } else {
228d5c1e04aSchristos prlen = snprintf_ss(p, tbuf_left, "- ");
229d5c1e04aSchristos if (data->log_stat & (LOG_PERROR|LOG_CONS)) {
230d5c1e04aSchristos iov[iovcnt].iov_base = __UNCONST(BRCOSP + 1);
231d5c1e04aSchristos iov[iovcnt].iov_len = 2;
232d5c1e04aSchristos iovcnt++;
233d5c1e04aSchristos }
234d5c1e04aSchristos }
235d5c1e04aSchristos DEC();
236d5c1e04aSchristos
237d5c1e04aSchristos /*
238d5c1e04aSchristos * concat the format strings, then use one vsnprintf()
239d5c1e04aSchristos */
240d5c1e04aSchristos if (msgid != NULL && *msgid != '\0') {
241d5c1e04aSchristos strlcat(fmt_cat, msgid, FMT_LEN);
242d5c1e04aSchristos strlcat(fmt_cat, " ", FMT_LEN);
243d5c1e04aSchristos } else
244d5c1e04aSchristos strlcat(fmt_cat, "- ", FMT_LEN);
245d5c1e04aSchristos
246d5c1e04aSchristos if (sdfmt != NULL && *sdfmt != '\0') {
247d5c1e04aSchristos strlcat(fmt_cat, sdfmt, FMT_LEN);
248d5c1e04aSchristos } else
249d5c1e04aSchristos strlcat(fmt_cat, "-", FMT_LEN);
250d5c1e04aSchristos
251d5c1e04aSchristos if (data->log_stat & (LOG_PERROR|LOG_CONS))
252d5c1e04aSchristos msgsdlen = strlen(fmt_cat) + 1;
253d5c1e04aSchristos else
254d5c1e04aSchristos msgsdlen = 0; /* XXX: GCC */
255d5c1e04aSchristos
256d5c1e04aSchristos if (msgfmt != NULL && *msgfmt != '\0') {
257d5c1e04aSchristos strlcat(fmt_cat, " ", FMT_LEN);
258d5c1e04aSchristos strlcat(fmt_cat, msgfmt, FMT_LEN);
259d5c1e04aSchristos }
260d5c1e04aSchristos
261d5c1e04aSchristos /*
262d5c1e04aSchristos * We wouldn't need this mess if printf handled %m, or if
263d5c1e04aSchristos * strerror() had been invented before syslog().
264d5c1e04aSchristos */
265d5c1e04aSchristos for (t = fmt_cpy, fmt_left = FMT_LEN; (ch = *fmt) != '\0'; ++fmt) {
266d5c1e04aSchristos if (ch == '%' && fmt[1] == 'm') {
267d5c1e04aSchristos char buf[256];
268d5c1e04aSchristos
269d5c1e04aSchristos if ((*fun->errfun)(saved_errno, buf, sizeof(buf)) != 0)
270d5c1e04aSchristos prlen = snprintf_ss(t, fmt_left, "Error %d",
271d5c1e04aSchristos saved_errno);
272d5c1e04aSchristos else
273d5c1e04aSchristos prlen = strlcpy(t, buf, fmt_left);
274d5c1e04aSchristos if (prlen >= fmt_left)
275d5c1e04aSchristos prlen = fmt_left - 1;
276d5c1e04aSchristos t += prlen;
277d5c1e04aSchristos fmt++;
278d5c1e04aSchristos fmt_left -= prlen;
279d5c1e04aSchristos } else if (ch == '%' && fmt[1] == '%' && fmt_left > 2) {
280d5c1e04aSchristos *t++ = '%';
281d5c1e04aSchristos *t++ = '%';
282d5c1e04aSchristos fmt++;
283d5c1e04aSchristos fmt_left -= 2;
284d5c1e04aSchristos } else {
285d5c1e04aSchristos if (fmt_left > 1) {
286d5c1e04aSchristos *t++ = ch;
287d5c1e04aSchristos fmt_left--;
288d5c1e04aSchristos }
289d5c1e04aSchristos }
290d5c1e04aSchristos }
291d5c1e04aSchristos *t = '\0';
292d5c1e04aSchristos
293d5c1e04aSchristos prlen = (*fun->prfun)(p, tbuf_left, fmt_cpy, ap);
294d5c1e04aSchristos if (data->log_stat & (LOG_PERROR|LOG_CONS)) {
295d5c1e04aSchristos iov[iovcnt].iov_base = p + msgsdlen;
296d5c1e04aSchristos iov[iovcnt].iov_len = prlen - msgsdlen;
297d5c1e04aSchristos iovcnt++;
298d5c1e04aSchristos }
299d5c1e04aSchristos
300d5c1e04aSchristos DEC();
301d5c1e04aSchristos cnt = p - tbuf;
302d5c1e04aSchristos
303d5c1e04aSchristos /* Output to stderr if requested. */
304d5c1e04aSchristos if (data->log_stat & LOG_PERROR) {
305fe63b706Sroy struct iovec *piov;
306fe63b706Sroy int piovcnt;
307fe63b706Sroy
308d5c1e04aSchristos iov[iovcnt].iov_base = __UNCONST(CRLF + 1);
309d5c1e04aSchristos iov[iovcnt].iov_len = 1;
310fe63b706Sroy if (data->log_stat & LOG_PTRIM) {
311fe63b706Sroy piov = &iov[iovcnt - 1];
312fe63b706Sroy piovcnt = 2;
313fe63b706Sroy } else {
314fe63b706Sroy piov = iov;
315fe63b706Sroy piovcnt = iovcnt + 1;
316d5c1e04aSchristos }
317fe63b706Sroy (void)writev(STDERR_FILENO, piov, piovcnt);
318fe63b706Sroy }
319fe63b706Sroy
320fe63b706Sroy if (data->log_stat & LOG_NLOG)
321fe63b706Sroy goto out;
322d5c1e04aSchristos
323d5c1e04aSchristos /* Get connected, output the message to the local logger. */
324af7b41adSchristos (*fun->lock)(data);
325d5c1e04aSchristos opened = !data->log_opened;
326d5c1e04aSchristos if (opened)
327af7b41adSchristos _openlog_unlocked_r(data->log_tag, data->log_stat, 0, data);
328d5c1e04aSchristos connectlog_r(data);
329d5c1e04aSchristos
330d5c1e04aSchristos /*
331d5c1e04aSchristos * If the send() failed, there are two likely scenarios:
332d5c1e04aSchristos * 1) syslogd was restarted
333d5c1e04aSchristos * 2) /dev/log is out of socket buffer space
334d5c1e04aSchristos * We attempt to reconnect to /dev/log to take care of
335d5c1e04aSchristos * case #1 and keep send()ing data to cover case #2
336d5c1e04aSchristos * to give syslogd a chance to empty its socket buffer.
337d5c1e04aSchristos */
338d5c1e04aSchristos for (tries = 0; tries < MAXTRIES; tries++) {
339d5c1e04aSchristos if (send(data->log_file, tbuf, cnt, 0) != -1)
340d5c1e04aSchristos break;
341d5c1e04aSchristos if (errno != ENOBUFS) {
342d5c1e04aSchristos disconnectlog_r(data);
343d5c1e04aSchristos connectlog_r(data);
344d5c1e04aSchristos } else
345d5c1e04aSchristos (void)usleep(1);
346d5c1e04aSchristos }
347d5c1e04aSchristos
348d5c1e04aSchristos /*
349d5c1e04aSchristos * Output the message to the console; try not to block
350d5c1e04aSchristos * as a blocking console should not stop other processes.
351d5c1e04aSchristos * Make sure the error reported is the one from the syslogd failure.
352d5c1e04aSchristos */
353d5c1e04aSchristos if (tries == MAXTRIES && (data->log_stat & LOG_CONS) &&
354d5c1e04aSchristos (fd = open(_PATH_CONSOLE,
355d5c1e04aSchristos O_WRONLY | O_NONBLOCK | O_CLOEXEC, 0)) >= 0) {
356d5c1e04aSchristos iov[iovcnt].iov_base = __UNCONST(CRLF);
357d5c1e04aSchristos iov[iovcnt].iov_len = 2;
358d5c1e04aSchristos (void)writev(fd, iov, iovcnt + 1);
359d5c1e04aSchristos (void)close(fd);
360d5c1e04aSchristos }
361d5c1e04aSchristos
362fe63b706Sroy out:
363af7b41adSchristos if (!(*fun->unlock)(data) && opened)
364af7b41adSchristos _closelog_unlocked_r(data);
365d5c1e04aSchristos }
366d5c1e04aSchristos
367d5c1e04aSchristos int
setlogmask_r(int pmask,struct syslog_data * data)368d5c1e04aSchristos setlogmask_r(int pmask, struct syslog_data *data)
369d5c1e04aSchristos {
370d5c1e04aSchristos int omask;
371d5c1e04aSchristos
372d5c1e04aSchristos omask = data->log_mask;
373d5c1e04aSchristos if (pmask != 0)
374d5c1e04aSchristos data->log_mask = pmask;
375d5c1e04aSchristos return omask;
376d5c1e04aSchristos }
377