X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Fhostname%2Fhostnamectl.c;h=3150396c70e89d2167e94616f4953d7694cea43f;hp=7591d99988759c7d0873d8697b43d2c4677eef3c;hb=145b1f799ff6712f333cef92c44218254a1320c5;hpb=b028f3e410a7b9ba6b6291a79164d3de2638eb0b diff --git a/src/hostname/hostnamectl.c b/src/hostname/hostnamectl.c index 7591d9998..3150396c7 100644 --- a/src/hostname/hostnamectl.c +++ b/src/hostname/hostnamectl.c @@ -48,6 +48,18 @@ static bool arg_transient = false; static bool arg_pretty = false; static bool arg_static = false; +static void polkit_agent_open_if_enabled(void) { + + /* Open the polkit agent as a child process if necessary */ + if (!arg_ask_password) + return; + + if (arg_transport != BUS_TRANSPORT_LOCAL) + return; + + polkit_agent_open(); +} + typedef struct StatusInfo { char *hostname; char *static_hostname; @@ -133,7 +145,7 @@ static int show_one_name(sd_bus *bus, const char* attr) { r = sd_bus_message_read(reply, "s", &s); if (r < 0) - return r; + return bus_log_parse_error(r); printf("%s\n", s); @@ -142,12 +154,12 @@ static int show_one_name(sd_bus *bus, const char* attr) { static int show_all_names(sd_bus *bus) { StatusInfo info = {}; - const struct bus_properties_map map[] = { - { "s", "Hostname", &info.hostname }, - { "s", "StaticHostname", &info.static_hostname }, - { "s", "PrettyHostname", &info.pretty_hostname }, - { "s", "IconName", &info.icon_name }, - { "s", "Chassis", &info.chassis }, + static const struct bus_properties_map map[] = { + { "Hostname", "s", NULL, offsetof(StatusInfo, hostname) }, + { "StaticHostname", "s", NULL, offsetof(StatusInfo, static_hostname) }, + { "PrettyHostname", "s", NULL, offsetof(StatusInfo, pretty_hostname) }, + { "IconName", "s", NULL, offsetof(StatusInfo, icon_name) }, + { "Chassis", "s", NULL, offsetof(StatusInfo, chassis) }, {} }; int r; @@ -155,7 +167,8 @@ static int show_all_names(sd_bus *bus) { r = bus_map_all_properties(bus, "org.freedesktop.hostname1", "/org/freedesktop/hostname1", - map); + map, + &info); if (r < 0) goto fail; @@ -191,12 +204,9 @@ static int show_status(sd_bus *bus, char **args, unsigned n) { static int set_simple_string(sd_bus *bus, const char *method, const char *value) { _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL; - static bool first=true; int r = 0; - if (first && arg_ask_password) - polkit_agent_open(); - first = false; + polkit_agent_open_if_enabled(); r = sd_bus_call_method( bus, @@ -275,7 +285,7 @@ static int set_chassis(sd_bus *bus, char **args, unsigned n) { assert(args); assert(n == 2); - return set_simple_string(bus, "SetChasis", args[1]); + return set_simple_string(bus, "SetChassis", args[1]); } static int help(void) { @@ -284,12 +294,12 @@ static int help(void) { "Query or change system hostname.\n\n" " -h --help Show this help\n" " --version Show package version\n" - " --transient Only set transient hostname\n" - " --static Only set static hostname\n" - " --pretty Only set pretty hostname\n" " --no-ask-password Do not prompt for password\n" " -H --host=[USER@]HOST Operate on remote host\n" - " -M --machine=CONTAINER Operate on local container\n\n" + " -M --machine=CONTAINER Operate on local container\n" + " --transient Only set transient hostname\n" + " --static Only set static hostname\n" + " --pretty Only set pretty hostname\n\n" "Commands:\n" " status Show current hostname settings\n" " set-hostname NAME Set system hostname\n" @@ -313,13 +323,13 @@ static int parse_argv(int argc, char *argv[]) { static const struct option options[] = { { "help", no_argument, NULL, 'h' }, { "version", no_argument, NULL, ARG_VERSION }, - { "transient", no_argument, NULL, ARG_TRANSIENT }, - { "static", no_argument, NULL, ARG_STATIC }, - { "pretty", no_argument, NULL, ARG_PRETTY }, + { "transient", no_argument, NULL, ARG_TRANSIENT }, + { "static", no_argument, NULL, ARG_STATIC }, + { "pretty", no_argument, NULL, ARG_PRETTY }, { "host", required_argument, NULL, 'H' }, { "machine", required_argument, NULL, 'M' }, { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD }, - { NULL, 0, NULL, 0 } + {} }; int c; @@ -327,13 +337,12 @@ static int parse_argv(int argc, char *argv[]) { assert(argc >= 0); assert(argv); - while ((c = getopt_long(argc, argv, "hHM:P", options, NULL)) >= 0) { + while ((c = getopt_long(argc, argv, "hH:M:", options, NULL)) >= 0) { switch (c) { case 'h': - help(); - return 0; + return help(); case ARG_VERSION: puts(PACKAGE_STRING); @@ -370,8 +379,7 @@ static int parse_argv(int argc, char *argv[]) { return -EINVAL; default: - log_error("Unknown option code %c", c); - return -EINVAL; + assert_not_reached("Unhandled option"); } } @@ -457,8 +465,8 @@ static int hostnamectl_main(sd_bus *bus, int argc, char *argv[]) { } int main(int argc, char *argv[]) { - int r; _cleanup_bus_unref_ sd_bus *bus = NULL; + int r; setlocale(LC_ALL, ""); log_parse_environment();