mirror of
https://github.com/aristocratos/btop.git
synced 2024-09-27 22:01:29 +02:00
Fixed leaks and proc tree mode
This commit is contained in:
parent
cd3fd1a529
commit
735894b6ca
@ -948,6 +948,17 @@ namespace Proc {
|
||||
// }
|
||||
}
|
||||
|
||||
//* RAII wrapper for kvm_openfiles
|
||||
class kvm_openfiles_wrapper {
|
||||
kvm_t* kd = NULL;
|
||||
public:
|
||||
kvm_openfiles_wrapper(const char* execf, const char* coref, const char* swapf, int flags, char* err) {
|
||||
this->kd = kvm_openfiles(execf, coref, swapf, flags, err);
|
||||
}
|
||||
~kvm_openfiles_wrapper() { kvm_close(kd); }
|
||||
auto operator()() -> kvm_t* { return kd; }
|
||||
};
|
||||
|
||||
//* Collects and sorts process information from /proc
|
||||
auto collect(const bool no_update) -> vector<proc_info> & {
|
||||
const auto &sorting = Config::getS("proc_sorting");
|
||||
@ -982,12 +993,13 @@ namespace Proc {
|
||||
|
||||
int count = 0;
|
||||
char buf[_POSIX2_LINE_MAX];
|
||||
kvm_t *kd = kvm_openfiles(NULL, _PATH_DEVNULL, NULL, O_RDONLY, buf);
|
||||
const struct kinfo_proc* kprocs = kvm_getprocs(kd, KERN_PROC_PROC, 0, &count);
|
||||
kvm_openfiles_wrapper kd(NULL, _PATH_DEVNULL, NULL, O_RDONLY, buf);
|
||||
const struct kinfo_proc* kprocs = kvm_getprocs(kd(), KERN_PROC_PROC, 0, &count);
|
||||
|
||||
for (int i = 0; i < count; i++) {
|
||||
const struct kinfo_proc* kproc = &kprocs[i];
|
||||
const size_t pid = (size_t)kproc->ki_pid;
|
||||
if (pid < 1) continue;
|
||||
found.push_back(pid);
|
||||
|
||||
//? Check if pid already exists in current_procs
|
||||
@ -1003,13 +1015,17 @@ namespace Proc {
|
||||
|
||||
//? Get program name, command, username, parent pid, nice and status
|
||||
if (no_cache) {
|
||||
if (kproc->ki_comm == NULL or kproc->ki_comm == "idle"s) {
|
||||
current_procs.pop_back();
|
||||
continue;
|
||||
}
|
||||
new_proc.name = kproc->ki_comm;
|
||||
char** argv = kvm_getargv(kd, kproc, 0);
|
||||
char** argv = kvm_getargv(kd(), kproc, 0);
|
||||
if (argv) {
|
||||
for (int i = 0; argv[i]; i++) {
|
||||
new_proc.cmd += " ";
|
||||
new_proc.cmd += argv[i];
|
||||
new_proc.cmd += argv[i] + " "s;
|
||||
}
|
||||
if (not new_proc.cmd.empty()) new_proc.cmd.pop_back();
|
||||
}
|
||||
if (new_proc.cmd.empty()) new_proc.cmd = new_proc.name;
|
||||
new_proc.ppid = kproc->ki_ppid;
|
||||
|
Loading…
Reference in New Issue
Block a user