mirror of https://github.com/xzeldon/htop.git
Merge pull request #398 from natoscott/harden-linux-btime-init
Harden the extraction of boot time for the Linux platform
This commit is contained in:
commit
4507911cc3
|
@ -226,26 +226,25 @@ ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidMatchList, ui
|
||||||
// Test /proc/PID/smaps_rollup availability (faster to parse, Linux 4.14+)
|
// Test /proc/PID/smaps_rollup availability (faster to parse, Linux 4.14+)
|
||||||
this->haveSmapsRollup = (access(PROCDIR "/self/smaps_rollup", R_OK) == 0);
|
this->haveSmapsRollup = (access(PROCDIR "/self/smaps_rollup", R_OK) == 0);
|
||||||
|
|
||||||
// Read btime
|
// Read btime (the kernel boot time, as number of seconds since the epoch)
|
||||||
{
|
{
|
||||||
FILE* statfile = fopen(PROCSTATFILE, "r");
|
FILE* statfile = fopen(PROCSTATFILE, "r");
|
||||||
if (statfile == NULL) {
|
if (statfile == NULL)
|
||||||
CRT_fatalError("Cannot open " PROCSTATFILE);
|
CRT_fatalError("Cannot open " PROCSTATFILE);
|
||||||
}
|
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
char buffer[PROC_LINE_LENGTH + 1];
|
char buffer[PROC_LINE_LENGTH + 1];
|
||||||
if (fgets(buffer, sizeof(buffer), statfile) == NULL) {
|
if (fgets(buffer, sizeof(buffer), statfile) == NULL)
|
||||||
CRT_fatalError("No btime in " PROCSTATFILE);
|
break;
|
||||||
} else if (String_startsWith(buffer, "btime ")) {
|
if (String_startsWith(buffer, "btime ") == false)
|
||||||
if (sscanf(buffer, "btime %lld\n", &btime) != 1) {
|
continue;
|
||||||
|
if (sscanf(buffer, "btime %lld\n", &btime) == 1)
|
||||||
|
break;
|
||||||
CRT_fatalError("Failed to parse btime from " PROCSTATFILE);
|
CRT_fatalError("Failed to parse btime from " PROCSTATFILE);
|
||||||
}
|
}
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fclose(statfile);
|
fclose(statfile);
|
||||||
|
|
||||||
|
if (!btime)
|
||||||
|
CRT_fatalError("No btime in " PROCSTATFILE);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialize CPU count
|
// Initialize CPU count
|
||||||
|
|
Loading…
Reference in New Issue