[PATCH procd 1/1] system: add diskfree infos to ubus

Florian Eckert fe at dev.tdt.de
Wed Nov 17 02:41:17 PST 2021


This change adds the missing information about how much space is available
on the root directory and in the temp directory. I took this
implementation from the luci2 repository and adapted it for the procd
service.

Signed-off-by: Florian Eckert <fe at dev.tdt.de>
---
 system.c | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)

diff --git a/system.c b/system.c
index 83aea42..a671f5d 100644
--- a/system.c
+++ b/system.c
@@ -20,6 +20,7 @@
 #include <sys/types.h>
 #include <sys/reboot.h>
 #include <sys/stat.h>
+#include <sys/statvfs.h>
 #include <fcntl.h>
 #include <signal.h>
 #include <unistd.h>
@@ -239,6 +240,12 @@ static int system_board(struct ubus_context *ctx, struct ubus_object *obj,
 	return UBUS_STATUS_OK;
 }
 
+static unsigned long
+kscale(unsigned long b, unsigned long bs)
+{
+	return (b * (unsigned long long) bs + 1024/2) / 1024;
+}
+
 static int system_info(struct ubus_context *ctx, struct ubus_object *obj,
                 struct ubus_request_data *req, const char *method,
                 struct blob_attr *msg)
@@ -252,6 +259,12 @@ static int system_info(struct ubus_context *ctx, struct ubus_object *obj,
 	char *key, *val;
 	unsigned long long available, cached;
 	FILE *f;
+	int i;
+	struct statvfs s;
+	const char *fslist[] = {
+		"/",    "root",
+		"/tmp", "tmp",
+	};
 
 	if (sysinfo(&info))
 		return UBUS_STATUS_UNKNOWN_ERROR;
@@ -311,6 +324,23 @@ static int system_info(struct ubus_context *ctx, struct ubus_object *obj,
 	blobmsg_add_u64(&b, "cached", cached);
 	blobmsg_close_table(&b, c);
 
+	for (i = 0; i < sizeof(fslist) / sizeof(fslist[0]); i += 2) {
+		if (statvfs(fslist[i], &s))
+			continue;
+
+		c = blobmsg_open_table(&b, fslist[i+1]);
+
+		if (!s.f_frsize)
+			s.f_frsize = s.f_bsize;
+
+		blobmsg_add_u64(&b, "total", kscale(s.f_blocks, s.f_frsize));
+		blobmsg_add_u64(&b, "free",  kscale(s.f_bfree, s.f_frsize));
+		blobmsg_add_u64(&b, "used", kscale(s.f_blocks - s.f_bfree, s.f_frsize));
+		blobmsg_add_u64(&b, "avail", kscale(s.f_bavail, s.f_frsize));
+
+		blobmsg_close_table(&b, c);
+	}
+
 	c = blobmsg_open_table(&b, "swap");
 	blobmsg_add_u64(&b, "total",
 			(uint64_t)info.mem_unit * (uint64_t)info.totalswap);
-- 
2.20.1




More information about the openwrt-devel mailing list