Skip to content

Commit 0f224bb

Browse files
authored
Merge pull request #756 from imwints/unix-duplicates
2 parents e813767 + 65f3ade commit 0f224bb

File tree

4 files changed

+50
-77
lines changed

4 files changed

+50
-77
lines changed

src/btop_shared.hpp

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,19 @@ tab-size = 4
2525
#include <string>
2626
#include <tuple>
2727
#include <vector>
28-
#include <ifaddrs.h>
2928
#include <unordered_map>
3029
#include <unistd.h>
3130

31+
// From `man 3 getifaddrs`: <net/if.h> must be included before <ifaddrs.h>
32+
// clang-format off
33+
#include <net/if.h>
34+
#include <ifaddrs.h>
35+
// clang-format on
36+
37+
#if defined(__FreeBSD__) || defined(__OpenBSD__)
38+
# include <kvm.h>
39+
#endif
40+
3241
using std::array;
3342
using std::atomic;
3443
using std::deque;
@@ -83,6 +92,15 @@ namespace Shared {
8392
void init();
8493

8594
extern long coreCount, page_size, clk_tck;
95+
96+
#if defined(__FreeBSD__) || defined(__OpenBSD__)
97+
struct KvmDeleter {
98+
void operator()(kvm_t* handle) {
99+
kvm_close(handle);
100+
}
101+
};
102+
using KvmPtr = std::unique_ptr<kvm_t, KvmDeleter>;
103+
#endif
86104
}
87105

88106

@@ -289,6 +307,17 @@ namespace Net {
289307
bool connected{};
290308
};
291309

310+
class IfAddrsPtr {
311+
struct ifaddrs* ifaddr;
312+
int status;
313+
public:
314+
IfAddrsPtr() { status = getifaddrs(&ifaddr); }
315+
~IfAddrsPtr() { freeifaddrs(ifaddr); }
316+
[[nodiscard]] constexpr auto operator()() -> struct ifaddrs* { return ifaddr; }
317+
[[nodiscard]] constexpr auto get() -> struct ifaddrs* { return ifaddr; }
318+
[[nodiscard]] constexpr auto get_status() const noexcept -> int { return status; };
319+
};
320+
292321
extern std::unordered_map<string, net_info> current_net;
293322

294323
//* Collect net upload/download stats

src/freebsd/btop_collect.cpp

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -189,18 +189,6 @@ namespace Shared {
189189
Logger::debug("Init -> Mem::get_zpools()");
190190
Mem::get_zpools();
191191
}
192-
193-
//* RAII wrapper for kvm_openfiles
194-
class kvm_openfiles_wrapper {
195-
kvm_t* kd = nullptr;
196-
public:
197-
kvm_openfiles_wrapper(const char* execf, const char* coref, const char* swapf, int flags, char* err) {
198-
this->kd = kvm_openfiles(execf, coref, swapf, flags, err);
199-
}
200-
~kvm_openfiles_wrapper() { kvm_close(kd); }
201-
auto operator()() -> kvm_t* { return kd; }
202-
};
203-
204192
} // namespace Shared
205193

