]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
nspawn: make failure in sd_listen_fds() critical 36788/head
authorYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 18 Mar 2025 16:38:18 +0000 (01:38 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 18 Mar 2025 17:09:40 +0000 (02:09 +0900)
Also, drop doubled call of sd_listen_fds().

src/nspawn/nspawn.c
src/shared/fdset.c

index 87cae525ca870d1a93b06a2999294e3658b0d422..3004bfc1b7d1bd48f8bbcb235c313c930e5631cc 100644 (file)
@@ -5970,7 +5970,7 @@ static int run(int argc, char *argv[]) {
         bool remove_directory = false, remove_image = false, veth_created = false;
         _cleanup_close_ int master = -EBADF, userns_fd = -EBADF, mount_fd = -EBADF;
         _cleanup_fdset_free_ FDSet *fds = NULL;
-        int r, n_fd_passed, ret = EXIT_SUCCESS;
+        int r, ret = EXIT_SUCCESS;
         char veth_name[IFNAMSIZ] = "";
         struct ExposeArgs expose_args = {};
         _cleanup_(release_lock_file) LockFile tree_global_lock = LOCK_FILE_INIT, tree_local_lock = LOCK_FILE_INIT;
@@ -6057,13 +6057,10 @@ static int run(int argc, char *argv[]) {
          * so just turning this off here means we only turn it off in nspawn itself, not any children. */
         (void) ignore_signals(SIGPIPE);
 
-        n_fd_passed = sd_listen_fds(false);
-        if (n_fd_passed > 0) {
-                r = fdset_new_listen_fds(&fds, false);
-                if (r < 0) {
-                        log_error_errno(r, "Failed to collect file descriptors: %m");
-                        goto finish;
-                }
+        r = fdset_new_listen_fds(&fds, /* unset = */ false);
+        if (r < 0) {
+                log_error_errno(r, "Failed to collect file descriptors: %m");
+                goto finish;
         }
 
         /* The "default" umask. This is appropriate for most file and directory
index 42092ada258f2fdaf99b97e53edd6e15caccfb38..7f4b15b1790f27c995f31021513c997519b68849 100644 (file)
@@ -254,25 +254,32 @@ int fdset_cloexec(FDSet *fds, bool b) {
 
 int fdset_new_listen_fds(FDSet **ret, bool unset) {
         _cleanup_(fdset_shallow_freep) FDSet *s = NULL;
-        int n, fd, r;
+        int n, r;
 
         assert(ret);
 
         /* Creates an fdset and fills in all passed file descriptors */
 
+        n = sd_listen_fds(unset);
+        if (n < 0)
+                return n;
+        if (n == 0) {
+                *ret = NULL;
+                return 0;
+        }
+
         s = fdset_new();
         if (!s)
                 return -ENOMEM;
 
-        n = sd_listen_fds(unset);
-        for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd++) {
+        for (int fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd++) {
                 r = fdset_put(s, fd);
                 if (r < 0)
                         return r;
         }
 
         *ret = TAKE_PTR(s);
-        return 0;
+        return n;
 }
 
 int fdset_to_array(FDSet *fds, int **ret) {