From: Karel Zak Date: Wed, 7 Dec 2022 10:43:20 +0000 (+0100) Subject: blkdiscard: add extra exit code when device does not support discard X-Git-Tag: v2.39-rc1~383 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=4cf6f545ee28fe468efcb1000704055207cfabcd;p=thirdparty%2Futil-linux.git blkdiscard: add extra exit code when device does not support discard Fixes: https://github.com/util-linux/util-linux/issues/1941 Signed-off-by: Karel Zak --- diff --git a/sys-utils/blkdiscard.8.adoc b/sys-utils/blkdiscard.8.adoc index 240693d1cd..578f300f73 100644 --- a/sys-utils/blkdiscard.8.adoc +++ b/sys-utils/blkdiscard.8.adoc @@ -54,9 +54,23 @@ Display the aligned values of _offset_ and _length_. If the *--step* option is s include::man-common/help-version.adoc[] +== EXIT STATUS + +*blkdiscard* has the following exit status values: + +*0*:: +success + +*1*:: +failure; incorrect invocation, permissions or any other generic error + +*2*:: +failure; since v2.39, the device does not support discard operation + == AUTHORS -mailto:lczerner@redhat.com[Lukas Czerner] +mailto:lczerner@redhat.com[Lukas Czerner], +mailto:kzak@redhat.com[Karel Zak] == SEE ALSO diff --git a/sys-utils/blkdiscard.c b/sys-utils/blkdiscard.c index e90fdfdb35..68e6f107f9 100644 --- a/sys-utils/blkdiscard.c +++ b/sys-utils/blkdiscard.c @@ -48,6 +48,9 @@ #include "closestream.h" #include "monotonic.h" +/* exit() status if discard unsupported by device */ +#define BLKDISCARD_EXIT_NOTSUPP (EXIT_FAILURE + 1) + #ifndef BLKDISCARD # define BLKDISCARD _IO(0x12,119) #endif @@ -152,6 +155,15 @@ out: } #endif /* HAVE_LIBBLKID */ +static void __attribute__((__noreturn__)) err_on_ioctl( + const char *ioctlname, const char *path) +{ + int exno = errno == EOPNOTSUPP ? + BLKDISCARD_EXIT_NOTSUPP : EXIT_FAILURE; + + err(exno, _("%s: %s ioctl failed"), ioctlname, path); +} + int main(int argc, char **argv) { char *path; @@ -299,18 +311,20 @@ int main(int argc, char **argv) if (range[0] + range[1] > end) range[1] = end - range[0]; + errno = 0; + switch (act) { case ACT_ZEROOUT: if (ioctl(fd, BLKZEROOUT, &range)) - err(EXIT_FAILURE, _("%s: BLKZEROOUT ioctl failed"), path); + err_on_ioctl("BLKZEROOUT", path); break; case ACT_SECURE: if (ioctl(fd, BLKSECDISCARD, &range)) - err(EXIT_FAILURE, _("%s: BLKSECDISCARD ioctl failed"), path); + err_on_ioctl("BLKSECDISCARD", path); break; case ACT_DISCARD: if (ioctl(fd, BLKDISCARD, &range)) - err(EXIT_FAILURE, _("%s: BLKDISCARD ioctl failed"), path); + err_on_ioctl("BLKDISCARD", path); break; }