From 11c4e8510a0de2850f5ee637ae846980fcabe759 Mon Sep 17 00:00:00 2001 From: "David A. Madore" Date: Mon, 22 Feb 2010 15:03:47 +0100 Subject: Implement LOGM command (write a message to syslog). --- emergencyd.pl | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) (limited to 'emergencyd.pl') diff --git a/emergencyd.pl b/emergencyd.pl index fe21de6..0668110 100755 --- a/emergencyd.pl +++ b/emergencyd.pl @@ -33,10 +33,13 @@ # quit. The RKEY command causes the daemon to reread its key file: # the server responds either DONE or !ERR in case of error, in which # case the next line in the response is a human-readable error line. -# The SYRQ command is followed by whitespace and then by data to be -# written to /proc/sysrq-trigger: the server responds either DONE or -# !ERR in case of error, in which case the next line in the response -# is a human-readable error line. +# The LOGM command is followed by whitespace, an optional priority (as +# in the LOG_* syslog macros: WARNING, DEBUG, etc.; again followed by +# whitespace), and a message to be written to syslog: the server +# responds DONE. The SYRQ command is followed by whitespace and then +# by data to be written to /proc/sysrq-trigger: the server responds +# either DONE or !ERR in case of error, in which case the next line in +# the response is a human-readable error line. # *** The emergency daemon itself *** # @@ -60,10 +63,12 @@ use Digest::SHA qw(hmac_sha256_hex); use Socket; use Socket6; use POSIX (); +use Sys::Syslog qw(:standard :macros); use Getopt::Std; use constant { - DEFAULT_PORT => 911 + DEFAULT_PORT => 911, + SYSLOG_IDENT => "emergencyd.pl" }; my %opts; @@ -103,6 +108,8 @@ if ( defined(*IPV6_V6ONLY{CODE}) ) { } bind $socket, sockaddr_in6($port, in6addr_any) or die "Can't bind socket: $!"; +openlog(SYSLOG_IDENT, "ndelay,pid", LOG_DAEMON); + if ( $opts{f} ) { chdir("/"); open STDIN, "/dev/null"; @@ -177,6 +184,18 @@ while (1) { $resp = "!ERR\n$@"; } send $socket, $resp, 0, $sender; + } elsif ( $command =~ /^LOGM\s+(.*)$/ ) { + my $s = $1; + my $level = LOG_NOTICE; + my %levels = ("EMERG"=>LOG_EMERG, "ALERT"=>LOG_ALERT, + "CRIT"=>LOG_CRIT, "ERR"=>LOG_ERR, "WARNING"=>LOG_WARNING, + "NOTICE"=>LOG_NOTICE, "INFO"=>LOG_INFO, "DEBUG"=>LOG_DEBUG); + if ( $s =~ /^([A-Z0-9]*)\s+(.*)/ && exists($levels{$1}) ) { + $level = $levels{$1}; + $s = $2; + } + syslog $level, $s; + send $socket, "DONE\n", 0, $sender; } elsif ( $command =~ /^SYRQ\s+(.*)$/ ) { my $s = $1; my $resp = "DONE\n"; -- cgit v1.2.3