Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
netlog: Remove daemon features
We now run the netlog daemon as a systemd service. The features to
demonize, redirect logging, restart and kill previous daemons are all
not needed. Additionally, the restart is buggy, because
Donald::Tools::kill_previous_server doesn't kill the parent - assuming
this is an rc.d script. However, in `--foregound` mode, the previous
daemon is in fact the parent.

- Remove unused features and leave job control and logging to systemd.
- Log into journal instead of into syslog.
- Remove "Description=" from the unit, because its easier to identify
  things, when the name used in messages is the same as the unit name
  and the unit file name.
  • Loading branch information
donald committed Jan 20, 2023
1 parent 815a36b commit 4f97485
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 46 deletions.
43 changes: 1 addition & 42 deletions netlog/netlog
Expand Up @@ -3,12 +3,9 @@
use warnings;
use strict;

use Donald::Tools;
use Donald::Select;
use IO::Socket;
use Getopt::Long;
use Sys::Syslog;
use Donald::Select::Watchfile;

our ($RCS_REVISION)='$Revision: 1.36 $'=~/([\d.]+)/;

Expand Down Expand Up @@ -325,28 +322,17 @@ sub do_syslog {
sub USAGE {
return<<'__EOF__';
usage:
$0 --kill # try to kill a running server
$0 --daemon [options] logfile...
--kill # try to kill previous server first
--foreground # stay in foreground, log to stderr
--syslog # log to syslog instead of stderr
$0 --daemon logfile...
$0 --test logfile.... # debug: just run filters against files
__EOF__
}


GetOptions (
'kill' => \$options{'kill'},
'daemon' => \$options{'daemon'},
'foreground' => \$options{'foreground'},
'syslog' => \$options{'syslog'},
'test' => \$options{'test'},
) or die USAGE;


if ($options{'test'}) {
@ARGV>=1 or die USAGE;
while (<>) {
Expand All @@ -359,30 +345,6 @@ if ($options{'test'}) {

$SIG{PIPE}='IGNORE';

$options{'kill'} and Donald::Tools::kill_previous_server('netlog') and sleep 2;

unless ($options{'foreground'}) {
my $pid=fork;
defined $pid or die "$!\n";
$pid and exit;
}

if ($options{'syslog'} or not $options{'foreground'}) {
openlog('netlog','pid','daemon');
Sys::Syslog::setlogsock('unix'); # with 'native' we get EOLs in the logfile, option "noeol" doesn't work
$SIG{__WARN__} = sub { syslog('warning',@_); };
$SIG{__DIE__} = sub { syslog('crit',@_);syslog('crit','exiting');exit 1;};
open (STDOUT,'>','/dev/null');
open (STDERR,'>','/dev/null');
open (STDIN,'<','/dev/null');
}
warn "server started\n";
Donald::Select::Watchfile->new ($0,5,sub {
system $0,'--kill','--daemon',($options{'foreground'}?'--foreground':()),($options{'syslog'}?'--syslog':()),@ARGV or exit;
warn "restart failed\n";
}
);

{ # dirty hack
chomp(my $hostname=`hostname -s`);
push @ARGV,'/package/syslog/log/current.log' if defined $hostname && $hostname eq 'wtf';
Expand All @@ -391,9 +353,6 @@ if ($options{'test'}) {
syslog_new(@ARGV);
Donald::Select::timeout(5,\&check_idle);
Donald::Select::run();
} elsif ($options{'kill'}) {
@ARGV==0 or die USAGE;
Donald::Tools::kill_previous_server('netlog');
} else {
die USAGE;
}
5 changes: 1 addition & 4 deletions netlog/netlog.service
@@ -1,13 +1,10 @@
[Unit]
Description=Donalds netlog service
Requires=network.target
After=network.target

[Service]
ExecStart=/usr/sbin/netlog --daemon --foreground --kill --syslog /var/log/messages /var/log/mail.log
ExecStart=/usr/sbin/netlog --daemon /var/log/messages /var/log/mail.log
Restart=always
StandardOutput=syslog


[Install]
WantedBy=multi-user.target
Expand Down

0 comments on commit 4f97485

Please sign in to comment.