diff options
author | Florian Eckert <fe@dev.tdt.de> | 2021-11-17 11:41:17 +0100 |
---|---|---|
committer | Daniel Golle <daniel@makrotopia.org> | 2021-11-23 13:00:54 +0000 |
commit | 8de12dec298898d133256fdaac584a26adae2cc5 (patch) | |
tree | 5188a1f20bce1f179832e5772f8bed11b916fe4c | |
parent | 9d1431e1309e1acee06c016acc08cb7650b1d0af (diff) | |
download | procd-8de12dec298898d133256fdaac584a26adae2cc5.tar.gz |
system: add diskfree infos to ubus
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@dev.tdt.de>
-rw-r--r-- | system.c | 30 |
1 files changed, 30 insertions, 0 deletions
@@ -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> @@ -312,6 +313,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) @@ -325,6 +332,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; @@ -384,6 +397,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); |