From e2e586757a4a56c73180aa3ca0b046c0c9f94b97 Mon Sep 17 00:00:00 2001 From: flouthoc Date: Wed, 25 Aug 2021 13:39:17 +0530 Subject: [PATCH] container: Set primary process to 1 via LISTEN_PID by default if user 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 --- src/create.c | 6 +++++- src/exec.c | 6 +++++- src/libcrun/container.c | 7 +++++++ src/libcrun/container.h | 3 +++ src/run.c | 6 +++++- 5 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/create.c b/src/create.c index 71128ed0d3..f9cf3ce6a3 100644 --- a/src/create.c +++ b/src/create.c @@ -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; @@ -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); } diff --git a/src/exec.c b/src/exec.c index 7f966d77dc..264dbe6d90 100644 --- a/src/exec.c +++ b/src/exec.c @@ -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); @@ -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); diff --git a/src/libcrun/container.c b/src/libcrun/container.c index 63ef75d5c3..2313088f3c 100644 --- a/src/libcrun/container.c +++ b/src/libcrun/container.c @@ -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; diff --git a/src/libcrun/container.h b/src/libcrun/container.h index 42524362b5..b4ab591f2d 100644 --- a/src/libcrun/container.h +++ b/src/libcrun/container.h @@ -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; diff --git a/src/run.c b/src/run.c index 0ed0a5b675..589fb373f9 100644 --- a/src/run.c +++ b/src/run.c @@ -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; @@ -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); }