diff options
Diffstat (limited to 'src/programs/pkttyagent.c')
-rw-r--r-- | src/programs/pkttyagent.c | 178 |
1 files changed, 70 insertions, 108 deletions
diff --git a/src/programs/pkttyagent.c b/src/programs/pkttyagent.c index 488ca8b..7bd8031 100644 --- a/src/programs/pkttyagent.c +++ b/src/programs/pkttyagent.c @@ -24,134 +24,93 @@ #endif #include <stdio.h> +#include <glib/gi18n.h> #include <polkit/polkit.h> #define POLKIT_AGENT_I_KNOW_API_IS_SUBJECT_TO_CHANGE #include <polkitagent/polkitagent.h> -static void -usage (int argc, char *argv[]) -{ - GError *error; - - error = NULL; - if (!g_spawn_command_line_sync ("man pkttyagent", - NULL, - NULL, - NULL, - &error)) - { - g_printerr ("Cannot show manual page: %s (%s, %d)\n", - error->message, g_quark_to_string (error->domain), error->code); - g_error_free (error); - } -} - - int main (int argc, char *argv[]) { - gboolean opt_show_help = FALSE; gboolean opt_show_version = FALSE; gboolean opt_fallback = FALSE; + gchar *opt_process = NULL; + gchar *opt_system_bus_name = NULL; + gint opt_notify_fd = -1; + GOptionEntry options[] = + { + { + "fallback", 0, 0, G_OPTION_ARG_NONE, &opt_fallback, + N_("Don't replace existing agent if any"), NULL + }, + { + "notify-fd", 0, 0, G_OPTION_ARG_INT, &opt_notify_fd, + N_("Close FD when the agent is registered"), N_("FD") + }, + { + "process", 'p', 0, G_OPTION_ARG_STRING, &opt_process, + N_("Register the agent for the specified process"), + N_("PID[,START_TIME]") + }, + { + "system-bus-name", 's', 0, G_OPTION_ARG_STRING, &opt_system_bus_name, + N_("Register the agent owner of BUS_NAME"), N_("BUS_NAME") + }, + { + "version", 0, 0, G_OPTION_ARG_NONE, &opt_show_version, + N_("Show version"), NULL + }, + { NULL, 0, 0, 0, NULL, NULL, NULL } + }; + GOptionContext *context; PolkitAuthority *authority = NULL; PolkitSubject *subject = NULL; gpointer local_agent_handle = NULL; PolkitAgentListener *listener = NULL; - GVariant *options = NULL; + GVariant *listener_options = NULL; GError *error; GMainLoop *loop = NULL; - guint n; guint ret = 126; - gint notify_fd = -1; GVariantBuilder builder; g_type_init (); - for (n = 1; n < (guint) argc; n++) - { - if (g_strcmp0 (argv[n], "--help") == 0) - { - opt_show_help = TRUE; - } - else if (g_strcmp0 (argv[n], "--version") == 0) - { - opt_show_version = TRUE; - } - else if (g_strcmp0 (argv[n], "--fallback") == 0) - { - opt_fallback = TRUE; - } - else if (g_strcmp0 (argv[n], "--notify-fd") == 0) - { - n++; - if (n >= (guint) argc) - { - usage (argc, argv); - goto out; - } - - if (sscanf (argv[n], "%i", ¬ify_fd) != 1) - { - usage (argc, argv); - goto out; - } - } - else if (g_strcmp0 (argv[n], "--process") == 0 || g_strcmp0 (argv[n], "-p") == 0) - { - gint pid; - guint64 pid_start_time; - - n++; - if (n >= (guint) argc) - { - usage (argc, argv); - goto out; - } - - if (sscanf (argv[n], "%i,%" G_GUINT64_FORMAT, &pid, &pid_start_time) == 2) - { - subject = polkit_unix_process_new_full (pid, pid_start_time); - } - else if (sscanf (argv[n], "%i", &pid) == 1) - { - subject = polkit_unix_process_new (pid); - } - else - { - usage (argc, argv); - goto out; - } - } - else if (g_strcmp0 (argv[n], "--system-bus-name") == 0 || g_strcmp0 (argv[n], "-s") == 0) - { - n++; - if (n >= (guint) argc) - { - usage (argc, argv); - goto out; - } - - subject = polkit_system_bus_name_new (argv[n]); - } - else - { - break; - } - } - - if (opt_show_help) + error = NULL; + context = g_option_context_new (""); + g_option_context_add_main_entries (context, options, GETTEXT_PACKAGE); + if (!g_option_context_parse (context, &argc, &argv, &error)) { - usage (argc, argv); - ret = 0; + g_printerr ("%s: %s\n", g_get_prgname (), error->message); + g_error_free (error); goto out; } - else if (opt_show_version) + + if (opt_show_version) { g_print ("pkttyagent version %s\n", PACKAGE_VERSION); ret = 0; goto out; } + if (opt_process != NULL) + { + gint pid; + guint64 pid_start_time; + + if (sscanf (opt_process, "%i,%" G_GUINT64_FORMAT, &pid, &pid_start_time) + == 2) + subject = polkit_unix_process_new_full (pid, pid_start_time); + else if (sscanf (opt_process, "%i", &pid) == 1) + subject = polkit_unix_process_new (pid); + else + { + g_printerr (_("%s: Invalid process specifier `%s'\n"), + g_get_prgname (), opt_process); + goto out; + } + } + if (opt_system_bus_name != NULL) + subject = polkit_system_bus_name_new (opt_system_bus_name); /* Use parent process, if no subject has been specified */ if (subject == NULL) { @@ -176,7 +135,6 @@ main (int argc, char *argv[]) g_assert (polkit_unix_process_get_start_time (POLKIT_UNIX_PROCESS (subject)) > 0); } - error = NULL; authority = polkit_authority_get_sync (NULL /* GCancellable* */, &error); if (authority == NULL) { @@ -191,7 +149,7 @@ main (int argc, char *argv[]) { g_variant_builder_init (&builder, G_VARIANT_TYPE_VARDICT); g_variant_builder_add (&builder, "{sv}", "fallback", g_variant_new_boolean (TRUE)); - options = g_variant_builder_end (&builder); + listener_options = g_variant_builder_end (&builder); } error = NULL; @@ -209,10 +167,10 @@ main (int argc, char *argv[]) POLKIT_AGENT_REGISTER_FLAGS_RUN_IN_THREAD, subject, NULL, /* object_path */ - options, + listener_options, NULL, /* GCancellable */ &error); - options = NULL; /* consumed */ + listener_options = NULL; /* consumed */ g_object_unref (listener); if (local_agent_handle == NULL) { @@ -222,11 +180,11 @@ main (int argc, char *argv[]) goto out; } - if (notify_fd != -1) + if (opt_notify_fd != -1) { - if (close (notify_fd) != 0) + if (close (opt_notify_fd) != 0) { - g_printerr ("Error closing notify-fd %d: %m\n", notify_fd); + g_printerr ("Error closing notify-fd %d: %m\n", opt_notify_fd); goto out; } } @@ -241,8 +199,8 @@ main (int argc, char *argv[]) if (local_agent_handle != NULL) polkit_agent_listener_unregister (local_agent_handle); - if (options != NULL) - g_variant_unref (options); + if (listener_options != NULL) + g_variant_unref (listener_options); if (subject != NULL) g_object_unref (subject); @@ -250,5 +208,9 @@ main (int argc, char *argv[]) if (authority != NULL) g_object_unref (authority); + g_free (opt_process); + g_free (opt_system_bus_name); + g_option_context_free (context); + return ret; } |