]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
blkdiscard: add extra exit code when device does not support discard
authorKarel Zak <kzak@redhat.com>
Wed, 7 Dec 2022 10:43:20 +0000 (11:43 +0100)
committerKarel Zak <kzak@redhat.com>
Wed, 7 Dec 2022 10:43:20 +0000 (11:43 +0100)
Fixes: https://github.com/util-linux/util-linux/issues/1941
Signed-off-by: Karel Zak <kzak@redhat.com>
sys-utils/blkdiscard.8.adoc
sys-utils/blkdiscard.c

index 240693d1cdf77641f1c8603b3eb73f79abd5e37e..578f300f73b9369ef2f8e214d6f267b0a012fbd0 100644 (file)
@@ -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
 
index e90fdfdb35d6cae49a4d1033d2e3be49148595a5..68e6f107f94a71a8696dc340541e4d196bf36f6e 100644 (file)
@@ -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;
                }