Skip site navigation (1)Skip section navigation (2)
Date:      23 Mar 2002 23:52:42 +0100
From:      Dag-Erling Smorgrav <des@ofug.org>
To:        Mikhail Teterin <mi@aldan.algebra.com>
Cc:        arch@FreeBSD.org
Subject:   Re: review request for bin/11294
Message-ID:  <xzpn0wydhhh.fsf@flood.ping.uio.no>
In-Reply-To: <xzpr8mbcb1m.fsf@flood.ping.uio.no>
References:  <200203231928.g2NJSasb063343@aldan.algebra.com> <xzpr8mbcb1m.fsf@flood.ping.uio.no>

next in thread | previous in thread | raw e-mail | index | archive | help
--=-=-=

Here's a patch.  To log to a remote host, make /etc/loghost a symlink
to its name or IP address ('ln -fs 10.0.5.14 /etc/loghost').

DES
-- 
Dag-Erling Smorgrav - des@ofug.org


--=-=-=
Content-Type: text/x-patch
Content-Disposition: attachment; filename=syslog.diff

Index: lib/libc/gen/syslog.c
===================================================================
RCS file: /home/ncvs/src/lib/libc/gen/syslog.c,v
retrieving revision 1.25
diff -u -r1.25 syslog.c
--- lib/libc/gen/syslog.c	9 Mar 2002 11:38:01 -0000	1.25
+++ lib/libc/gen/syslog.c	23 Mar 2002 22:49:07 -0000
@@ -38,7 +38,7 @@
 __FBSDID("$FreeBSD: src/lib/libc/gen/syslog.c,v 1.25 2002/03/09 11:38:01 dwmalone Exp $");
 
 #include "namespace.h"
-#include <sys/types.h>
+#include <sys/param.h>
 #include <sys/socket.h>
 #include <sys/syslog.h>
 #include <sys/uio.h>
@@ -71,6 +71,8 @@
 
 static void	disconnectlog(void); /* disconnect from syslogd */
 static void	connectlog(void);	/* (re)connect to syslogd */
+static void	connectlog_remote(const char *);
+static void	connectlog_local(void);
 
 /*
  * Format of the magic cookie passed through the stdio hook
@@ -274,6 +276,7 @@
 		(void)_close(fd);
 	}
 }
+
 static void
 disconnectlog()
 {
@@ -289,8 +292,61 @@
 	connected = 0;			/* retry connect */
 }
 
+#define _PATH_LOGHOST	"/etc/loghost"
 static void
 connectlog()
+{
+	char loghost[MAXHOSTNAMELEN];
+	int len;
+
+	if (LogFile != -1 && connected)
+		return;
+	if (LogFile != -1) {
+		_close(LogFile);
+		LogFile = -1;
+	}
+	connected = 0;
+	len = _readlink(_PATH_LOGHOST, loghost, sizeof loghost);
+	if (len > 0 && len < sizeof loghost) {
+		loghost[len] = '\0';
+		connectlog_remote(loghost);
+	}
+	if (!connected)
+		connectlog_local();
+}
+
+#define SYSLOG_SERVICE	"syslog"
+static void
+connectlog_remote(const char *host)
+{
+	struct addrinfo hints, *res = NULL;
+
+	memset(&hints, 0, sizeof hints);
+	hints.ai_family = AF_UNSPEC;
+	hints.ai_socktype = SOCK_DGRAM;
+	hints.ai_protocol = PF_UNSPEC;
+	if (getaddrinfo(host, SYSLOG_SERVICE, &hints, &res) != 0)
+		goto fail;
+	LogFile = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
+	if (LogFile == -1)
+		goto fail;
+	if (connect(LogFile, res->ai_addr, res->ai_addrlen) == -1)
+		goto fail;
+	(void)_fcntl(LogFile, F_SETFD, 1);
+	freeaddrinfo(res);
+	connected = 1;
+	return;
+ fail:
+	if (res != NULL)
+		freeaddrinfo(res);
+	if (LogFile != -1) {
+		_close(LogFile);
+		LogFile = 1;
+	}
+}
+
+static void
+connectlog_local()
 {
 	struct sockaddr_un SyslogAddr;	/* AF_UNIX address of local logger */
 

--=-=-=--

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-arch" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?xzpn0wydhhh.fsf>