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 sccsid[] = "@(#)syslog.c 8.4.3 (2.11BSD) 1995/07/15"; 36: #endif /* LIBC_SCCS and not lint */ 37: 38: #include <sys/types.h> 39: #include <sys/socket.h> 40: #include <syslog.h> 41: #include <sys/uio.h> 42: #include <netdb.h> 43: 44: #include <errno.h> 45: #include <fcntl.h> 46: #include <paths.h> 47: #include <stdio.h> 48: #include <string.h> 49: #include <time.h> 50: 51: #include <varargs.h> 52: 53: #define STDERR_FILENO 2 54: 55: static int LogFile = -1; /* fd for log */ 56: static char connected; /* have done connect */ 57: #ifdef pdp11 58: static char ToFile = 0; /* set if logfile is used */ 59: #endif 60: static int LogStat = 0; /* status bits, set by openlog() */ 61: static char *LogTag = NULL; /* string to tag the entry with */ 62: static int LogFacility = LOG_USER; /* default facility code */ 63: static int LogMask = 0xff; /* mask of priorities to be logged */ 64: #ifdef pdp11 65: static char logfile[] = "/usr/adm/messages"; 66: #endif 67: 68: extern char *__progname; /* Program name, from crt0. */ 69: extern int errno; /* error number */ 70: 71: /* 72: * syslog, vsyslog -- 73: * print message on log file; output is intended for syslogd(8). 74: */ 75: void 76: syslog(pri, fmt, va_alist) 77: int pri; 78: char *fmt; 79: va_dcl 80: { 81: va_list ap; 82: 83: va_start(ap); 84: vsyslog(pri, fmt, ap); 85: va_end(ap); 86: } 87: 88: void 89: vsyslog(pri, fmt, ap) 90: int pri; 91: register char *fmt; 92: va_list ap; 93: { 94: int cnt; 95: char ch; 96: register char *p, *t; 97: time_t now; 98: int fd, saved_errno; 99: char *stdp, tbuf[640], fmt_cpy[512]; 100: pid_t pid; 101: 102: #define INTERNALLOG LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID 103: /* Check for invalid bits. */ 104: if (pri & ~(LOG_PRIMASK|LOG_FACMASK)) { 105: syslog(INTERNALLOG, 106: "syslog: bad fac/pri: %x", pri); 107: pri &= LOG_PRIMASK|LOG_FACMASK; 108: } 109: 110: /* Check priority against setlogmask values. */ 111: if (!LOG_MASK(LOG_PRI(pri)) & LogMask) 112: return; 113: 114: saved_errno = errno; 115: 116: /* Set default facility if none specified. */ 117: if ((pri & LOG_FACMASK) == 0) 118: pri |= LogFacility; 119: 120: /* Build the message. */ 121: (void)time(&now); 122: p = tbuf + sprintf(tbuf, "<%d>", pri); 123: p += strftime(p, sizeof (tbuf) - (p - tbuf), "%h %e %T ", 124: localtime(&now)); 125: if (LogStat & LOG_PERROR) 126: stdp = p; 127: if (LogTag == NULL) 128: LogTag = __progname; 129: if (LogTag != NULL) 130: p += sprintf(p, "%s", LogTag); 131: if (LogStat & LOG_PID) 132: p += sprintf(p, "[%d]", getpid()); 133: if (LogTag != NULL) { 134: *p++ = ':'; 135: *p++ = ' '; 136: } 137: 138: /* Substitute error message for %m. */ 139: for (t = fmt_cpy; ch = *fmt; ++fmt) 140: if (ch == '%' && fmt[1] == 'm') { 141: ++fmt; 142: t += sprintf(t, "%s", strerror(saved_errno)); 143: } else 144: *t++ = ch; 145: *t = '\0'; 146: 147: p += vsprintf(p, fmt_cpy, ap); 148: cnt = p - tbuf; 149: 150: /* Output to stderr if requested. */ 151: if (LogStat & LOG_PERROR) { 152: struct iovec iov[2]; 153: register struct iovec *v = iov; 154: 155: v->iov_base = stdp; 156: v->iov_len = cnt - (stdp - tbuf); 157: ++v; 158: v->iov_base = "\n"; 159: v->iov_len = 1; 160: (void)writev(STDERR_FILENO, iov, 2); 161: } 162: 163: /* Get connected, output the message to the local logger. */ 164: if (!connected) 165: openlog(LogTag, LogStat | LOG_NDELAY, 0); 166: #ifdef pdp11 167: if (ToFile) { 168: if (write(LogFile, tbuf, cnt) == cnt) 169: return; 170: } 171: else 172: #endif 173: if (send(LogFile, tbuf, cnt, 0) >= 0) 174: return; 175: 176: /* 177: * Output the message to the console; don't worry about blocking, 178: * if console blocks everything will. Make sure the error reported 179: * is the one from the syslogd failure. 180: * 181: * 2.11BSD has to do a more complicated dance because we do not 182: * want to acquire a controlling terminal (bad news for 'init'!). 183: * Until either the tty driver is ported from 4.4 or O_NOCTTY 184: * is implemented we have to fork and let the child do the open of 185: * the console. 186: */ 187: if (LogStat & LOG_CONS) { 188: pid = vfork(); 189: if (pid == -1) 190: return; 191: if (pid == 0) { 192: fd = open(_PATH_CONSOLE, O_WRONLY, 0); 193: (void)strcat(tbuf, "\r\n"); 194: cnt += 2; 195: p = index(tbuf, '>') + 1; 196: (void)write(fd, p, cnt - (p - tbuf)); 197: (void)close(fd); 198: _exit(0); 199: } 200: while (waitpid(pid, NULL, NULL) == -1 && (errno == EINTR)) 201: ; 202: } 203: } 204: 205: static struct sockaddr SyslogAddr; /* AF_UNIX address of local logger */ 206: 207: void 208: openlog(ident, logstat, logfac) 209: char *ident; 210: int logstat; 211: register int logfac; 212: { 213: if (ident != NULL) 214: LogTag = ident; 215: LogStat = logstat; 216: if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0) 217: LogFacility = logfac; 218: 219: if (LogFile == -1) { 220: SyslogAddr.sa_family = AF_UNIX; 221: (void)strncpy(SyslogAddr.sa_data, _PATH_LOG, 222: sizeof(SyslogAddr.sa_data)); 223: if (LogStat & LOG_NDELAY) { 224: LogFile = socket(AF_UNIX, SOCK_DGRAM, 0); 225: #ifdef pdp11 226: if (LogFile == -1) { 227: LogFile = open(logfile, O_WRONLY|O_APPEND); 228: ToFile = 1; 229: connected = 1; 230: } 231: else 232: ToFile = 0; 233: #endif 234: if (LogFile == -1) 235: return; 236: (void)fcntl(LogFile, F_SETFD, 1); 237: } 238: } 239: if (LogFile != -1 && !connected) 240: if (connect(LogFile, &SyslogAddr, sizeof(SyslogAddr)) == -1) { 241: (void)close(LogFile); 242: LogFile = -1; 243: } else 244: connected = 1; 245: } 246: 247: void 248: closelog() 249: { 250: (void)close(LogFile); 251: LogFile = -1; 252: connected = 0; 253: } 254: 255: /* setlogmask -- set the log mask level */ 256: int 257: setlogmask(pmask) 258: register int pmask; 259: { 260: register int omask; 261: 262: omask = LogMask; 263: if (pmask != 0) 264: LogMask = pmask; 265: return (omask); 266: }