From d17c6d475ac554afcf9525cf0cb4c4c86c713b10 Mon Sep 17 00:00:00 2001 From: aristocratos Date: Thu, 20 May 2021 09:36:36 +0200 Subject: [PATCH] Switch /proc/pid/stat to string parsing to avoid bad charaters in comm field --- src/btop_linux.h | 29 ++++++++++------------------- src/btop_tools.h | 1 + 2 files changed, 11 insertions(+), 19 deletions(-) diff --git a/src/btop_linux.h b/src/btop_linux.h index 3dcbb51..eac3266 100644 --- a/src/btop_linux.h +++ b/src/btop_linux.h @@ -130,7 +130,6 @@ namespace Proc { vector c_pids; c_pids.reserve((numpids + 10)); numpids = 0; - uint parenthesis = 0; //* Update uid_user map if /etc/passwd changed since last run if (!passwd_path.empty() && fs::last_write_time(passwd_path) != passwd_time) { @@ -209,46 +208,38 @@ namespace Proc { //* Get cpu usage, cpu cumulative and threads from /proc/[pid]/stat if (fs::exists((string)d.path() + "/stat")) { - pread.clear(); instr.clear(); pstat.clear(); pstat.reserve(40); pstat.resize(3, ""); + pread.clear(); instr.clear(); pstat.clear(); ifstream pread((string)d.path() + "/stat"); - if (pread.good()) { - parenthesis = 1; - pread.ignore(numeric_limits::max(), '('); - while (parenthesis > 0 && !pread.eof()) { - instr = pread.get(); - if (instr == "(") ++parenthesis; - else if (instr == ")") --parenthesis; - } - pread.ignore(1); - while (getline(pread, instr, ' ') && pstat.size() < 40) pstat.push_back(instr); + if (pread.good() && getline(pread, instr)) { + pstat = ssplit(instr.substr(instr.rfind(')') + 1), " ", 38); } pread.close(); if (pstat.size() < 22) continue; //? Process state - state = pstat[3][0]; + state = pstat[0][0]; //? Process parent pid - ppid = stoul(pstat[4]); + ppid = stoul(pstat[1]); //? Process nice value - p_nice = stoi(pstat[19]); + p_nice = stoi(pstat[16]); //? Process number of threads - threads = stoul(pstat[20]); + threads = stoul(pstat[17]); //? Process utime + stime - cpu_t = stoull(pstat[14]) + stoull(pstat[15]); + cpu_t = stoull(pstat[11]) + stoull(pstat[12]); //? Cache cpu times and cpu seconds if (new_cache) { cache[pid].cpu_t = cpu_t; - cache[pid].cpu_s = stoull(pstat[22]); + cache[pid].cpu_s = stoull(pstat[19]); } //? CPU number last executed on - if (pstat.size() > 39) cpu_n = stoi(pstat[39]); + if (pstat.size() > 39) cpu_n = stoi(pstat[36]); //? Process cpu usage since last update, 100'000 because (100 percent * 1000 milliseconds) for correct conversion diff --git a/src/btop_tools.h b/src/btop_tools.h index aad41ba..06762ce 100644 --- a/src/btop_tools.h +++ b/src/btop_tools.h @@ -262,6 +262,7 @@ namespace Tools { //* Split at