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 * 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 #if defined(LIBC_SCCS) && !defined(lint) 35 static char rcsid[] = "$OpenBSD: syslog.c,v 1.20 2002/11/24 01:49:37 cloder Exp $"; 36 #endif /* LIBC_SCCS and not lint */ 37 38 #include <sys/types.h> 39 #include <sys/socket.h> 40 #include <sys/syslog.h> 41 #include <sys/uio.h> 42 #include <sys/un.h> 43 #include <netdb.h> 44 45 #include <errno.h> 46 #include <fcntl.h> 47 #include <paths.h> 48 #include <stdio.h> 49 #include <string.h> 50 #include <time.h> 51 #include <unistd.h> 52 #include <stdarg.h> 53 54 static struct syslog_data sdata = SYSLOG_DATA_INIT; 55 56 extern char *__progname; /* Program name, from crt0. */ 57 58 static void disconnectlog(void); /* disconnect from syslogd */ 59 static void connectlog(void); /* (re)connect to syslogd */ 60 static void disconnectlog_r(struct syslog_data *); 61 static void connectlog_r(struct syslog_data *); 62 63 /* 64 * syslog, vsyslog -- 65 * print message on log file; output is intended for syslogd(8). 66 */ 67 void 68 syslog(int pri, const char *fmt, ...) 69 { 70 va_list ap; 71 72 va_start(ap, fmt); 73 vsyslog(pri, fmt, ap); 74 va_end(ap); 75 } 76 77 void 78 vsyslog(pri, fmt, ap) 79 int pri; 80 register const char *fmt; 81 va_list ap; 82 { 83 vsyslog_r(pri, &sdata, fmt, ap); 84 } 85 86 static void 87 disconnectlog() 88 { 89 disconnectlog_r(&sdata); 90 } 91 92 static void 93 connectlog() 94 { 95 connectlog_r(&sdata); 96 } 97 98 void 99 openlog(ident, logstat, logfac) 100 const char *ident; 101 int logstat, logfac; 102 { 103 openlog_r(ident, logstat, logfac, &sdata); 104 } 105 106 void 107 closelog() 108 { 109 closelog_r(&sdata); 110 } 111 112 /* setlogmask -- set the log mask level */ 113 int 114 setlogmask(pmask) 115 int pmask; 116 { 117 return setlogmask_r(pmask, &sdata); 118 } 119 120 /* Reentrant version of syslog, i.e. syslog_r() */ 121 122 void 123 syslog_r(int pri, struct syslog_data *data, const char *fmt, ...) 124 { 125 va_list ap; 126 127 va_start(ap, fmt); 128 vsyslog_r(pri, data, fmt, ap); 129 va_end(ap); 130 } 131 132 void 133 vsyslog_r(pri, data, fmt, ap) 134 int pri; 135 struct syslog_data *data; 136 const char *fmt; 137 va_list ap; 138 { 139 int cnt; 140 char ch, *p, *t; 141 time_t now; 142 int fd, saved_errno; 143 #define TBUF_LEN 2048 144 #define FMT_LEN 1024 145 char *stdp, tbuf[TBUF_LEN], fmt_cpy[FMT_LEN]; 146 int tbuf_left, fmt_left, prlen; 147 148 #define INTERNALLOG LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID 149 /* Check for invalid bits. */ 150 if (pri & ~(LOG_PRIMASK|LOG_FACMASK)) { 151 if (data == &sdata) { 152 syslog(INTERNALLOG, 153 "syslog: unknown facility/priority: %x", pri); 154 } else { 155 syslog_r(INTERNALLOG, data, 156 "syslog_r: unknown facility/priority: %x", pri); 157 } 158 pri &= LOG_PRIMASK|LOG_FACMASK; 159 } 160 161 /* Check priority against setlogmask values. */ 162 if (!(LOG_MASK(LOG_PRI(pri)) & data->log_mask)) 163 return; 164 165 saved_errno = errno; 166 167 /* Set default facility if none specified. */ 168 if ((pri & LOG_FACMASK) == 0) 169 pri |= data->log_fac; 170 171 /* If we have been called through syslog(), no need for reentrancy. */ 172 if (data == &sdata) 173 (void)time(&now); 174 175 p = tbuf; 176 tbuf_left = TBUF_LEN; 177 178 #define DEC() \ 179 do { \ 180 if (prlen < 0) \ 181 prlen = 0; \ 182 if (prlen >= tbuf_left) \ 183 prlen = tbuf_left - 1; \ 184 p += prlen; \ 185 tbuf_left -= prlen; \ 186 } while (0) 187 188 prlen = snprintf(p, tbuf_left, "<%d>", pri); 189 DEC(); 190 191 /* 192 * syslogd will expand time automagically for reentrant case, and 193 * for normal case, just do like before 194 */ 195 if (data == &sdata) { 196 prlen = strftime(p, tbuf_left, "%h %e %T ", localtime(&now)); 197 DEC(); 198 } 199 200 if (data->log_stat & LOG_PERROR) 201 stdp = p; 202 if (data->log_tag == NULL) 203 data->log_tag = __progname; 204 if (data->log_tag != NULL) { 205 prlen = snprintf(p, tbuf_left, "%s", data->log_tag); 206 DEC(); 207 } 208 if (data->log_stat & LOG_PID) { 209 prlen = snprintf(p, tbuf_left, "[%ld]", (long)getpid()); 210 DEC(); 211 } 212 if (data->log_tag != NULL) { 213 if (tbuf_left > 1) { 214 *p++ = ':'; 215 tbuf_left--; 216 } 217 if (tbuf_left > 1) { 218 *p++ = ' '; 219 tbuf_left--; 220 } 221 } 222 223 /* strerror() is not reentrant */ 224 225 for (t = fmt_cpy, fmt_left = FMT_LEN; (ch = *fmt); ++fmt) { 226 if (ch == '%' && fmt[1] == 'm') { 227 ++fmt; 228 if (data == &sdata) { 229 prlen = snprintf(t, fmt_left, "%s", 230 strerror(saved_errno)); 231 } else { 232 prlen = snprintf(t, fmt_left, "Error %d", 233 saved_errno); 234 } 235 if (prlen >= fmt_left) 236 prlen = fmt_left - 1; 237 t += prlen; 238 fmt_left -= prlen; 239 } else { 240 if (fmt_left > 1) { 241 *t++ = ch; 242 fmt_left--; 243 } 244 } 245 } 246 *t = '\0'; 247 248 prlen = vsnprintf(p, tbuf_left, fmt_cpy, ap); 249 DEC(); 250 cnt = p - tbuf; 251 252 /* Output to stderr if requested. */ 253 if (data->log_stat & LOG_PERROR) { 254 struct iovec iov[2]; 255 256 iov[0].iov_base = stdp; 257 iov[0].iov_len = cnt - (stdp - tbuf); 258 iov[1].iov_base = "\n"; 259 iov[1].iov_len = 1; 260 (void)writev(STDERR_FILENO, iov, 2); 261 } 262 263 /* Get connected, output the message to the local logger. */ 264 if (!data->opened) 265 openlog_r(data->log_tag, data->log_stat, 0, data); 266 connectlog_r(data); 267 if (send(data->log_file, tbuf, cnt, 0) >= 0) 268 return; 269 270 /* 271 * If the send() failed, there are two likely scenarios: 272 * 1) syslogd was restarted 273 * 2) /dev/log is out of socket buffer space 274 * We attempt to reconnect to /dev/log to take care of 275 * case #1 and keep send()ing data to cover case #2 276 * to give syslogd a chance to empty its socket buffer. 277 */ 278 disconnectlog_r(data); 279 connectlog_r(data); 280 do { 281 usleep(1); 282 if (send(data->log_file, tbuf, cnt, 0) >= 0) 283 return; 284 } while (errno == ENOBUFS); 285 286 /* 287 * Output the message to the console; try not to block 288 * as a blocking console should not stop other processes. 289 * Make sure the error reported is the one from the syslogd failure. 290 */ 291 if (data->log_stat & LOG_CONS && 292 (fd = open(_PATH_CONSOLE, O_WRONLY|O_NONBLOCK, 0)) >= 0) { 293 struct iovec iov[2]; 294 295 p = strchr(tbuf, '>') + 1; 296 iov[0].iov_base = p; 297 iov[0].iov_len = cnt - (p - tbuf); 298 iov[1].iov_base = "\r\n"; 299 iov[1].iov_len = 2; 300 (void)writev(fd, iov, 2); 301 (void)close(fd); 302 } 303 304 if (data != &sdata) { 305 closelog_r(data); 306 } 307 } 308 309 static void 310 disconnectlog_r(data) 311 struct syslog_data *data; 312 { 313 /* 314 * If the user closed the FD and opened another in the same slot, 315 * that's their problem. They should close it before calling on 316 * system services. 317 */ 318 if (data->log_file != -1) { 319 close(data->log_file); 320 data->log_file = -1; 321 } 322 data->connected = 0; /* retry connect */ 323 } 324 325 static void 326 connectlog_r(data) 327 struct syslog_data *data; 328 { 329 struct sockaddr_un SyslogAddr; /* AF_UNIX address of local logger */ 330 331 if (data->log_file == -1) { 332 if ((data->log_file = socket(AF_UNIX, SOCK_DGRAM, 0)) == -1) 333 return; 334 (void)fcntl(data->log_file, F_SETFD, 1); 335 } 336 if (data->log_file != -1 && !data->connected) { 337 memset(&SyslogAddr, '\0', sizeof(SyslogAddr)); 338 SyslogAddr.sun_len = sizeof(SyslogAddr); 339 SyslogAddr.sun_family = AF_UNIX; 340 strlcpy(SyslogAddr.sun_path, _PATH_LOG, 341 sizeof(SyslogAddr.sun_path)); 342 if (connect(data->log_file, (struct sockaddr *)&SyslogAddr, 343 sizeof(SyslogAddr)) == -1) { 344 (void)close(data->log_file); 345 data->log_file = -1; 346 } else 347 data->connected = 1; 348 } 349 } 350 351 void 352 openlog_r(ident, logstat, logfac, data) 353 const char *ident; 354 int logstat, logfac; 355 struct syslog_data *data; 356 { 357 if (ident != NULL) 358 data->log_tag = ident; 359 data->log_stat = logstat; 360 if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0) 361 data->log_fac = logfac; 362 363 if (data->log_stat & LOG_NDELAY) /* open immediately */ 364 connectlog_r(data); 365 366 data->opened = 1; /* ident and facility has been set */ 367 } 368 369 void 370 closelog_r(data) 371 struct syslog_data *data; 372 { 373 (void)close(data->log_file); 374 data->log_file = -1; 375 data->connected = 0; 376 data->log_tag = NULL; 377 } 378 379 /* setlogmask -- set the log mask level */ 380 int 381 setlogmask_r(pmask, data) 382 int pmask; 383 struct syslog_data *data; 384 { 385 int omask; 386 387 omask = data->log_mask; 388 if (pmask != 0) 389 data->log_mask = pmask; 390 return (omask); 391 } 392