206194
namespace Cpu {
@@ -673,9 +661,9 @@ namespace Mem {
673661

674662
if (show_swap) {
675663
char buf[_POSIX2_LINE_MAX];
676-
Shared::kvm_openfiles_wrapper kd(nullptr, _PATH_DEVNULL, nullptr, O_RDONLY, buf);
664+
Shared::KvmPtr kd {kvm_openfiles(nullptr, _PATH_DEVNULL, nullptr, O_RDONLY, buf)};
677665
struct kvm_swap swap[16];
678-
int nswap = kvm_getswapinfo(kd(), swap, 16, 0);
666+
int nswap = kvm_getswapinfo(kd.get(), swap, 16, 0);
679667
int totalSwap = 0, usedSwap = 0;
680668
for (int i = 0; i < nswap; i++) {
681669
totalSwap += swap[i].ksw_total;
@@ -828,17 +816,6 @@ namespace Net {
828816
bool rescale = true;
829817
uint64_t timestamp = 0;
830818

831-
//* RAII wrapper for getifaddrs
832-
class getifaddr_wrapper {
833-
struct ifaddrs *ifaddr;
834-
835-
public:
836-
int status;
837-
getifaddr_wrapper() { status = getifaddrs(&ifaddr); }
838-
~getifaddr_wrapper() { freeifaddrs(ifaddr); }
839-
auto operator()() -> struct ifaddrs * { return ifaddr; }
840-
};
841-
842819
auto collect(bool no_update) -> net_info & {
843820
auto &net = current_net;
844821
auto &config_iface = Config::getS("net_iface");
@@ -848,10 +825,10 @@ namespace Net {
848825

849826
if (not no_update and errors < 3) {
850827
//? Get interface list using getifaddrs() wrapper
851-
getifaddr_wrapper if_wrap{};
852-
if (if_wrap.status != 0) {
828+
IfAddrsPtr if_addrs {};
829+
if (if_addrs.get_status() != 0) {
853830
errors++;
854-
Logger::error("Net::collect() -> getifaddrs() failed with id " + to_string(if_wrap.status));
831+
Logger::error("Net::collect() -> getifaddrs() failed with id " + to_string(if_addrs.get_status()));
855832
redraw = true;
856833
return empty_net;
857834
}
@@ -863,7 +840,7 @@ namespace Net {
863840
string ipv4, ipv6;
864841

865842
//? Iteration over all items in getifaddrs() list
866-
for (auto *ifa = if_wrap(); ifa != nullptr; ifa = ifa->ifa_next) {
843+
for (auto *ifa = if_addrs.get(); ifa != nullptr; ifa = ifa->ifa_next) {
867844
if (ifa->ifa_addr == nullptr) continue;
868845
family = ifa->ifa_addr->sa_family;
869846
const auto &iface = ifa->ifa_name;
@@ -1169,8 +1146,8 @@ namespace Proc {
11691146

11701147
int count = 0;
11711148
char buf[_POSIX2_LINE_MAX];
1172-
Shared::kvm_openfiles_wrapper kd(nullptr, _PATH_DEVNULL, nullptr, O_RDONLY, buf);
1173-
const struct kinfo_proc* kprocs = kvm_getprocs(kd(), KERN_PROC_PROC, 0, &count);
1149+
Shared::KvmPtr kd {kvm_openfiles(nullptr, _PATH_DEVNULL, nullptr, O_RDONLY, buf)};
1150+
const struct kinfo_proc* kprocs = kvm_getprocs(kd.get(), KERN_PROC_PROC, 0, &count);
11741151

11751152
for (int i = 0; i < count; i++) {
11761153
const struct kinfo_proc* kproc = &kprocs[i];
@@ -1197,7 +1174,7 @@ namespace Proc {
11971174
continue;
11981175
}
11991176
new_proc.name = kproc->ki_comm;
1200-
char** argv = kvm_getargv(kd(), kproc, 0);
1177+
char** argv = kvm_getargv(kd.get(), kproc, 0);
12011178
if (argv) {
12021179
for (int i = 0; argv[i] and cmp_less(new_proc.cmd.size(), 1000); i++) {
12031180
new_proc.cmd += argv[i] + " "s;

src/linux/btop_collect.cpp

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2197,16 +2197,6 @@ namespace Net {
21972197
bool rescale{true};
21982198
uint64_t timestamp{};
21992199

2200-
//* RAII wrapper for getifaddrs
2201-
class getifaddr_wrapper {
2202-
struct ifaddrs* ifaddr;
2203-
public:
2204-
int status;
2205-
getifaddr_wrapper() { status = getifaddrs(&ifaddr); }
2206-
~getifaddr_wrapper() { freeifaddrs(ifaddr); }
2207-
auto operator()() -> struct ifaddrs* { return ifaddr; }
2208-
};
2209-
22102200
auto collect(bool no_update) -> net_info& {
22112201
if (Runner::stopping) return empty_net;
22122202
auto& net = current_net;
@@ -2217,10 +2207,10 @@ namespace Net {
22172207

22182208
if (not no_update and errors < 3) {
22192209
//? Get interface list using getifaddrs() wrapper
2220-
getifaddr_wrapper if_wrap {};
2221-
if (if_wrap.status != 0) {
2210+
IfAddrsPtr if_addrs {};
2211+
if (if_addrs.get_status() != 0) {
22222212
errors++;
2223-
Logger::error("Net::collect() -> getifaddrs() failed with id " + to_string(if_wrap.status));
2213+
Logger::error("Net::collect() -> getifaddrs() failed with id " + to_string(if_addrs.get_status()));
22242214
redraw = true;
22252215
return empty_net;
22262216
}
@@ -2232,7 +2222,7 @@ namespace Net {
22322222
string ipv4, ipv6;
22332223

22342224
//? Iteration over all items in getifaddrs() list
2235-
for (auto* ifa = if_wrap(); ifa != nullptr; ifa = ifa->ifa_next) {
2225+
for (auto* ifa = if_addrs.get(); ifa != nullptr; ifa = ifa->ifa_next) {
22362226
if (ifa->ifa_addr == nullptr) continue;
22372227
family = ifa->ifa_addr->sa_family;
22382228
const auto& iface = ifa->ifa_name;

src/openbsd/btop_collect.cpp

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -184,18 +184,6 @@ namespace Shared {
184184
Mem::old_uptime = system_uptime();
185185
Mem::collect();
186186
}
187-
188-
//* RAII wrapper for kvm_openfiles
189-
class kvm_openfiles_wrapper {
190-
kvm_t* kd = nullptr;
191-
public:
192-
kvm_openfiles_wrapper(const char* execf, const char* coref, const char* swapf, int flags, char* err) {
193-
this->kd = kvm_openfiles(execf, coref, swapf, flags, err);
194-
}
195-
~kvm_openfiles_wrapper() { kvm_close(kd); }
196-
auto operator()() -> kvm_t* { return kd; }
197-
};
198-
199187
} // namespace Shared
200188

201189
namespace Cpu {
@@ -780,17 +768,6 @@ namespace Net {
780768
bool rescale = true;
781769
uint64_t timestamp = 0;
782770

783-
//* RAII wrapper for getifaddrs
784-
class getifaddr_wrapper {
785-
struct ifaddrs *ifaddr;
786-
787-
public:
788-
int status;
789-
getifaddr_wrapper() { status = getifaddrs(&ifaddr); }
790-
~getifaddr_wrapper() { freeifaddrs(ifaddr); }
791-
auto operator()() -> struct ifaddrs * { return ifaddr; }
792-
};
793-
794771
auto collect(bool no_update) -> net_info & {
795772
auto &net = current_net;
796773
auto &config_iface = Config::getS("net_iface");
@@ -800,10 +777,10 @@ namespace Net {
800777

801778
if (not no_update and errors < 3) {
802779
//? Get interface list using getifaddrs() wrapper
803-
getifaddr_wrapper if_wrap{};
804-
if (if_wrap.status != 0) {
780+
IfAddrsPtr if_addrs {};
781+
if (if_addrs.get_status() != 0) {
805782
errors++;
806-
Logger::error("Net::collect() -> getifaddrs() failed with id " + to_string(if_wrap.status));
783+
Logger::error("Net::collect() -> getifaddrs() failed with id " + to_string(if_addrs.get_status()));
807784
redraw = true;
808785
return empty_net;
809786
}
@@ -815,7 +792,7 @@ namespace Net {
815792
string ipv4, ipv6;
816793

817794
//? Iteration over all items in getifaddrs() list
818-
for (auto *ifa = if_wrap(); ifa != nullptr; ifa = ifa->ifa_next) {
795+
for (auto *ifa = if_addrs.get(); ifa != nullptr; ifa = ifa->ifa_next) {
819796
if (ifa->ifa_addr == nullptr) continue;
820797
family = ifa->ifa_addr->sa_family;
821798
const auto &iface = ifa->ifa_name;
@@ -1102,8 +1079,8 @@ namespace Proc {
11021079

11031080
int count = 0;
11041081
char buf[_POSIX2_LINE_MAX];
1105-
Shared::kvm_openfiles_wrapper kd(nullptr, nullptr, nullptr, KVM_NO_FILES, buf);
1106-
const struct kinfo_proc* kprocs = kvm_getprocs(kd(), KERN_PROC_ALL, 0, sizeof(struct kinfo_proc), &count);
1082+
Shared::KvmPtr kd {kvm_openfiles(nullptr, nullptr, nullptr, KVM_NO_FILES, buf)};
1083+
const struct kinfo_proc* kprocs = kvm_getprocs(kd.get() , KERN_PROC_ALL, 0, sizeof(struct kinfo_proc), &count);
11071084

11081085
for (int i = 0; i < count; i++) {
11091086
const struct kinfo_proc* kproc = &kprocs[i];
@@ -1130,7 +1107,7 @@ namespace Proc {
11301107
continue;
11311108
}
11321109
new_proc.name = kproc->p_comm;
1133-
char** argv = kvm_getargv(kd(), kproc, 0);
1110+
char** argv = kvm_getargv(kd.get(), kproc, 0);
11341111
if (argv) {
11351112
for (int i = 0; argv[i] and cmp_less(new_proc.cmd.size(), 1000); i++) {
11361113
new_proc.cmd += argv[i] + " "s;

0 commit comments

Comments
 (0)