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.
This commit is contained in:
Risto Salminen 2015-01-26 20:53:14 +02:00 committed by sin
parent f701698297
commit ab4f93cf47

17
df.c
View File

@ -13,7 +13,7 @@ static int aflag = 0;
static int hflag = 0; static int hflag = 0;
static int kflag = 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 static void
usage(void) usage(void)
@ -26,6 +26,7 @@ main(int argc, char *argv[])
{ {
struct mntent *me = NULL; struct mntent *me = NULL;
FILE *fp; FILE *fp;
int ret = 0;
ARGBEGIN { ARGBEGIN {
case 'a': case 'a':
@ -61,11 +62,12 @@ main(int argc, char *argv[])
if (aflag == 0) if (aflag == 0)
if (strcmp(me->mnt_type, "rootfs") == 0) if (strcmp(me->mnt_type, "rootfs") == 0)
continue; continue;
mnt_show(me->mnt_fsname, me->mnt_dir); if (mnt_show(me->mnt_fsname, me->mnt_dir) < 0)
ret = 1;
} }
endmntent(fp); endmntent(fp);
return 0; return ret;
} }
#define CALC_POWER(n, power, base, i) do { \ #define CALC_POWER(n, power, base, i) do { \
@ -107,7 +109,7 @@ print_human(
avail, postfixes[k], capacity, dir); avail, postfixes[k], capacity, dir);
} }
static void static int
mnt_show(const char *fsname, const char *dir) mnt_show(const char *fsname, const char *dir)
{ {
struct statvfs s; struct statvfs s;
@ -115,7 +117,10 @@ mnt_show(const char *fsname, const char *dir)
int capacity = 0; int capacity = 0;
int bs; int bs;
statvfs(dir, &s); if (statvfs(dir, &s) < 0) {
weprintf("statvfs %s:", dir);
return -1;
}
bs = s.f_frsize / blksize; bs = s.f_frsize / blksize;
total = s.f_blocks * bs; total = s.f_blocks * bs;
@ -133,4 +138,6 @@ mnt_show(const char *fsname, const char *dir)
else else
printf("%-12s %9llu %9llu %9llu %7d%% %s\n", printf("%-12s %9llu %9llu %9llu %7d%% %s\n",
fsname, total, used, avail, capacity, dir); fsname, total, used, avail, capacity, dir);
return 0;
} }