Update from main archive 961219
[glibc.git] / misc / syslog.c
blob6e9cdef547d000a872c87def0a57225bd81aabec
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 static void cancel_handler (void *);
79 * syslog, vsyslog --
80 * print message on log file; output is intended for syslogd(8).
82 void
83 #if __STDC__
84 syslog(int pri, const char *fmt, ...)
85 #else
86 syslog(pri, fmt, va_alist)
87 int pri;
88 char *fmt;
89 va_dcl
90 #endif
92 va_list ap;
94 #if __STDC__
95 va_start(ap, fmt);
96 #else
97 va_start(ap);
98 #endif
99 vsyslog(pri, fmt, ap);
100 va_end(ap);
103 void
104 vsyslog(pri, fmt, ap)
105 int pri;
106 register const char *fmt;
107 va_list ap;
109 struct tm now_tm;
110 time_t now;
111 int fd;
112 FILE *f;
113 char *buf = 0;
114 size_t bufsize = 0;
115 size_t prioff, msgoff;
116 struct sigaction action, oldaction;
117 struct sigaction *oldaction_ptr = NULL;
118 int sigpipe;
120 #define INTERNALLOG LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID
121 /* Check for invalid bits. */
122 if (pri & ~(LOG_PRIMASK|LOG_FACMASK)) {
123 syslog(INTERNALLOG,
124 "syslog: unknown facility/priority: %x", pri);
125 pri &= LOG_PRIMASK|LOG_FACMASK;
128 /* Check priority against setlogmask values. */
129 if (!LOG_MASK(LOG_PRI(pri)) & LogMask)
130 return;
132 /* Set default facility if none specified. */
133 if ((pri & LOG_FACMASK) == 0)
134 pri |= LogFacility;
136 /* Build the message in a memory-buffer stream. */
137 f = open_memstream (&buf, &bufsize);
138 prioff = fprintf (f, "<%d>", pri);
139 (void) time (&now);
140 #ifdef USE_IN_LIBIO
141 f->_IO_write_ptr += strftime (f->_IO_write_ptr,
142 f->_IO_write_end - f->_IO_write_ptr,
143 "%h %e %T ",
144 __localtime_r (&now, &now_tm));
145 #else
146 f->__bufp += strftime (f->__bufp, f->__put_limit - f->__bufp,
147 "%h %e %T ", __localtime_r (&now, &now_tm));
148 #endif
149 msgoff = ftell (f);
150 if (LogTag == NULL)
151 LogTag = __progname;
152 if (LogTag != NULL)
153 fputs (LogTag, f);
154 if (LogStat & LOG_PID)
155 fprintf (f, "[%d]", getpid ());
156 if (LogTag != NULL)
157 putc (':', f), putc (' ', f);
159 /* We have the header. Print the user's format into the buffer. */
160 vfprintf (f, fmt, ap);
162 /* Close the memory stream; this will finalize the data
163 into a malloc'd buffer in BUF. */
164 fclose (f);
166 /* Output to stderr if requested. */
167 if (LogStat & LOG_PERROR) {
168 struct iovec iov[2];
169 register struct iovec *v = iov;
171 v->iov_base = buf + msgoff;
172 v->iov_len = bufsize - msgoff;
173 ++v;
174 v->iov_base = (char *) "\n";
175 v->iov_len = 1;
176 (void)writev(STDERR_FILENO, iov, 2);
179 /* Prepare for multiple users. We have to take care: open and
180 write are cancellation points. */
181 __libc_cleanup_region_start ((void (*) (void *)) cancel_handler,
182 &oldaction_ptr);
183 __libc_lock_lock (syslog_lock);
185 /* Prepare for a broken connection. */
186 memset (&action, 0, sizeof (action));
187 action.sa_handler = sigpipe_handler;
188 sigemptyset (&action.sa_mask);
189 sigpipe = sigaction (SIGPIPE, &action, &oldaction);
190 if (sigpipe == 0)
191 oldaction_ptr = &oldaction;
193 /* Get connected, output the message to the local logger. */
194 if (!connected)
195 openlog_internal(LogTag, LogStat | LOG_NDELAY, 0);
197 /* If we have a SOCK_STREAM connection, also send ASCII NUL as
198 a record terminator. */
199 if (LogType == SOCK_STREAM)
200 ++bufsize;
202 if (__send(LogFile, buf, bufsize, 0) < 0)
204 closelog_internal (); /* attempt re-open next time */
206 * Output the message to the console; don't worry about blocking,
207 * if console blocks everything will. Make sure the error reported
208 * is the one from the syslogd failure.
210 if (LogStat & LOG_CONS &&
211 (fd = open(_PATH_CONSOLE, O_WRONLY, 0)) >= 0)
213 dprintf (fd, "%s\r\n", buf + msgoff);
214 (void)close(fd);
218 if (sigpipe == 0)
219 sigaction (SIGPIPE, &oldaction, (struct sigaction *) NULL);
221 /* End of critical section. */
222 __libc_cleanup_region_end (0);
223 __libc_lock_unlock (syslog_lock);
225 free (buf);
228 static struct sockaddr SyslogAddr; /* AF_UNIX address of local logger */
230 static void
231 openlog_internal(const char *ident, int logstat, int logfac)
233 if (ident != NULL)
234 LogTag = ident;
235 LogStat = logstat;
236 if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0)
237 LogFacility = logfac;
239 while (1) {
240 if (LogFile == -1) {
241 SyslogAddr.sa_family = AF_UNIX;
242 (void)strncpy(SyslogAddr.sa_data, _PATH_LOG,
243 sizeof(SyslogAddr.sa_data));
244 if (LogStat & LOG_NDELAY) {
245 if ((LogFile = socket(AF_UNIX, LogType, 0))
246 == -1)
247 return;
248 (void)fcntl(LogFile, F_SETFD, 1);
251 if (LogFile != -1 && !connected)
252 if (__connect(LogFile, &SyslogAddr, sizeof(SyslogAddr))
253 == -1)
255 int saved_errno = errno;
256 (void)close(LogFile);
257 LogFile = -1;
258 if (LogType == SOCK_DGRAM
259 && saved_errno == EPROTOTYPE)
261 /* retry with next SOCK_STREAM: */
262 LogType = SOCK_STREAM;
263 continue;
265 } else
266 connected = 1;
267 break;
271 void
272 openlog (const char *ident, int logstat, int logfac)
274 /* Protect against multiple users. */
275 __libc_cleanup_region_start ((void (*) __P ((void *))) __libc_mutex_unlock,
276 &syslog_lock);
277 __libc_lock_lock (syslog_lock);
279 openlog_internal (ident, logstat, logfac);
281 /* Free the lock. */
282 __libc_cleanup_region_end (1);
285 static void
286 sigpipe_handler (int signo)
288 closelog_internal();
291 static void
292 closelog_internal()
294 (void)close(LogFile);
295 LogFile = -1;
296 connected = 0;
299 void
300 closelog ()
302 /* Protect against multiple users. */
303 __libc_cleanup_region_start ((void (*) __P ((void *))) __libc_mutex_unlock,
304 &syslog_lock);
305 __libc_lock_lock (syslog_lock);
307 closelog_internal ();
309 /* Free the lock. */
310 __libc_cleanup_region_end (1);
313 static void
314 cancel_handler (void *ptr)
316 /* Restore the old signal handler. */
317 struct sigaction *oldaction = *((struct sigaction **) ptr);
319 if (oldaction != (struct sigaction *) NULL)
320 sigaction (SIGPIPE, oldaction, (struct sigaction *) NULL);
322 /* Free the lock. */
323 __libc_lock_unlock (syslog_lock);
326 /* setlogmask -- set the log mask level */
328 setlogmask(pmask)
329 int pmask;
331 int omask;
333 omask = LogMask;
334 if (pmask != 0)
335 LogMask = pmask;
336 return (omask);