blob: f1b848f0bb522727caeefb82ca02f6d910e00fb7 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/*
2 * Copyright (c) 1983, 1988, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34/*
35 * SYSLOG -- print message on log file
36 *
37 * This routine looks a lot like printf, except that it outputs to the
38 * log file instead of the standard output. Also:
39 * adds a timestamp,
40 * prints the module name in front of the message,
41 * has some other formatting types (or will sometime),
42 * adds a newline on the end of the message.
43 *
44 * The output of this routine is intended to be read by syslogd(8).
45 *
46 * Author: Eric Allman
47 * Modified to use UNIX domain IPC by Ralph Campbell
48 * Patched March 12, 1996 by A. Ian Vogelesang <vogelesang@hdshq.com>
49 * - to correct the handling of message & format string truncation,
50 * - to visibly tag truncated records to facilitate
51 * investigation of such Bad Things with grep, and,
52 * - to correct the handling of case where "write"
53 * returns after writing only part of the message.
54 * Rewritten by Martin Mares <mj@atrey.karlin.mff.cuni.cz> on May 14, 1997
55 * - better buffer overrun checks.
56 * - special handling of "%m" removed as we use GNU sprintf which handles
57 * it automatically.
58 * - Major code cleanup.
59 */
60
61#define __FORCE_GLIBC
62#include <features.h>
63#include <sys/types.h>
64#include <sys/socket.h>
65#include <sys/file.h>
66#include <sys/signal.h>
67#include <sys/syslog.h>
68
69#include <sys/uio.h>
70#include <sys/wait.h>
71#include <netdb.h>
72#include <string.h>
73#include <time.h>
74#include <unistd.h>
75#include <errno.h>
76#include <stdarg.h>
77#include <paths.h>
78#include <stdio.h>
79#include <ctype.h>
80#include <signal.h>
81
82
83#include <bits/uClibc_mutex.h>
84
85__UCLIBC_MUTEX_STATIC(mylock, PTHREAD_MUTEX_INITIALIZER);
86
87
88/* !glibc_compat: glibc uses argv[0] by default
89 * (default: if there was no openlog or if openlog passed NULL),
90 * not string "syslog"
91 */
92static const char *LogTag = "syslog"; /* string to tag the entry with */
93static int LogFile = -1; /* fd for log */
94static smalluint connected; /* have done connect */
95/* all bits in option argument for openlog fit in 8 bits */
96static smalluint LogStat = 0; /* status bits, set by openlog */
97/* default facility code if openlog is not called */
98/* (this fits in 8 bits even without >> 3 shift, but playing extra safe) */
99static smalluint LogFacility = LOG_USER >> 3;
100/* bits mask of priorities to be logged (eight prios - 8 bits is enough) */
101static smalluint LogMask = 0xff;
102/* AF_UNIX address of local logger (we use struct sockaddr
103 * instead of struct sockaddr_un since "/dev/log" is small enough) */
104static const struct sockaddr SyslogAddr = {
105 .sa_family = AF_UNIX, /* sa_family_t (usually a short) */
106 .sa_data = _PATH_LOG /* char [14] */
107};
108
109static void
110closelog_intern(int sig)
111{
112 /* mylock must be held by the caller */
113 if (LogFile != -1) {
114 (void) close(LogFile);
115 }
116 LogFile = -1;
117 connected = 0;
118 if (sig == 0) { /* called from closelog()? - reset to defaults */
119 LogStat = 0;
120 LogTag = "syslog";
121 LogFacility = LOG_USER >> 3;
122 LogMask = 0xff;
123 }
124}
125
126static void
127openlog_intern(const char *ident, int logstat, int logfac)
128{
129 int fd;
130 int logType = SOCK_DGRAM;
131
132 if (ident != NULL)
133 LogTag = ident;
134 LogStat = logstat;
135 /* (we were checking also for logfac != 0, but it breaks
136 * openlog(xx, LOG_KERN) since LOG_KERN == 0) */
137 if ((logfac & ~LOG_FACMASK) == 0) /* if we don't have invalid bits */
138 LogFacility = (unsigned)logfac >> 3;
139
140 fd = LogFile;
141 if (fd == -1) {
142 retry:
143 if (logstat & LOG_NDELAY) {
144 LogFile = fd = socket(AF_UNIX, logType, 0);
145 if (fd == -1) {
146 return;
147 }
148 fcntl(fd, F_SETFD, FD_CLOEXEC);
149 /* We don't want to block if e.g. syslogd is SIGSTOPed */
150 fcntl(fd, F_SETFL, O_NONBLOCK | fcntl(fd, F_GETFL));
151 }
152 }
153
154 if (fd != -1 && !connected) {
155 if (connect(fd, &SyslogAddr, sizeof(SyslogAddr)) != -1) {
156 connected = 1;
157 } else {
158 if (fd != -1) {
159 close(fd);
160 LogFile = fd = -1;
161 }
162 if (logType == SOCK_DGRAM) {
163 logType = SOCK_STREAM;
164 goto retry;
165 }
166 }
167 }
168}
169
170/*
171 * OPENLOG -- open system log
172 */
173void
174openlog(const char *ident, int logstat, int logfac)
175{
176 __UCLIBC_MUTEX_LOCK(mylock);
177 openlog_intern(ident, logstat, logfac);
178 __UCLIBC_MUTEX_UNLOCK(mylock);
179}
180libc_hidden_def(openlog)
181
182/*
183 * syslog, vsyslog --
184 * print message on log file; output is intended for syslogd(8).
185 */
186void
187vsyslog(int pri, const char *fmt, va_list ap)
188{
189 register char *p;
190 char *last_chr, *head_end, *end, *stdp;
191 time_t now;
192 int fd, saved_errno;
193 int rc;
194 char tbuf[1024]; /* syslogd is unable to handle longer messages */
195
196 /* Just throw out this message if pri has bad bits. */
197 if ((pri & ~(LOG_PRIMASK|LOG_FACMASK)) != 0)
198 return;
199
200 saved_errno = errno;
201
202 __UCLIBC_MUTEX_LOCK(mylock);
203
204 /* See if we should just throw out this message according to LogMask. */
205 if ((LogMask & LOG_MASK(LOG_PRI(pri))) == 0)
206 goto getout;
207 if (LogFile < 0 || !connected)
208 openlog_intern(NULL, LogStat | LOG_NDELAY, (int)LogFacility << 3);
209
210 /* Set default facility if none specified. */
211 if ((pri & LOG_FACMASK) == 0)
212 pri |= ((int)LogFacility << 3);
213
214 /* Build the message. We know the starting part of the message can take
215 * no longer than 64 characters plus length of the LogTag. So it's
216 * safe to test only LogTag and use normal sprintf everywhere else.
217 */
218 (void)time(&now);
219 stdp = p = tbuf + sprintf(tbuf, "<%d>%.15s ", pri, ctime(&now) + 4);
220 /*if (LogTag) - always true */ {
221 if (strlen(LogTag) < sizeof(tbuf) - 64)
222 p += sprintf(p, "%s", LogTag);
223 else
224 p += sprintf(p, "<BUFFER OVERRUN ATTEMPT>");
225 }
226 if (LogStat & LOG_PID)
227 p += sprintf(p, "[%d]", getpid());
228 /*if (LogTag) - always true */ {
229 *p++ = ':';
230 *p++ = ' ';
231 }
232 head_end = p;
233
234 /* We format the rest of the message. If the buffer becomes full, we mark
235 * the message as truncated. Note that we require at least 2 free bytes
236 * in the buffer as we might want to add "\r\n" there.
237 */
238
239 end = tbuf + sizeof(tbuf) - 1;
240 __set_errno(saved_errno);
241 p += vsnprintf(p, end - p, fmt, ap);
242 if (p >= end || p < head_end) { /* Returned -1 in case of error... */
243 static const char truncate_msg[12] = "[truncated] "; /* no NUL! */
244 memmove(head_end + sizeof(truncate_msg), head_end,
245 end - head_end - sizeof(truncate_msg));
246 memcpy(head_end, truncate_msg, sizeof(truncate_msg));
247 if (p < head_end) {
248 while (p < end && *p) {
249 p++;
250 }
251 }
252 else {
253 p = end - 1;
254 }
255
256 }
257 last_chr = p;
258
259 /* Output to stderr if requested. */
260 if (LogStat & LOG_PERROR) {
261 *last_chr = '\n';
262 (void)write(STDERR_FILENO, stdp, last_chr - stdp + 1);
263 }
264
265 /* Output the message to the local logger using NUL as a message delimiter. */
266 p = tbuf;
267 *last_chr = '\0';
268 if (LogFile >= 0) {
269 do {
270 /* can't just use write, it can result in SIGPIPE */
271 rc = send(LogFile, p, last_chr + 1 - p, MSG_NOSIGNAL);
272 if (rc < 0) {
273 /* I don't think looping forever on EAGAIN is a good idea.
274 * Imagine that syslogd is SIGSTOPed... */
275 if (/* (errno != EAGAIN) && */ (errno != EINTR)) {
276 closelog_intern(1); /* 1: do not reset LogXXX globals to default */
277 goto write_err;
278 }
279 rc = 0;
280 }
281 p += rc;
282 } while (p <= last_chr);
283 goto getout;
284 }
285
286 write_err:
287 /*
288 * Output the message to the console; don't worry about blocking,
289 * if console blocks everything will. Make sure the error reported
290 * is the one from the syslogd failure.
291 */
292 /* should mode be O_WRONLY | O_NOCTTY? -- Uli */
293 /* yes, but in Linux "/dev/console" never becomes ctty anyway -- vda */
294 if ((LogStat & LOG_CONS) &&
295 (fd = open(_PATH_CONSOLE, O_WRONLY | O_NOCTTY)) >= 0) {
296 p = strchr(tbuf, '>') + 1;
297 last_chr[0] = '\r';
298 last_chr[1] = '\n';
299 (void)write(fd, p, last_chr - p + 2);
300 (void)close(fd);
301 }
302
303 getout:
304 __UCLIBC_MUTEX_UNLOCK(mylock);
305}
306libc_hidden_def(vsyslog)
307
308void
309syslog(int pri, const char *fmt, ...)
310{
311 va_list ap;
312
313 va_start(ap, fmt);
314 vsyslog(pri, fmt, ap);
315 va_end(ap);
316}
317libc_hidden_def(syslog)
318
319/*
320 * CLOSELOG -- close the system log
321 */
322void
323closelog(void)
324{
325 __UCLIBC_MUTEX_LOCK(mylock);
326 closelog_intern(0); /* 0: reset LogXXX globals to default */
327 __UCLIBC_MUTEX_UNLOCK(mylock);
328}
329libc_hidden_def(closelog)
330
331/* setlogmask -- set the log mask level */
332int setlogmask(int pmask)
333{
334 int omask;
335
336 omask = LogMask;
337 if (pmask != 0) {
338 __UCLIBC_MUTEX_LOCK(mylock);
339 LogMask = pmask;
340 __UCLIBC_MUTEX_UNLOCK(mylock);
341 }
342 return omask;
343}