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