delete extraneous definitions
[unix-history] / usr / src / lib / libc / gen / syslog.c
CommitLineData
b97c7a10 1#ifndef lint
043efade 2static char sccsid[] = "@(#)syslog.c 4.7 (Berkeley) %G%";
b97c7a10
SL
3#endif
4
5/*
6 * SYSLOG -- print message on log file
7 *
8 * This routine looks a lot like printf, except that it
9 * outputs to the log file instead of the standard output.
bc51229a
RC
10 * Also:
11 * adds a timestamp,
12 * prints the module name in front of the message,
13 * has some other formatting types (or will sometime),
14 * adds a newline on the end of the message.
b97c7a10 15 *
bc51229a 16 * The output of this routine is intended to be read by /etc/syslogd.
4d707151
RC
17 *
18 * Author: Eric Allman
19 * Modified to use UNIX domain IPC by Ralph Campbell
b97c7a10 20 */
bc51229a 21
b97c7a10
SL
22#include <sys/types.h>
23#include <sys/socket.h>
bc51229a 24#include <sys/file.h>
b97c7a10
SL
25#include <syslog.h>
26#include <netdb.h>
27
bc51229a
RC
28#define MAXLINE 1024 /* max message size */
29#define NULL 0 /* manifest */
30
5cd7a5ab
RC
31#define mask(p) (1 << (p))
32#define IMPORTANT (mask(KERN_EMERG)|mask(KERN_ALERT)|mask(KERN_ERR)|mask(KERN_FAIL)\
33 |mask(KERN_RECOV)|mask(KERN_INFO)|mask(LOG_EMERG)|mask(LOG_ALERT)\
34 |mask(LOG_CRIT)|mask(LOG_ERR)|mask(LOG_FAIL))
35
bc51229a
RC
36static char logname[] = "/dev/log";
37static char ctty[] = "/dev/console";
b97c7a10 38
bc51229a
RC
39static int LogFile = -1; /* fd for log */
40static int LogStat = 0; /* status bits, set by openlog() */
41static char *LogTag = NULL; /* string to tag the entry with */
043efade
RC
42 /* mask of priorities to be logged */
43static int LogMask = ~(mask(KERN_EMERG)|mask(KERN_ALERT)|mask(KERN_ERR)|
44 mask(KERN_FAIL)|mask(KERN_RECOV)|mask(KERN_INFO));
b97c7a10 45
bc51229a 46static struct sockaddr SyslogAddr;
b97c7a10
SL
47
48extern int errno, sys_nerr;
49extern char *sys_errlist[];
50
51syslog(pri, fmt, p0, p1, p2, p3, p4)
52 int pri;
53 char *fmt;
54{
bc51229a
RC
55 char buf[MAXLINE + 1], outline[MAXLINE + 1];
56 register char *b, *f, *o;
57 register int c;
58 long now;
4d707151 59 int pid, olderrno = errno;
b97c7a10 60
b97c7a10 61 /* see if we should just throw out this message */
5cd7a5ab 62 if (pri <= 0 || pri >= 32 || (mask(pri) & LogMask) == 0)
b97c7a10 63 return;
bc51229a 64 if (LogFile < 0)
5cd7a5ab 65 openlog(LogTag, LogStat & ~LOG_ODELAY, 0);
bc51229a 66 o = outline;
4d707151
RC
67 sprintf(o, "<%d>", pri);
68 o += strlen(o);
bc51229a
RC
69 if (LogTag) {
70 strcpy(o, LogTag);
71 o += strlen(o);
72 }
73 if (LogStat & LOG_PID) {
4d707151 74 sprintf(o, "[%d]", getpid());
bc51229a
RC
75 o += strlen(o);
76 }
77 time(&now);
4d707151 78 sprintf(o, ": %.15s-- ", ctime(&now) + 4);
bc51229a 79 o += strlen(o);
b97c7a10 80
bc51229a
RC
81 b = buf;
82 f = fmt;
83 while ((c = *f++) != '\0' && c != '\n' && b < &buf[MAXLINE]) {
84 if (c != '%') {
85 *b++ = c;
86 continue;
b97c7a10 87 }
bc51229a
RC
88 if ((c = *f++) != 'm') {
89 *b++ = '%';
90 *b++ = c;
91 continue;
b97c7a10 92 }
4d707151
RC
93 if ((unsigned)olderrno > sys_nerr)
94 sprintf(b, "error %d", olderrno);
b97c7a10 95 else
4d707151 96 strcpy(b, sys_errlist[olderrno]);
bc51229a 97 b += strlen(b);
b97c7a10 98 }
bc51229a
RC
99 *b++ = '\n';
100 *b = '\0';
101 sprintf(o, buf, p0, p1, p2, p3, p4);
102 c = strlen(outline);
103 if (c > MAXLINE)
104 c = MAXLINE;
105 if (sendto(LogFile, outline, c, 0, &SyslogAddr, sizeof SyslogAddr) >= 0)
106 return;
5cd7a5ab 107 if (!(LogStat & LOG_CONS) && !(mask(pri) & IMPORTANT))
bc51229a
RC
108 return;
109 pid = fork();
110 if (pid == -1)
111 return;
112 if (pid == 0) {
113 LogFile = open(ctty, O_RDWR);
5cd7a5ab
RC
114 strcat(o, "\r");
115 write(LogFile, outline, c+1);
bc51229a
RC
116 close(LogFile);
117 exit(0);
118 }
4d707151 119 while ((c = wait((int *)0)) > 0 && c != pid)
bc51229a 120 ;
b97c7a10
SL
121}
122
123/*
124 * OPENLOG -- open system log
125 */
bc51229a 126openlog(ident, logstat, logmask)
b97c7a10 127 char *ident;
bc51229a 128 int logstat, logmask;
b97c7a10 129{
b97c7a10 130
4d707151 131 LogTag = (ident != NULL) ? ident : "syslog";
b97c7a10 132 LogStat = logstat;
df714f23 133 if (logmask != 0)
bc51229a 134 LogMask = logmask;
b97c7a10
SL
135 if (LogFile >= 0)
136 return;
bc51229a
RC
137 SyslogAddr.sa_family = AF_UNIX;
138 strncpy(SyslogAddr.sa_data, logname, sizeof SyslogAddr.sa_data);
5cd7a5ab
RC
139 if (!(LogStat & LOG_ODELAY)) {
140 LogFile = socket(AF_UNIX, SOCK_DGRAM, 0);
141 fcntl(LogFile, F_SETFD, 1);
142 }
b97c7a10
SL
143}
144
145/*
146 * CLOSELOG -- close the system log
147 */
148closelog()
149{
150
151 (void) close(LogFile);
152 LogFile = -1;
153}
bc51229a
RC
154
155/*
156 * SETLOGMASK -- set the log mask level
157 */
df714f23
RC
158setlogmask(pmask)
159 int pmask;
bc51229a 160{
df714f23 161 int omask;
bc51229a 162
df714f23
RC
163 omask = LogMask;
164 if (pmask != 0)
165 LogMask = pmask;
166 return (omask);
bc51229a 167}