mirror of
https://git.proxmox.com/git/pve-common
synced 2025-04-29 09:17:15 +00:00
implement a clean way to read /prod/[pid]/stat
Added the method check_process_running() to check if a process is running.
This commit is contained in:
parent
8104dfe38b
commit
5157a3a246
@ -53,7 +53,7 @@ sub read_proc_uptime {
|
|||||||
my $line = PVE::Tools::file_read_firstline("/proc/uptime");
|
my $line = PVE::Tools::file_read_firstline("/proc/uptime");
|
||||||
if ($line && $line =~ m|^(\d+\.\d+)\s+(\d+\.\d+)\s*$|) {
|
if ($line && $line =~ m|^(\d+\.\d+)\s+(\d+\.\d+)\s*$|) {
|
||||||
if ($ticks) {
|
if ($ticks) {
|
||||||
return (int($1*100), int($2*100));
|
return (int($1*$clock_ticks), int($2*$clock_ticks));
|
||||||
} else {
|
} else {
|
||||||
return (int($1), int($2));
|
return (int($1), int($2));
|
||||||
}
|
}
|
||||||
@ -124,18 +124,43 @@ sub read_proc_stat {
|
|||||||
return $res;
|
return $res;
|
||||||
}
|
}
|
||||||
|
|
||||||
sub read_proc_starttime {
|
sub read_proc_pid_stat {
|
||||||
my $pid = shift;
|
my $pid = shift;
|
||||||
|
|
||||||
my $statstr = PVE::Tools::file_read_firstline("/proc/$pid/stat");
|
my $statstr = PVE::Tools::file_read_firstline("/proc/$pid/stat");
|
||||||
|
|
||||||
if ($statstr && $statstr =~ m/^$pid \(.*\) \S (-?\d+) -?\d+ -?\d+ -?\d+ -?\d+ \d+ \d+ \d+ \d+ \d+ (\d+) (\d+) (-?\d+) (-?\d+) -?\d+ -?\d+ -?\d+ 0 (\d+) (\d+) (-?\d+) \d+ \d+ \d+ \d+ \d+ \d+ \d+ \d+ \d+ \d+ \d+ \d+ \d+ -?\d+ -?\d+ \d+ \d+ \d+/) {
|
if ($statstr && $statstr =~ m/^$pid \(.*\) (\S) (-?\d+) -?\d+ -?\d+ -?\d+ -?\d+ \d+ \d+ \d+ \d+ \d+ (\d+) (\d+) (-?\d+) (-?\d+) -?\d+ -?\d+ -?\d+ 0 (\d+) (\d+) (-?\d+) \d+ \d+ \d+ \d+ \d+ \d+ \d+ \d+ \d+ \d+ \d+ \d+ \d+ -?\d+ -?\d+ \d+ \d+ \d+/) {
|
||||||
my $starttime = $6;
|
return {
|
||||||
|
status => $1,
|
||||||
return $starttime;
|
utime => $3,
|
||||||
|
stime => $4,
|
||||||
|
starttime => $7,
|
||||||
|
vsize => $8,
|
||||||
|
rss => $9 * 4096,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return undef;
|
||||||
|
}
|
||||||
|
|
||||||
|
sub check_process_running {
|
||||||
|
my ($pid, $pstart) = @_;
|
||||||
|
|
||||||
|
# note: waitpid only work for child processes, but not
|
||||||
|
# for processes spanned by other processes.
|
||||||
|
# kill(0, pid) return succes for zombies.
|
||||||
|
# So we read the status form /proc/$pid/stat instead
|
||||||
|
|
||||||
|
my $info = read_proc_pid_stat($pid);
|
||||||
|
|
||||||
|
return $info && ($info->{starttime} eq $pstart) && ($info->{status} ne 'Z') ? $info : undef;
|
||||||
|
}
|
||||||
|
|
||||||
|
sub read_proc_starttime {
|
||||||
|
my $pid = shift;
|
||||||
|
|
||||||
|
my $info = read_proc_pid_stat($pid);
|
||||||
|
return $info ? $info->{starttime} : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
sub read_meminfo {
|
sub read_meminfo {
|
||||||
|
Loading…
Reference in New Issue
Block a user