Skip to content

Commit

Permalink
Use kvm and ifaddr defs from shared.hpp.
Browse files Browse the repository at this point in the history
  • Loading branch information
fraggerfox committed Mar 23, 2024
1 parent 61c5aa1 commit eac3086
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 32 deletions.
4 changes: 2 additions & 2 deletions src/btop_shared.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ tab-size = 4
#include <ifaddrs.h>
// clang-format on

#if defined(__FreeBSD__) || defined(__OpenBSD__)
#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
# include <kvm.h>
#endif

Expand Down Expand Up @@ -93,7 +93,7 @@ namespace Shared {

extern long coreCount, page_size, clk_tck;

#if defined(__FreeBSD__) || defined(__OpenBSD__)
#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
struct KvmDeleter {
void operator()(kvm_t* handle) {
kvm_close(handle);
Expand Down
37 changes: 7 additions & 30 deletions src/netbsd/btop_collect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,18 +183,6 @@ namespace Shared {
Mem::old_uptime = system_uptime();
Mem::collect();
}

//* RAII wrapper for kvm_openfiles
class kvm_openfiles_wrapper {
kvm_t* kd = nullptr;
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; }
};

} // namespace Shared

namespace Cpu {
Expand Down Expand Up @@ -922,17 +910,6 @@ namespace Net {
bool rescale = true;
uint64_t timestamp = 0;

//* RAII wrapper for getifaddrs
class getifaddr_wrapper {
struct ifaddrs *ifaddr;

public:
int status;
getifaddr_wrapper() { status = getifaddrs(&ifaddr); }
~getifaddr_wrapper() { freeifaddrs(ifaddr); }
auto operator()() -> struct ifaddrs * { return ifaddr; }
};

auto collect(bool no_update) -> net_info & {
auto &net = current_net;
auto &config_iface = Config::getS("net_iface");
Expand All @@ -942,10 +919,10 @@ namespace Net {

if (not no_update and errors < 3) {
//? Get interface list using getifaddrs() wrapper
getifaddr_wrapper if_wrap{};
if (if_wrap.status != 0) {
IfAddrsPtr if_addrs {};
if (if_addrs.get_status() != 0) {
errors++;
Logger::error("Net::collect() -> getifaddrs() failed with id " + to_string(if_wrap.status));
Logger::error("Net::collect() -> getifaddrs() failed with id " + to_string(if_addrs.get_status()));
redraw = true;
return empty_net;
}
Expand All @@ -957,7 +934,7 @@ namespace Net {
string ipv4, ipv6;

//? Iteration over all items in getifaddrs() list
for (auto *ifa = if_wrap(); ifa != nullptr; ifa = ifa->ifa_next) {
for (auto *ifa = if_addrs.get(); ifa != nullptr; ifa = ifa->ifa_next) {
if (ifa->ifa_addr == nullptr) continue;
family = ifa->ifa_addr->sa_family;
const auto &iface = ifa->ifa_name;
Expand Down Expand Up @@ -1244,8 +1221,8 @@ namespace Proc {

int count = 0;
char buf[_POSIX2_LINE_MAX];
Shared::kvm_openfiles_wrapper kd(nullptr, nullptr, nullptr, KVM_NO_FILES, buf);
const struct kinfo_proc2* kprocs = kvm_getproc2(kd(), KERN_PROC_ALL, 0, sizeof(struct kinfo_proc2), &count);
Shared::KvmPtr kd {kvm_openfiles(nullptr, nullptr, nullptr, KVM_NO_FILES, buf)};
const struct kinfo_proc2* kprocs = kvm_getproc2(kd.get(), KERN_PROC_ALL, 0, sizeof(struct kinfo_proc2), &count);

for (int i = 0; i < count; i++) {
const struct kinfo_proc2* kproc = &kprocs[i];
Expand All @@ -1272,7 +1249,7 @@ namespace Proc {
continue;
}
new_proc.name = kproc->p_comm;
char** argv = kvm_getargv2(kd(), kproc, 0);
char** argv = kvm_getargv2(kd.get(), kproc, 0);
if (argv) {
for (int i = 0; argv[i] and cmp_less(new_proc.cmd.size(), 1000); i++) {
new_proc.cmd += argv[i] + " "s;
Expand Down

0 comments on commit eac3086

Please sign in to comment.