xref: /csrg-svn/lib/libc/gen/syslog.c (revision 27343)
1 /*
2  * Copyright (c) 1983 Regents of the University of California.
3  * All rights reserved.  The Berkeley software License Agreement
4  * specifies the terms and conditions for redistribution.
5  */
6 
7 #if defined(LIBC_SCCS) && !defined(lint)
8 static char sccsid[] = "@(#)syslog.c	5.8 (Berkeley) 04/24/86";
9 #endif LIBC_SCCS and not lint
10 
11 /*
12  * SYSLOG -- print message on log file
13  *
14  * This routine looks a lot like printf, except that it
15  * outputs to the log file instead of the standard output.
16  * Also:
17  *	adds a timestamp,
18  *	prints the module name in front of the message,
19  *	has some other formatting types (or will sometime),
20  *	adds a newline on the end of the message.
21  *
22  * The output of this routine is intended to be read by /etc/syslogd.
23  *
24  * Author: Eric Allman
25  * Modified to use UNIX domain IPC by Ralph Campbell
26  */
27 
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <sys/file.h>
31 #include <sys/signal.h>
32 #include <sys/syslog.h>
33 #include <netdb.h>
34 #include <strings.h>
35 
36 #define	MAXLINE	1024			/* max message size */
37 #define NULL	0			/* manifest */
38 
39 #define PRIMASK(p)	(1 << ((p) & LOG_PRIMASK))
40 #define PRIFAC(p)	(((p) & LOG_FACMASK) >> 3)
41 #define IMPORTANT 	LOG_ERR
42 
43 static char	logname[] = "/dev/log";
44 static char	ctty[] = "/dev/console";
45 
46 static int	LogFile = -1;		/* fd for log */
47 static int	LogStat	= 0;		/* status bits, set by openlog() */
48 static char	*LogTag = "syslog";	/* string to tag the entry with */
49 static int	LogMask = 0xff;		/* mask of priorities to be logged */
50 static int	LogFacility = LOG_USER;	/* default facility code */
51 
52 static struct sockaddr SyslogAddr;	/* AF_UNIX address of local logger */
53 
54 extern	int errno, sys_nerr;
55 extern	char *sys_errlist[];
56 
57 syslog(pri, fmt, p0, p1, p2, p3, p4)
58 	int pri;
59 	char *fmt;
60 {
61 	char buf[MAXLINE + 1], outline[MAXLINE + 1];
62 	register char *b, *f, *o;
63 	register int c;
64 	long now;
65 	int pid, olderrno = errno;
66 
67 	/* see if we should just throw out this message */
68 	if (pri <= 0 || PRIFAC(pri) >= LOG_NFACILITIES || (PRIMASK(pri) & LogMask) == 0)
69 		return;
70 	if (LogFile < 0)
71 		openlog(LogTag, LogStat | LOG_NDELAY, 0);
72 
73 	/* set default facility if none specified */
74 	if ((pri & LOG_FACMASK) == 0)
75 		pri |= LogFacility;
76 
77 	/* build the message */
78 	o = outline;
79 	sprintf(o, "<%d>", pri);
80 	o += strlen(o);
81 	time(&now);
82 	sprintf(o, "%.15s ", ctime(&now) + 4);
83 	o += strlen(o);
84 	if (LogTag) {
85 		strcpy(o, LogTag);
86 		o += strlen(o);
87 	}
88 	if (LogStat & LOG_PID) {
89 		sprintf(o, "[%d]", getpid());
90 		o += strlen(o);
91 	}
92 	if (LogTag) {
93 		strcpy(o, ": ");
94 		o += 2;
95 	}
96 
97 	b = buf;
98 	f = fmt;
99 	while ((c = *f++) != '\0' && c != '\n' && b < &buf[MAXLINE]) {
100 		if (c != '%') {
101 			*b++ = c;
102 			continue;
103 		}
104 		if ((c = *f++) != 'm') {
105 			*b++ = '%';
106 			*b++ = c;
107 			continue;
108 		}
109 		if ((unsigned)olderrno > sys_nerr)
110 			sprintf(b, "error %d", olderrno);
111 		else
112 			strcpy(b, sys_errlist[olderrno]);
113 		b += strlen(b);
114 	}
115 	*b++ = '\n';
116 	*b = '\0';
117 	sprintf(o, buf, p0, p1, p2, p3, p4);
118 	c = strlen(outline);
119 	if (c > MAXLINE)
120 		c = MAXLINE;
121 
122 	/* output the message to the local logger */
123 	if (sendto(LogFile, outline, c, 0, &SyslogAddr, sizeof SyslogAddr) >= 0)
124 		return;
125 	if (!(LogStat & LOG_CONS))
126 		return;
127 
128 	/* output the message to the console */
129 	pid = vfork();
130 	if (pid == -1)
131 		return;
132 	if (pid == 0) {
133 		signal(SIGALRM, SIG_DFL);
134 		sigsetmask(sigblock(0) & ~sigmask(SIGALRM));
135 		alarm(5);
136 		LogFile = open(ctty, O_WRONLY);
137 		alarm(0);
138 		strcat(o, "\r");
139 		o = index(outline, '>') + 1;
140 		write(LogFile, o, c + 1 - (o - outline));
141 		close(LogFile);
142 		exit(0);
143 	}
144 	if (!(LogStat & LOG_NOWAIT))
145 		while ((c = wait((int *)0)) > 0 && c != pid)
146 			;
147 }
148 
149 /*
150  * OPENLOG -- open system log
151  */
152 
153 openlog(ident, logstat, logfac)
154 	char *ident;
155 	int logstat, logfac;
156 {
157 	if (ident != NULL)
158 		LogTag = ident;
159 	LogStat = logstat;
160 	if (logfac != 0)
161 		LogFacility = logfac & LOG_FACMASK;
162 	if (LogFile >= 0)
163 		return;
164 	SyslogAddr.sa_family = AF_UNIX;
165 	strncpy(SyslogAddr.sa_data, logname, sizeof SyslogAddr.sa_data);
166 	if (LogStat & LOG_NDELAY) {
167 		LogFile = socket(AF_UNIX, SOCK_DGRAM, 0);
168 		fcntl(LogFile, F_SETFD, 1);
169 	}
170 }
171 
172 /*
173  * CLOSELOG -- close the system log
174  */
175 
176 closelog()
177 {
178 
179 	(void) close(LogFile);
180 	LogFile = -1;
181 }
182 
183 /*
184  * SETLOGMASK -- set the log mask level
185  */
186 setlogmask(pmask)
187 	int pmask;
188 {
189 	int omask;
190 
191 	omask = LogMask;
192 	if (pmask != 0)
193 		LogMask = pmask;
194 	return (omask);
195 }
196