Skip to content

Commit

Permalink
container: Set primary process to 1 via LISTEN_PID by default if user…
Browse files Browse the repository at this point in the history
… configuration is missing

Adds a new field to context listen_fds which differentiates between the
fds coming from preserve_fds and the ones coming from LISTEN_FDS if
LISTEN_FDS is configured set primary process to 1.

Signed-off-by: flouthoc <flouthoc.git@gmail.com>
  • Loading branch information
flouthoc committed Aug 31, 2021
1 parent 97a6e5c commit e2e5867
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/create.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ crun_command_create (struct crun_global_arguments *global_args, int argc, char *
cleanup_free char *config_file_cleanup = NULL;

crun_context.preserve_fds = 0;
crun_context.listen_fds = 0;
/* Check if global handler is configured and pass it down to crun context */
crun_context.handler = global_args->handler;

Expand Down Expand Up @@ -167,7 +168,10 @@ crun_command_create (struct crun_global_arguments *global_args, int argc, char *

crun_context.bundle = bundle;
if (getenv ("LISTEN_FDS"))
crun_context.preserve_fds += strtoll (getenv ("LISTEN_FDS"), NULL, 10);
{
crun_context.preserve_fds += strtoll (getenv ("LISTEN_FDS"), NULL, 10);
crun_context.listen_fds = strtoll (getenv ("LISTEN_FDS"), NULL, 10);
}

return libcrun_container_create (&crun_context, container, 0, err);
}
6 changes: 5 additions & 1 deletion src/exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ crun_command_exec (struct crun_global_arguments *global_args, int argc, char **a
};

crun_context.preserve_fds = 0;
crun_context.listen_fds = 0;

argp_parse (&run_argp, argc, argv, ARGP_IN_ORDER, &first_arg, &exec_options);
crun_assert_n_args (argc - first_arg, exec_options.process ? 1 : 2, -1);
Expand All @@ -243,7 +244,10 @@ crun_command_exec (struct crun_global_arguments *global_args, int argc, char **a
crun_context.preserve_fds = exec_options.preserve_fds;

if (getenv ("LISTEN_FDS"))
crun_context.preserve_fds += strtoll (getenv ("LISTEN_FDS"), NULL, 10);
{
crun_context.preserve_fds += strtoll (getenv ("LISTEN_FDS"), NULL, 10);
crun_context.listen_fds = strtoll (getenv ("LISTEN_FDS"), NULL, 10);
}

if (exec_options.process)
return libcrun_container_exec_process_file (&crun_context, argv[first_arg], exec_options.process, err);
Expand Down
7 changes: 7 additions & 0 deletions src/libcrun/container.c
Original file line number Diff line number Diff line change
Expand Up @@ -1187,6 +1187,13 @@ container_init_setup (void *args, pid_t own_pid, char *notify_socket, int sync_s
if (clearenv ())
return crun_make_error (err, errno, "clearenv");

// set primary process to 1 explicitly if nothing is configured and LISTEN_FD is not set
if (entrypoint_args->context->listen_fds > 0)
{
setenv ("LISTEN_PID", "1", 1);
libcrun_warning ("setting LISTEN_PID=1 since no previous configuration was found");
}

if (def->process)
{
size_t i;
Expand Down
3 changes: 3 additions & 0 deletions src/libcrun/container.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ struct libcrun_context_s
const char *notify_socket;
const char *handler;
int preserve_fds;
// For some use-cases we need differentiation between preserve_fds and listen_fds.
// Following context variable makes sure we get exact value of listen_fds irrespective of preserve_fds.
int listen_fds;

crun_output_handler output_handler;
void *output_handler_arg;
Expand Down
6 changes: 5 additions & 1 deletion src/run.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ crun_command_run (struct crun_global_arguments *global_args, int argc, char **ar
cleanup_free char *config_file_cleanup = NULL;

crun_context.preserve_fds = 0;
crun_context.listen_fds = 0;
/* Check if global handler is configured and pass it down to crun context */
crun_context.handler = global_args->handler;

Expand Down Expand Up @@ -171,7 +172,10 @@ crun_command_run (struct crun_global_arguments *global_args, int argc, char **ar

crun_context.bundle = bundle;
if (getenv ("LISTEN_FDS"))
crun_context.preserve_fds += strtoll (getenv ("LISTEN_FDS"), NULL, 10);
{
crun_context.preserve_fds += strtoll (getenv ("LISTEN_FDS"), NULL, 10);
crun_context.listen_fds = strtoll (getenv ("LISTEN_FDS"), NULL, 10);
}

return libcrun_container_run (&crun_context, container, 0, err);
}

0 comments on commit e2e5867

Please sign in to comment.