]>
Commit | Line | Data |
---|---|---|
28f540f4 RM |
1 | /* |
2 | * Copyright (c) 1983, 1988, 1993 | |
3 | * The Regents of the University of California. All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions | |
7 | * are met: | |
8 | * 1. Redistributions of source code must retain the above copyright | |
9 | * notice, this list of conditions and the following disclaimer. | |
10 | * 2. Redistributions in binary form must reproduce the above copyright | |
11 | * notice, this list of conditions and the following disclaimer in the | |
12 | * documentation and/or other materials provided with the distribution. | |
13 | * 3. All advertising materials mentioning features or use of this software | |
14 | * must display the following acknowledgement: | |
15 | * This product includes software developed by the University of | |
16 | * California, Berkeley and its contributors. | |
17 | * 4. Neither the name of the University nor the names of its contributors | |
18 | * may be used to endorse or promote products derived from this software | |
19 | * without specific prior written permission. | |
20 | * | |
21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
31 | * SUCH DAMAGE. | |
32 | */ | |
33 | ||
34 | #if defined(LIBC_SCCS) && !defined(lint) | |
35 | static char sccsid[] = "@(#)syslog.c 8.4 (Berkeley) 3/18/94"; | |
36 | #endif /* LIBC_SCCS and not lint */ | |
37 | ||
38 | #include <sys/types.h> | |
39 | #include <sys/socket.h> | |
40 | #include <sys/syslog.h> | |
41 | #include <sys/uio.h> | |
42 | #include <netdb.h> | |
43 | ||
44 | #include <errno.h> | |
45 | #include <fcntl.h> | |
46 | #include <paths.h> | |
47 | #include <stdio.h> | |
48 | #include <string.h> | |
49 | #include <time.h> | |
50 | #include <unistd.h> | |
0324daa0 | 51 | #include <stdlib.h> |
28f540f4 RM |
52 | |
53 | #if __STDC__ | |
54 | #include <stdarg.h> | |
55 | #else | |
56 | #include <varargs.h> | |
57 | #endif | |
58 | ||
a5113b14 | 59 | static int LogType = SOCK_DGRAM; /* type of socket connection */ |
28f540f4 RM |
60 | static int LogFile = -1; /* fd for log */ |
61 | static int connected; /* have done connect */ | |
62 | static int LogStat = 0; /* status bits, set by openlog() */ | |
63 | static const char *LogTag = NULL; /* string to tag the entry with */ | |
64 | static int LogFacility = LOG_USER; /* default facility code */ | |
65 | static int LogMask = 0xff; /* mask of priorities to be logged */ | |
66 | extern char *__progname; /* Program name, from crt0. */ | |
67 | ||
68 | /* | |
69 | * syslog, vsyslog -- | |
70 | * print message on log file; output is intended for syslogd(8). | |
71 | */ | |
72 | void | |
73 | #if __STDC__ | |
74 | syslog(int pri, const char *fmt, ...) | |
75 | #else | |
76 | syslog(pri, fmt, va_alist) | |
77 | int pri; | |
78 | char *fmt; | |
79 | va_dcl | |
80 | #endif | |
81 | { | |
82 | va_list ap; | |
83 | ||
84 | #if __STDC__ | |
85 | va_start(ap, fmt); | |
86 | #else | |
87 | va_start(ap); | |
88 | #endif | |
89 | vsyslog(pri, fmt, ap); | |
90 | va_end(ap); | |
91 | } | |
92 | ||
93 | void | |
94 | vsyslog(pri, fmt, ap) | |
95 | int pri; | |
96 | register const char *fmt; | |
97 | va_list ap; | |
98 | { | |
dcf0671d | 99 | struct tm now_tm; |
28f540f4 | 100 | time_t now; |
0324daa0 RM |
101 | int fd; |
102 | FILE *f; | |
103 | char *buf = 0; | |
104 | size_t bufsize = 0; | |
105 | size_t prioff, msgoff; | |
28f540f4 RM |
106 | |
107 | #define INTERNALLOG LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID | |
108 | /* Check for invalid bits. */ | |
109 | if (pri & ~(LOG_PRIMASK|LOG_FACMASK)) { | |
110 | syslog(INTERNALLOG, | |
111 | "syslog: unknown facility/priority: %x", pri); | |
112 | pri &= LOG_PRIMASK|LOG_FACMASK; | |
113 | } | |
114 | ||
115 | /* Check priority against setlogmask values. */ | |
116 | if (!LOG_MASK(LOG_PRI(pri)) & LogMask) | |
117 | return; | |
118 | ||
28f540f4 RM |
119 | /* Set default facility if none specified. */ |
120 | if ((pri & LOG_FACMASK) == 0) | |
121 | pri |= LogFacility; | |
122 | ||
0324daa0 RM |
123 | /* Build the message in a memory-buffer stream. */ |
124 | f = open_memstream (&buf, &bufsize); | |
125 | prioff = fprintf (f, "<%d>", pri); | |
126 | (void) time (&now); | |
fbaad149 RM |
127 | #ifdef USE_IN_LIBIO |
128 | f->_IO_write_ptr += strftime (f->_IO_write_ptr, | |
129 | f->_IO_write_end - f->_IO_write_ptr, | |
dcf0671d UD |
130 | "%h %e %T ", |
131 | __localtime_r (&now, &now_tm)); | |
fbaad149 | 132 | #else |
0324daa0 | 133 | f->__bufp += strftime (f->__bufp, f->__put_limit - f->__bufp, |
dcf0671d | 134 | "%h %e %T ", __localtime_r (&now, &mow_tm)); |
fbaad149 | 135 | #endif |
0324daa0 | 136 | msgoff = ftell (f); |
28f540f4 | 137 | if (LogTag == NULL) |
0324daa0 | 138 | LogTag = __progname; |
28f540f4 | 139 | if (LogTag != NULL) |
0324daa0 | 140 | fputs (LogTag, f); |
28f540f4 | 141 | if (LogStat & LOG_PID) |
0324daa0 RM |
142 | fprintf (f, "[%d]", getpid ()); |
143 | if (LogTag != NULL) | |
144 | putc (':', f), putc (' ', f); | |
28f540f4 | 145 | |
0324daa0 RM |
146 | /* We have the header. Print the user's format into the buffer. */ |
147 | vfprintf (f, fmt, ap); | |
28f540f4 | 148 | |
0324daa0 RM |
149 | /* Close the memory stream; this will finalize the data |
150 | into a malloc'd buffer in BUF. */ | |
151 | fclose (f); | |
28f540f4 RM |
152 | |
153 | /* Output to stderr if requested. */ | |
154 | if (LogStat & LOG_PERROR) { | |
155 | struct iovec iov[2]; | |
156 | register struct iovec *v = iov; | |
157 | ||
0324daa0 RM |
158 | v->iov_base = buf + msgoff; |
159 | v->iov_len = bufsize - msgoff; | |
28f540f4 | 160 | ++v; |
0324daa0 | 161 | v->iov_base = (char *) "\n"; |
28f540f4 RM |
162 | v->iov_len = 1; |
163 | (void)writev(STDERR_FILENO, iov, 2); | |
164 | } | |
165 | ||
166 | /* Get connected, output the message to the local logger. */ | |
167 | if (!connected) | |
168 | openlog(LogTag, LogStat | LOG_NDELAY, 0); | |
a5113b14 UD |
169 | |
170 | /* If we have a SOCK_STREAM connection, also send ASCII NUL as | |
171 | a record terminator. */ | |
172 | if (LogType == SOCK_STREAM) | |
173 | ++bufsize; | |
174 | ||
dbe31b9a | 175 | if (__send(LogFile, buf, bufsize, 0) < 0) |
0324daa0 | 176 | { |
a5113b14 | 177 | closelog (); /* attempt re-open next time */ |
0324daa0 RM |
178 | /* |
179 | * Output the message to the console; don't worry about blocking, | |
180 | * if console blocks everything will. Make sure the error reported | |
181 | * is the one from the syslogd failure. | |
182 | */ | |
183 | if (LogStat & LOG_CONS && | |
184 | (fd = open(_PATH_CONSOLE, O_WRONLY, 0)) >= 0) | |
185 | { | |
186 | dprintf (fd, "%s\r\n", buf + msgoff); | |
28f540f4 | 187 | (void)close(fd); |
0324daa0 RM |
188 | } |
189 | } | |
190 | ||
191 | free (buf); | |
28f540f4 RM |
192 | } |
193 | ||
194 | static struct sockaddr SyslogAddr; /* AF_UNIX address of local logger */ | |
195 | ||
196 | void | |
197 | openlog(ident, logstat, logfac) | |
198 | const char *ident; | |
199 | int logstat, logfac; | |
200 | { | |
201 | if (ident != NULL) | |
202 | LogTag = ident; | |
203 | LogStat = logstat; | |
204 | if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0) | |
205 | LogFacility = logfac; | |
206 | ||
a5113b14 UD |
207 | while (1) { |
208 | if (LogFile == -1) { | |
209 | SyslogAddr.sa_family = AF_UNIX; | |
210 | (void)strncpy(SyslogAddr.sa_data, _PATH_LOG, | |
211 | sizeof(SyslogAddr.sa_data)); | |
212 | if (LogStat & LOG_NDELAY) { | |
213 | if ((LogFile = socket(AF_UNIX, LogType, 0)) | |
214 | == -1) | |
215 | return; | |
216 | (void)fcntl(LogFile, F_SETFD, 1); | |
217 | } | |
28f540f4 | 218 | } |
a5113b14 UD |
219 | if (LogFile != -1 && !connected) |
220 | if (__connect(LogFile, &SyslogAddr, sizeof(SyslogAddr)) | |
221 | == -1) | |
222 | { | |
223 | int saved_errno = errno; | |
224 | (void)close(LogFile); | |
225 | LogFile = -1; | |
226 | if (LogType == SOCK_DGRAM | |
227 | && saved_errno == EPROTOTYPE) | |
228 | { | |
229 | /* retry with next SOCK_STREAM: */ | |
230 | LogType = SOCK_STREAM; | |
231 | continue; | |
232 | } | |
233 | } else | |
234 | connected = 1; | |
235 | break; | |
28f540f4 | 236 | } |
28f540f4 RM |
237 | } |
238 | ||
239 | void | |
240 | closelog() | |
241 | { | |
242 | (void)close(LogFile); | |
243 | LogFile = -1; | |
244 | connected = 0; | |
245 | } | |
246 | ||
247 | /* setlogmask -- set the log mask level */ | |
248 | int | |
249 | setlogmask(pmask) | |
250 | int pmask; | |
251 | { | |
252 | int omask; | |
253 | ||
254 | omask = LogMask; | |
255 | if (pmask != 0) | |
256 | LogMask = pmask; | |
257 | return (omask); | |
258 | } |