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