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> |
38 | #include <sys/un.h> |
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> |
50 | #include <libc-lock.h> |
51 | #include <signal.h> |
52 | #include <locale.h> |
53 | |
54 | #include <stdarg.h> |
55 | |
56 | #include <libio/libioP.h> |
57 | #include <math_ldbl_opt.h> |
58 | |
59 | #include <kernel-features.h> |
60 | |
61 | #define ftell(s) _IO_ftell (s) |
62 | |
63 | static int LogType = SOCK_DGRAM; /* type of socket connection */ |
64 | static int LogFile = -1; /* fd for log */ |
65 | static int connected; /* have done connect */ |
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 | |
75 | static void openlog_internal(const char *, int, int); |
76 | static void closelog_internal(void); |
77 | #ifndef NO_SIGPIPE |
78 | static void sigpipe_handler (int); |
79 | #endif |
80 | |
81 | #ifndef send_flags |
82 | # define send_flags 0 |
83 | #endif |
84 | |
85 | struct cleanup_arg |
86 | { |
87 | void *buf; |
88 | struct sigaction *oldaction; |
89 | }; |
90 | |
91 | static void |
92 | cancel_handler (void *ptr) |
93 | { |
94 | #ifndef NO_SIGPIPE |
95 | /* Restore the old signal handler. */ |
96 | struct cleanup_arg *clarg = (struct cleanup_arg *) ptr; |
97 | |
98 | if (clarg != NULL && clarg->oldaction != NULL) |
99 | __sigaction (SIGPIPE, clarg->oldaction, NULL); |
100 | #endif |
101 | |
102 | /* Free the lock. */ |
103 | __libc_lock_unlock (syslog_lock); |
104 | } |
105 | |
106 | |
107 | /* |
108 | * syslog, vsyslog -- |
109 | * print message on log file; output is intended for syslogd(8). |
110 | */ |
111 | void |
112 | __syslog(int pri, const char *fmt, ...) |
113 | { |
114 | va_list ap; |
115 | |
116 | va_start(ap, fmt); |
117 | __vsyslog_internal(pri, fmt, ap, 0); |
118 | va_end(ap); |
119 | } |
120 | ldbl_hidden_def (__syslog, syslog) |
121 | ldbl_strong_alias (__syslog, syslog) |
122 | |
123 | void |
124 | __vsyslog(int pri, const char *fmt, va_list ap) |
125 | { |
126 | __vsyslog_internal(pri, fmt, ap, 0); |
127 | } |
128 | ldbl_weak_alias (__vsyslog, vsyslog) |
129 | |
130 | void |
131 | __syslog_chk(int pri, int flag, const char *fmt, ...) |
132 | { |
133 | va_list ap; |
134 | |
135 | va_start(ap, fmt); |
136 | __vsyslog_internal(pri, fmt, ap, (flag > 0) ? PRINTF_FORTIFY : 0); |
137 | va_end(ap); |
138 | } |
139 | |
140 | void |
141 | __vsyslog_chk(int pri, int flag, const char *fmt, va_list ap) |
142 | { |
143 | __vsyslog_internal(pri, fmt, ap, (flag > 0) ? PRINTF_FORTIFY : 0); |
144 | } |
145 | |
146 | void |
147 | __vsyslog_internal(int pri, const char *fmt, va_list ap, |
148 | unsigned int mode_flags) |
149 | { |
150 | struct tm now_tm; |
151 | time_t now; |
152 | int fd; |
153 | FILE *f; |
154 | char *buf = 0; |
155 | size_t bufsize = 0; |
156 | size_t msgoff; |
157 | #ifndef NO_SIGPIPE |
158 | struct sigaction action, oldaction; |
159 | int sigpipe; |
160 | #endif |
161 | int saved_errno = errno; |
162 | char failbuf[3 * sizeof (pid_t) + sizeof "out of memory []" ]; |
163 | |
164 | #define INTERNALLOG LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID |
165 | /* Check for invalid bits. */ |
166 | if (pri & ~(LOG_PRIMASK|LOG_FACMASK)) { |
167 | syslog(INTERNALLOG, |
168 | "syslog: unknown facility/priority: %x" , pri); |
169 | pri &= LOG_PRIMASK|LOG_FACMASK; |
170 | } |
171 | |
172 | /* Check priority against setlogmask values. */ |
173 | if ((LOG_MASK (LOG_PRI (pri)) & LogMask) == 0) |
174 | return; |
175 | |
176 | /* Set default facility if none specified. */ |
177 | if ((pri & LOG_FACMASK) == 0) |
178 | pri |= LogFacility; |
179 | |
180 | /* Build the message in a memory-buffer stream. */ |
181 | f = __open_memstream (&buf, &bufsize); |
182 | if (f == NULL) |
183 | { |
184 | /* We cannot get a stream. There is not much we can do but |
185 | emitting an error messages. */ |
186 | char numbuf[3 * sizeof (pid_t)]; |
187 | char *nump; |
188 | char *endp = __stpcpy (failbuf, "out of memory [" ); |
189 | pid_t pid = __getpid (); |
190 | |
191 | nump = numbuf + sizeof (numbuf); |
192 | /* The PID can never be zero. */ |
193 | do |
194 | *--nump = '0' + pid % 10; |
195 | while ((pid /= 10) != 0); |
196 | |
197 | endp = __mempcpy (endp, nump, (numbuf + sizeof (numbuf)) - nump); |
198 | *endp++ = ']'; |
199 | *endp = '\0'; |
200 | buf = failbuf; |
201 | bufsize = endp - failbuf; |
202 | msgoff = 0; |
203 | } |
204 | else |
205 | { |
206 | __fsetlocking (f, FSETLOCKING_BYCALLER); |
207 | fprintf (f, "<%d>" , pri); |
208 | (void) time (&now); |
209 | f->_IO_write_ptr += __strftime_l (f->_IO_write_ptr, |
210 | f->_IO_write_end |
211 | - f->_IO_write_ptr, |
212 | "%h %e %T " , |
213 | __localtime_r (&now, &now_tm), |
214 | _nl_C_locobj_ptr); |
215 | msgoff = ftell (f); |
216 | if (LogTag == NULL) |
217 | LogTag = __progname; |
218 | if (LogTag != NULL) |
219 | __fputs_unlocked (LogTag, f); |
220 | if (LogStat & LOG_PID) |
221 | fprintf (f, "[%d]" , (int) __getpid ()); |
222 | if (LogTag != NULL) |
223 | { |
224 | __putc_unlocked (':', f); |
225 | __putc_unlocked (' ', f); |
226 | } |
227 | |
228 | /* Restore errno for %m format. */ |
229 | __set_errno (saved_errno); |
230 | |
231 | /* We have the header. Print the user's format into the |
232 | buffer. */ |
233 | __vfprintf_internal (f, fmt, ap, mode_flags); |
234 | |
235 | /* Close the memory stream; this will finalize the data |
236 | into a malloc'd buffer in BUF. */ |
237 | fclose (f); |
238 | } |
239 | |
240 | /* Output to stderr if requested. */ |
241 | if (LogStat & LOG_PERROR) { |
242 | struct iovec iov[2]; |
243 | struct iovec *v = iov; |
244 | |
245 | v->iov_base = buf + msgoff; |
246 | v->iov_len = bufsize - msgoff; |
247 | /* Append a newline if necessary. */ |
248 | if (buf[bufsize - 1] != '\n') |
249 | { |
250 | ++v; |
251 | v->iov_base = (char *) "\n" ; |
252 | v->iov_len = 1; |
253 | } |
254 | |
255 | __libc_cleanup_push (free, buf == failbuf ? NULL : buf); |
256 | |
257 | /* writev is a cancellation point. */ |
258 | (void)__writev(STDERR_FILENO, iov, v - iov + 1); |
259 | |
260 | __libc_cleanup_pop (0); |
261 | } |
262 | |
263 | /* Prepare for multiple users. We have to take care: open and |
264 | write are cancellation points. */ |
265 | struct cleanup_arg clarg; |
266 | clarg.buf = buf; |
267 | clarg.oldaction = NULL; |
268 | __libc_cleanup_push (cancel_handler, &clarg); |
269 | __libc_lock_lock (syslog_lock); |
270 | |
271 | #ifndef NO_SIGPIPE |
272 | /* Prepare for a broken connection. */ |
273 | memset (&action, 0, sizeof (action)); |
274 | action.sa_handler = sigpipe_handler; |
275 | sigemptyset (&action.sa_mask); |
276 | sigpipe = __sigaction (SIGPIPE, &action, &oldaction); |
277 | if (sigpipe == 0) |
278 | clarg.oldaction = &oldaction; |
279 | #endif |
280 | |
281 | /* Get connected, output the message to the local logger. */ |
282 | if (!connected) |
283 | openlog_internal(LogTag, LogStat | LOG_NDELAY, 0); |
284 | |
285 | /* If we have a SOCK_STREAM connection, also send ASCII NUL as |
286 | a record terminator. */ |
287 | if (LogType == SOCK_STREAM) |
288 | ++bufsize; |
289 | |
290 | if (!connected || __send(LogFile, buf, bufsize, send_flags) < 0) |
291 | { |
292 | if (connected) |
293 | { |
294 | /* Try to reopen the syslog connection. Maybe it went |
295 | down. */ |
296 | closelog_internal (); |
297 | openlog_internal(LogTag, LogStat | LOG_NDELAY, 0); |
298 | } |
299 | |
300 | if (!connected || __send(LogFile, buf, bufsize, send_flags) < 0) |
301 | { |
302 | closelog_internal (); /* attempt re-open next time */ |
303 | /* |
304 | * Output the message to the console; don't worry |
305 | * about blocking, if console blocks everything will. |
306 | * Make sure the error reported is the one from the |
307 | * syslogd failure. |
308 | */ |
309 | if (LogStat & LOG_CONS && |
310 | (fd = __open(_PATH_CONSOLE, O_WRONLY|O_NOCTTY, 0)) >= 0) |
311 | { |
312 | __dprintf (fd, "%s\r\n" , buf + msgoff); |
313 | (void)__close(fd); |
314 | } |
315 | } |
316 | } |
317 | |
318 | #ifndef NO_SIGPIPE |
319 | if (sigpipe == 0) |
320 | __sigaction (SIGPIPE, &oldaction, (struct sigaction *) NULL); |
321 | #endif |
322 | |
323 | /* End of critical section. */ |
324 | __libc_cleanup_pop (0); |
325 | __libc_lock_unlock (syslog_lock); |
326 | |
327 | if (buf != failbuf) |
328 | free (buf); |
329 | } |
330 | |
331 | static struct sockaddr_un SyslogAddr; /* AF_UNIX address of local logger */ |
332 | |
333 | |
334 | static void |
335 | openlog_internal(const char *ident, int logstat, int logfac) |
336 | { |
337 | if (ident != NULL) |
338 | LogTag = ident; |
339 | LogStat = logstat; |
340 | if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0) |
341 | LogFacility = logfac; |
342 | |
343 | int retry = 0; |
344 | while (retry < 2) { |
345 | if (LogFile == -1) { |
346 | SyslogAddr.sun_family = AF_UNIX; |
347 | (void)strncpy(SyslogAddr.sun_path, _PATH_LOG, |
348 | sizeof(SyslogAddr.sun_path)); |
349 | if (LogStat & LOG_NDELAY) { |
350 | LogFile = __socket(AF_UNIX, LogType | SOCK_CLOEXEC, 0); |
351 | if (LogFile == -1) |
352 | return; |
353 | } |
354 | } |
355 | if (LogFile != -1 && !connected) |
356 | { |
357 | int old_errno = errno; |
358 | if (__connect(LogFile, &SyslogAddr, sizeof(SyslogAddr)) |
359 | == -1) |
360 | { |
361 | int saved_errno = errno; |
362 | int fd = LogFile; |
363 | LogFile = -1; |
364 | (void)__close(fd); |
365 | __set_errno (old_errno); |
366 | if (saved_errno == EPROTOTYPE) |
367 | { |
368 | /* retry with the other type: */ |
369 | LogType = (LogType == SOCK_DGRAM |
370 | ? SOCK_STREAM : SOCK_DGRAM); |
371 | ++retry; |
372 | continue; |
373 | } |
374 | } else |
375 | connected = 1; |
376 | } |
377 | break; |
378 | } |
379 | } |
380 | |
381 | void |
382 | openlog (const char *ident, int logstat, int logfac) |
383 | { |
384 | /* Protect against multiple users and cancellation. */ |
385 | __libc_cleanup_push (cancel_handler, NULL); |
386 | __libc_lock_lock (syslog_lock); |
387 | |
388 | openlog_internal (ident, logstat, logfac); |
389 | |
390 | __libc_cleanup_pop (1); |
391 | } |
392 | |
393 | #ifndef NO_SIGPIPE |
394 | static void |
395 | sigpipe_handler (int signo) |
396 | { |
397 | closelog_internal (); |
398 | } |
399 | #endif |
400 | |
401 | static void |
402 | closelog_internal (void) |
403 | { |
404 | if (!connected) |
405 | return; |
406 | |
407 | __close (LogFile); |
408 | LogFile = -1; |
409 | connected = 0; |
410 | } |
411 | |
412 | void |
413 | closelog (void) |
414 | { |
415 | /* Protect against multiple users and cancellation. */ |
416 | __libc_cleanup_push (cancel_handler, NULL); |
417 | __libc_lock_lock (syslog_lock); |
418 | |
419 | closelog_internal (); |
420 | LogTag = NULL; |
421 | LogType = SOCK_DGRAM; /* this is the default */ |
422 | |
423 | /* Free the lock. */ |
424 | __libc_cleanup_pop (1); |
425 | } |
426 | |
427 | /* setlogmask -- set the log mask level */ |
428 | int |
429 | setlogmask (int pmask) |
430 | { |
431 | int omask; |
432 | |
433 | omask = LogMask; |
434 | if (pmask != 0) |
435 | LogMask = pmask; |
436 | return (omask); |
437 | } |
438 | |