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 /* 36 static char sccsid[] = "From: @(#)syslog.c 8.4 (Berkeley) 3/18/94"; 37 */ 38 static const char rcsid[] = 39 "$Id: syslog.c,v 1.6 1995/10/21 07:05:01 peter Exp $"; 40 #endif /* LIBC_SCCS and not lint */ 41 42 #include <sys/types.h> 43 #include <sys/socket.h> 44 #include <sys/syslog.h> 45 #include <sys/uio.h> 46 #include <netdb.h> 47 48 #include <errno.h> 49 #include <fcntl.h> 50 #include <paths.h> 51 #include <stdio.h> 52 #include <string.h> 53 #include <time.h> 54 #include <unistd.h> 55 56 #if __STDC__ 57 #include <stdarg.h> 58 #else 59 #include <varargs.h> 60 #endif 61 62 static int LogFile = -1; /* fd for log */ 63 static int connected; /* have done connect */ 64 static int LogStat = 0; /* status bits, set by openlog() */ 65 static const char *LogTag = NULL; /* string to tag the entry with */ 66 static int LogFacility = LOG_USER; /* default facility code */ 67 static int LogMask = 0xff; /* mask of priorities to be logged */ 68 extern char *__progname; /* Program name, from crt0. */ 69 70 /* 71 * Format of the magic cookie passed through the stdio hook 72 */ 73 struct bufcookie { 74 char *base; /* start of buffer */ 75 int left; 76 }; 77 78 /* 79 * stdio write hook for writing to a static string buffer 80 * XXX: Maybe one day, dynamically allocate it so that the line length 81 * is `unlimited'. 82 */ 83 static writehook(cookie, buf, len) 84 void *cookie; /* really [struct bufcookie *] */ 85 char *buf; /* characters to copy */ 86 int len; /* length to copy */ 87 { 88 struct bufcookie *h; /* private `handle' */ 89 90 h = (struct bufcookie *)cookie; 91 if (len > h->left) { 92 /* clip in case of wraparound */ 93 len = h->left; 94 } 95 if (len > 0) { 96 (void)memcpy(h->base, buf, len); /* `write' it. */ 97 h->base += len; 98 h->left -= len; 99 } 100 return 0; 101 } 102 103 /* 104 * syslog, vsyslog -- 105 * print message on log file; output is intended for syslogd(8). 106 */ 107 void 108 #if __STDC__ 109 syslog(int pri, const char *fmt, ...) 110 #else 111 syslog(pri, fmt, va_alist) 112 int pri; 113 char *fmt; 114 va_dcl 115 #endif 116 { 117 va_list ap; 118 119 #if __STDC__ 120 va_start(ap, fmt); 121 #else 122 va_start(ap); 123 #endif 124 vsyslog(pri, fmt, ap); 125 va_end(ap); 126 } 127 128 void 129 vsyslog(pri, fmt, ap) 130 int pri; 131 register const char *fmt; 132 va_list ap; 133 { 134 register int cnt; 135 register char ch, *p; 136 time_t now; 137 int fd, saved_errno; 138 char *stdp, tbuf[2048], fmt_cpy[1024]; 139 FILE *fp, *fmt_fp; 140 struct bufcookie tbuf_cookie; 141 struct bufcookie fmt_cookie; 142 143 #define INTERNALLOG LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID 144 /* Check for invalid bits. */ 145 if (pri & ~(LOG_PRIMASK|LOG_FACMASK)) { 146 syslog(INTERNALLOG, 147 "syslog: unknown facility/priority: %x", pri); 148 pri &= LOG_PRIMASK|LOG_FACMASK; 149 } 150 151 /* Check priority against setlogmask values. */ 152 if (!(LOG_MASK(LOG_PRI(pri)) & LogMask)) 153 return; 154 155 saved_errno = errno; 156 157 /* Set default facility if none specified. */ 158 if ((pri & LOG_FACMASK) == 0) 159 pri |= LogFacility; 160 161 /* Create the primary stdio hook */ 162 tbuf_cookie.base = tbuf; 163 tbuf_cookie.left = sizeof(tbuf); 164 fp = fwopen(&tbuf_cookie, writehook); 165 if (fp == NULL) 166 return; 167 168 /* Build the message. */ 169 (void)time(&now); 170 (void)fprintf(fp, "<%d>", pri); 171 (void)fprintf(fp, "%.15s ", ctime(&now) + 4); 172 if (LogStat & LOG_PERROR) { 173 /* Transfer to string buffer */ 174 (void)fflush(fp); 175 stdp = tbuf + (sizeof(tbuf) - tbuf_cookie.left); 176 } 177 if (LogTag == NULL) 178 LogTag = __progname; 179 if (LogTag != NULL) 180 (void)fprintf(fp, "%s", LogTag); 181 if (LogStat & LOG_PID) 182 (void)fprintf(fp, "[%d]", getpid()); 183 if (LogTag != NULL) { 184 (void)fprintf(fp, ": "); 185 } 186 187 /* Check to see if we can skip expanding the %m */ 188 if (strstr(fmt, "%m")) { 189 190 /* Create the second stdio hook */ 191 fmt_cookie.base = fmt_cpy; 192 fmt_cookie.left = sizeof(fmt_cpy) - 1; 193 fmt_fp = fwopen(&fmt_cookie, writehook); 194 if (fmt_fp == NULL) { 195 fclose(fp); 196 return; 197 } 198 199 /* Substitute error message for %m. */ 200 for ( ; ch = *fmt; ++fmt) 201 if (ch == '%' && fmt[1] == 'm') { 202 ++fmt; 203 fputs(strerror(saved_errno), fmt_fp); 204 } else 205 fputc(ch, fmt_fp); 206 207 /* Null terminate if room */ 208 fputc(0, fmt_fp); 209 fclose(fmt_fp); 210 211 /* Guarantee null termination */ 212 fmt_cpy[sizeof(fmt_cpy) - 1] = '\0'; 213 214 fmt = fmt_cpy; 215 } 216 217 (void)vfprintf(fp, fmt, ap); 218 (void)fclose(fp); 219 220 cnt = sizeof(tbuf) - tbuf_cookie.left; 221 222 /* Output to stderr if requested. */ 223 if (LogStat & LOG_PERROR) { 224 struct iovec iov[2]; 225 register struct iovec *v = iov; 226 227 v->iov_base = stdp; 228 v->iov_len = cnt - (stdp - tbuf); 229 ++v; 230 v->iov_base = "\n"; 231 v->iov_len = 1; 232 (void)writev(STDERR_FILENO, iov, 2); 233 } 234 235 /* Get connected, output the message to the local logger. */ 236 if (!connected) 237 openlog(LogTag, LogStat | LOG_NDELAY, 0); 238 if (send(LogFile, tbuf, cnt, 0) >= 0) 239 return; 240 241 /* 242 * Output the message to the console; don't worry about blocking, 243 * if console blocks everything will. Make sure the error reported 244 * is the one from the syslogd failure. 245 */ 246 if (LogStat & LOG_CONS && 247 (fd = open(_PATH_CONSOLE, O_WRONLY, 0)) >= 0) { 248 struct iovec iov[2]; 249 register struct iovec *v = iov; 250 251 p = strchr(tbuf, '>') + 1; 252 v->iov_base = p; 253 v->iov_len = cnt - (p - tbuf); 254 ++v; 255 v->iov_base = "\r\n"; 256 v->iov_len = 2; 257 (void)writev(fd, iov, 2); 258 (void)close(fd); 259 } 260 } 261 262 static struct sockaddr SyslogAddr; /* AF_UNIX address of local logger */ 263 264 void 265 openlog(ident, logstat, logfac) 266 const char *ident; 267 int logstat, logfac; 268 { 269 if (ident != NULL) 270 LogTag = ident; 271 LogStat = logstat; 272 if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0) 273 LogFacility = logfac; 274 275 if (LogFile == -1) { 276 SyslogAddr.sa_family = AF_UNIX; 277 (void)strncpy(SyslogAddr.sa_data, _PATH_LOG, 278 sizeof(SyslogAddr.sa_data)); 279 if (LogStat & LOG_NDELAY) { 280 if ((LogFile = socket(AF_UNIX, SOCK_DGRAM, 0)) == -1) 281 return; 282 (void)fcntl(LogFile, F_SETFD, 1); 283 } 284 } 285 if (LogFile != -1 && !connected) 286 if (connect(LogFile, &SyslogAddr, sizeof(SyslogAddr)) == -1) { 287 (void)close(LogFile); 288 LogFile = -1; 289 } else 290 connected = 1; 291 } 292 293 void 294 closelog() 295 { 296 (void)close(LogFile); 297 LogFile = -1; 298 connected = 0; 299 } 300 301 /* setlogmask -- set the log mask level */ 302 int 303 setlogmask(pmask) 304 int pmask; 305 { 306 int omask; 307 308 omask = LogMask; 309 if (pmask != 0) 310 LogMask = pmask; 311 return (omask); 312 } 313