[dev] [ubase][PATCH] Fix df hanging when statvfs() fails.

From: Risto Salminen <ripejcp_AT_gmail.com>
Date: Mon, 26 Jan 2015 20:53:14 +0200

Now df prints out an appropriate error message when statvfs() fails
instead of just hanging. Also make df return 1 when statvfs() fails.
---
 df.c | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)
diff --git a/df.c b/df.c
index f14b56b..f3a3049 100644
--- a/df.c
+++ b/df.c
_AT_@ -13,7 +13,7 @@ static int aflag = 0;
 static int hflag = 0;
 static int kflag = 0;
 
-static void mnt_show(const char *fsname, const char *dir);
+static int mnt_show(const char *fsname, const char *dir);
 
 static void
 usage(void)
_AT_@ -26,6 +26,7 @@ main(int argc, char *argv[])
 {
 	struct mntent *me = NULL;
 	FILE *fp;
+	int ret = 0;
 
 	ARGBEGIN {
 	case 'a':
_AT_@ -61,11 +62,12 @@ main(int argc, char *argv[])
 		if (aflag == 0)
 			if (strcmp(me->mnt_type, "rootfs") == 0)
 				continue;
-		mnt_show(me->mnt_fsname, me->mnt_dir);
+		if (mnt_show(me->mnt_fsname, me->mnt_dir) < 0)
+			ret = 1;
 	}
 	endmntent(fp);
 
-	return 0;
+	return ret;
 }
 
 #define CALC_POWER(n, power, base, i) do { \
_AT_@ -107,7 +109,7 @@ print_human(
 	       avail, postfixes[k], capacity, dir);
 }
 
-static void
+static int
 mnt_show(const char *fsname, const char *dir)
 {
 	struct statvfs s;
_AT_@ -115,7 +117,10 @@ mnt_show(const char *fsname, const char *dir)
 	int capacity = 0;
 	int bs;
 
-	statvfs(dir, &s);
+	if (statvfs(dir, &s) < 0) {
+		weprintf("statvfs %s:", dir);
+		return -1;
+	}
 
 	bs = s.f_frsize / blksize;
 	total = s.f_blocks * bs;
_AT_@ -133,4 +138,6 @@ mnt_show(const char *fsname, const char *dir)
 	else
 		printf("%-12s %9llu %9llu %9llu %7d%%  %s\n",
 		       fsname, total, used, avail, capacity, dir);
+
+	return 0;
 }
-- 
2.2.1
Received on Mon Jan 26 2015 - 19:53:14 CET

This archive was generated by hypermail 2.3.0 : Mon Jan 26 2015 - 20:00:10 CET