xref: /csrg-svn/lib/libc/gen/syslog.c (revision 36443)
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.19 (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 	register char *fmt;
67 	va_list ap;
68 {
69 	extern int errno;
70 	register int cnt;
71 	register char *p;
72 	time_t now, time();
73 	int pid, saved_errno;
74 	char tbuf[2048], fmt_cpy[1024], *ctime();
75 
76 	saved_errno = errno;
77 
78 	/* see if we should just throw out this message */
79 	if ((u_int)LOG_FAC(pri) >= LOG_NFACILITIES ||
80 	    !LOG_MASK(LOG_PRI(pri)) || (pri &~ (LOG_PRIMASK|LOG_FACMASK)))
81 		return;
82 	if (LogFile < 0 || !connected)
83 		openlog(LogTag, LogStat | LOG_NDELAY, 0);
84 
85 	/* set default facility if none specified */
86 	if ((pri & LOG_FACMASK) == 0)
87 		pri |= LogFacility;
88 
89 	/* build the message */
90 	(void)time(&now);
91 	(void)sprintf(tbuf, "<%d>%.15s ", pri, ctime(&now) + 4);
92 	for (p = tbuf; *p; ++p);
93 	if (LogTag) {
94 		(void)strcpy(p, LogTag);
95 		for (; *p; ++p);
96 	}
97 	if (LogStat & LOG_PID) {
98 		(void)sprintf(p, "[%d]", getpid());
99 		for (; *p; ++p);
100 	}
101 	if (LogTag) {
102 		*p++ = ':';
103 		*p++ = ' ';
104 	}
105 
106 	/* substitute error message for %m */
107 	{
108 		register char ch, *t1, *t2;
109 		char *strerror();
110 
111 		for (t1 = fmt_cpy; ch = *fmt; ++fmt)
112 			if (ch == '%' && fmt[1] == 'm') {
113 				++fmt;
114 				for (t2 = strerror(saved_errno);
115 				    *t1 = *t2++; ++t1);
116 			}
117 			else
118 				*t1++ = ch;
119 	}
120 
121 	(void)vsprintf(p, fmt_cpy, ap);
122 
123 	/* output the message to the local logger */
124 	if (send(LogFile, tbuf, cnt = strlen(tbuf), 0) >= 0 ||
125 	    !(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 		int fd;
134 		long sigsetmask();
135 
136 		(void)signal(SIGALRM, SIG_DFL);
137 		sigsetmask((long)~sigmask(SIGALRM));
138 		(void)alarm((u_int)5);
139 		if ((fd = open(CONSOLE, O_WRONLY, 0)) < 0)
140 			return;
141 		(void)alarm((u_int)0);
142 		(void)strcat(tbuf, "\r");
143 		p = index(tbuf, '>') + 1;
144 		(void)write(fd, p, cnt + 1 - (p - tbuf));
145 		(void)close(fd);
146 		_exit(0);
147 	}
148 	if (!(LogStat & LOG_NOWAIT))
149 		while ((cnt = wait((int *)0)) > 0 && cnt != pid);
150 }
151 
152 static struct sockaddr SyslogAddr;	/* AF_UNIX address of local logger */
153 /*
154  * OPENLOG -- open system log
155  */
156 openlog(ident, logstat, logfac)
157 	char *ident;
158 	int logstat, logfac;
159 {
160 	if (ident != NULL)
161 		LogTag = ident;
162 	LogStat = logstat;
163 	if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0)
164 		LogFacility = logfac;
165 	if (LogFile == -1) {
166 		SyslogAddr.sa_family = AF_UNIX;
167 		strncpy(SyslogAddr.sa_data, LOGNAME, sizeof SyslogAddr.sa_data);
168 		if (LogStat & LOG_NDELAY) {
169 			LogFile = socket(AF_UNIX, SOCK_DGRAM, 0);
170 			fcntl(LogFile, F_SETFD, 1);
171 		}
172 	}
173 	if (LogFile != -1 && !connected &&
174 	    connect(LogFile, &SyslogAddr, sizeof(SyslogAddr)) != -1)
175 		connected = 1;
176 }
177 
178 /*
179  * CLOSELOG -- close the system log
180  */
181 closelog()
182 {
183 	(void) close(LogFile);
184 	LogFile = -1;
185 	connected = 0;
186 }
187 
188 static int	LogMask = 0xff;		/* mask of priorities to be logged */
189 /*
190  * SETLOGMASK -- set the log mask level
191  */
192 setlogmask(pmask)
193 	int pmask;
194 {
195 	int omask;
196 
197 	omask = LogMask;
198 	if (pmask != 0)
199 		LogMask = pmask;
200 	return (omask);
201 }
202