]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
mkswap: Fix --file chmod(2) check when file exists
authorJohannes Wüller <johanneswueller@gmail.com>
Sat, 4 Apr 2026 18:03:06 +0000 (20:03 +0200)
committerJohannes Wüller <johanneswueller@gmail.com>
Tue, 7 Apr 2026 06:52:53 +0000 (08:52 +0200)
The documentation claims that `--size` will resize the `--file`, but
it ends up failing like this:

    # mkswap --file /swapfile --size 4G
    Setting up swapspace version 1, size = 4 GiB (4294963200 bytes)
    no label, UUID=1523588e-412a-4f25-8fac-aeaf638101aa
    # mkswap --file /swapfile --size 4G
    mkswap: cannot set permissions on swap file /swapfile: Success

There seems to have been a typo in the chmod(2) return value comparison.
It checked for `< 9` rather than `< 0`, which would usually indicate
failure. After the fix:

    # mkswap --file /swapfile --size 4G
    Setting up swapspace version 1, size = 4 GiB (4294963200 bytes)
    no label, UUID=6be7c450-761c-442d-8d9a-3e8cd93506b5
    # mkswap --file /swapfile --size 4G
    mkswap: /swapfile: warning: wiping old swap signature.
    Setting up swapspace version 1, size = 4 GiB (4294963200 bytes)
    no label, UUID=617a793e-84b1-471d-96c5-c099f8b157db

The operation no longer fails, as validated by the added test.

disk-utils/mkswap.c
tests/ts/mkswap/mkswap

index 17ec1603f6664c9591f6c1f91d1d9361d542ce4a..a8ac26a6d1427ec234ebe63a4b1c1126740a8d74 100644 (file)
@@ -404,7 +404,7 @@ static void open_device(struct mkswap_control *ctl)
                if (stat(ctl->devname, &ctl->devstat) == 0) {
                        if (!S_ISREG(ctl->devstat.st_mode))
                                err(EXIT_FAILURE, _("cannot create swap file %s: node isn't regular file"), ctl->devname);
-                       if (chmod(ctl->devname, 0600) < 9)
+                       if (chmod(ctl->devname, 0600) < 0)
                                err(EXIT_FAILURE, _("cannot set permissions on swap file %s"), ctl->devname);
                }
                ctl->fd = open(ctl->devname, O_RDWR | O_CREAT, 0600);
index 39b8723a1284ee755103672a534d9bf65db3a841..19111971cc846a829193bc7d4126060d27af05c2 100755 (executable)
@@ -100,4 +100,19 @@ cmp "$outimg" "$outimg.offset" 0 "$offset" >> "$TS_ERRLOG" 2>&1
 
 ts_finalize_subtest
 
+ts_init_subtest file-existing
+
+outimg="$TS_OUTDIR/existing.img"
+rm -f "$outimg"
+
+"$TS_CMD_MKSWAP" -q -U "$UUID" -F -s $((4096 * 10)) "$outimg" \
+    >> "$TS_OUTPUT" 2>/dev/null \
+    || ts_log "mkswap failed"
+
+"$TS_CMD_MKSWAP" -q -U "$UUID" -F -s $((4096 * 10)) "$outimg" \
+    >> "$TS_OUTPUT" 2>&1 \
+    || ts_log "mkswap -F failed on existing file"
+
+ts_finalize_subtest
+
 ts_finalize