diff mbox

[OSSTEST,03/15] ts-syslog-server: New test script

Message ID 1498054447-11281-4-git-send-email-ian.jackson@eu.citrix.com (mailing list archive)
State New, archived
Headers show

Commit Message

Ian Jackson June 21, 2017, 2:13 p.m. UTC
This is a "complete implementation of the UDP syslog protocol".

In fact, the UDP syslog protocol is trivial: the UDP packets contain
newline-terminated unix text strings.  We can simply chomp them and
log them (along with the sending address and port).

This script is designed to be run with the new "|" spawn-ts mode: it
will keep running until it is reaped.

Currently it is not part of any jobs.

Signed-off-by: Ian Jackson <ian.jackson@eu.citrix.com>
---
 ts-syslog-server | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 85 insertions(+)
 create mode 100755 ts-syslog-server
diff mbox

Patch

diff --git a/ts-syslog-server b/ts-syslog-server
new file mode 100755
index 0000000..b17c531
--- /dev/null
+++ b/ts-syslog-server
@@ -0,0 +1,85 @@ 
+#!/usr/bin/perl -w
+# This is part of "osstest", an automated testing framework for Xen.
+# Copyright (C) 2009-2013 Citrix Inc.
+# 
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+# 
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Affero General Public License for more details.
+# 
+# You should have received a copy of the GNU Affero General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+use strict;
+
+unshift @INC, qw(.);
+use Osstest;
+use Osstest::TestSupport;
+
+use POSIX;
+use Fcntl;
+use Socket qw(PF_INET SOCK_DGRAM INADDR_ANY
+              pack_sockaddr_in unpack_sockaddr_in inet_aton inet_ntoa);
+
+tsreadconfig();
+
+die if @ARGV && $ARGV[0] =~ m/^-/;
+
+logm("starting syslog server ...");
+
+socket S, PF_INET, SOCK_DGRAM, 0 or die $!;
+
+my $sockname = pack_sockaddr_in(0, inet_aton(controller_ipaddr()));
+bind S, $sockname or die $!;
+$sockname = getsockname S or die $!;
+my ($port,$myaddr) = unpack_sockaddr_in($sockname) or die $!;
+$myaddr = inet_ntoa($myaddr);
+
+store_runvar('syslog_server',"$myaddr:$port");
+
+my @fhs = qw(S STDIN);
+
+foreach my $fh (@fhs) {
+    my $fl = fcntl($fh, F_GETFL, 0) // die $!;
+    fcntl($fh, F_SETFL, $fl | O_NONBLOCK) // die $!;
+}
+
+logm("syslog server running on $myaddr:$port");
+
+for (;;) {
+    my $rfds = '';
+    foreach my $fh (@fhs) {
+	vec($rfds, fileno($fh), 1) = 1;
+    }
+    my $efds = $rfds;
+    select($rfds, undef, $efds, undef);
+    if (vec($rfds, fileno(STDIN), 1)) {
+	my $d;
+	my $r = sysread STDIN, $d, 1;
+	if (defined $r) {
+	    die "read something from stdin!" if $r>0;
+	    last;
+	} else {
+	    die $! unless $!==EAGAIN || $!==EWOULDBLOCK;
+	}
+    }
+    if (vec($rfds, fileno(S), 1)) {
+	my $d;
+	my $sender = recv S, $d, 1024, 0;
+	if (defined $sender) {
+	    my ($sport,$saddr) = unpack_sockaddr_in($sender) or die $!;
+	    $saddr = inet_ntoa($saddr) // die $!;
+	    chomp $d;
+	    logm("$saddr:$sport $d");
+	} else {
+	    die $! unless $!==EAGAIN || $!==EWOULDBLOCK;
+	}
+    }
+}	
+
+logm("stopping.");