thorpedosg

Untitled

Aug 6th, 2018
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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 strict;
  11.  
  12. if($> != 0 and $< != 0)
  13. {
  14. print STDERR "Only root wants to run start-memcached.\n";
  15. exit;
  16. }
  17.  
  18. my $params; my $etchandle; my $etcfile = "/etc/memcached.conf";
  19.  
  20. # This script assumes that memcached is located at /usr/bin/memcached, and
  21. # that the pidfile is writable at /var/run/memcached.pid
  22.  
  23. my $memcached = "/usr/bin/memcached";
  24. my $pidfile = "/var/run/memcached.pid";
  25.  
  26. if (scalar(@ARGV) == 2) {
  27. $etcfile = shift(@ARGV);
  28. $pidfile = shift(@ARGV);
  29. }
  30.  
  31. # If we don't get a valid logfile parameter in the /etc/memcached.conf file,
  32. # we'll just throw away all of our in-daemon output. We need to re-tie it so
  33. # that non-bash shells will not hang on logout. Thanks to Michael Renner for
  34. # the tip
  35. my $fd_reopened = "/dev/null";
  36.  
  37. sub handle_logfile
  38. {
  39. my ($logfile) = @_;
  40. $fd_reopened = $logfile;
  41. }
  42.  
  43. sub reopen_logfile
  44. {
  45. my ($logfile) = @_;
  46.  
  47. open *STDERR, ">>$logfile";
  48. open *STDOUT, ">>$logfile";
  49. open *STDIN, ">>/dev/null";
  50. $fd_reopened = $logfile;
  51. }
  52.  
  53. # This is set up in place here to support other non -[a-z] directives
  54.  
  55. my $conf_directives = {
  56. "logfile" => \&handle_logfile,
  57. };
  58.  
  59. if(open $etchandle, $etcfile)
  60. {
  61. foreach my $line (<$etchandle>)
  62. {
  63. $line ||= "";
  64. $line =~ s/\#.*//g;
  65. $line =~ s/\s+$//g;
  66. $line =~ s/^\s+//g;
  67. next unless $line;
  68. next if $line =~ /^\-[dh]/;
  69.  
  70. if($line =~ /^[^\-]/)
  71. {
  72. my ($directive, $arg) = $line =~ /^(.*?)\s+(.*)/;
  73. $conf_directives->{$directive}->($arg);
  74. next;
  75. }
  76.  
  77. push @$params, $line;
  78. }
  79.  
  80. }else{
  81. $params = [];
  82. }
  83.  
  84. push @$params, "-u root" unless(grep "-u", @$params);
  85. $params = join " ", @$params;
  86.  
  87. if(-e $pidfile)
  88. {
  89. open PIDHANDLE, "$pidfile";
  90. my $localpid = <PIDHANDLE>;
  91. close PIDHANDLE;
  92.  
  93. chomp $localpid;
  94. if(-d "/proc/$localpid")
  95. {
  96. print STDERR "memcached is already running.\n";
  97. exit;
  98. }else{
  99. `rm -f $localpid`;
  100. }
  101.  
  102. }
  103.  
  104. my $pid = fork();
  105.  
  106. if($pid == 0)
  107. {
  108. reopen_logfile($fd_reopened);
  109. exec "$memcached $params";
  110. exit(0);
  111.  
  112. }else{
  113. if(open PIDHANDLE,">$pidfile")
  114. {
  115. print PIDHANDLE $pid;
  116. close PIDHANDLE;
  117. }else{
  118.  
  119. print STDERR "Can't write pidfile to $pidfile.\n";
  120. }
  121. }
Add Comment
Please, Sign In to add comment