summaryrefslogtreecommitdiffstats
path: root/sys-utils/blkdiscard.c
diff options
context:
space:
mode:
authorKarel Zak2013-08-06 10:29:04 +0200
committerKarel Zak2013-08-06 10:29:04 +0200
commit777519226d033fdea9bcad043c01aa6539c8b71d (patch)
tree2fb7de949759a93629c1a8e109efd29de306918f /sys-utils/blkdiscard.c
parentlosetup: fix loop device name usage (diff)
downloadkernel-qcow2-util-linux-777519226d033fdea9bcad043c01aa6539c8b71d.tar.gz
kernel-qcow2-util-linux-777519226d033fdea9bcad043c01aa6539c8b71d.tar.xz
kernel-qcow2-util-linux-777519226d033fdea9bcad043c01aa6539c8b71d.zip
blkdiscard: use fstat() rather than stat() [coverity scan]
Calling function "open(char const *, int, ...)" that uses "path" after a check function. This can cause a time-of-check, time-of-use race condition. .. well, in blkdiscard context it's mostly cosmetic change. Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'sys-utils/blkdiscard.c')
-rw-r--r--sys-utils/blkdiscard.c11
1 files changed, 5 insertions, 6 deletions
diff --git a/sys-utils/blkdiscard.c b/sys-utils/blkdiscard.c
index 0e15c01b0..af9ed6630 100644
--- a/sys-utils/blkdiscard.c
+++ b/sys-utils/blkdiscard.c
@@ -130,18 +130,17 @@ int main(int argc, char **argv)
usage(stderr);
}
- if (stat(path, &sb) == -1)
- err(EXIT_FAILURE, _("stat failed %s"), path);
- if (!S_ISBLK(sb.st_mode))
- errx(EXIT_FAILURE, _("%s: not a block device"), path);
-
fd = open(path, O_WRONLY);
if (fd < 0)
err(EXIT_FAILURE, _("cannot open %s"), path);
+ if (fstat(fd, &sb) == -1)
+ err(EXIT_FAILURE, _("stat failed %s"), path);
+ if (!S_ISBLK(sb.st_mode))
+ errx(EXIT_FAILURE, _("%s: not a block device"), path);
+
if (ioctl(fd, BLKGETSIZE64, &blksize))
err(EXIT_FAILURE, _("%s: BLKGETSIZE64 ioctl failed"), path);
-
if (ioctl(fd, BLKSSZGET, &secsize))
err(EXIT_FAILURE, _("%s: BLKSSZGET ioctl failed"), path);