1 /* 2 * Copyright (c) 1983, 1988 Regents of the University of California. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms are permitted 6 * provided that the above copyright notice and this paragraph are 7 * duplicated in all such forms and that any documentation, 8 * advertising materials, and other materials related to such 9 * distribution and use acknowledge that the software was developed 10 * by the University of California, Berkeley. The name of the 11 * University may not be used to endorse or promote products derived 12 * from this software without specific prior written permission. 13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 15 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 16 */ 17 18 #if defined(LIBC_SCCS) && !defined(lint) 19 static char sccsid[] = "@(#)syslog.c 5.23 (Berkeley) 08/04/89"; 20 #endif /* LIBC_SCCS and not lint */ 21 22 /* 23 * SYSLOG -- print message on log file 24 * 25 * This routine looks a lot like printf, except that it outputs to the 26 * log file instead of the standard output. Also: 27 * adds a timestamp, 28 * prints the module name in front of the message, 29 * has some other formatting types (or will sometime), 30 * adds a newline on the end of the message. 31 * 32 * The output of this routine is intended to be read by syslogd(8). 33 * 34 * Author: Eric Allman 35 * Modified to use UNIX domain IPC by Ralph Campbell 36 */ 37 38 #include <sys/types.h> 39 #include <sys/socket.h> 40 #include <sys/file.h> 41 #include <sys/signal.h> 42 #include <sys/syslog.h> 43 #include <sys/uio.h> 44 #include <sys/wait.h> 45 #include <netdb.h> 46 #include <strings.h> 47 #include <varargs.h> 48 #include <paths.h> 49 #include <stdio.h> 50 51 #define _PATH_LOGNAME "/dev/log" 52 53 static int LogFile = -1; /* fd for log */ 54 static int connected; /* have done connect */ 55 static int LogStat = 0; /* status bits, set by openlog() */ 56 static char *LogTag = "syslog"; /* string to tag the entry with */ 57 static int LogFacility = LOG_USER; /* default facility code */ 58 59 syslog(pri, fmt, args) 60 int pri, args; 61 char *fmt; 62 { 63 vsyslog(pri, fmt, &args); 64 } 65 66 vsyslog(pri, fmt, ap) 67 int pri; 68 register char *fmt; 69 va_list ap; 70 { 71 extern int errno; 72 register int cnt; 73 register char *p; 74 time_t now, time(); 75 int fd, saved_errno; 76 char tbuf[2048], fmt_cpy[1024], *stdp, *ctime(); 77 78 saved_errno = errno; 79 80 /* see if we should just throw out this message */ 81 if ((u_int)LOG_FAC(pri) >= LOG_NFACILITIES || 82 !LOG_MASK(LOG_PRI(pri)) || (pri &~ (LOG_PRIMASK|LOG_FACMASK))) 83 return; 84 if (LogFile < 0 || !connected) 85 openlog(LogTag, LogStat | LOG_NDELAY, 0); 86 87 /* set default facility if none specified */ 88 if ((pri & LOG_FACMASK) == 0) 89 pri |= LogFacility; 90 91 /* build the message */ 92 (void)time(&now); 93 (void)sprintf(tbuf, "<%d>%.15s ", pri, ctime(&now) + 4); 94 for (p = tbuf; *p; ++p); 95 if (LogStat & LOG_PERROR) 96 stdp = p; 97 if (LogTag) { 98 (void)strcpy(p, LogTag); 99 for (; *p; ++p); 100 } 101 if (LogStat & LOG_PID) { 102 (void)sprintf(p, "[%d]", getpid()); 103 for (; *p; ++p); 104 } 105 if (LogTag) { 106 *p++ = ':'; 107 *p++ = ' '; 108 } 109 110 /* substitute error message for %m */ 111 { 112 register char ch, *t1, *t2; 113 char *strerror(); 114 115 for (t1 = fmt_cpy; ch = *fmt; ++fmt) 116 if (ch == '%' && fmt[1] == 'm') { 117 ++fmt; 118 for (t2 = strerror(saved_errno); 119 *t1 = *t2++; ++t1); 120 } 121 else 122 *t1++ = ch; 123 *t1 = '\0'; 124 } 125 126 (void)vsprintf(p, fmt_cpy, ap); 127 128 cnt = strlen(tbuf); 129 130 /* output to stderr if requested */ 131 if (LogStat & LOG_PERROR) { 132 struct iovec iov[2]; 133 register struct iovec *v = iov; 134 135 v->iov_base = stdp; 136 v->iov_len = cnt - (stdp - tbuf); 137 ++v; 138 v->iov_base = "\n"; 139 v->iov_len = 1; 140 (void)writev(2, iov, 2); 141 } 142 143 /* output the message to the local logger */ 144 if (send(LogFile, tbuf, cnt, 0) >= 0 || !(LogStat&LOG_CONS)) 145 return; 146 147 /* 148 * output the message to the console; don't worry about 149 * blocking, if console blocks everything will. 150 */ 151 if ((fd = open(_PATH_CONSOLE, O_WRONLY, 0)) < 0) 152 return; 153 (void)strcat(tbuf, "\r"); 154 p = index(tbuf, '>') + 1; 155 (void)write(fd, p, cnt + 1 - (p - tbuf)); 156 (void)close(fd); 157 } 158 159 static struct sockaddr SyslogAddr; /* AF_UNIX address of local logger */ 160 /* 161 * OPENLOG -- open system log 162 */ 163 openlog(ident, logstat, logfac) 164 char *ident; 165 int logstat, logfac; 166 { 167 if (ident != NULL) 168 LogTag = ident; 169 LogStat = logstat; 170 if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0) 171 LogFacility = logfac; 172 if (LogFile == -1) { 173 SyslogAddr.sa_family = AF_UNIX; 174 strncpy(SyslogAddr.sa_data, _PATH_LOGNAME, 175 sizeof(SyslogAddr.sa_data)); 176 if (LogStat & LOG_NDELAY) { 177 LogFile = socket(AF_UNIX, SOCK_DGRAM, 0); 178 fcntl(LogFile, F_SETFD, 1); 179 } 180 } 181 if (LogFile != -1 && !connected && 182 connect(LogFile, &SyslogAddr, sizeof(SyslogAddr)) != -1) 183 connected = 1; 184 } 185 186 /* 187 * CLOSELOG -- close the system log 188 */ 189 closelog() 190 { 191 (void) close(LogFile); 192 LogFile = -1; 193 connected = 0; 194 } 195 196 static int LogMask = 0xff; /* mask of priorities to be logged */ 197 /* 198 * SETLOGMASK -- set the log mask level 199 */ 200 setlogmask(pmask) 201 int pmask; 202 { 203 int omask; 204 205 omask = LogMask; 206 if (pmask != 0) 207 LogMask = pmask; 208 return (omask); 209 } 210