]> git.proxmox.com Git - pve-common.git/blobdiff - src/PVE/ProcFSTools.pm
ProcFSTools: add kernel_version
[pve-common.git] / src / PVE / ProcFSTools.pm
index 027aa3ad1bc5140926e8367dc07f76b7f6469bf9..94a2d549ece5d410d02be23a2761787038bddcef 100644 (file)
@@ -80,6 +80,23 @@ sub read_proc_uptime {
     return (0, 0);
 }
 
+sub kernel_version {
+    my $line = PVE::Tools::file_read_firstline("/proc/version");
+
+    if ($line && $line =~ m|^Linux\sversion\s((\d+(?:\.\d+)+)-?(\S+)?)|) {
+        my ($fullversion, $version_numbers, $extra) = ($1, $2, $3);
+
+       # variable names are the one from the Linux kernel Makefile
+       my ($version, $patchlevel, $sublevel) = split(/\./, $version_numbers);
+
+       return wantarray
+           ? (int($version), int($patchlevel), int($sublevel), $extra, $fullversion)
+           : $fullversion;
+    }
+
+    return (0, 0, 0, '', '');
+}
+
 sub read_loadavg {
 
     my $line = PVE::Tools::file_read_firstline('/proc/loadavg');
@@ -150,6 +167,7 @@ sub read_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+/) {
        return {
            status => $1,
+           ppid => $2,
            utime => $3,
            stime => $4,
            starttime => $7,
@@ -168,9 +186,9 @@ sub check_process_running {
     # 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 && (!$pstart || ($info->{starttime} eq $pstart)) && ($info->{status} ne 'Z') ? $info : undef;
 }
 
@@ -200,7 +218,7 @@ sub read_meminfo {
     while (my $line = <$fh>) {
        if ($line =~ m/^(\S+):\s+(\d+)\s*kB/i) {
            $d->{lc ($1)} = $2 * 1024;
-       } 
+       }
     }
     close($fh);