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