"Fossies" - the Fresh Open Source Software Archive

Member "memcached-1.6.15/scripts/start-memcached" (16 Jul 2020, 2770 Bytes) of package /linux/www/memcached-1.6.15.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Perl source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 #!/usr/bin/perl -w
    2 
    3 # start-memcached
    4 # 2003/2004 - Jay Bonci <jaybonci@debian.org>
    5 # This script handles the parsing of the /etc/memcached.conf file
    6 # and was originally created for the Debian distribution.
    7 # Anyone may use this little script under the same terms as
    8 # memcached itself.
    9 
   10 use POSIX qw(setsid);
   11 use strict;
   12 
   13 if($> != 0 and $< != 0)
   14 {
   15     print STDERR "Only root wants to run start-memcached.\n";
   16     exit;
   17 }
   18 
   19 my $params; my $etchandle; my $etcfile = "/etc/memcached.conf";
   20 
   21 # This script assumes that memcached is located at /usr/bin/memcached, and
   22 # that the pidfile is writable at /var/run/memcached.pid
   23 
   24 my $memcached = "/usr/bin/memcached";
   25 my $pidfile = "/var/run/memcached.pid";
   26 
   27 if (scalar(@ARGV) == 2) {
   28     $etcfile = shift(@ARGV);
   29     $pidfile = shift(@ARGV);
   30 }
   31 
   32 # If we don't get a valid logfile parameter in the /etc/memcached.conf file,
   33 # we'll just throw away all of our in-daemon output. We need to re-tie it so
   34 # that non-bash shells will not hang on logout. Thanks to Michael Renner for
   35 # the tip
   36 my $fd_reopened = "/dev/null";
   37 
   38 sub handle_logfile
   39 {
   40     my ($logfile) = @_;
   41     $fd_reopened = $logfile;
   42 }
   43 
   44 sub reopen_logfile
   45 {
   46     my ($logfile) = @_;
   47 
   48     open *STDERR, ">>$logfile";
   49     open *STDOUT, ">>$logfile";
   50     open *STDIN, ">>/dev/null";
   51     $fd_reopened = $logfile;
   52 }
   53 
   54 # This is set up in place here to support other non -[a-z] directives
   55 
   56 my $conf_directives = {
   57     "logfile" => \&handle_logfile,
   58 };
   59 
   60 if(open $etchandle, $etcfile)
   61 {
   62     foreach my $line (<$etchandle>)
   63     {
   64         $line ||= "";
   65         $line =~ s/(?<!\\)\#[^\#]*//g;
   66         $line =~ s/\s+$//g;
   67         $line =~ s/^\s+//g;
   68         next unless $line;
   69         next if $line =~ /^\-[dh]/;
   70 
   71         if($line =~ /^[^\-]/)
   72         {
   73             my ($directive, $arg) = $line =~ /^(.*?)\s+(.*)/;
   74             $conf_directives->{$directive}->($arg);
   75             next;
   76         }
   77 
   78         push @$params, $line;
   79     }
   80 
   81 }else{
   82     $params = [];
   83 }
   84 
   85 push @$params, "-u root" unless(grep "-u", @$params);
   86 $params = join " ", @$params;
   87 
   88 if(-e $pidfile)
   89 {
   90     open PIDHANDLE, "$pidfile";
   91     my $localpid = <PIDHANDLE>;
   92     close PIDHANDLE;
   93 
   94     chomp $localpid;
   95     if(-d "/proc/$localpid")
   96     {
   97         print STDERR "memcached is already running.\n";
   98         exit;
   99     }else{
  100         unlink $pidfile;
  101     }
  102 
  103 }
  104 
  105 my $pid = fork();
  106 
  107 if($pid == 0)
  108 {
  109     # setsid makes us the session leader
  110     setsid();
  111     reopen_logfile($fd_reopened);
  112     # must fork again now that tty is closed
  113     $pid = fork();
  114     if ($pid) {
  115       if(open PIDHANDLE,">$pidfile")
  116       {
  117           print PIDHANDLE $pid;
  118           close PIDHANDLE;
  119       }else{
  120 
  121           print STDERR "Can't write pidfile to $pidfile.\n";
  122       }
  123       exit(0);
  124     }
  125     exec "$memcached $params";
  126     exit(0);
  127 
  128 }