ff6046
From 5e75fbf0ccc427fbe5151ab2096f75dcad5b00e7 Mon Sep 17 00:00:00 2001
ff6046
From: Lennart Poettering <lennart@poettering.net>
ff6046
Date: Thu, 5 Jul 2018 09:56:54 +0200
ff6046
Subject: [PATCH] core: swap order of "n_storage_fds" and "n_socket_fds"
ff6046
 parameters
ff6046
ff6046
When process fd lists to pass to activated programs we always place the
ff6046
socket activation fds first, and the storage fds last. Irritatingly in
ff6046
almost all calls the "n_storage_fds" parameter (i.e. the number of
ff6046
storage fds to pass) came first so far, and the "n_socket_fds" parameter
ff6046
second. Let's clean this up, and specify the number of fds in the order
ff6046
the fds themselves are passed.
ff6046
ff6046
(Also, let's fix one more case where "unsigned" was used to size an
ff6046
array, while we should use "size_t" instead.)
ff6046
ff6046
(cherry picked from commit 25b583d7ffd699384435eba8e49f6ce927a83af0)
ff6046
ff6046
Resolves: #1683334
ff6046
---
ff6046
 src/core/execute.c | 14 +++++++-------
ff6046
 src/core/execute.h |  2 +-
ff6046
 src/core/service.c | 25 ++++++++++++++-----------
ff6046
 3 files changed, 22 insertions(+), 19 deletions(-)
ff6046
ff6046
diff --git a/src/core/execute.c b/src/core/execute.c
ff6046
index ffb92ddfc7..7476ac51da 100644
ff6046
--- a/src/core/execute.c
ff6046
+++ b/src/core/execute.c
ff6046
@@ -147,11 +147,11 @@ static int shift_fds(int fds[], size_t n_fds) {
ff6046
         return 0;
ff6046
 }
ff6046
 
