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.33 (Berkeley) 02/23/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 36 /* 37 * syslog, vsyslog -- 38 * print message on log file; output is intended for syslogd(8). 39 */ 40 void 41 #if __STDC__ 42 syslog(int pri, const char *fmt, ...) 43 #else 44 syslog(pri, fmt, va_alist) 45 int pri; 46 char *fmt; 47 va_dcl 48 #endif 49 { 50 va_list ap; 51 52 #if __STDC__ 53 va_start(ap, fmt); 54 #else 55 va_start(ap); 56 #endif 57 vsyslog(pri, fmt, ap); 58 va_end(ap); 59 } 60 61 void 62 vsyslog(pri, fmt, ap) 63 int pri; 64 register const char *fmt; 65 va_list ap; 66 { 67 register int cnt; 68 register char *p; 69 time_t now, time(); 70 int fd, saved_errno; 71 char tbuf[2048], fmt_cpy[1024], *stdp, *ctime(); 72 73 /* check for invalid bits or no priority set */ 74 if (!LOG_PRI(pri) || (pri &~ (LOG_PRIMASK|LOG_FACMASK))) 75 return; 76 77 saved_errno = errno; 78 79 /* set default facility if none specified */ 80 if ((pri & LOG_FACMASK) == 0) 81 pri |= LogFacility; 82 83 /* build the message */ 84 (void)time(&now); 85 (void)sprintf(tbuf, "<%d>%.15s ", pri, ctime(&now) + 4); 86 for (p = tbuf; *p; ++p); 87 if (LogStat & LOG_PERROR) 88 stdp = p; 89 if (LogTag) { 90 (void)strcpy(p, LogTag); 91 for (; *p; ++p); 92 } 93 if (LogStat & LOG_PID) { 94 (void)sprintf(p, "[%d]", getpid()); 95 for (; *p; ++p); 96 } 97 if (LogTag) { 98 *p++ = ':'; 99 *p++ = ' '; 100 } 101 102 /* substitute error message for %m */ 103 { 104 register char ch, *t1, *t2; 105 char *strerror(); 106 107 for (t1 = fmt_cpy; ch = *fmt; ++fmt) 108 if (ch == '%' && fmt[1] == 'm') { 109 ++fmt; 110 for (t2 = strerror(saved_errno); 111 *t1 = *t2++; ++t1); 112 } 113 else 114 *t1++ = ch; 115 *t1 = '\0'; 116 } 117 118 (void)vsprintf(p, fmt_cpy, ap); 119 120 cnt = strlen(tbuf); 121 122 /* output to stderr if requested */ 123 if (LogStat & LOG_PERROR) { 124 struct iovec iov[2]; 125 register struct iovec *v = iov; 126 127 v->iov_base = stdp; 128 v->iov_len = cnt - (stdp - tbuf); 129 ++v; 130 v->iov_base = "\n"; 131 v->iov_len = 1; 132 (void)writev(STDERR_FILENO, iov, 2); 133 } 134 135 /* get connected, output the message to the local logger */ 136 if (!connected) 137 openlog(LogTag, LogStat | LOG_NDELAY, 0); 138 if (send(LogFile, tbuf, cnt, 0) >= 0) 139 return; 140 141 /* see if should attempt the console */ 142 if (!(LogStat&LOG_CONS)) 143 return; 144 145 /* 146 * Output the message to the console; don't worry about blocking, 147 * if console blocks everything will. Make sure the error reported 148 * is the one from the syslogd failure. 149 */ 150 if ((fd = open(_PATH_CONSOLE, O_WRONLY, 0)) >= 0) { 151 (void)strcat(tbuf, "\r\n"); 152 cnt += 2; 153 p = index(tbuf, '>') + 1; 154 (void)write(fd, p, cnt - (p - tbuf)); 155 (void)close(fd); 156 } 157 } 158 159 static struct sockaddr SyslogAddr; /* AF_UNIX address of local logger */ 160 161 void 162 openlog(ident, logstat, logfac) 163 const char *ident; 164 int logstat, logfac; 165 { 166 if (ident != NULL) 167 LogTag = ident; 168 LogStat = logstat; 169 if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0) 170 LogFacility = logfac; 171 172 if (LogFile == -1) { 173 SyslogAddr.sa_family = AF_UNIX; 174 (void)strncpy(SyslogAddr.sa_data, _PATH_LOG, 175 sizeof(SyslogAddr.sa_data)); 176 if (LogStat & LOG_NDELAY) { 177 if ((LogFile = socket(AF_UNIX, SOCK_DGRAM, 0)) == -1) 178 return; 179 (void)fcntl(LogFile, F_SETFD, 1); 180 } 181 } 182 if (LogFile != -1 && !connected) 183 if (connect(LogFile, &SyslogAddr, sizeof(SyslogAddr)) == -1) { 184 (void)close(LogFile); 185 LogFile = -1; 186 } else 187 connected = 1; 188 } 189 190 void 191 closelog() 192 { 193 (void)close(LogFile); 194 LogFile = -1; 195 connected = 0; 196 } 197 198 static int LogMask = 0xff; /* mask of priorities to be logged */ 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