xref: /original-bsd/lib/libc/gen/syslog.c (revision 0842ddeb)
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.5 (Berkeley) 04/29/95";
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 = NULL;	/* 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 extern char	*__progname;		/* Program name, from crt0. */
39 
40 /*
41  * syslog, vsyslog --
42  *	print message on log file; output is intended for syslogd(8).
43  */
44 void
45 #if __STDC__
46 syslog(int pri, const char *fmt, ...)
47 #else
48 syslog(pri, fmt, va_alist)
49 	int pri;
50 	char *fmt;
51 	va_dcl
52 #endif
53 {
54 	va_list ap;
55 
56 #if __STDC__
57 	va_start(ap, fmt);
58 #else
59 	va_start(ap);
60 #endif
61 	vsyslog(pri, fmt, ap);
62 	va_end(ap);
63 }
64 
65 void
66 vsyslog(pri, fmt, ap)
67 	int pri;
68 	register const char *fmt;
69 	va_list ap;
70 {
71 	register int cnt;
72 	register char ch, *p, *t;
73 	time_t now;
74 	int fd, saved_errno;
75 	char *stdp, tbuf[2048], fmt_cpy[1024];
76 
77 #define	INTERNALLOG	LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID
78 	/* Check for invalid bits. */
79 	if (pri & ~(LOG_PRIMASK|LOG_FACMASK)) {
80 		syslog(INTERNALLOG,
81 		    "syslog: unknown facility/priority: %x", pri);
82 		pri &= LOG_PRIMASK|LOG_FACMASK;
83 	}
84 
85 	/* Check priority against setlogmask values. */
86 	if (!(LOG_MASK(LOG_PRI(pri)) & LogMask))
87 		return;
88 
89 	saved_errno = errno;
90 
91 	/* Set default facility if none specified. */
92 	if ((pri & LOG_FACMASK) == 0)
93 		pri |= LogFacility;
94 
95 	/* Build the message. */
96 	(void)time(&now);
97 	p = tbuf + sprintf(tbuf, "<%d>", pri);
98 	p += strftime(p, sizeof (tbuf) - (p - tbuf), "%h %e %T ",
99 	    localtime(&now));
100 	if (LogStat & LOG_PERROR)
101 		stdp = p;
102 	if (LogTag == NULL)
103 		LogTag = __progname;
104 	if (LogTag != NULL)
105 		p += sprintf(p, "%s", LogTag);
106 	if (LogStat & LOG_PID)
107 		p += sprintf(p, "[%d]", getpid());
108 	if (LogTag != NULL) {
109 		*p++ = ':';
110 		*p++ = ' ';
111 	}
112 
113 	/* Substitute error message for %m. */
114 	for (t = fmt_cpy; ch = *fmt; ++fmt)
115 		if (ch == '%' && fmt[1] == 'm') {
116 			++fmt;
117 			t += sprintf(t, "%s", strerror(saved_errno));
118 		} else
119 			*t++ = ch;
120 	*t = '\0';
121 
122 	p += vsprintf(p, fmt_cpy, ap);
123 	cnt = p - tbuf;
124 
125 	/* Output to stderr if requested. */
126 	if (LogStat & LOG_PERROR) {
127 		struct iovec iov[2];
128 		register struct iovec *v = iov;
129 
130 		v->iov_base = stdp;
131 		v->iov_len = cnt - (stdp - tbuf);
132 		++v;
133 		v->iov_base = "\n";
134 		v->iov_len = 1;
135 		(void)writev(STDERR_FILENO, iov, 2);
136 	}
137 
138 	/* Get connected, output the message to the local logger. */
139 	if (!connected)
140 		openlog(LogTag, LogStat | LOG_NDELAY, 0);
141 	if (send(LogFile, tbuf, cnt, 0) >= 0)
142 		return;
143 
144 	/*
145 	 * Output the message to the console; don't worry about blocking,
146 	 * if console blocks everything will.  Make sure the error reported
147 	 * is the one from the syslogd failure.
148 	 */
149 	if (LogStat & LOG_CONS &&
150 	    (fd = open(_PATH_CONSOLE, O_WRONLY, 0)) >= 0) {
151 		(void)strcat(tbuf, "\r\n");
152 		cnt += 2;
153 		p = index(tbuf, '>') + 1;
154 		(void)write(fd, p, cnt - (p - tbuf));
155 		(void)close(fd);
156 	}
157 }
158 
159 static struct sockaddr SyslogAddr;	/* AF_UNIX address of local logger */
160 
161 void
162 openlog(ident, logstat, logfac)
163 	const char *ident;
164 	int logstat, logfac;
165 {
166 	if (ident != NULL)
167 		LogTag = ident;
168 	LogStat = logstat;
169 	if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0)
170 		LogFacility = logfac;
171 
172 	if (LogFile == -1) {
173 		SyslogAddr.sa_family = AF_UNIX;
174 		(void)strncpy(SyslogAddr.sa_data, _PATH_LOG,
175 		    sizeof(SyslogAddr.sa_data));
176 		if (LogStat & LOG_NDELAY) {
177 			if ((LogFile = socket(AF_UNIX, SOCK_DGRAM, 0)) == -1)
178 				return;
179 			(void)fcntl(LogFile, F_SETFD, 1);
180 		}
181 	}
182 	if (LogFile != -1 && !connected)
183 		if (connect(LogFile, &SyslogAddr, sizeof(SyslogAddr)) == -1) {
184 			(void)close(LogFile);
185 			LogFile = -1;
186 		} else
187 			connected = 1;
188 }
189 
190 void
191 closelog()
192 {
193 	(void)close(LogFile);
194 	LogFile = -1;
195 	connected = 0;
196 }
197 
198 /* setlogmask -- set the log mask level */
199 int
200 setlogmask(pmask)
201 	int pmask;
202 {
203 	int omask;
204 
205 	omask = LogMask;
206 	if (pmask != 0)
207 		LogMask = pmask;
208 	return (omask);
209 }
210