xref: /csrg-svn/lib/libc/gen/syslog.c (revision 16414)
112742Ssam #ifndef lint
2*16414Sralph static char SccsId[] =	"@(#)syslog.c	4.2 (Berkeley) 04/25/84";
312742Ssam #endif
412742Ssam 
512742Ssam /*
612742Ssam  * SYSLOG -- print message on log file
712742Ssam  *
812742Ssam  * This routine looks a lot like printf, except that it
912742Ssam  * outputs to the log file instead of the standard output.
10*16414Sralph  * Also:
11*16414Sralph  *	adds a timestamp,
12*16414Sralph  *	prints the module name in front of the message,
13*16414Sralph  *	has some other formatting types (or will sometime),
14*16414Sralph  *	adds a newline on the end of the message.
1512742Ssam  *
16*16414Sralph  * The output of this routine is intended to be read by /etc/syslogd.
1712742Ssam  */
18*16414Sralph 
1912742Ssam #include <sys/types.h>
2012742Ssam #include <sys/socket.h>
21*16414Sralph #include <sys/file.h>
2212742Ssam #include <syslog.h>
2312742Ssam #include <netdb.h>
2412742Ssam 
25*16414Sralph #define	MAXLINE	1024			/* max message size */
26*16414Sralph #define NULL	0			/* manifest */
2712742Ssam 
28*16414Sralph static char	logname[] = "/dev/log";
29*16414Sralph static char	ctty[] = "/dev/console";
3012742Ssam 
31*16414Sralph static int	LogFile = -1;		/* fd for log */
32*16414Sralph static int	LogStat	= 0;		/* status bits, set by openlog() */
33*16414Sralph static char	*LogTag = NULL;		/* string to tag the entry with */
34*16414Sralph static int	LogMask = LOG_DEBUG;	/* lowest priority to be logged */
3512742Ssam 
36*16414Sralph static struct sockaddr SyslogAddr;
37*16414Sralph 
3812742Ssam extern	int errno, sys_nerr;
3912742Ssam extern	char *sys_errlist[];
4012742Ssam 
4112742Ssam syslog(pri, fmt, p0, p1, p2, p3, p4)
4212742Ssam 	int pri;
4312742Ssam 	char *fmt;
4412742Ssam {
45*16414Sralph 	char buf[MAXLINE + 1], outline[MAXLINE + 1];
46*16414Sralph 	register char *b, *f, *o;
47*16414Sralph 	register int c;
48*16414Sralph 	long now;
49*16414Sralph 	int pid;
5012742Ssam 
5112742Ssam 	/* see if we should just throw out this message */
5212742Ssam 	if (pri > LogMask)
5312742Ssam 		return;
54*16414Sralph 	if (LogFile < 0)
55*16414Sralph 		openlog(NULL, 0, 0);
56*16414Sralph 	o = outline;
57*16414Sralph 	if (pri > 0) {
58*16414Sralph 		sprintf(o, "<%d>", pri);
59*16414Sralph 		o += strlen(o);
60*16414Sralph 	}
61*16414Sralph 	if (LogTag) {
62*16414Sralph 		strcpy(o, LogTag);
63*16414Sralph 		o += strlen(o);
64*16414Sralph 	}
65*16414Sralph 	if (LogStat & LOG_PID) {
66*16414Sralph 		sprintf(o, " (%d)", getpid());
67*16414Sralph 		o += strlen(o);
68*16414Sralph 	}
69*16414Sralph 	time(&now);
70*16414Sralph 	sprintf(o, " %.15s -- ", ctime(&now) + 4);
71*16414Sralph 	o += strlen(o);
7212742Ssam 
73*16414Sralph 	b = buf;
74*16414Sralph 	f = fmt;
75*16414Sralph 	while ((c = *f++) != '\0' && c != '\n' && b < &buf[MAXLINE]) {
76*16414Sralph 		if (c != '%') {
77*16414Sralph 			*b++ = c;
78*16414Sralph 			continue;
7912742Ssam 		}
80*16414Sralph 		if ((c = *f++) != 'm') {
81*16414Sralph 			*b++ = '%';
82*16414Sralph 			*b++ = c;
83*16414Sralph 			continue;
8412742Ssam 		}
85*16414Sralph 		if ((unsigned)errno > sys_nerr)
86*16414Sralph 			sprintf(b, "error %d", errno);
8712742Ssam 		else
88*16414Sralph 			strcpy(b, sys_errlist[errno]);
89*16414Sralph 		b += strlen(b);
9012742Ssam 	}
91*16414Sralph 	*b++ = '\n';
92*16414Sralph 	*b = '\0';
93*16414Sralph 	sprintf(o, buf, p0, p1, p2, p3, p4);
94*16414Sralph 	c = strlen(outline);
95*16414Sralph 	if (c > MAXLINE)
96*16414Sralph 		c = MAXLINE;
97*16414Sralph 	if (sendto(LogFile, outline, c, 0, &SyslogAddr, sizeof SyslogAddr) >= 0)
98*16414Sralph 		return;
99*16414Sralph 	if (pri > LOG_CRIT)
100*16414Sralph 		return;
101*16414Sralph 	pid = fork();
102*16414Sralph 	if (pid == -1)
103*16414Sralph 		return;
104*16414Sralph 	if (pid == 0) {
105*16414Sralph 		LogFile = open(ctty, O_RDWR);
106*16414Sralph 		write(LogFile, outline, c);
107*16414Sralph 		close(LogFile);
108*16414Sralph 		exit(0);
109*16414Sralph 	}
110*16414Sralph 	while (wait((int *)0) != pid)
111*16414Sralph 		;
11212742Ssam }
11312742Ssam 
11412742Ssam /*
11512742Ssam  * OPENLOG -- open system log
11612742Ssam  */
117*16414Sralph openlog(ident, logstat, logmask)
11812742Ssam 	char *ident;
119*16414Sralph 	int logstat, logmask;
12012742Ssam {
12112742Ssam 
12212742Ssam 	LogTag = ident;
12312742Ssam 	LogStat = logstat;
124*16414Sralph 	if (logmask > 0 && logmask <= LOG_DEBUG)
125*16414Sralph 		LogMask = logmask;
12612742Ssam 	if (LogFile >= 0)
12712742Ssam 		return;
128*16414Sralph 	SyslogAddr.sa_family = AF_UNIX;
129*16414Sralph 	strncpy(SyslogAddr.sa_data, logname, sizeof SyslogAddr.sa_data);
130*16414Sralph 	LogFile = socket(AF_UNIX, SOCK_DGRAM, 0);
13112742Ssam }
13212742Ssam 
13312742Ssam /*
13412742Ssam  * CLOSELOG -- close the system log
13512742Ssam  */
13612742Ssam closelog()
13712742Ssam {
13812742Ssam 
13912742Ssam 	(void) close(LogFile);
14012742Ssam 	LogFile = -1;
14112742Ssam }
142*16414Sralph 
143*16414Sralph /*
144*16414Sralph  * SETLOGMASK -- set the log mask level
145*16414Sralph  */
146*16414Sralph setlogmask(pri)
147*16414Sralph 	int pri;
148*16414Sralph {
149*16414Sralph 	int opri;
150*16414Sralph 
151*16414Sralph 	opri = LogMask;
152*16414Sralph 	LogMask = pri;
153*16414Sralph 	return (opri);
154*16414Sralph }
155