1 /* 2 * Copyright (c) 1983, 1988, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 */ 7 8 #if defined(LIBC_SCCS) && !defined(lint) 9 static char sccsid[] = "@(#)syslog.c 8.3 (Berkeley) 03/17/94"; 10 #endif /* LIBC_SCCS and not lint */ 11 12 #include <sys/types.h> 13 #include <sys/socket.h> 14 #include <sys/syslog.h> 15 #include <sys/uio.h> 16 #include <netdb.h> 17 18 #include <errno.h> 19 #include <fcntl.h> 20 #include <paths.h> 21 #include <stdio.h> 22 #include <string.h> 23 #include <time.h> 24 #include <unistd.h> 25 26 #if __STDC__ 27 #include <stdarg.h> 28 #else 29 #include <varargs.h> 30 #endif 31 32 static int LogFile = -1; /* fd for log */ 33 static int connected; /* have done connect */ 34 static int LogStat = 0; /* status bits, set by openlog() */ 35 static const char *LogTag = NULL; /* string to tag the entry with */ 36 static int LogFacility = LOG_USER; /* default facility code */ 37 static int LogMask = 0xff; /* mask of priorities to be logged */ 38 extern char *__progname; /* Program name, from crt0. */ 39 40 /* 41 * syslog, vsyslog -- 42 * print message on log file; output is intended for syslogd(8). 43 */ 44 void 45 #if __STDC__ 46 syslog(int pri, const char *fmt, ...) 47 #else 48 syslog(pri, fmt, va_alist) 49 int pri; 50 char *fmt; 51 va_dcl 52 #endif 53 { 54 va_list ap; 55 56 #if __STDC__ 57 va_start(ap, fmt); 58 #else 59 va_start(ap); 60 #endif 61 vsyslog(pri, fmt, ap); 62 va_end(ap); 63 } 64 65 void 66 vsyslog(pri, fmt, ap) 67 int pri; 68 register const char *fmt; 69 va_list ap; 70 { 71 register int cnt; 72 register char *p; 73 time_t now; 74 int fd, saved_errno; 75 char *stdp, tbuf[2048], fmt_cpy[1024]; 76 77 #define INTERNALLOG LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID 78 /* Check for invalid bits. */ 79 if (pri & ~(LOG_PRIMASK|LOG_FACMASK)) { 80 syslog(INTERNALLOG, 81 "syslog: unknown facility/priority: %x", pri); 82 pri &= LOG_PRIMASK|LOG_FACMASK; 83 } 84 85 /* Check priority against setlogmask values. */ 86 if (!LOG_MASK(LOG_PRI(pri)) & LogMask) 87 return; 88 89 saved_errno = errno; 90 91 /* set default facility if none specified */ 92 if ((pri & LOG_FACMASK) == 0) 93 pri |= LogFacility; 94 95 /* build the message */ 96 (void)time(&now); 97 p = tbuf + sprintf(tbuf, "<%d>", pri); 98 p += strftime(p, sizeof (tbuf) - (p - tbuf), "%h %e %T ", 99 localtime(&now)); 100 if (LogStat & LOG_PERROR) 101 stdp = p; 102 if (!LogTag) 103 LogTag = __progname; 104 if (LogTag) { 105 (void)strcpy(p, LogTag); 106 for (; *p; ++p); 107 } 108 if (LogStat & LOG_PID) 109 p += sprintf(p, "[%d]", getpid()); 110 if (LogTag) { 111 *p++ = ':'; 112 *p++ = ' '; 113 } 114 115 /* substitute error message for %m */ 116 { 117 register char ch, *t1, *t2; 118 119 for (t1 = fmt_cpy; ch = *fmt; ++fmt) 120 if (ch == '%' && fmt[1] == 'm') { 121 ++fmt; 122 for (t2 = strerror(saved_errno); 123 *t1 = *t2++; ++t1); 124 } 125 else 126 *t1++ = ch; 127 *t1 = '\0'; 128 } 129 130 p += vsprintf(p, fmt_cpy, ap); 131 cnt = p - tbuf; 132 133 /* output to stderr if requested */ 134 if (LogStat & LOG_PERROR) { 135 struct iovec iov[2]; 136 register struct iovec *v = iov; 137 138 v->iov_base = stdp; 139 v->iov_len = cnt - (stdp - tbuf); 140 ++v; 141 v->iov_base = "\n"; 142 v->iov_len = 1; 143 (void)writev(STDERR_FILENO, iov, 2); 144 } 145 146 /* get connected, output the message to the local logger */ 147 if (!connected) 148 openlog(LogTag, LogStat | LOG_NDELAY, 0); 149 if (send(LogFile, tbuf, cnt, 0) >= 0) 150 return; 151 152 /* see if should attempt the console */ 153 if (!(LogStat&LOG_CONS)) 154 return; 155 156 /* 157 * Output the message to the console; don't worry about blocking, 158 * if console blocks everything will. Make sure the error reported 159 * is the one from the syslogd failure. 160 */ 161 if ((fd = open(_PATH_CONSOLE, O_WRONLY, 0)) >= 0) { 162 (void)strcat(tbuf, "\r\n"); 163 cnt += 2; 164 p = index(tbuf, '>') + 1; 165 (void)write(fd, p, cnt - (p - tbuf)); 166 (void)close(fd); 167 } 168 } 169 170 static struct sockaddr SyslogAddr; /* AF_UNIX address of local logger */ 171 172 void 173 openlog(ident, logstat, logfac) 174 const char *ident; 175 int logstat, logfac; 176 { 177 if (ident != NULL) 178 LogTag = ident; 179 LogStat = logstat; 180 if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0) 181 LogFacility = logfac; 182 183 if (LogFile == -1) { 184 SyslogAddr.sa_family = AF_UNIX; 185 (void)strncpy(SyslogAddr.sa_data, _PATH_LOG, 186 sizeof(SyslogAddr.sa_data)); 187 if (LogStat & LOG_NDELAY) { 188 if ((LogFile = socket(AF_UNIX, SOCK_DGRAM, 0)) == -1) 189 return; 190 (void)fcntl(LogFile, F_SETFD, 1); 191 } 192 } 193 if (LogFile != -1 && !connected) 194 if (connect(LogFile, &SyslogAddr, sizeof(SyslogAddr)) == -1) { 195 (void)close(LogFile); 196 LogFile = -1; 197 } else 198 connected = 1; 199 } 200 201 void 202 closelog() 203 { 204 (void)close(LogFile); 205 LogFile = -1; 206 connected = 0; 207 } 208 209 /* setlogmask -- set the log mask level */ 210 int 211 setlogmask(pmask) 212 int pmask; 213 { 214 int omask; 215 216 omask = LogMask; 217 if (pmask != 0) 218 LogMask = pmask; 219 return (omask); 220 } 221