Skip to content
Snippets Groups Projects
Commit ff556ce8 authored by Ludovico de Nittis's avatar Ludovico de Nittis :palm_tree:
Browse files

launcher: add support to unset environment variables


Instead of using `env -u VAR` to unset a variable now the variables can
be passed using the DBUS interface.

Signed-off-by: default avatarLudovico de Nittis <ludovico.denittis@collabora.com>
parent e49cb627
Branches
Tags
1 merge request!142Various improvements to pressure-vessel
Pipeline #4629 passed
......@@ -69,10 +69,11 @@
<term>1</term>
<listitem><para>
Clear the environment. If set, the environment will be
exactly @envs, similar to `env - X=1 Y=2 COMMAND`.
If unset, @envs are used to override environment variables
and any environment variable that is not overridden is
left unchanged, similar to `env X=1 Y=2 COMMAND`.
exactly @envs, minus the eventual variables listed in
"unset-env" in @options, similar to `env - X=1 Y=2 COMMAND`.
If unset, @envs and the option "unset-env" are used to override
environment variables and any environment variable that is not
overridden is left unchanged, similar to `env X=1 Y=2 COMMAND`.
</para></listitem>
</varlistentry>
</variablelist>
......@@ -93,6 +94,13 @@
successfully or unsuccessfully.
</para></listitem>
</varlistentry>
<varlistentry>
<term>unset-env as</term>
<listitem><para>
If present, the given array of variables will be unset in the
environment of the new process.
</para></listitem>
</varlistentry>
</variablelist>
This method returns as soon as the process ID of the new process
......
......@@ -460,7 +460,6 @@ main (int argc,
g_autoptr(GOptionContext) context = NULL;
g_autoptr(GError) local_error = NULL;
GError **error = &local_error;
g_autoptr(GPtrArray) env_prefix = NULL;
char **command_and_args;
g_autoptr(FILE) original_stdout = NULL;
g_autoptr(GDBusConnection) session_bus = NULL;
......@@ -764,6 +763,21 @@ main (int argc,
g_variant_builder_add (&options_builder, "{s@v}", "terminate-after",
g_variant_new_variant (g_variant_new_boolean (TRUE)));
if (g_hash_table_size (opt_unsetenv) > 0)
{
GVariantBuilder strv_builder;
g_variant_builder_init (&strv_builder, G_VARIANT_TYPE_STRING_ARRAY);
g_hash_table_iter_init (&iter, opt_unsetenv);
while (g_hash_table_iter_next (&iter, &key, NULL))
g_variant_builder_add (&strv_builder, "s", key);
g_variant_builder_add (&options_builder, "{s@v}", "unset-env",
g_variant_new_variant (g_variant_builder_end (&strv_builder)));
}
if (!opt_directory)
{
opt_directory = g_get_current_dir ();
......@@ -781,39 +795,6 @@ main (int argc,
g_main_loop_ref (loop),
(GDestroyNotify) g_main_loop_unref);
/* Prepend "env -u" if necessary */
if (g_hash_table_size (opt_unsetenv) > 0)
{
env_prefix = g_ptr_array_new_full (2 * g_hash_table_size (opt_unsetenv) + 6,
g_free);
g_ptr_array_add (env_prefix, g_strdup ("env"));
g_hash_table_iter_init (&iter, opt_unsetenv);
while (g_hash_table_iter_next (&iter, &key, NULL))
{
g_ptr_array_add (env_prefix, g_strdup ("-u"));
g_ptr_array_add (env_prefix, g_strdup (key));
}
/* If command_and_args[0] contains '=', env(1) can't deal with
* that, so use the standard workaround */
if (strchr (command_and_args[0], '=') != NULL)
{
g_ptr_array_add (env_prefix, g_strdup ("sh"));
g_ptr_array_add (env_prefix, g_strdup ("-euc"));
g_ptr_array_add (env_prefix, g_strdup ("exec \"$@\""));
g_ptr_array_add (env_prefix, g_strdup ("sh")); /* argv[0] */
}
for (i = 0; command_and_args[i] != NULL; i++)
g_ptr_array_add (env_prefix, g_strdup (command_and_args[i]));
g_ptr_array_add (env_prefix, NULL);
command_and_args = (char **) env_prefix->pdata;
}
{
g_autoptr(GVariant) fds = NULL;
g_autoptr(GVariant) env = NULL;
......
......@@ -265,6 +265,7 @@ handle_launch (PvLauncher1 *object,
gint fds_len = 0;
g_autofree FdMapEntry *fd_map = NULL;
g_auto(GStrv) env = NULL;
g_auto(GStrv) unset_env = NULL;
gint32 max_fd;
gboolean terminate_after = FALSE;
......@@ -368,6 +369,14 @@ handle_launch (PvLauncher1 *object,
env = g_environ_setenv (env, var, val, TRUE);
}
g_variant_lookup (arg_options, "unset-env", "^as", &unset_env);
for (i = 0; unset_env != NULL && unset_env[i] != NULL; i++)
{
g_debug ("Unsetting the environment variable %s...", unset_env[i]);
env = g_environ_unsetenv (env, unset_env[i]);
}
/* We use LEAVE_DESCRIPTORS_OPEN to work around dead-lock, see flatpak_close_fds_workaround */
if (!g_spawn_async_with_pipes (arg_cwd_path,
(gchar **) arg_argv,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment