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.22 (Berkeley) 08/02/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 pid, 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 /* output the message to the console */ 148 pid = vfork(); 149 if (pid == -1) 150 return; 151 if (pid == 0) { 152 int fd; 153 long sigsetmask(); 154 155 (void)signal(SIGALRM, SIG_DFL); 156 sigsetmask((long)~sigmask(SIGALRM)); 157 (void)alarm((u_int)5); 158 if ((fd = open(_PATH_CONSOLE, O_WRONLY, 0)) < 0) 159 return; 160 (void)alarm((u_int)0); 161 (void)strcat(tbuf, "\r"); 162 p = index(tbuf, '>') + 1; 163 (void)write(fd, p, cnt + 1 - (p - tbuf)); 164 (void)close(fd); 165 _exit(0); 166 } 167 if (!(LogStat & LOG_NOWAIT)) 168 (void)waitpid(pid, (union wait *)NULL, WSIGRESTART); 169 } 170 171 static struct sockaddr SyslogAddr; /* AF_UNIX address of local logger */ 172 /* 173 * OPENLOG -- open system log 174 */ 175 openlog(ident, logstat, logfac) 176 char *ident; 177 int logstat, logfac; 178 { 179 if (ident != NULL) 180 LogTag = ident; 181 LogStat = logstat; 182 if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0) 183 LogFacility = logfac; 184 if (LogFile == -1) { 185 SyslogAddr.sa_family = AF_UNIX; 186 strncpy(SyslogAddr.sa_data, _PATH_LOGNAME, 187 sizeof(SyslogAddr.sa_data)); 188 if (LogStat & LOG_NDELAY) { 189 LogFile = socket(AF_UNIX, SOCK_DGRAM, 0); 190 fcntl(LogFile, F_SETFD, 1); 191 } 192 } 193 if (LogFile != -1 && !connected && 194 connect(LogFile, &SyslogAddr, sizeof(SyslogAddr)) != -1) 195 connected = 1; 196 } 197 198 /* 199 * CLOSELOG -- close the system log 200 */ 201 closelog() 202 { 203 (void) close(LogFile); 204 LogFile = -1; 205 connected = 0; 206 } 207 208 static int LogMask = 0xff; /* mask of priorities to be logged */ 209 /* 210 * SETLOGMASK -- set the log mask level 211 */ 212 setlogmask(pmask) 213 int pmask; 214 { 215 int omask; 216 217 omask = LogMask; 218 if (pmask != 0) 219 LogMask = pmask; 220 return (omask); 221 } 222