Commit ae2122f7 authored by Leigh B Stoller's avatar Leigh B Stoller

Add some utility routines for dealing with pidfiles in /var/run;

CheckDaemonRunning(), MarkDaemonRunning(), and MarkDaemonStopped()
do what you expect.
parent 8854414a
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
# #
# EMULAB-COPYRIGHT # EMULAB-COPYRIGHT
# Copyright (c) 2000-2009 University of Utah and the Flux Group. # Copyright (c) 2000-2010 University of Utah and the Flux Group.
# All rights reserved. # All rights reserved.
# #
...@@ -20,7 +20,8 @@ use Exporter; ...@@ -20,7 +20,8 @@ use Exporter;
TBSCRIPTLOCK_IGNORE TBSCRIPTLOCK_FAILED TBSCRIPTLOCK_IGNORE TBSCRIPTLOCK_FAILED
PROJROOT GROUPROOT USERROOT SCRATCHROOT SHAREROOT PROJROOT GROUPROOT USERROOT SCRATCHROOT SHAREROOT
TBValidUserDir TBValidUserDirList TBMakeTempFile NewUUID System TBValidUserDir TBValidUserDirList TBMakeTempFile NewUUID System
SendProjAdminMail ReOpenLog); SendProjAdminMail ReOpenLog
CheckDaemonRunning MarkDaemonRunning MarkDaemonStopped);
@EXPORT_OK = @EXPORT_OK =
qw ( TBMAIL_PROJADMIN); qw ( TBMAIL_PROJADMIN);
...@@ -804,4 +805,53 @@ sub System($) ...@@ -804,4 +805,53 @@ sub System($)
return $retval; return $retval;
} }
#
# Check for the existence of a pid file and see if that file is
# running. Mostly cause of devel tree versions.
#
sub CheckDaemonRunning($)
{
my ($name) = @_;
my $pidfile = "/var/run/${name}.pid";
if (-e $pidfile) {
my $opid = `cat $pidfile`;
if ($opid =~ /^(\d*)$/) {
$opid = $1;
}
else {
print STDERR "$pidfile exists, but $opid is malformed\n";
return 1;
}
if (kill(0, $opid)) {
print STDERR "$pidfile exists, and process $opid is running\n";
return 1;
}
unlink($pidfile);
}
return 0;
}
#
# Mark a daemon as running.
#
sub MarkDaemonRunning($)
{
my ($name) = @_;
my $pidfile = "/var/run/${name}.pid";
if (system("echo '$PID' > $pidfile")) {
print STDERR "Could not create $pidfile\n";
return -1;
}
return 0;
}
sub MarkDaemonStopped($)
{
my ($name) = @_;
my $pidfile = "/var/run/${name}.pid";
unlink($pidfile);
return 0;
}
1; 1;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment