Update.
[glibc.git] / misc / syslog.c
blobc58639ee30fd3bbca1a283c2d928bea79183e8b1
1 /*
2 * Copyright (c) 1983, 1988, 1993
3 * The Regents of the University of California. All rights reserved.
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.
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.
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 */
38 #include <sys/types.h>
39 #include <sys/socket.h>
40 #include <sys/syslog.h>
41 #include <sys/uio.h>
42 #include <netdb.h>
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>
51 #include <stdlib.h>
52 #include <libc-lock.h>
53 #include <signal.h>
55 #if __STDC__
56 #include <stdarg.h>
57 #else
58 #include <varargs.h>
59 #endif
61 static int LogType = SOCK_DGRAM; /* type of socket connection */
62 static int LogFile = -1; /* fd for log */
63 static int connected; /* have done connect */
64 static int LogStat = 0; /* status bits, set by openlog() */
65 static const char *LogTag = NULL; /* string to tag the entry with */
66 static int LogFacility = LOG_USER; /* default facility code */
67 static int LogMask = 0xff; /* mask of priorities to be logged */
68 extern char *__progname; /* Program name, from crt0. */
70 /* Define the lock. */
71 __libc_lock_define_initialized (static, syslog_lock)
73 static void openlog_internal(const char *, int, int);
74 static void closelog_internal(void);
75 static void sigpipe_handler (int);
76 #ifdef _LIBC_REENTRANT
77 static void cancel_handler (void *);
78 #endif
81 * syslog, vsyslog --
82 * print message on log file; output is intended for syslogd(8).
84 void
85 #if __STDC__
86 syslog(int pri, const char *fmt, ...)
87 #else
88 syslog(pri, fmt, va_alist)
89 int pri;
90 char *fmt;
91 va_dcl
92 #endif
94 va_list ap;
96 #if __STDC__
97 va_start(ap, fmt);
98 #else
99 va_start(ap);
100 #endif
101 vsyslog(pri, fmt, ap);
102 va_end(ap);
105 void
106 vsyslog(pri, fmt, ap)
107 int pri;
108 register const char *fmt;
109 va_list ap;
111 struct tm now_tm;
112 time_t now;
113 int fd;
114 FILE *f;
115 char *buf = 0;
116 size_t bufsize = 0;
117 size_t prioff, msgoff;
118 struct sigaction action, oldaction;
119 struct sigaction *oldaction_ptr = NULL;
120 int sigpipe;
122 #define INTERNALLOG LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID
123 /* Check for invalid bits. */
124 if (pri & ~(LOG_PRIMASK|LOG_FACMASK)) {
125 syslog(INTERNALLOG,
126 "syslog: unknown facility/priority: %x", pri);
127 pri &= LOG_PRIMASK|LOG_FACMASK;
130 /* Check priority against setlogmask values. */
131 if (!LOG_MASK(LOG_PRI(pri)) & LogMask)
132 return;
134 /* Set default facility if none specified. */
135 if ((pri & LOG_FACMASK) == 0)
136 pri |= LogFacility;
138 /* Build the message in a memory-buffer stream. */
139 f = open_memstream (&buf, &bufsize);
140 prioff = fprintf (f, "<%d>", pri);
141 (void) time (&now);
142 #ifdef USE_IN_LIBIO
143 f->_IO_write_ptr += strftime (f->_IO_write_ptr,
144 f->_IO_write_end - f->_IO_write_ptr,
145 "%h %e %T ",
146 __localtime_r (&now, &now_tm));
147 #else
148 f->__bufp += strftime (f->__bufp, f->__put_limit - f->__bufp,
149 "%h %e %T ", __localtime_r (&now, &now_tm));
150 #endif
151 msgoff = ftell (f);
152 if (LogTag == NULL)
153 LogTag = __progname;
154 if (LogTag != NULL)
155 fputs (LogTag, f);
156 if (LogStat & LOG_PID)
157 fprintf (f, "[%d]", getpid ());
158 if (LogTag != NULL)
159 putc (':', f), putc (' ', f);
161 /* We have the header. Print the user's format into the buffer. */
162 vfprintf (f, fmt, ap);
164 /* Close the memory stream; this will finalize the data
165 into a malloc'd buffer in BUF. */
166 fclose (f);
168 /* Output to stderr if requested. */
169 if (LogStat & LOG_PERROR) {
170 struct iovec iov[2];
171 register struct iovec *v = iov;
173 v->iov_base = buf + msgoff;
174 v->iov_len = bufsize - msgoff;
175 ++v;
176 v->iov_base = (char *) "\n";
177 v->iov_len = 1;
178 (void)writev(STDERR_FILENO, iov, 2);
181 /* Prepare for multiple users. We have to take care: open and
182 write are cancellation points. */
183 __libc_cleanup_region_start ((void (*) (void *)) cancel_handler,
184 &oldaction_ptr);
185 __libc_lock_lock (syslog_lock);
187 /* Prepare for a broken connection. */
188 memset (&action, 0, sizeof (action));
189 action.sa_handler = sigpipe_handler;
190 sigemptyset (&action.sa_mask);
191 sigpipe = sigaction (SIGPIPE, &action, &oldaction);
192 if (sigpipe == 0)
193 oldaction_ptr = &oldaction;
195 /* Get connected, output the message to the local logger. */
196 if (!connected)
197 openlog_internal(LogTag, LogStat | LOG_NDELAY, 0);
199 /* If we have a SOCK_STREAM connection, also send ASCII NUL as
200 a record terminator. */
201 if (LogType == SOCK_STREAM)
202 ++bufsize;
204 if (!connected || __send(LogFile, buf, bufsize, 0) < 0)
206 closelog_internal (); /* attempt re-open next time */
208 * Output the message to the console; don't worry about blocking,
209 * if console blocks everything will. Make sure the error reported
210 * is the one from the syslogd failure.
212 if (LogStat & LOG_CONS &&
213 (fd = open(_PATH_CONSOLE, O_WRONLY|O_NOCTTY, 0)) >= 0)
215 dprintf (fd, "%s\r\n", buf + msgoff);
216 (void)close(fd);
220 if (sigpipe == 0)
221 sigaction (SIGPIPE, &oldaction, (struct sigaction *) NULL);
223 /* End of critical section. */
224 __libc_cleanup_region_end (0);
225 __libc_lock_unlock (syslog_lock);
227 free (buf);
230 static struct sockaddr SyslogAddr; /* AF_UNIX address of local logger */
232 static void
233 openlog_internal(const char *ident, int logstat, int logfac)
235 if (ident != NULL)
236 LogTag = ident;
237 LogStat = logstat;
238 if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0)
239 LogFacility = logfac;
241 while (1) {
242 if (LogFile == -1) {
243 SyslogAddr.sa_family = AF_UNIX;
244 (void)strncpy(SyslogAddr.sa_data, _PATH_LOG,
245 sizeof(SyslogAddr.sa_data));
246 if (LogStat & LOG_NDELAY) {
247 if ((LogFile = socket(AF_UNIX, LogType, 0))
248 == -1)
249 return;
250 (void)fcntl(LogFile, F_SETFD, 1);
253 if (LogFile != -1 && !connected)
255 int old_errno = errno;
256 if (__connect(LogFile, &SyslogAddr, sizeof(SyslogAddr))
257 == -1)
259 int saved_errno = errno;
260 (void)close(LogFile);
261 LogFile = -1;
262 if (LogType == SOCK_DGRAM
263 && saved_errno == EPROTOTYPE)
265 /* retry with next SOCK_STREAM: */
266 LogType = SOCK_STREAM;
267 __set_errno (old_errno);
268 continue;
270 } else
271 connected = 1;
273 break;
277 void
278 openlog (const char *ident, int logstat, int logfac)
280 /* Protect against multiple users. */
281 __libc_cleanup_region_start ((void (*) __P ((void *))) __libc_mutex_unlock,
282 &syslog_lock);
283 __libc_lock_lock (syslog_lock);
285 openlog_internal (ident, logstat, logfac);
287 /* Free the lock. */
288 __libc_cleanup_region_end (1);
291 static void
292 sigpipe_handler (int signo)
294 closelog_internal();
297 static void
298 closelog_internal()
300 if (!connected)
301 return;
302 (void)close(LogFile);
303 LogFile = -1;
304 connected = 0;
307 void
308 closelog ()
310 /* Protect against multiple users. */
311 __libc_cleanup_region_start ((void (*) __P ((void *))) __libc_mutex_unlock,
312 &syslog_lock);
313 __libc_lock_lock (syslog_lock);
315 closelog_internal ();
317 /* Free the lock. */
318 __libc_cleanup_region_end (1);
321 #ifdef _LIBC_REENTRANT
322 static void
323 cancel_handler (void *ptr)
325 /* Restore the old signal handler. */
326 struct sigaction *oldaction = *((struct sigaction **) ptr);
328 if (oldaction != (struct sigaction *) NULL)
329 sigaction (SIGPIPE, oldaction, (struct sigaction *) NULL);
331 /* Free the lock. */
332 __libc_lock_unlock (syslog_lock);
334 #endif
336 /* setlogmask -- set the log mask level */
338 setlogmask(pmask)
339 int pmask;
341 int omask;
343 omask = LogMask;
344 if (pmask != 0)
345 LogMask = pmask;
346 return (omask);