From 34b056e3a322352df7ecc8a2ae89a340f233237a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Joakim=20Nohlg=C3=A5rd?= <joakim@nohlgard.se>
Date: Fri, 29 Dec 2023 15:06:50 +0100
Subject: [PATCH 0102/1160] udevadm: Propagate return code from verb result

udevadm lock did not propagate the return code from the child process
because all positive values were treated as success.

v2:
Now 'udevadm test-builtin' ignores all positive return values from the
builtin commands. Otherwise, as the hwdb builtin returns an positive value
when a matching entry found, 'udevadm test-builtin hwdb' will fail.

v3:
Initialize partition table before calling 'sfdisk --delete'.

Co-authored-by: Yu Watanabe <watanabe.yu+github@gmail.com>
(cherry picked from commit ba340e2a75a0a16031fcb7efa05cfd250e859f17)
---
 src/udev/udevadm-test-builtin.c | 5 ++++-
 src/udev/udevadm.c              | 2 +-
 test/units/testsuite-64.sh      | 5 +++++
 3 files changed, 10 insertions(+), 2 deletions(-)

diff --git a/src/udev/udevadm-test-builtin.c b/src/udev/udevadm-test-builtin.c
index 5d1fafbd03..f5498a1e5b 100644
--- a/src/udev/udevadm-test-builtin.c
+++ b/src/udev/udevadm-test-builtin.c
@@ -104,9 +104,12 @@ int builtin_main(int argc, char *argv[], void *userdata) {
         }
 
         r = udev_builtin_run(event, cmd, arg_command, true);
-        if (r < 0)
+        if (r < 0) {
                 log_debug_errno(r, "Builtin command '%s' fails: %m", arg_command);
+                goto finish;
+        }
 
+        r = 0;
 finish:
         udev_builtin_exit();
         return r;
diff --git a/src/udev/udevadm.c b/src/udev/udevadm.c
index 51dc041a29..687b927f72 100644
--- a/src/udev/udevadm.c
+++ b/src/udev/udevadm.c
@@ -137,4 +137,4 @@ static int run(int argc, char *argv[]) {
         return udevadm_main(argc, argv);
 }
 
-DEFINE_MAIN_FUNCTION(run);
+DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run);
diff --git a/test/units/testsuite-64.sh b/test/units/testsuite-64.sh
index 0e598cc6b3..299c5eb183 100755
--- a/test/units/testsuite-64.sh
+++ b/test/units/testsuite-64.sh
@@ -396,6 +396,11 @@ EOF
 
     udevadm control --reload
 
+    # initialize partition table
+    for disk in {0..9}; do
+        echo 'label: gpt' | udevadm lock --device="${devices[$disk]}" sfdisk -q "${devices[$disk]}"
+    done
+
     # Delete the partitions, immediately recreate them, wait for udev to settle
     # down, and then check if we have any dangling symlinks in /dev/disk/. Rinse
     # and repeat.
-- 
2.33.0