xref: /csrg-svn/lib/libc/gen/syslog.c (revision 36792)
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.21 (Berkeley) 02/15/89";
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 <sys/uio.h>
44 #include <netdb.h>
45 #include <strings.h>
46 #include <varargs.h>
47 #include <stdio.h>
48 
49 #define	LOGNAME	"/dev/log"
50 #define	CONSOLE	"/dev/console"
51 
52 static int	LogFile = -1;		/* fd for log */
53 static int	connected;		/* have done connect */
54 static int	LogStat = 0;		/* status bits, set by openlog() */
55 static char	*LogTag = "syslog";	/* string to tag the entry with */
56 static int	LogFacility = LOG_USER;	/* default facility code */
57 
58 syslog(pri, fmt, args)
59 	int pri, args;
60 	char *fmt;
61 {
62 	vsyslog(pri, fmt, &args);
63 }
64 
65 vsyslog(pri, fmt, ap)
66 	int pri;
67 	register char *fmt;
68 	va_list ap;
69 {
70 	extern int errno;
71 	register int cnt;
72 	register char *p;
73 	time_t now, time();
74 	int pid, saved_errno;
75 	char tbuf[2048], fmt_cpy[1024], *stdp, *ctime();
76 
77 	saved_errno = errno;
78 
79 	/* see if we should just throw out this message */
80 	if ((u_int)LOG_FAC(pri) >= LOG_NFACILITIES ||
81 	    !LOG_MASK(LOG_PRI(pri)) || (pri &~ (LOG_PRIMASK|LOG_FACMASK)))
82 		return;
83 	if (LogFile < 0 || !connected)
84 		openlog(LogTag, LogStat | LOG_NDELAY, 0);
85 
86 	/* set default facility if none specified */
87 	if ((pri & LOG_FACMASK) == 0)
88 		pri |= LogFacility;
89 
90 	/* build the message */
91 	(void)time(&now);
92 	(void)sprintf(tbuf, "<%d>%.15s ", pri, ctime(&now) + 4);
93 	for (p = tbuf; *p; ++p);
94 	if (LogStat & LOG_PERROR)
95 		stdp = p;
96 	if (LogTag) {
97 		(void)strcpy(p, LogTag);
98 		for (; *p; ++p);
99 	}
100 	if (LogStat & LOG_PID) {
101 		(void)sprintf(p, "[%d]", getpid());
102 		for (; *p; ++p);
103 	}
104 	if (LogTag) {
105 		*p++ = ':';
106 		*p++ = ' ';
107 	}
108 
109 	/* substitute error message for %m */
110 	{
111 		register char ch, *t1, *t2;
112 		char *strerror();
113 
114 		for (t1 = fmt_cpy; ch = *fmt; ++fmt)
115 			if (ch == '%' && fmt[1] == 'm') {
116 				++fmt;
117 				for (t2 = strerror(saved_errno);
118 				    *t1 = *t2++; ++t1);
119 			}
120 			else
121 				*t1++ = ch;
122 		*t1 = '\0';
123 	}
124 
125 	(void)vsprintf(p, fmt_cpy, ap);
126 
127 	cnt = strlen(tbuf);
128 
129 	/* output to stderr if requested */
130 	if (LogStat & LOG_PERROR) {
131 		struct iovec iov[2];
132 		register struct iovec *v = iov;
133 
134 		v->iov_base = stdp;
135 		v->iov_len = cnt - (stdp - tbuf);
136 		++v;
137 		v->iov_base = "\n";
138 		v->iov_len = 1;
139 		(void)writev(2, iov, 2);
140 	}
141 
142 	/* output the message to the local logger */
143 	if (send(LogFile, tbuf, cnt, 0) >= 0 ||
144 	    !(LogStat&LOG_CONS))
145 		return;
146 
147 	/* output the message to the console */
148 	pid = vfork();
149 	if (pid == -1)
150 		return;
151 	if (pid == 0) {
152 		int fd;
153 		long sigsetmask();
154 
155 		(void)signal(SIGALRM, SIG_DFL);
156 		sigsetmask((long)~sigmask(SIGALRM));
157 		(void)alarm((u_int)5);
158 		if ((fd = open(CONSOLE, O_WRONLY, 0)) < 0)
159 			return;
160 		(void)alarm((u_int)0);
161 		(void)strcat(tbuf, "\r");
162 		p = index(tbuf, '>') + 1;
163 		(void)write(fd, p, cnt + 1 - (p - tbuf));
164 		(void)close(fd);
165 		_exit(0);
166 	}
167 	if (!(LogStat & LOG_NOWAIT))
168 		while ((cnt = wait((int *)0)) > 0 && cnt != pid);
169 }
170 
171 static struct sockaddr SyslogAddr;	/* AF_UNIX address of local logger */
172 /*
173  * OPENLOG -- open system log
174  */
175 openlog(ident, logstat, logfac)
176 	char *ident;
177 	int logstat, logfac;
178 {
179 	if (ident != NULL)
180 		LogTag = ident;
181 	LogStat = logstat;
182 	if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0)
183 		LogFacility = logfac;
184 	if (LogFile == -1) {
185 		SyslogAddr.sa_family = AF_UNIX;
186 		strncpy(SyslogAddr.sa_data, LOGNAME, sizeof SyslogAddr.sa_data);
187 		if (LogStat & LOG_NDELAY) {
188 			LogFile = socket(AF_UNIX, SOCK_DGRAM, 0);
189 			fcntl(LogFile, F_SETFD, 1);
190 		}
191 	}
192 	if (LogFile != -1 && !connected &&
193 	    connect(LogFile, &SyslogAddr, sizeof(SyslogAddr)) != -1)
194 		connected = 1;
195 }
196 
197 /*
198  * CLOSELOG -- close the system log
199  */
200 closelog()
201 {
202 	(void) close(LogFile);
203 	LogFile = -1;
204 	connected = 0;
205 }
206 
207 static int	LogMask = 0xff;		/* mask of priorities to be logged */
208 /*
209  * SETLOGMASK -- set the log mask level
210  */
211 setlogmask(pmask)
212 	int pmask;
213 {
214 	int omask;
215 
216 	omask = LogMask;
217 	if (pmask != 0)
218 		LogMask = pmask;
219 	return (omask);
220 }
221