From 8e1a125f70e504986e8d1e68fafa297cb783aa05 Mon Sep 17 00:00:00 2001 From: Mark Date: Tue, 5 Oct 2021 21:34:16 -0500 Subject: [PATCH] startup: Optionally run (and exit with) a command Treat the first non-option as a command, and any others as its CLI arguments. Run the command with those arguments; communicate its exit status (if nonzero); and exit with it. Document this added functionality in the manpage and usage description. Closes: https://gitlab.gnome.org/GNOME/mutter/-/merge_requests/1981 Part-of: --- doc/man/mutter.1 | 2 +- src/core/mutter.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/doc/man/mutter.1 b/doc/man/mutter.1 index 998d0f826..6d9122cde 100644 --- a/doc/man/mutter.1 +++ b/doc/man/mutter.1 @@ -19,7 +19,7 @@ MUTTER \- Clutter based compositing GTK2 Window Manager .SH SYNOPSIS .B mutter -[\-\-display=\fIDISPLAY\fP] [\-\-replace] [\-\-sm\-client\-id=\fIID\fP] [\-\-sm\-disable] [\-\-sm\-save\-file=\fIFILENAME\fP] [\-\-version] [\-\-help] +[\-\-display=\fIDISPLAY\fP] [\-\-replace] [\-\-sm\-client\-id=\fIID\fP] [\-\-sm\-disable] [\-\-sm\-save\-file=\fIFILENAME\fP] [\-\-version] [\-\-help] [[\-\-] command [argument...]] .SH DESCRIPTION This manual page documents briefly .B mutter\fP. diff --git a/src/core/mutter.c b/src/core/mutter.c index 01041e734..8af146b4b 100644 --- a/src/core/mutter.c +++ b/src/core/mutter.c @@ -39,7 +39,33 @@ print_version (const gchar *option_name, exit (0); } +static void +command_exited_cb (GPid command_pid, + int status, + gpointer user_data) +{ + MetaContext *context = user_data; + + g_spawn_close_pid (command_pid); + + if (status) + { + GError *error; + + error = g_error_new (G_IO_ERROR, G_IO_ERROR_FAILED, + "The command exited with a nonzero status: %d\n", + status); + + meta_context_terminate_with_error (context, error); + } + else + { + meta_context_terminate (context); + } +} + static const char *plugin = "libdefault"; +static char **argv_ignored = NULL; GOptionEntry mutter_options[] = { { @@ -54,6 +80,12 @@ GOptionEntry mutter_options[] = { N_("Mutter plugin to use"), "PLUGIN", }, + { + G_OPTION_REMAINING, + .arg = G_OPTION_ARG_STRING_ARRAY, + &argv_ignored, + .arg_description = "[[--] COMMAND [ARGUMENT…]]" + }, { NULL } }; @@ -120,6 +152,23 @@ main (int argc, char **argv) } meta_context_notify_ready (context); + if (argv_ignored) + { + GPid command_pid; + g_auto (GStrv) command_argv = NULL; + + command_argv = g_steal_pointer (&argv_ignored); + + if (!g_spawn_async (NULL, command_argv, NULL, + G_SPAWN_DO_NOT_REAP_CHILD | G_SPAWN_SEARCH_PATH, + NULL, NULL, &command_pid, &error)) + { + g_printerr ("Failed to run the command: %s", error->message); + return EXIT_FAILURE; + } + + g_child_watch_add (command_pid, command_exited_cb, context); + } if (!meta_context_run_main_loop (context, &error)) {