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