# HG changeset patch # User Matthias Förste foerste@schlittermann.de # Date 1336044794 -7200 # Node ID a9b77650cbc4b50c04f2ab43af72fef33e5e57ac # Parent 8d116efc6867f0120b4051c27637bbaa0f65bef8 added init script; run as daemon by default diff -r 8d116efc6867 -r a9b77650cbc4 .hgignore --- a/.hgignore Mon Apr 30 12:57:15 2012 +0200 +++ b/.hgignore Thu May 03 13:33:14 2012 +0200 @@ -1,5 +1,5 @@ syntax: re -^wgnd-watch +^(rc\.)?wgnd-watch ^wgnd-watch.map.pl ^debian/files$ ^debian/wgnd-watch[./] diff -r 8d116efc6867 -r a9b77650cbc4 Makefile --- a/Makefile Mon Apr 30 12:57:15 2012 +0200 +++ b/Makefile Thu May 03 13:33:14 2012 +0200 @@ -8,12 +8,17 @@ mandir = $(datarootdir)/man sbinscripts = $(package) +rcdir = /etc/init.d + %: %.pl cp -a $< $@ %.1: %.pl pod2man $< $@ -all: $(package) $(package).1 +rc.%: rc.%.in + perl -pe 's{__sbindir__}{${sbindir}}g' <$< >$@ + +all: $(package) $(package).1 rc.$(package) install: all install -m 0755 -d $(DESTDIR)$(sbindir) @@ -22,6 +27,8 @@ install -m 0755 $(package).map.pl.ex $(DESTDIR)$(docdir) install -m 0755 -d $(DESTDIR)$(mandir)/man1 install -m 0755 $(package).1 $(DESTDIR)$(mandir)/man1 + install -m 0755 -d $(DESTDIR)$(rcdir) + install -m 0755 rc.$(package) $(DESTDIR)$(rcdir)/$(package) clean: - -rm -f $(package) $(package).1 + -rm -f $(package) $(package).1 rc.$(package) diff -r 8d116efc6867 -r a9b77650cbc4 rc.wgnd-watch.in --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rc.wgnd-watch.in Thu May 03 13:33:14 2012 +0200 @@ -0,0 +1,38 @@ +#! /bin/sh + +### BEGIN INIT INFO +# Provides: wgnd-watch +# Required-Start: $local_fs $remote_fs $syslog $named $network $time +# Required-Stop: $local_fs $remote_fs $syslog $named $network +# Should-Start: +# Should-Stop: +# Default-Start: 2 3 4 5 +# Default-Stop: 0 1 6 +# Short-Description: Start/Stop the wgnd-watch daemon +### END INIT INFO + +DAEMON=__sbindir__/wgnd-watch +PIDFILE=/var/run/`basename $DAEMON`.pid + +case $1 in + + start) + start-stop-daemon -v --start --pidfile $PIDFILE --startas $DAEMON + ;; + + stop) + start-stop-daemon -v --stop --retry 30 --pidfile $PIDFILE + ;; + + restart) echo "Restarting $NAME..." + $0 stop + $0 start + ;; + + *) + echo "Usage: $0 {start|stop|restart}" >&2 + exit 1 + ;; +esac + +exit 0 diff -r 8d116efc6867 -r a9b77650cbc4 wgnd-watch.pl --- a/wgnd-watch.pl Mon Apr 30 12:57:15 2012 +0200 +++ b/wgnd-watch.pl Thu May 03 13:33:14 2012 +0200 @@ -25,17 +25,17 @@ my $VERSION = '0.1'; my $ME = $0; + +use File::Basename; +# File::Rsync in squeeze does not support --xattrs yet +#use File::Rsync; use Getopt::Long; use Pod::Usage; use Linux::Inotify2; -# File::Rsync in squeeze does not support --xattrs yet -#use File::Rsync; - -my $map = 'wgnd-watch.map.pl'; +use Sys::Syslog; BEGIN { - use Sys::Syslog; openlog($ME, 'ndelay,nowait,pid', 'LOG_USER'); $SIG{__WARN__} = sub { warn @_ if not defined $^S; @@ -48,16 +48,25 @@ syslog('err', "@_"); exit $?; }; - sub dolog { print "@_"; syslog('info', "@_"); } sub trace { print @_ if $ENV{DEBUG}; } + use sigtrap qw(die normal-signals); + } +my %opts = ( + daemon => 1, + map => 'wgnd-watch.map.pl', + pidfile => '/var/run/wgnd-watch.pid' +); + GetOptions( - "map=s" => \$map, - "h|help" => sub { pod2usage( -verbose => 0, -exitval => 0 ) }, - "m|man" => sub { + "map=s" => \$opts{map}, + "daemon!" => \$opts{daemon}, + "pidfile=s" => \$opts{pidfile}, + "h|help!" => sub { pod2usage( -verbose => 0, -exitval => 0 ) }, + "m|man!" => sub { pod2usage( -verbose => 2, -exitval => 0, @@ -68,10 +77,35 @@ our $source; use lib ('.', $ENV{HOME}, '/etc'); -require $map; +require $opts{map}; + +$0 = "$ME @ARGV"; + +chdir("/") or die "Can't chdir to /: $!\n"; + +if ($opts{daemon}) { + + open STDIN, "/dev/null" or die "Can't redir STDOUT: $!\n"; + + defined (my $pid = fork) or die "Can't fork: $!\n"; + if ($pid) { + dolog "Child is [$pid]"; + exit 0; + } + POSIX::setsid() + or die "Can't setsid: $!\n"; + open(STDERR, ">&STDOUT") or die "Can't dup stdout: $!\n"; + + open(P, '>', $opts{pidfile}) + or die "Can't open '>', '$opts{pidfile}': $!\n"; + print P $$; + close P; + +} my $inotify = new Linux::Inotify2 - or die "Can't create new inotify object: $!"; + or die "Can't create new inotify object: $!\n"; my @rsync = qw(/usr/bin/rsync -ihv -aAX); for (keys %{$source}) { @@ -102,13 +136,17 @@ $inotify->poll; while (-1 != (my $pid = wait)) { my $e = $? >> 8; - dolog "child ${ME}[$pid]: exit $e\n"; + dolog "Child ${ME}[$pid]: exit $e\n"; } } END { + + unlink $opts{pidfile} if $opts{daemon}; + dolog "exit"; closelog; + } __END__ @@ -135,11 +173,20 @@ =over +=item B<--[no]daemon> + +[Don't] run as a daemon. Default: do. + =item B<--map> I Name of a file containing mappings between templates and directories. Defaults to F. +=item B<--pidfile> I + +Location of the pid file. This is ignored unless we are running as a daemon. +Defaults to F. + =back =head1 FILES @@ -150,6 +197,10 @@ default for B<--map> +=item F + +default for B<--pidfile> + =back =head1 AUTHOR