]>
Commit | Line | Data |
---|---|---|
790aa1f2 UD |
1 | /* |
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 | * 4. Neither the name of the University nor the names of its contributors | |
14 | * may be used to endorse or promote products derived from this software | |
15 | * without specific prior written permission. | |
16 | * | |
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
27 | * SUCH DAMAGE. | |
28 | */ | |
29 | ||
30 | #if defined(LIBC_SCCS) && !defined(lint) | |
31 | static char sccsid[] = "@(#)syslog.c 8.4 (Berkeley) 3/18/94"; | |
32 | #endif /* LIBC_SCCS and not lint */ | |
33 | ||
34 | #include <sys/types.h> | |
35 | #include <sys/socket.h> | |
36 | #include <sys/syslog.h> | |
37 | #include <sys/uio.h> | |
7ab7969c | 38 | #include <sys/un.h> |
790aa1f2 UD |
39 | #include <netdb.h> |
40 | ||
41 | #include <errno.h> | |
42 | #include <fcntl.h> | |
43 | #include <paths.h> | |
44 | #include <stdio.h> | |
45 | #include <stdio_ext.h> | |
46 | #include <string.h> | |
47 | #include <time.h> | |
48 | #include <unistd.h> | |
49 | #include <stdlib.h> | |
ec999b8e | 50 | #include <libc-lock.h> |
790aa1f2 UD |
51 | #include <signal.h> |
52 | #include <locale.h> | |
53 | ||
790aa1f2 | 54 | #include <stdarg.h> |
790aa1f2 | 55 | |
124fc732 | 56 | #include <libio/libioP.h> |
c6251f03 RM |
57 | #include <math_ldbl_opt.h> |
58 | ||
37233df9 TS |
59 | #include <kernel-features.h> |
60 | ||
d18ea0c5 | 61 | #define ftell(s) _IO_ftell (s) |
790aa1f2 UD |
62 | |
63 | static int LogType = SOCK_DGRAM; /* type of socket connection */ | |
64 | static int LogFile = -1; /* fd for log */ | |
7cb10381 | 65 | static bool connected; /* have done connect */ |
790aa1f2 UD |
66 | static int LogStat; /* status bits, set by openlog() */ |
67 | static const char *LogTag; /* string to tag the entry with */ | |
68 | static int LogFacility = LOG_USER; /* default facility code */ | |
69 | static int LogMask = 0xff; /* mask of priorities to be logged */ | |
70 | extern char *__progname; /* Program name, from crt0. */ | |
71 | ||
72 | /* Define the lock. */ | |
73 | __libc_lock_define_initialized (static, syslog_lock) | |
74 | ||
ab5bf7cf | 75 | static void openlog_internal(const char *, int, int); |
790aa1f2 | 76 | static void closelog_internal(void); |
790aa1f2 UD |
77 | |
78 | struct cleanup_arg | |
79 | { | |
80 | void *buf; | |
81 | struct sigaction *oldaction; | |
82 | }; | |
83 | ||
84 | static void | |
85 | cancel_handler (void *ptr) | |
86 | { | |
790aa1f2 UD |
87 | /* Restore the old signal handler. */ |
88 | struct cleanup_arg *clarg = (struct cleanup_arg *) ptr; | |
89 | ||
c4e4b2e1 | 90 | if (clarg != NULL) |
ded3cef3 AZ |
91 | /* Free the memstream buffer, */ |
92 | free (clarg->buf); | |
c4e4b2e1 | 93 | |
790aa1f2 UD |
94 | /* Free the lock. */ |
95 | __libc_lock_unlock (syslog_lock); | |
96 | } | |
97 | ||
98 | ||
99 | /* | |
100 | * syslog, vsyslog -- | |
101 | * print message on log file; output is intended for syslogd(8). | |
102 | */ | |
103 | void | |
c6251f03 | 104 | __syslog(int pri, const char *fmt, ...) |
790aa1f2 UD |
105 | { |
106 | va_list ap; | |
107 | ||
790aa1f2 | 108 | va_start(ap, fmt); |
124fc732 | 109 | __vsyslog_internal(pri, fmt, ap, 0); |
790aa1f2 UD |
110 | va_end(ap); |
111 | } | |
c6251f03 RM |
112 | ldbl_hidden_def (__syslog, syslog) |
113 | ldbl_strong_alias (__syslog, syslog) | |
790aa1f2 | 114 | |
124fc732 ZW |
115 | void |
116 | __vsyslog(int pri, const char *fmt, va_list ap) | |
117 | { | |
118 | __vsyslog_internal(pri, fmt, ap, 0); | |
119 | } | |
120 | ldbl_weak_alias (__vsyslog, vsyslog) | |
121 | ||
790aa1f2 | 122 | void |
29c21e49 UD |
123 | __syslog_chk(int pri, int flag, const char *fmt, ...) |
124 | { | |
790aa1f2 | 125 | va_list ap; |
29c21e49 UD |
126 | |
127 | va_start(ap, fmt); | |
124fc732 | 128 | __vsyslog_internal(pri, fmt, ap, (flag > 0) ? PRINTF_FORTIFY : 0); |
29c21e49 UD |
129 | va_end(ap); |
130 | } | |
131 | ||
132 | void | |
133 | __vsyslog_chk(int pri, int flag, const char *fmt, va_list ap) | |
124fc732 ZW |
134 | { |
135 | __vsyslog_internal(pri, fmt, ap, (flag > 0) ? PRINTF_FORTIFY : 0); | |
136 | } | |
137 | ||
138 | void | |
139 | __vsyslog_internal(int pri, const char *fmt, va_list ap, | |
140 | unsigned int mode_flags) | |
790aa1f2 UD |
141 | { |
142 | struct tm now_tm; | |
143 | time_t now; | |
144 | int fd; | |
145 | FILE *f; | |
146 | char *buf = 0; | |
147 | size_t bufsize = 0; | |
5615eaf2 | 148 | size_t msgoff; |
790aa1f2 UD |
149 | int saved_errno = errno; |
150 | char failbuf[3 * sizeof (pid_t) + sizeof "out of memory []"]; | |
151 | ||
152 | #define INTERNALLOG LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID | |
153 | /* Check for invalid bits. */ | |
154 | if (pri & ~(LOG_PRIMASK|LOG_FACMASK)) { | |
155 | syslog(INTERNALLOG, | |
156 | "syslog: unknown facility/priority: %x", pri); | |
157 | pri &= LOG_PRIMASK|LOG_FACMASK; | |
158 | } | |
159 | ||
c4e4b2e1 AS |
160 | /* Prepare for multiple users. We have to take care: most |
161 | syscalls we are using are cancellation points. */ | |
162 | struct cleanup_arg clarg; | |
163 | clarg.buf = NULL; | |
164 | clarg.oldaction = NULL; | |
165 | __libc_cleanup_push (cancel_handler, &clarg); | |
166 | __libc_lock_lock (syslog_lock); | |
167 | ||
790aa1f2 UD |
168 | /* Check priority against setlogmask values. */ |
169 | if ((LOG_MASK (LOG_PRI (pri)) & LogMask) == 0) | |
c4e4b2e1 | 170 | goto out; |
790aa1f2 UD |
171 | |
172 | /* Set default facility if none specified. */ | |
173 | if ((pri & LOG_FACMASK) == 0) | |
174 | pri |= LogFacility; | |
175 | ||
176 | /* Build the message in a memory-buffer stream. */ | |
711f67a7 | 177 | f = __open_memstream (&buf, &bufsize); |
790aa1f2 UD |
178 | if (f == NULL) |
179 | { | |
180 | /* We cannot get a stream. There is not much we can do but | |
181 | emitting an error messages. */ | |
182 | char numbuf[3 * sizeof (pid_t)]; | |
183 | char *nump; | |
184 | char *endp = __stpcpy (failbuf, "out of memory ["); | |
185 | pid_t pid = __getpid (); | |
186 | ||
187 | nump = numbuf + sizeof (numbuf); | |
188 | /* The PID can never be zero. */ | |
189 | do | |
190 | *--nump = '0' + pid % 10; | |
191 | while ((pid /= 10) != 0); | |
192 | ||
193 | endp = __mempcpy (endp, nump, (numbuf + sizeof (numbuf)) - nump); | |
194 | *endp++ = ']'; | |
195 | *endp = '\0'; | |
196 | buf = failbuf; | |
197 | bufsize = endp - failbuf; | |
198 | msgoff = 0; | |
199 | } | |
200 | else | |
201 | { | |
202 | __fsetlocking (f, FSETLOCKING_BYCALLER); | |
5615eaf2 | 203 | fprintf (f, "<%d>", pri); |
f9a75540 | 204 | now = time_now (); |
790aa1f2 UD |
205 | f->_IO_write_ptr += __strftime_l (f->_IO_write_ptr, |
206 | f->_IO_write_end | |
207 | - f->_IO_write_ptr, | |
208 | "%h %e %T ", | |
209 | __localtime_r (&now, &now_tm), | |
4b5b009c | 210 | _nl_C_locobj_ptr); |
790aa1f2 UD |
211 | msgoff = ftell (f); |
212 | if (LogTag == NULL) | |
213 | LogTag = __progname; | |
214 | if (LogTag != NULL) | |
3164bf09 | 215 | __fputs_unlocked (LogTag, f); |
790aa1f2 UD |
216 | if (LogStat & LOG_PID) |
217 | fprintf (f, "[%d]", (int) __getpid ()); | |
218 | if (LogTag != NULL) | |
219 | { | |
039c721a JM |
220 | __putc_unlocked (':', f); |
221 | __putc_unlocked (' ', f); | |
790aa1f2 UD |
222 | } |
223 | ||
224 | /* Restore errno for %m format. */ | |
225 | __set_errno (saved_errno); | |
226 | ||
227 | /* We have the header. Print the user's format into the | |
124fc732 ZW |
228 | buffer. */ |
229 | __vfprintf_internal (f, fmt, ap, mode_flags); | |
790aa1f2 UD |
230 | |
231 | /* Close the memory stream; this will finalize the data | |
232 | into a malloc'd buffer in BUF. */ | |
233 | fclose (f); | |
c4e4b2e1 AS |
234 | |
235 | /* Tell the cancellation handler to free this buffer. */ | |
236 | clarg.buf = buf; | |
790aa1f2 UD |
237 | } |
238 | ||
239 | /* Output to stderr if requested. */ | |
240 | if (LogStat & LOG_PERROR) { | |
241 | struct iovec iov[2]; | |
2e09a79a | 242 | struct iovec *v = iov; |
790aa1f2 UD |
243 | |
244 | v->iov_base = buf + msgoff; | |
245 | v->iov_len = bufsize - msgoff; | |
246 | /* Append a newline if necessary. */ | |
247 | if (buf[bufsize - 1] != '\n') | |
248 | { | |
249 | ++v; | |
250 | v->iov_base = (char *) "\n"; | |
251 | v->iov_len = 1; | |
252 | } | |
253 | ||
790aa1f2 UD |
254 | /* writev is a cancellation point. */ |
255 | (void)__writev(STDERR_FILENO, iov, v - iov + 1); | |
790aa1f2 UD |
256 | } |
257 | ||
790aa1f2 UD |
258 | /* Get connected, output the message to the local logger. */ |
259 | if (!connected) | |
f2913118 | 260 | openlog_internal(NULL, LogStat | LOG_NDELAY, LogFacility); |
790aa1f2 UD |
261 | |
262 | /* If we have a SOCK_STREAM connection, also send ASCII NUL as | |
263 | a record terminator. */ | |
264 | if (LogType == SOCK_STREAM) | |
265 | ++bufsize; | |
266 | ||
ded3cef3 | 267 | if (!connected || __send(LogFile, buf, bufsize, MSG_NOSIGNAL) < 0) |
790aa1f2 UD |
268 | { |
269 | if (connected) | |
270 | { | |
271 | /* Try to reopen the syslog connection. Maybe it went | |
272 | down. */ | |
273 | closelog_internal (); | |
f2913118 | 274 | openlog_internal(NULL, LogStat | LOG_NDELAY, LogFacility); |
790aa1f2 UD |
275 | } |
276 | ||
ded3cef3 | 277 | if (!connected || __send(LogFile, buf, bufsize, MSG_NOSIGNAL) < 0) |
790aa1f2 UD |
278 | { |
279 | closelog_internal (); /* attempt re-open next time */ | |
280 | /* | |
281 | * Output the message to the console; don't worry | |
282 | * about blocking, if console blocks everything will. | |
283 | * Make sure the error reported is the one from the | |
284 | * syslogd failure. | |
285 | */ | |
286 | if (LogStat & LOG_CONS && | |
7f3ab33f AZ |
287 | (fd = __open(_PATH_CONSOLE, O_WRONLY|O_NOCTTY|O_CLOEXEC, |
288 | 0)) >= 0) | |
790aa1f2 | 289 | { |
be7dc2b7 | 290 | __dprintf (fd, "%s\r\n", buf + msgoff); |
790aa1f2 UD |
291 | (void)__close(fd); |
292 | } | |
293 | } | |
294 | } | |
295 | ||
c4e4b2e1 | 296 | out: |
790aa1f2 UD |
297 | /* End of critical section. */ |
298 | __libc_cleanup_pop (0); | |
299 | __libc_lock_unlock (syslog_lock); | |
300 | ||
301 | if (buf != failbuf) | |
302 | free (buf); | |
303 | } | |
790aa1f2 | 304 | |
5ad1a81c AZ |
305 | /* AF_UNIX address of local logger */ |
306 | static const struct sockaddr_un SyslogAddr = | |
307 | { | |
308 | .sun_family = AF_UNIX, | |
309 | .sun_path = _PATH_LOG | |
310 | }; | |
790aa1f2 UD |
311 | |
312 | static void | |
790aa1f2 UD |
313 | openlog_internal(const char *ident, int logstat, int logfac) |
314 | { | |
315 | if (ident != NULL) | |
316 | LogTag = ident; | |
317 | LogStat = logstat; | |
f2913118 | 318 | if ((logfac &~ LOG_FACMASK) == 0) |
790aa1f2 UD |
319 | LogFacility = logfac; |
320 | ||
321 | int retry = 0; | |
322 | while (retry < 2) { | |
323 | if (LogFile == -1) { | |
790aa1f2 | 324 | if (LogStat & LOG_NDELAY) { |
52fb79d6 FW |
325 | LogFile = __socket(AF_UNIX, LogType | SOCK_CLOEXEC, 0); |
326 | if (LogFile == -1) | |
327 | return; | |
790aa1f2 UD |
328 | } |
329 | } | |
330 | if (LogFile != -1 && !connected) | |
331 | { | |
332 | int old_errno = errno; | |
333 | if (__connect(LogFile, &SyslogAddr, sizeof(SyslogAddr)) | |
334 | == -1) | |
335 | { | |
336 | int saved_errno = errno; | |
337 | int fd = LogFile; | |
338 | LogFile = -1; | |
339 | (void)__close(fd); | |
340 | __set_errno (old_errno); | |
341 | if (saved_errno == EPROTOTYPE) | |
342 | { | |
343 | /* retry with the other type: */ | |
344 | LogType = (LogType == SOCK_DGRAM | |
345 | ? SOCK_STREAM : SOCK_DGRAM); | |
346 | ++retry; | |
347 | continue; | |
348 | } | |
349 | } else | |
7cb10381 | 350 | connected = true; |
790aa1f2 UD |
351 | } |
352 | break; | |
353 | } | |
354 | } | |
355 | ||
356 | void | |
357 | openlog (const char *ident, int logstat, int logfac) | |
358 | { | |
359 | /* Protect against multiple users and cancellation. */ | |
360 | __libc_cleanup_push (cancel_handler, NULL); | |
361 | __libc_lock_lock (syslog_lock); | |
362 | ||
363 | openlog_internal (ident, logstat, logfac); | |
364 | ||
365 | __libc_cleanup_pop (1); | |
366 | } | |
367 | ||
790aa1f2 | 368 | static void |
60d2f8f3 | 369 | closelog_internal (void) |
790aa1f2 UD |
370 | { |
371 | if (!connected) | |
372 | return; | |
373 | ||
374 | __close (LogFile); | |
375 | LogFile = -1; | |
7cb10381 | 376 | connected = false; |
790aa1f2 UD |
377 | } |
378 | ||
379 | void | |
60d2f8f3 | 380 | closelog (void) |
790aa1f2 UD |
381 | { |
382 | /* Protect against multiple users and cancellation. */ | |
383 | __libc_cleanup_push (cancel_handler, NULL); | |
384 | __libc_lock_lock (syslog_lock); | |
385 | ||
386 | closelog_internal (); | |
387 | LogTag = NULL; | |
388 | LogType = SOCK_DGRAM; /* this is the default */ | |
389 | ||
390 | /* Free the lock. */ | |
391 | __libc_cleanup_pop (1); | |
392 | } | |
393 | ||
394 | /* setlogmask -- set the log mask level */ | |
395 | int | |
9d46370c | 396 | setlogmask (int pmask) |
790aa1f2 UD |
397 | { |
398 | int omask; | |
399 | ||
c4e4b2e1 AS |
400 | /* Protect against multiple users. */ |
401 | __libc_lock_lock (syslog_lock); | |
402 | ||
790aa1f2 UD |
403 | omask = LogMask; |
404 | if (pmask != 0) | |
405 | LogMask = pmask; | |
c4e4b2e1 AS |
406 | |
407 | __libc_lock_unlock (syslog_lock); | |
408 | ||
790aa1f2 UD |
409 | return (omask); |
410 | } |