summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJames Sanford2017-10-14 09:57:20 +0200
committerKarel Zak2017-10-16 11:04:03 +0200
commite1b1c7b082803446ceb6fbc860d37c916c45c321 (patch)
treead1fe4b12b43f5f6af9b03a19ac46433ca51bb27
parentzramctl: remove unused ZRAM_EMPTY and blank line (diff)
downloadkernel-qcow2-util-linux-e1b1c7b082803446ceb6fbc860d37c916c45c321.tar.gz
kernel-qcow2-util-linux-e1b1c7b082803446ceb6fbc860d37c916c45c321.tar.xz
kernel-qcow2-util-linux-e1b1c7b082803446ceb6fbc860d37c916c45c321.zip
zramctl: fix show all non-zero zram devices
Addresses: https://github.com/karelzak/util-linux/issues/521 Signed-off-by: Karel Zak <kzak@redhat.com>
-rw-r--r--sys-utils/zramctl.c37
1 files changed, 24 insertions, 13 deletions
diff --git a/sys-utils/zramctl.c b/sys-utils/zramctl.c
index 425a8c341..fb64150d6 100644
--- a/sys-utils/zramctl.c
+++ b/sys-utils/zramctl.c
@@ -23,6 +23,8 @@
#include <string.h>
#include <stdarg.h>
#include <assert.h>
+#include <sys/types.h>
+#include <dirent.h>
#include <libsmartcols.h>
@@ -476,6 +478,8 @@ static void status(struct zram *z)
{
struct libscols_table *tb;
size_t i;
+ DIR *dir;
+ struct dirent *d;
scols_init_debug(0);
@@ -493,22 +497,29 @@ static void status(struct zram *z)
err(EXIT_FAILURE, _("failed to initialize output column"));
}
- if (z)
- fill_table_row(tb, z); /* just one device specified */
- else {
- /* list all used devices */
- z = new_zram(NULL);
+ if (z) {
+ /* just one device specified */
+ fill_table_row(tb, z);
+ goto print_table;
+ }
- for (i = 0; ; i++) {
- zram_set_devname(z, NULL, i);
- if (!zram_exist(z))
- break;
- if (zram_used(z))
- fill_table_row(tb, z);
- }
- free_zram(z);
+ /* list all used devices */
+ z = new_zram(NULL);
+ if (!(dir = opendir(_PATH_DEV)))
+ err(EXIT_FAILURE, _("cannot open %s"), _PATH_DEV);
+
+ while ((d = readdir(dir))) {
+ int n;
+ if (sscanf(d->d_name, "zram%d", &n) != 1)
+ continue;
+ zram_set_devname(z, NULL, n);
+ if (zram_exist(z) && zram_used(z))
+ fill_table_row(tb, z);
}
+ closedir(dir);
+ free_zram(z);
+print_table:
scols_print_table(tb);
scols_unref_table(tb);
}