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