From 0241a7148973acf32d36c3da5e176caa3b16f92c Mon Sep 17 00:00:00 2001 From: Simon McVittie <smcv@collabora.com> Date: Mon, 17 Aug 2020 20:10:01 +0100 Subject: [PATCH] adverb, wrap: Implement --exit-with-parent -wrap eventually replaces itself with bubblewrap. If the bubblewrap process is killed, we want the adverb command to be terminated, passing the termination signal on to its own child process. Signed-off-by: Simon McVittie <smcv@collabora.com> --- src/adverb.c | 23 +++++++++++++++++++++++ src/wrap.c | 33 +++++++++++++++------------------ 2 files changed, 38 insertions(+), 18 deletions(-) diff --git a/src/adverb.c b/src/adverb.c index 76a2e848a..13047fae5 100644 --- a/src/adverb.c +++ b/src/adverb.c @@ -46,6 +46,7 @@ static GPtrArray *global_locks = NULL; static GArray *global_pass_fds = NULL; static gboolean opt_create = FALSE; +static gboolean opt_exit_with_parent = FALSE; static gboolean opt_generate_locales = FALSE; static gboolean opt_subreaper = FALSE; static double opt_terminate_idle_timeout = 0.0; @@ -344,6 +345,16 @@ static GOptionEntry options[] = "Don't create subsequent nonexistent lock files [default].", NULL }, + { "exit-with-parent", '\0', + G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, &opt_exit_with_parent, + "Terminate child process and self with SIGTERM when parent process " + "exits.", + NULL }, + { "no-exit-with-parent", '\0', + G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &opt_exit_with_parent, + "Don't do anything special when parent process exits [default].", + NULL }, + { "generate-locales", '\0', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, &opt_generate_locales, "Attempt to generate any missing locales.", NULL }, @@ -525,6 +536,18 @@ main (int argc, command_and_args = argv + 1; + if (opt_exit_with_parent) + { + g_debug ("Setting up to exit when parent does"); + + if (prctl (PR_SET_PDEATHSIG, SIGTERM, 0, 0, 0) != 0) + { + glnx_throw_errno_prefix (error, + "Unable to set parent death signal"); + goto out; + } + } + if ((opt_subreaper || opt_terminate_timeout >= 0) && prctl (PR_SET_CHILD_SUBREAPER, 1, 0, 0, 0) != 0) { diff --git a/src/wrap.c b/src/wrap.c index 764dfc261..d5a201dd9 100644 --- a/src/wrap.c +++ b/src/wrap.c @@ -1464,27 +1464,24 @@ main (int argc, opt_terminate_timeout); } - if (adverb_args->argv->len > 0) - { - /* If not using a runtime, the adverb in the container has the - * same path as outside */ - if (adverb_in_container == NULL) - adverb_in_container = g_build_filename (tools_dir, - "pressure-vessel-adverb", - NULL); + /* If not using a runtime, the adverb in the container has the + * same path as outside */ + if (adverb_in_container == NULL) + adverb_in_container = g_build_filename (tools_dir, + "pressure-vessel-adverb", + NULL); - flatpak_bwrap_add_args (bwrap, - adverb_in_container, - "--subreaper", - NULL); + flatpak_bwrap_add_args (bwrap, + adverb_in_container, + "--exit-with-parent", + "--subreaper", + NULL); - if (opt_verbose) - flatpak_bwrap_add_arg (bwrap, "--verbose"); + if (opt_verbose) + flatpak_bwrap_add_arg (bwrap, "--verbose"); - flatpak_bwrap_append_bwrap (bwrap, adverb_args); - flatpak_bwrap_add_arg (bwrap, "--"); - } - /* else just run the wrapped command directly */ + flatpak_bwrap_append_bwrap (bwrap, adverb_args); + flatpak_bwrap_add_arg (bwrap, "--"); g_debug ("Adding wrapped command..."); flatpak_bwrap_append_args (bwrap, wrapped_command->argv); -- GitLab