ff6046
-static int flags_fds(const int fds[], size_t n_storage_fds, size_t n_socket_fds, bool nonblock) {
ff6046
+static int flags_fds(const int fds[], size_t n_socket_fds, size_t n_storage_fds, bool nonblock) {
ff6046
         size_t i, n_fds;
ff6046
         int r;
ff6046
 
ff6046
-        n_fds = n_storage_fds + n_socket_fds;
ff6046
+        n_fds = n_socket_fds + n_storage_fds;
ff6046
         if (n_fds <= 0)
ff6046
                 return 0;
ff6046
 
ff6046
@@ -2718,8 +2718,8 @@ static int exec_child(
ff6046
                 int socket_fd,
ff6046
                 int named_iofds[3],
ff6046
                 int *fds,
ff6046
-                size_t n_storage_fds,
ff6046
                 size_t n_socket_fds,
ff6046
+                size_t n_storage_fds,
ff6046
                 char **files_env,
ff6046
                 int user_lookup_fd,
ff6046
                 int *exit_status) {
ff6046
@@ -3171,7 +3171,7 @@ static int exec_child(
ff6046
         if (r >= 0)
ff6046
                 r = shift_fds(fds, n_fds);
ff6046
         if (r >= 0)
ff6046
-                r = flags_fds(fds, n_storage_fds, n_socket_fds, context->non_blocking);
ff6046
+                r = flags_fds(fds, n_socket_fds, n_storage_fds, context->non_blocking);
ff6046
         if (r < 0) {
ff6046
                 *exit_status = EXIT_FDS;
ff6046
                 return log_unit_error_errno(unit, r, "Failed to adjust passed file descriptors: %m");
ff6046
@@ -3449,7 +3449,7 @@ int exec_spawn(Unit *unit,
ff6046
         assert(context);
ff6046
         assert(ret);
ff6046
         assert(params);
ff6046
-        assert(params->fds || (params->n_storage_fds + params->n_socket_fds <= 0));
ff6046
+        assert(params->fds || (params->n_socket_fds + params->n_storage_fds <= 0));
ff6046
 
ff6046
         if (context->std_input == EXEC_INPUT_SOCKET ||
ff6046
             context->std_output == EXEC_OUTPUT_SOCKET ||
ff6046
@@ -3469,8 +3469,8 @@ int exec_spawn(Unit *unit,
ff6046
         } else {
ff6046
                 socket_fd = -1;
ff6046
                 fds = params->fds;
ff6046
-                n_storage_fds = params->n_storage_fds;
ff6046
                 n_socket_fds = params->n_socket_fds;
ff6046
+                n_storage_fds = params->n_storage_fds;
ff6046
         }
ff6046
 
ff6046
         r = exec_context_named_iofds(context, params, named_iofds);
ff6046
@@ -3509,8 +3509,8 @@ int exec_spawn(Unit *unit,
ff6046
                                socket_fd,
ff6046
                                named_iofds,
ff6046
                                fds,
ff6046
-                               n_storage_fds,
ff6046
                                n_socket_fds,
ff6046
+                               n_storage_fds,
ff6046
                                files_env,
ff6046
                                unit->manager->user_lookup_fds[1],
ff6046
                                &exit_status);
ff6046
diff --git a/src/core/execute.h b/src/core/execute.h
ff6046
index 77ffe82323..49705e0d3a 100644
ff6046
--- a/src/core/execute.h
ff6046
+++ b/src/core/execute.h
ff6046
@@ -296,8 +296,8 @@ struct ExecParameters {
ff6046
 
ff6046
         int *fds;
ff6046
         char **fd_names;
ff6046
-        size_t n_storage_fds;
ff6046
         size_t n_socket_fds;
ff6046
+        size_t n_storage_fds;
ff6046
 
ff6046
         ExecFlags flags;
ff6046
         bool selinux_context_net:1;
ff6046
diff --git a/src/core/service.c b/src/core/service.c
ff6046
index db17221888..7f8ce1b998 100644
ff6046
--- a/src/core/service.c
ff6046
+++ b/src/core/service.c
ff6046
@@ -1178,21 +1178,23 @@ static int service_coldplug(Unit *u) {
ff6046
         return 0;
ff6046
 }
ff6046
 
ff6046
-static int service_collect_fds(Service *s,
ff6046
-                               int **fds,
ff6046
-                               char ***fd_names,
ff6046
-                               unsigned *n_storage_fds,
ff6046
-                               unsigned *n_socket_fds) {
ff6046
+static int service_collect_fds(
ff6046
+                Service *s,
ff6046
+                int **fds,
ff6046
+                char ***fd_names,
ff6046
+                size_t *n_socket_fds,
ff6046
+                size_t *n_storage_fds) {
ff6046
 
ff6046
         _cleanup_strv_free_ char **rfd_names = NULL;
ff6046
         _cleanup_free_ int *rfds = NULL;
ff6046
-        unsigned rn_socket_fds = 0, rn_storage_fds = 0;
ff6046
+        size_t rn_socket_fds = 0, rn_storage_fds = 0;
ff6046
         int r;
ff6046
 
ff6046
         assert(s);
ff6046
         assert(fds);
ff6046
         assert(fd_names);
ff6046
         assert(n_socket_fds);
ff6046
+        assert(n_storage_fds);
ff6046
 
ff6046
         if (s->socket_fd >= 0) {
ff6046
 
ff6046
@@ -1256,7 +1258,7 @@ static int service_collect_fds(Service *s,
ff6046
 
ff6046
         if (s->n_fd_store > 0) {
ff6046
                 ServiceFDStore *fs;
ff6046
-                unsigned n_fds;
ff6046
+                size_t n_fds;
ff6046
                 char **nl;
ff6046
                 int *t;
ff6046
 
ff6046
@@ -1325,9 +1327,10 @@ static int service_spawn(
ff6046
                 .stdin_fd   = -1,
ff6046
                 .stdout_fd  = -1,
ff6046
                 .stderr_fd  = -1,
ff6046
+                .exec_fd    = -1,
ff6046
         };
ff6046
         _cleanup_strv_free_ char **final_env = NULL, **our_env = NULL, **fd_names = NULL;
ff6046
-        unsigned n_storage_fds = 0, n_socket_fds = 0, n_env = 0;
ff6046
+        size_t n_socket_fds = 0, n_storage_fds = 0, n_env = 0;
ff6046
         _cleanup_free_ int *fds = NULL;
ff6046
         pid_t pid;
ff6046
         int r;
ff6046
@@ -1353,11 +1356,11 @@ static int service_spawn(
ff6046
             s->exec_context.std_output == EXEC_OUTPUT_SOCKET ||
ff6046
             s->exec_context.std_error == EXEC_OUTPUT_SOCKET) {
ff6046
 
ff6046
-                r = service_collect_fds(s, &fds, &fd_names, &n_storage_fds, &n_socket_fds);
ff6046
+                r = service_collect_fds(s, &fds, &fd_names, &n_socket_fds, &n_storage_fds);
ff6046
                 if (r < 0)
ff6046
                         return r;
ff6046
 
ff6046
-                log_unit_debug(UNIT(s), "Passing %i fds to service", n_storage_fds + n_socket_fds);
ff6046
+                log_unit_debug(UNIT(s), "Passing %zu fds to service", n_socket_fds + n_storage_fds);
ff6046
         }
ff6046
 
ff6046
         r = service_arm_timer(s, usec_add(now(CLOCK_MONOTONIC), timeout));
ff6046
@@ -1450,8 +1453,8 @@ static int service_spawn(
ff6046
         exec_params.environment = final_env;
ff6046
         exec_params.fds = fds;
ff6046
         exec_params.fd_names = fd_names;
ff6046
-        exec_params.n_storage_fds = n_storage_fds;
ff6046
         exec_params.n_socket_fds = n_socket_fds;
ff6046
+        exec_params.n_storage_fds = n_storage_fds;
ff6046
         exec_params.watchdog_usec = s->watchdog_usec;
ff6046
         exec_params.selinux_context_net = s->socket_fd_selinux_context_net;
ff6046
         if (s->type == SERVICE_IDLE)