diff options
author | Nathan Scott <nathans@redhat.com> | 2020-12-14 12:16:32 +1100 |
---|---|---|
committer | Nathan Scott <nathans@redhat.com> | 2020-12-14 12:16:32 +1100 |
commit | b7836515e8db8ff7a7cb99247472ac10992a4518 (patch) | |
tree | 8e85f748a0b5a119168b28ffabae0031c3d32997 /linux | |
parent | a3db2da4a7eac5287d347529dd8fe7780e57d6c9 (diff) |
Harden the extraction of boot time for the Linux platform
There is a possible path - albeit theoretical really - through
the btime initialization code in Linux ProcessList_new(), when
String_startsWith() is always false, which can result in btime
not being initialized.
This commit refactors the code to remove that possibility.
Diffstat (limited to 'linux')
-rw-r--r-- | linux/LinuxProcessList.c | 23 |
1 files changed, 11 insertions, 12 deletions
diff --git a/linux/LinuxProcessList.c b/linux/LinuxProcessList.c index 969c4e73..45161290 100644 --- a/linux/LinuxProcessList.c +++ b/linux/LinuxProcessList.c @@ -226,26 +226,25 @@ ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidMatchList, ui // Test /proc/PID/smaps_rollup availability (faster to parse, Linux 4.14+) 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"); - if (statfile == NULL) { + if (statfile == NULL) CRT_fatalError("Cannot open " PROCSTATFILE); - } - while (true) { char buffer[PROC_LINE_LENGTH + 1]; - if (fgets(buffer, sizeof(buffer), statfile) == NULL) { - CRT_fatalError("No btime in " PROCSTATFILE); - } else if (String_startsWith(buffer, "btime ")) { - if (sscanf(buffer, "btime %lld\n", &btime) != 1) { - CRT_fatalError("Failed to parse btime from " PROCSTATFILE); - } + if (fgets(buffer, sizeof(buffer), statfile) == NULL) break; - } + if (String_startsWith(buffer, "btime ") == false) + continue; + if (sscanf(buffer, "btime %lld\n", &btime) == 1) + break; + CRT_fatalError("Failed to parse btime from " PROCSTATFILE); } - fclose(statfile); + + if (!btime) + CRT_fatalError("No btime in " PROCSTATFILE); } // Initialize CPU count |