summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJos Dehaes <jos.dehaes@gmail.com>2021-11-27 20:30:43 +0100
committerJos Dehaes <jos.dehaes@gmail.com>2021-11-27 20:30:43 +0100
commit860a9fe472c7f4b6cc3f2ef38fb06101f5af7e21 (patch)
tree1af436646e7a2772ac083758c0bd6b8c88e8b0ed
parent7c433be4a6b6b0cac84713e976a63d661a9338ae (diff)
RAII
-rw-r--r--src/freebsd/btop_collect.cpp18
1 files changed, 13 insertions, 5 deletions
diff --git a/src/freebsd/btop_collect.cpp b/src/freebsd/btop_collect.cpp
index d80aeae..f03c621 100644
--- a/src/freebsd/btop_collect.cpp
+++ b/src/freebsd/btop_collect.cpp
@@ -496,6 +496,15 @@ namespace Mem {
while (cmp_greater(disk.io_activity.size(), width * 2)) disk.io_activity.pop_front();
}
+ class PipeWrapper {
+ public:
+ PipeWrapper(const char *file, const char *mode) {fd = popen(file, mode);}
+ virtual ~PipeWrapper() {std::fclose(fd);}
+ auto operator()() -> FILE* { return fd;};
+ private:
+ FILE *fd;
+ };
+
void collect_disk(unordered_flat_map<string, disk_info> &disks, unordered_flat_map<string, string> &mapping) {
// this bit is for 'regular' mounts
static struct statinfo cur, last;
@@ -526,14 +535,14 @@ namespace Mem {
}
// this code is for ZFS mounts
- FILE *f = popen("sysctl kstat.zfs.zroot.dataset", "r");
- if (f) {
+ PipeWrapper f = PipeWrapper("sysctl kstat.zfs.zroot.dataset", "r");
+ if (f()) {
size_t len = 512;
char buf[512];
- while (not std::feof(f)) {
+ while (not std::feof(f())) {
uint64_t nread, nwritten;
string datasetname; // this is the zfs volume, like 'zroot/usr/home' -> this maps onto the device we get back from getmntinfo(3)
- if (fgets(buf, len, f)) {
+ if (fgets(buf, len, f())) {
char *name = std::strtok(buf, ": \n");
char *value = std::strtok(NULL, ": \n");
if (string(name).find("dataset_name") != string::npos) {
@@ -556,7 +565,6 @@ namespace Mem {
}
}
}
- std::fclose(f);
}
}