#!/usr/bin/perl -Tw # # use strict; use POSIX qw(setsid); use constant VERSION => 1.10; $ENV{'PATH'} = '/bin'; delete @ENV{qw(IFS CDPATH ENV BASH_ENV)}; my $PID_FILENAME = "/var/run/one-user-only.pid"; my $ONE_USER_ONLY = "/usr/local/bin/one-user-only.sh"; # # If this file does exist, then just die accordingly # die("$PID_FILENAME exists!!") if ( -f $PID_FILENAME ); # # If this file does not exist, then just die accordingly # die("Cannot find $ONE_USER_ONLY") if ( ! -f $ONE_USER_ONLY ); sub daemonize { chdir "/"; # Close all default file descriptors open (STDIN, "/dev/null"); open (STDERR, ">&STDOUT"); my $pid = fork (); # Write the PID to /var/run open (FD, ">$PID_FILENAME") or die("Cannot write PID: $!"); print FD "$$\n"; close(FD); # Detach from the terminal so interrupts cannot occur if ($pid < 0) { die "fork: $!"; } elsif ($pid) { exit 0; } # Make session leader setsid or die "setsid: $!"; umask 0; foreach (0 .. (POSIX::sysconf (&POSIX::_SC_OPEN_MAX) || 1024)) { POSIX::close $_ } } my $continue = 1; $SIG{TERM} = sub { unlink $PID_FILENAME; $continue = 0; }; &daemonize; while ($continue) { sleep 2; system($ONE_USER_ONLY); }