summaryrefslogtreecommitdiff
path: root/lib/libc/gen/syslog.c
diff options
context:
space:
mode:
authorHugh Graham <hugh@cvs.openbsd.org>2000-01-02 23:35:59 +0000
committerHugh Graham <hugh@cvs.openbsd.org>2000-01-02 23:35:59 +0000
commitf95ba9fa5d5ccecbae449a3fbe7065fd82b4bbd9 (patch)
tree5ef9a0cab67d4ce1414761f0caca6e42375f9ab6 /lib/libc/gen/syslog.c
parentd058cd399506bf5b5e3d43742d30cdabda06a8e7 (diff)
Try to reopen socket if send fails. (perhaps syslogd restarted)
Adapted from FreeBSD (N.B. NetBSD); millert@ OK.
Diffstat (limited to 'lib/libc/gen/syslog.c')
-rw-r--r--lib/libc/gen/syslog.c77
1 files changed, 56 insertions, 21 deletions
diff --git a/lib/libc/gen/syslog.c b/lib/libc/gen/syslog.c
index d1bb6af360e..11a106ba85c 100644
--- a/lib/libc/gen/syslog.c
+++ b/lib/libc/gen/syslog.c
@@ -32,7 +32,7 @@
*/
#if defined(LIBC_SCCS) && !defined(lint)
-static char rcsid[] = "$OpenBSD: syslog.c,v 1.8 1998/03/19 00:30:03 millert Exp $";
+static char rcsid[] = "$OpenBSD: syslog.c,v 1.9 2000/01/02 23:35:58 hugh Exp $";
#endif /* LIBC_SCCS and not lint */
#include <sys/types.h>
@@ -58,12 +58,15 @@ static char rcsid[] = "$OpenBSD: syslog.c,v 1.8 1998/03/19 00:30:03 millert Exp
static int LogFile = -1; /* fd for log */
static int connected; /* have done connect */
+static int opened; /* have done openlog() */
static int LogStat = 0; /* status bits, set by openlog() */
static const char *LogTag = NULL; /* string to tag the entry with */
static int LogFacility = LOG_USER; /* default facility code */
static int LogMask = 0xff; /* mask of priorities to be logged */
extern char *__progname; /* Program name, from crt0. */
+static void disconnectlog __P((void)); /* disconnect from syslogd */
+static void connectlog __P((void)); /* (re)connect to syslogd */
/*
* syslog, vsyslog --
* print message on log file; output is intended for syslogd(8).
@@ -212,8 +215,18 @@ vsyslog(pri, fmt, ap)
}
/* Get connected, output the message to the local logger. */
- if (!connected)
- openlog(LogTag, LogStat | LOG_NDELAY, 0);
+ if (!opened)
+ openlog(LogTag, LogStat, 0);
+ connectlog();
+ if (send(LogFile, tbuf, cnt, 0) >= 0)
+ return;
+
+ /*
+ * If the send() failed, the odds are syslogd was restarted.
+ * Make one (only) attempt to reconnect to /dev/log.
+ */
+ disconnectlog();
+ connectlog();
if (send(LogFile, tbuf, cnt, 0) >= 0)
return;
@@ -236,35 +249,40 @@ vsyslog(pri, fmt, ap)
}
}
-static struct sockaddr_un SyslogAddr; /* AF_UNIX address of local logger */
+static void
+disconnectlog()
+{
+ /*
+ * If the user closed the FD and opened another in the same slot,
+ * that's their problem. They should close it before calling on
+ * system services.
+ */
+ if (LogFile != -1) {
+ close(LogFile);
+ LogFile = -1;
+ }
+ connected = 0; /* retry connect */
+}
-void
-openlog(ident, logstat, logfac)
- const char *ident;
- int logstat, logfac;
+static void
+connectlog()
{
- if (ident != NULL)
- LogTag = ident;
- LogStat = logstat;
- if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0)
- LogFacility = logfac;
+ struct sockaddr_un SyslogAddr; /* AF_UNIX address of local logger */
if (LogFile == -1) {
+ if ((LogFile = socket(AF_UNIX, SOCK_DGRAM, 0)) == -1)
+ return;
+ (void)fcntl(LogFile, F_SETFD, 1);
+ }
+ if (LogFile != -1 && !connected) {
memset(&SyslogAddr, '\0', sizeof(SyslogAddr));
SyslogAddr.sun_len = sizeof(SyslogAddr);
SyslogAddr.sun_family = AF_UNIX;
(void)strncpy(SyslogAddr.sun_path, _PATH_LOG,
sizeof(SyslogAddr.sun_path) - 1);
SyslogAddr.sun_path[sizeof(SyslogAddr.sun_path) - 1] = '\0';
- if (LogStat & LOG_NDELAY) {
- if ((LogFile = socket(AF_UNIX, SOCK_DGRAM, 0)) == -1)
- return;
- (void)fcntl(LogFile, F_SETFD, 1);
- }
- }
- if (LogFile != -1 && !connected) {
if (connect(LogFile, (struct sockaddr *)&SyslogAddr,
- sizeof(SyslogAddr)) == -1) {
+ sizeof(SyslogAddr)) == -1) {
(void)close(LogFile);
LogFile = -1;
} else
@@ -273,6 +291,23 @@ openlog(ident, logstat, logfac)
}
void
+openlog(ident, logstat, logfac)
+ const char *ident;
+ int logstat, logfac;
+{
+ if (ident != NULL)
+ LogTag = ident;
+ LogStat = logstat;
+ if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0)
+ LogFacility = logfac;
+
+ if (LogStat & LOG_NDELAY) /* open immediately */
+ connectlog();
+
+ opened = 1; /* ident and facility has been set */
+}
+
+void
closelog()
{
(void)close(LogFile);