[hackers] [ubase] Fix df hanging when statvfs() fails. || Risto Salminen

From: <git_AT_suckless.org>
Date: Mon, 26 Jan 2015 20:26:40 +0100 (CET)

commit ab4f93cf47f423da2399cbe35cbc6c109e5e1825
Author: Risto Salminen <ripejcp_AT_gmail.com>
Date: Mon Jan 26 20:53:14 2015 +0200

    Fix df hanging when statvfs() fails.
    
    Now df prints out an appropriate error message when statvfs() fails
    instead of just hanging. Also make df return 1 when statvfs() fails.

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;
 }
Received on Mon Jan 26 2015 - 20:26:40 CET

This archive was generated by hypermail 2.3.0 : Mon Jan 26 2015 - 20:36:19 CET