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.18 (Berkeley) 12/19/88"; 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 <netdb.h> 44 #include <strings.h> 45 #include <varargs.h> 46 #include <stdio.h> 47 48 #define LOGNAME "/dev/log" 49 #define CONSOLE "/dev/console" 50 51 static int LogFile = -1; /* fd for log */ 52 static int connected; /* have done connect */ 53 static int LogStat = 0; /* status bits, set by openlog() */ 54 static char *LogTag = "syslog"; /* string to tag the entry with */ 55 static int LogFacility = LOG_USER; /* default facility code */ 56 57 syslog(pri, fmt, args) 58 int pri, args; 59 char *fmt; 60 { 61 vsyslog(pri, fmt, &args); 62 } 63 64 vsyslog(pri, fmt, ap) 65 int pri; 66 char *fmt; 67 va_list ap; 68 { 69 register int cnt; 70 register char *p; 71 time_t now, time(); 72 int pid; 73 char tbuf[2048], *ctime(); 74 75 /* see if we should just throw out this message */ 76 if ((u_int)LOG_FAC(pri) >= LOG_NFACILITIES || 77 !LOG_MASK(LOG_PRI(pri)) || (pri &~ (LOG_PRIMASK|LOG_FACMASK))) 78 return; 79 if (LogFile < 0 || !connected) 80 openlog(LogTag, LogStat | LOG_NDELAY, 0); 81 82 /* set default facility if none specified */ 83 if ((pri & LOG_FACMASK) == 0) 84 pri |= LogFacility; 85 86 /* build the message */ 87 (void)time(&now); 88 (void)sprintf(tbuf, "<%d>%.15s ", pri, ctime(&now) + 4); 89 for (p = tbuf; *p; ++p); 90 if (LogTag) { 91 (void)strcpy(p, LogTag); 92 for (; *p; ++p); 93 } 94 if (LogStat & LOG_PID) { 95 (void)sprintf(p, "[%d]", getpid()); 96 for (; *p; ++p); 97 } 98 if (LogTag) { 99 *p++ = ':'; 100 *p++ = ' '; 101 } 102 103 (void)vsprintf(p, fmt, ap); 104 105 /* output the message to the local logger */ 106 if (send(LogFile, tbuf, cnt = strlen(tbuf), 0) >= 0 || 107 !(LogStat&LOG_CONS)) 108 return; 109 110 /* output the message to the console */ 111 pid = vfork(); 112 if (pid == -1) 113 return; 114 if (pid == 0) { 115 int fd; 116 long sigsetmask(); 117 118 (void)signal(SIGALRM, SIG_DFL); 119 sigsetmask((long)~sigmask(SIGALRM)); 120 (void)alarm((u_int)5); 121 if ((fd = open(CONSOLE, O_WRONLY, 0)) < 0) 122 return; 123 (void)alarm((u_int)0); 124 (void)strcat(tbuf, "\r"); 125 p = index(tbuf, '>') + 1; 126 (void)write(fd, p, cnt + 1 - (p - tbuf)); 127 (void)close(fd); 128 _exit(0); 129 } 130 if (!(LogStat & LOG_NOWAIT)) 131 while ((cnt = wait((int *)0)) > 0 && cnt != pid); 132 } 133 134 static struct sockaddr SyslogAddr; /* AF_UNIX address of local logger */ 135 /* 136 * OPENLOG -- open system log 137 */ 138 openlog(ident, logstat, logfac) 139 char *ident; 140 int logstat, logfac; 141 { 142 if (ident != NULL) 143 LogTag = ident; 144 LogStat = logstat; 145 if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0) 146 LogFacility = logfac; 147 if (LogFile == -1) { 148 SyslogAddr.sa_family = AF_UNIX; 149 strncpy(SyslogAddr.sa_data, LOGNAME, sizeof SyslogAddr.sa_data); 150 if (LogStat & LOG_NDELAY) { 151 LogFile = socket(AF_UNIX, SOCK_DGRAM, 0); 152 fcntl(LogFile, F_SETFD, 1); 153 } 154 } 155 if (LogFile != -1 && !connected && 156 connect(LogFile, &SyslogAddr, sizeof(SyslogAddr)) != -1) 157 connected = 1; 158 } 159 160 /* 161 * CLOSELOG -- close the system log 162 */ 163 closelog() 164 { 165 (void) close(LogFile); 166 LogFile = -1; 167 connected = 0; 168 } 169 170 static int LogMask = 0xff; /* mask of priorities to be logged */ 171 /* 172 * SETLOGMASK -- set the log mask level 173 */ 174 setlogmask(pmask) 175 int pmask; 176 { 177 int omask; 178 179 omask = LogMask; 180 if (pmask != 0) 181 LogMask = pmask; 182 return (omask); 183 } 184