xref: /csrg-svn/lib/libc/gen/syslog.c (revision 16440)
112742Ssam #ifndef lint
2*16440Sralph static char SccsId[] =	"@(#)syslog.c	4.3 (Berkeley) 05/04/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.
1016414Sralph  * Also:
1116414Sralph  *	adds a timestamp,
1216414Sralph  *	prints the module name in front of the message,
1316414Sralph  *	has some other formatting types (or will sometime),
1416414Sralph  *	adds a newline on the end of the message.
1512742Ssam  *
1616414Sralph  * The output of this routine is intended to be read by /etc/syslogd.
1712742Ssam  */
1816414Sralph 
1912742Ssam #include <sys/types.h>
2012742Ssam #include <sys/socket.h>
2116414Sralph #include <sys/file.h>
2212742Ssam #include <syslog.h>
2312742Ssam #include <netdb.h>
2412742Ssam 
2516414Sralph #define	MAXLINE	1024			/* max message size */
2616414Sralph #define NULL	0			/* manifest */
2712742Ssam 
2816414Sralph static char	logname[] = "/dev/log";
2916414Sralph static char	ctty[] = "/dev/console";
3012742Ssam 
3116414Sralph static int	LogFile = -1;		/* fd for log */
3216414Sralph static int	LogStat	= 0;		/* status bits, set by openlog() */
3316414Sralph static char	*LogTag = NULL;		/* string to tag the entry with */
3416414Sralph static int	LogMask = LOG_DEBUG;	/* lowest priority to be logged */
3512742Ssam 
3616414Sralph static struct sockaddr SyslogAddr;
3716414Sralph 
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 {
4516414Sralph 	char buf[MAXLINE + 1], outline[MAXLINE + 1];
4616414Sralph 	register char *b, *f, *o;
4716414Sralph 	register int c;
4816414Sralph 	long now;
4916414Sralph 	int pid;
5012742Ssam 
5112742Ssam 	/* see if we should just throw out this message */
5212742Ssam 	if (pri > LogMask)
5312742Ssam 		return;
5416414Sralph 	if (LogFile < 0)
5516414Sralph 		openlog(NULL, 0, 0);
5616414Sralph 	o = outline;
5716414Sralph 	if (pri > 0) {
5816414Sralph 		sprintf(o, "<%d>", pri);
5916414Sralph 		o += strlen(o);
6016414Sralph 	}
6116414Sralph 	if (LogTag) {
6216414Sralph 		strcpy(o, LogTag);
6316414Sralph 		o += strlen(o);
6416414Sralph 	}
6516414Sralph 	if (LogStat & LOG_PID) {
6616414Sralph 		sprintf(o, " (%d)", getpid());
6716414Sralph 		o += strlen(o);
6816414Sralph 	}
6916414Sralph 	time(&now);
7016414Sralph 	sprintf(o, " %.15s -- ", ctime(&now) + 4);
7116414Sralph 	o += strlen(o);
7212742Ssam 
7316414Sralph 	b = buf;
7416414Sralph 	f = fmt;
7516414Sralph 	while ((c = *f++) != '\0' && c != '\n' && b < &buf[MAXLINE]) {
7616414Sralph 		if (c != '%') {
7716414Sralph 			*b++ = c;
7816414Sralph 			continue;
7912742Ssam 		}
8016414Sralph 		if ((c = *f++) != 'm') {
8116414Sralph 			*b++ = '%';
8216414Sralph 			*b++ = c;
8316414Sralph 			continue;
8412742Ssam 		}
8516414Sralph 		if ((unsigned)errno > sys_nerr)
8616414Sralph 			sprintf(b, "error %d", errno);
8712742Ssam 		else
8816414Sralph 			strcpy(b, sys_errlist[errno]);
8916414Sralph 		b += strlen(b);
9012742Ssam 	}
9116414Sralph 	*b++ = '\n';
9216414Sralph 	*b = '\0';
9316414Sralph 	sprintf(o, buf, p0, p1, p2, p3, p4);
9416414Sralph 	c = strlen(outline);
9516414Sralph 	if (c > MAXLINE)
9616414Sralph 		c = MAXLINE;
9716414Sralph 	if (sendto(LogFile, outline, c, 0, &SyslogAddr, sizeof SyslogAddr) >= 0)
9816414Sralph 		return;
9916414Sralph 	if (pri > LOG_CRIT)
10016414Sralph 		return;
10116414Sralph 	pid = fork();
10216414Sralph 	if (pid == -1)
10316414Sralph 		return;
10416414Sralph 	if (pid == 0) {
10516414Sralph 		LogFile = open(ctty, O_RDWR);
10616414Sralph 		write(LogFile, outline, c);
10716414Sralph 		close(LogFile);
10816414Sralph 		exit(0);
10916414Sralph 	}
11016414Sralph 	while (wait((int *)0) != pid)
11116414Sralph 		;
11212742Ssam }
11312742Ssam 
11412742Ssam /*
11512742Ssam  * OPENLOG -- open system log
11612742Ssam  */
11716414Sralph openlog(ident, logstat, logmask)
11812742Ssam 	char *ident;
11916414Sralph 	int logstat, logmask;
12012742Ssam {
12112742Ssam 
12212742Ssam 	LogTag = ident;
12312742Ssam 	LogStat = logstat;
12416414Sralph 	if (logmask > 0 && logmask <= LOG_DEBUG)
12516414Sralph 		LogMask = logmask;
12612742Ssam 	if (LogFile >= 0)
12712742Ssam 		return;
12816414Sralph 	SyslogAddr.sa_family = AF_UNIX;
12916414Sralph 	strncpy(SyslogAddr.sa_data, logname, sizeof SyslogAddr.sa_data);
13016414Sralph 	LogFile = socket(AF_UNIX, SOCK_DGRAM, 0);
131*16440Sralph 	fcntl(LogFile, F_SETFD, 1);
13212742Ssam }
13312742Ssam 
13412742Ssam /*
13512742Ssam  * CLOSELOG -- close the system log
13612742Ssam  */
13712742Ssam closelog()
13812742Ssam {
13912742Ssam 
14012742Ssam 	(void) close(LogFile);
14112742Ssam 	LogFile = -1;
14212742Ssam }
14316414Sralph 
14416414Sralph /*
14516414Sralph  * SETLOGMASK -- set the log mask level
14616414Sralph  */
14716414Sralph setlogmask(pri)
14816414Sralph 	int pri;
14916414Sralph {
15016414Sralph 	int opri;
15116414Sralph 
15216414Sralph 	opri = LogMask;
15316414Sralph 	LogMask = pri;
15416414Sralph 	return (opri);
15516414Sralph }
156