core: remove the redundancy of 'n_fds' and 'n_storage_fds' in ExecParameters struct
'n_fds' field in the ExecParameters structure was counting the total number of file descriptors to be passed to a unit. This counter also includes the number of passed socket fds which is counted by 'n_socket_fds' already. This patch removes that redundancy by replacing 'n_fds' with 'n_storage_fds'. The new field only counts the fds passed via the storage store mechanism. That way each fd is counted at one place only. Subsequently the patch makes sure to fix code that used 'n_fds' and also wanted to iterate through all of them by explicitly adding 'n_socket_fds' + 'n_storage_fds'. Suggested by Lennart.
This commit is contained in:
+19
-11
@@ -157,10 +157,11 @@ static int shift_fds(int fds[], unsigned n_fds) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int flags_fds(const int fds[], unsigned n_fds, unsigned n_socket_fds, bool nonblock) {
|
||||
unsigned i;
|
||||
static int flags_fds(const int fds[], unsigned n_storage_fds, unsigned n_socket_fds, bool nonblock) {
|
||||
unsigned i, n_fds;
|
||||
int r;
|
||||
|
||||
n_fds = n_storage_fds + n_socket_fds;
|
||||
if (n_fds <= 0)
|
||||
return 0;
|
||||
|
||||
@@ -2244,7 +2245,8 @@ static int exec_child(
|
||||
char **argv,
|
||||
int socket_fd,
|
||||
int named_iofds[3],
|
||||
int *fds, unsigned n_fds,
|
||||
int *fds,
|
||||
unsigned n_storage_fds,
|
||||
unsigned n_socket_fds,
|
||||
char **files_env,
|
||||
int user_lookup_fd,
|
||||
@@ -2262,6 +2264,7 @@ static int exec_child(
|
||||
uid_t uid = UID_INVALID;
|
||||
gid_t gid = GID_INVALID;
|
||||
int i, r, ngids = 0;
|
||||
unsigned n_fds;
|
||||
|
||||
assert(unit);
|
||||
assert(command);
|
||||
@@ -2302,6 +2305,7 @@ static int exec_child(
|
||||
|
||||
log_forget_fds();
|
||||
|
||||
n_fds = n_storage_fds + n_socket_fds;
|
||||
r = close_remaining_fds(params, runtime, dcreds, user_lookup_fd, socket_fd, fds, n_fds);
|
||||
if (r < 0) {
|
||||
*exit_status = EXIT_FDS;
|
||||
@@ -2673,7 +2677,7 @@ static int exec_child(
|
||||
if (r >= 0)
|
||||
r = shift_fds(fds, n_fds);
|
||||
if (r >= 0)
|
||||
r = flags_fds(fds, n_fds, n_socket_fds, context->non_blocking);
|
||||
r = flags_fds(fds, n_storage_fds, n_socket_fds, context->non_blocking);
|
||||
if (r < 0) {
|
||||
*exit_status = EXIT_FDS;
|
||||
return r;
|
||||
@@ -2914,7 +2918,7 @@ int exec_spawn(Unit *unit,
|
||||
|
||||
_cleanup_strv_free_ char **files_env = NULL;
|
||||
int *fds = NULL;
|
||||
unsigned n_fds = 0, n_socket_fds = 0;
|
||||
unsigned n_storage_fds = 0, n_socket_fds = 0;
|
||||
_cleanup_free_ char *line = NULL;
|
||||
int socket_fd, r;
|
||||
int named_iofds[3] = { -1, -1, -1 };
|
||||
@@ -2926,18 +2930,18 @@ int exec_spawn(Unit *unit,
|
||||
assert(context);
|
||||
assert(ret);
|
||||
assert(params);
|
||||
assert(params->fds || params->n_fds <= 0);
|
||||
assert(params->fds || (params->n_storage_fds + params->n_socket_fds <= 0));
|
||||
|
||||
if (context->std_input == EXEC_INPUT_SOCKET ||
|
||||
context->std_output == EXEC_OUTPUT_SOCKET ||
|
||||
context->std_error == EXEC_OUTPUT_SOCKET) {
|
||||
|
||||
if (params->n_fds > 1) {
|
||||
if (params->n_socket_fds > 1) {
|
||||
log_unit_error(unit, "Got more than one socket.");
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
if (params->n_fds == 0) {
|
||||
if (params->n_socket_fds == 0) {
|
||||
log_unit_error(unit, "Got no socket.");
|
||||
return -EINVAL;
|
||||
}
|
||||
@@ -2946,7 +2950,7 @@ int exec_spawn(Unit *unit,
|
||||
} else {
|
||||
socket_fd = -1;
|
||||
fds = params->fds;
|
||||
n_fds = params->n_fds;
|
||||
n_storage_fds = params->n_storage_fds;
|
||||
n_socket_fds = params->n_socket_fds;
|
||||
}
|
||||
|
||||
@@ -2985,7 +2989,8 @@ int exec_spawn(Unit *unit,
|
||||
argv,
|
||||
socket_fd,
|
||||
named_iofds,
|
||||
fds, n_fds,
|
||||
fds,
|
||||
n_storage_fds,
|
||||
n_socket_fds,
|
||||
files_env,
|
||||
unit->manager->user_lookup_fds[1],
|
||||
@@ -3195,6 +3200,7 @@ const char* exec_context_fdname(const ExecContext *c, int fd_index) {
|
||||
int exec_context_named_iofds(Unit *unit, const ExecContext *c, const ExecParameters *p, int named_iofds[3]) {
|
||||
unsigned i, targets;
|
||||
const char* stdio_fdname[3];
|
||||
unsigned n_fds;
|
||||
|
||||
assert(c);
|
||||
assert(p);
|
||||
@@ -3206,7 +3212,9 @@ int exec_context_named_iofds(Unit *unit, const ExecContext *c, const ExecParamet
|
||||
for (i = 0; i < 3; i++)
|
||||
stdio_fdname[i] = exec_context_fdname(c, i);
|
||||
|
||||
for (i = 0; i < p->n_fds && targets > 0; i++)
|
||||
n_fds = p->n_storage_fds + p->n_socket_fds;
|
||||
|
||||
for (i = 0; i < n_fds && targets > 0; i++)
|
||||
if (named_iofds[STDIN_FILENO] < 0 &&
|
||||
c->std_input == EXEC_INPUT_NAMED_FD &&
|
||||
stdio_fdname[STDIN_FILENO] &&
|
||||
|
||||
Reference in New Issue
Block a user