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
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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #if defined(LIBC_SCCS) && !defined(lint)
31 static char sccsid
[] = "@(#)syslog.c 8.4 (Berkeley) 3/18/94";
32 #endif /* LIBC_SCCS and not lint */
34 #include <sys/types.h>
35 #include <sys/socket.h>
36 #include <sys/syslog.h>
48 #include <bits/libc-lock.h>
58 # include <libio/iolibio.h>
59 # define ftell(s) _IO_ftell (s)
62 static int LogType
= SOCK_DGRAM
; /* type of socket connection */
63 static int LogFile
= -1; /* fd for log */
64 static int connected
; /* have done connect */
65 static int LogStat
; /* status bits, set by openlog() */
66 static const char *LogTag
; /* string to tag the entry with */
67 static int LogFacility
= LOG_USER
; /* default facility code */
68 static int LogMask
= 0xff; /* mask of priorities to be logged */
69 extern char *__progname
; /* Program name, from crt0. */
71 /* Define the lock. */
72 __libc_lock_define_initialized (static, syslog_lock
)
74 static void openlog_internal(const char *, int, int) internal_function
;
75 static void closelog_internal(void);
76 static void sigpipe_handler (int);
77 #ifdef _LIBC_REENTRANT
78 static void cancel_handler (void *);
83 * print message on log file; output is intended for syslogd(8).
87 syslog(int pri
, const char *fmt
, ...)
89 syslog(pri
, fmt
, va_alist
)
102 vsyslog(pri
, fmt
, ap
);
107 vsyslog(pri
, fmt
, ap
)
109 register const char *fmt
;
118 size_t prioff
, msgoff
;
119 struct sigaction action
, oldaction
;
120 struct sigaction
*oldaction_ptr
= NULL
;
122 int saved_errno
= errno
;
124 #define INTERNALLOG LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID
125 /* Check for invalid bits. */
126 if (pri
& ~(LOG_PRIMASK
|LOG_FACMASK
)) {
128 "syslog: unknown facility/priority: %x", pri
);
129 pri
&= LOG_PRIMASK
|LOG_FACMASK
;
132 /* Check priority against setlogmask values. */
133 if ((LOG_MASK (LOG_PRI (pri
)) & LogMask
) == 0)
136 /* Set default facility if none specified. */
137 if ((pri
& LOG_FACMASK
) == 0)
140 /* Build the message in a memory-buffer stream. */
141 f
= open_memstream (&buf
, &bufsize
);
142 prioff
= fprintf (f
, "<%d>", pri
);
145 f
->_IO_write_ptr
+= strftime (f
->_IO_write_ptr
,
146 f
->_IO_write_end
- f
->_IO_write_ptr
,
148 __localtime_r (&now
, &now_tm
));
150 f
->__bufp
+= strftime (f
->__bufp
, f
->__put_limit
- f
->__bufp
,
151 "%h %e %T ", __localtime_r (&now
, &now_tm
));
157 fputs_unlocked (LogTag
, f
);
158 if (LogStat
& LOG_PID
)
159 fprintf (f
, "[%d]", __getpid ());
161 putc_unlocked (':', f
), putc_unlocked (' ', f
);
163 /* Restore errno for %m format. */
164 __set_errno (saved_errno
);
166 /* We have the header. Print the user's format into the buffer. */
167 vfprintf (f
, fmt
, ap
);
169 /* Close the memory stream; this will finalize the data
170 into a malloc'd buffer in BUF. */
173 /* Output to stderr if requested. */
174 if (LogStat
& LOG_PERROR
) {
176 register struct iovec
*v
= iov
;
178 v
->iov_base
= buf
+ msgoff
;
179 v
->iov_len
= bufsize
- msgoff
;
180 /* Append a newline if necessary. */
181 if (buf
[bufsize
- 1] != '\n')
184 v
->iov_base
= (char *) "\n";
187 (void)__writev(STDERR_FILENO
, iov
, v
- iov
+ 1);
190 /* Prepare for multiple users. We have to take care: open and
191 write are cancellation points. */
192 __libc_cleanup_region_start ((void (*) (void *)) cancel_handler
,
194 __libc_lock_lock (syslog_lock
);
196 /* Prepare for a broken connection. */
197 memset (&action
, 0, sizeof (action
));
198 action
.sa_handler
= sigpipe_handler
;
199 sigemptyset (&action
.sa_mask
);
200 sigpipe
= __sigaction (SIGPIPE
, &action
, &oldaction
);
202 oldaction_ptr
= &oldaction
;
204 /* Get connected, output the message to the local logger. */
206 openlog_internal(LogTag
, LogStat
| LOG_NDELAY
, 0);
208 /* If we have a SOCK_STREAM connection, also send ASCII NUL as
209 a record terminator. */
210 if (LogType
== SOCK_STREAM
)
213 if (!connected
|| __send(LogFile
, buf
, bufsize
, 0) < 0)
215 closelog_internal (); /* attempt re-open next time */
217 * Output the message to the console; don't worry about blocking,
218 * if console blocks everything will. Make sure the error reported
219 * is the one from the syslogd failure.
221 if (LogStat
& LOG_CONS
&&
222 (fd
= __open(_PATH_CONSOLE
, O_WRONLY
|O_NOCTTY
, 0)) >= 0)
224 dprintf (fd
, "%s\r\n", buf
+ msgoff
);
230 __sigaction (SIGPIPE
, &oldaction
, (struct sigaction
*) NULL
);
232 /* End of critical section. */
233 __libc_cleanup_region_end (0);
234 __libc_lock_unlock (syslog_lock
);
239 static struct sockaddr SyslogAddr
; /* AF_UNIX address of local logger */
243 openlog_internal(const char *ident
, int logstat
, int logfac
)
248 if (logfac
!= 0 && (logfac
&~ LOG_FACMASK
) == 0)
249 LogFacility
= logfac
;
253 SyslogAddr
.sa_family
= AF_UNIX
;
254 (void)strncpy(SyslogAddr
.sa_data
, _PATH_LOG
,
255 sizeof(SyslogAddr
.sa_data
));
256 if (LogStat
& LOG_NDELAY
) {
257 if ((LogFile
= __socket(AF_UNIX
, LogType
, 0))
260 (void)__fcntl(LogFile
, F_SETFD
, 1);
263 if (LogFile
!= -1 && !connected
)
265 int old_errno
= errno
;
266 if (__connect(LogFile
, &SyslogAddr
, sizeof(SyslogAddr
))
269 int saved_errno
= errno
;
270 (void)__close(LogFile
);
272 if (LogType
== SOCK_DGRAM
273 && saved_errno
== EPROTOTYPE
)
275 /* retry with next SOCK_STREAM: */
276 LogType
= SOCK_STREAM
;
277 __set_errno (old_errno
);
288 openlog (const char *ident
, int logstat
, int logfac
)
290 /* Protect against multiple users. */
291 __libc_cleanup_region_start ((void (*) __P ((void *))) __libc_mutex_unlock
,
293 __libc_lock_lock (syslog_lock
);
295 openlog_internal (ident
, logstat
, logfac
);
298 __libc_cleanup_region_end (1);
302 sigpipe_handler (int signo
)
304 closelog_internal ();
321 /* Protect against multiple users. */
322 __libc_cleanup_region_start ((void (*) __P ((void *))) __libc_mutex_unlock
,
324 __libc_lock_lock (syslog_lock
);
326 closelog_internal ();
328 LogType
= SOCK_DGRAM
; /* this is the default */
331 __libc_cleanup_region_end (1);
334 #ifdef _LIBC_REENTRANT
336 cancel_handler (void *ptr
)
338 /* Restore the old signal handler. */
339 struct sigaction
*oldaction
= *((struct sigaction
**) ptr
);
341 if (oldaction
!= (struct sigaction
*) NULL
)
342 __sigaction (SIGPIPE
, oldaction
, (struct sigaction
*) NULL
);
345 __libc_lock_unlock (syslog_lock
);
349 /* setlogmask -- set the log mask level */