xref: /original-bsd/lib/libc/gen/syslog.c (revision 4092c5cc)
1 /*
2  * Copyright (c) 1983, 1988, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #if defined(LIBC_SCCS) && !defined(lint)
9 static char sccsid[] = "@(#)syslog.c	8.1 (Berkeley) 06/04/93";
10 #endif /* LIBC_SCCS and not lint */
11 
12 #include <sys/types.h>
13 #include <sys/socket.h>
14 #include <sys/syslog.h>
15 #include <sys/uio.h>
16 #include <netdb.h>
17 
18 #include <errno.h>
19 #include <fcntl.h>
20 #include <paths.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <time.h>
24 #include <unistd.h>
25 
26 #if __STDC__
27 #include <stdarg.h>
28 #else
29 #include <varargs.h>
30 #endif
31 
32 static int	LogFile = -1;		/* fd for log */
33 static int	connected;		/* have done connect */
34 static int	LogStat = 0;		/* status bits, set by openlog() */
35 static const char *LogTag = "syslog";	/* string to tag the entry with */
36 static int	LogFacility = LOG_USER;	/* default facility code */
37 static int	LogMask = 0xff;		/* mask of priorities to be logged */
38 
39 /*
40  * syslog, vsyslog --
41  *	print message on log file; output is intended for syslogd(8).
42  */
43 void
44 #if __STDC__
45 syslog(int pri, const char *fmt, ...)
46 #else
47 syslog(pri, fmt, va_alist)
48 	int pri;
49 	char *fmt;
50 	va_dcl
51 #endif
52 {
53 	va_list ap;
54 
55 #if __STDC__
56 	va_start(ap, fmt);
57 #else
58 	va_start(ap);
59 #endif
60 	vsyslog(pri, fmt, ap);
61 	va_end(ap);
62 }
63 
64 void
65 vsyslog(pri, fmt, ap)
66 	int pri;
67 	register const char *fmt;
68 	va_list ap;
69 {
70 	register int cnt;
71 	register char *p;
72 	time_t now;
73 	int fd, saved_errno;
74 	char *stdp, tbuf[2048], fmt_cpy[1024];
75 
76 #define	INTERNALLOG	LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID
77 	/* Check for invalid bits. */
78 	if (pri & ~(LOG_PRIMASK|LOG_FACMASK)) {
79 		syslog(INTERNALLOG,
80 		    "syslog: unknown facility/priority: %x", pri);
81 		pri &= LOG_PRIMASK|LOG_FACMASK;
82 	}
83 
84 	/* Check priority against setlogmask values. */
85 	if (!LOG_MASK(LOG_PRI(pri)) & LogMask)
86 		return;
87 
88 	saved_errno = errno;
89 
90 	/* set default facility if none specified */
91 	if ((pri & LOG_FACMASK) == 0)
92 		pri |= LogFacility;
93 
94 	/* build the message */
95 	(void)time(&now);
96 	(void)sprintf(tbuf, "<%d>%.15s ", pri, ctime(&now) + 4);
97 	for (p = tbuf; *p; ++p);
98 	if (LogStat & LOG_PERROR)
99 		stdp = p;
100 	if (LogTag) {
101 		(void)strcpy(p, LogTag);
102 		for (; *p; ++p);
103 	}
104 	if (LogStat & LOG_PID) {
105 		(void)sprintf(p, "[%d]", getpid());
106 		for (; *p; ++p);
107 	}
108 	if (LogTag) {
109 		*p++ = ':';
110 		*p++ = ' ';
111 	}
112 
113 	/* substitute error message for %m */
114 	{
115 		register char ch, *t1, *t2;
116 		char *strerror();
117 
118 		for (t1 = fmt_cpy; ch = *fmt; ++fmt)
119 			if (ch == '%' && fmt[1] == 'm') {
120 				++fmt;
121 				for (t2 = strerror(saved_errno);
122 				    *t1 = *t2++; ++t1);
123 			}
124 			else
125 				*t1++ = ch;
126 		*t1 = '\0';
127 	}
128 
129 	(void)vsprintf(p, fmt_cpy, ap);
130 
131 	cnt = strlen(tbuf);
132 
133 	/* output to stderr if requested */
134 	if (LogStat & LOG_PERROR) {
135 		struct iovec iov[2];
136 		register struct iovec *v = iov;
137 
138 		v->iov_base = stdp;
139 		v->iov_len = cnt - (stdp - tbuf);
140 		++v;
141 		v->iov_base = "\n";
142 		v->iov_len = 1;
143 		(void)writev(STDERR_FILENO, iov, 2);
144 	}
145 
146 	/* get connected, output the message to the local logger */
147 	if (!connected)
148 		openlog(LogTag, LogStat | LOG_NDELAY, 0);
149 	if (send(LogFile, tbuf, cnt, 0) >= 0)
150 		return;
151 
152 	/* see if should attempt the console */
153 	if (!(LogStat&LOG_CONS))
154 		return;
155 
156 	/*
157 	 * Output the message to the console; don't worry about blocking,
158 	 * if console blocks everything will.  Make sure the error reported
159 	 * is the one from the syslogd failure.
160 	 */
161 	if ((fd = open(_PATH_CONSOLE, O_WRONLY, 0)) >= 0) {
162 		(void)strcat(tbuf, "\r\n");
163 		cnt += 2;
164 		p = index(tbuf, '>') + 1;
165 		(void)write(fd, p, cnt - (p - tbuf));
166 		(void)close(fd);
167 	}
168 }
169 
170 static struct sockaddr SyslogAddr;	/* AF_UNIX address of local logger */
171 
172 void
173 openlog(ident, logstat, logfac)
174 	const char *ident;
175 	int logstat, logfac;
176 {
177 	if (ident != NULL)
178 		LogTag = ident;
179 	LogStat = logstat;
180 	if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0)
181 		LogFacility = logfac;
182 
183 	if (LogFile == -1) {
184 		SyslogAddr.sa_family = AF_UNIX;
185 		(void)strncpy(SyslogAddr.sa_data, _PATH_LOG,
186 		    sizeof(SyslogAddr.sa_data));
187 		if (LogStat & LOG_NDELAY) {
188 			if ((LogFile = socket(AF_UNIX, SOCK_DGRAM, 0)) == -1)
189 				return;
190 			(void)fcntl(LogFile, F_SETFD, 1);
191 		}
192 	}
193 	if (LogFile != -1 && !connected)
194 		if (connect(LogFile, &SyslogAddr, sizeof(SyslogAddr)) == -1) {
195 			(void)close(LogFile);
196 			LogFile = -1;
197 		} else
198 			connected = 1;
199 }
200 
201 void
202 closelog()
203 {
204 	(void)close(LogFile);
205 	LogFile = -1;
206 	connected = 0;
207 }
208 
209 /* setlogmask -- set the log mask level */
210 int
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