X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Fhostname%2Fhostnamed.c;h=b230ff6d5c3735a12405bd93ee8546057634e2dc;hp=dae73b390f7b4048687a3dc38c3e57a4a4b9645c;hb=ffbc903f030d9acd2c40e4defd8e549b046ec520;hpb=def9a7aa0182e5ecca3ac61b26f75136a5c4f103 diff --git a/src/hostname/hostnamed.c b/src/hostname/hostnamed.c index dae73b390..b230ff6d5 100644 --- a/src/hostname/hostnamed.c +++ b/src/hostname/hostnamed.c @@ -23,7 +23,6 @@ #include #include #include -#include #include "util.h" #include "strv.h" @@ -69,11 +68,11 @@ static void context_reset(Context *c) { } } -static void context_free(Context *c, sd_bus *bus) { +static void context_free(Context *c) { assert(c); context_reset(c); - bus_verify_polkit_async_registry_free(bus, c->polkit_registry); + bus_verify_polkit_async_registry_free(c->polkit_registry); } static int context_read_data(Context *c) { @@ -138,7 +137,8 @@ static bool valid_chassis(const char *chassis) { "server\0" "tablet\0" "handset\0" - "watch\0", + "watch\0" + "embedded\0", chassis); } @@ -275,7 +275,7 @@ static int context_update_kernel_hostname(Context *c) { if (hostname_is_useful(static_hn)) hn = static_hn; - /* ... the transient host name, (ie: DHCP) comes next ...*/ + /* ... the transient host name, (ie: DHCP) comes next ... */ else if (!isempty(c->data[PROP_HOSTNAME])) hn = c->data[PROP_HOSTNAME]; @@ -287,7 +287,7 @@ static int context_update_kernel_hostname(Context *c) { else hn = "localhost"; - if (sethostname(hn, strlen(hn)) < 0) + if (sethostname_idempotent(hn) < 0) return -errno; return 0; @@ -426,7 +426,7 @@ static int method_set_hostname(sd_bus *bus, sd_bus_message *m, void *userdata, s if (streq_ptr(name, c->data[PROP_HOSTNAME])) return sd_bus_reply_method_return(m, NULL); - r = bus_verify_polkit_async(bus, &c->polkit_registry, m, CAP_SYS_ADMIN, "org.freedesktop.hostname1.set-hostname", interactive, error, method_set_hostname, c); + r = bus_verify_polkit_async(m, CAP_SYS_ADMIN, "org.freedesktop.hostname1.set-hostname", interactive, &c->polkit_registry, error); if (r < 0) return r; if (r == 0) @@ -441,7 +441,7 @@ static int method_set_hostname(sd_bus *bus, sd_bus_message *m, void *userdata, s r = context_update_kernel_hostname(c); if (r < 0) { - log_error("Failed to set host name: %s", strerror(-r)); + log_error_errno(r, "Failed to set host name: %m"); return sd_bus_error_set_errnof(error, r, "Failed to set hostname: %s", strerror(-r)); } @@ -468,7 +468,7 @@ static int method_set_static_hostname(sd_bus *bus, sd_bus_message *m, void *user if (streq_ptr(name, c->data[PROP_STATIC_HOSTNAME])) return sd_bus_reply_method_return(m, NULL); - r = bus_verify_polkit_async(bus, &c->polkit_registry, m, CAP_SYS_ADMIN, "org.freedesktop.hostname1.set-static-hostname", interactive, error, method_set_static_hostname, c); + r = bus_verify_polkit_async(m, CAP_SYS_ADMIN, "org.freedesktop.hostname1.set-static-hostname", interactive, &c->polkit_registry, error); if (r < 0) return r; if (r == 0) @@ -493,13 +493,13 @@ static int method_set_static_hostname(sd_bus *bus, sd_bus_message *m, void *user r = context_update_kernel_hostname(c); if (r < 0) { - log_error("Failed to set host name: %s", strerror(-r)); + log_error_errno(r, "Failed to set host name: %m"); return sd_bus_error_set_errnof(error, r, "Failed to set hostname: %s", strerror(-r)); } r = context_write_data_static_hostname(c); if (r < 0) { - log_error("Failed to write static host name: %s", strerror(-r)); + log_error_errno(r, "Failed to write static host name: %m"); return sd_bus_error_set_errnof(error, r, "Failed to set static hostname: %s", strerror(-r)); } @@ -533,10 +533,10 @@ static int set_machine_info(Context *c, sd_bus *bus, sd_bus_message *m, int prop * same time as the static one, use the same policy action for * both... */ - r = bus_verify_polkit_async(bus, &c->polkit_registry, m, CAP_SYS_ADMIN, + r = bus_verify_polkit_async(m, CAP_SYS_ADMIN, prop == PROP_PRETTY_HOSTNAME ? "org.freedesktop.hostname1.set-static-hostname" : - "org.freedesktop.hostname1.set-machine-info", interactive, error, cb, c); + "org.freedesktop.hostname1.set-machine-info", interactive, &c->polkit_registry, error); if (r < 0) return r; if (r == 0) @@ -551,7 +551,7 @@ static int set_machine_info(Context *c, sd_bus *bus, sd_bus_message *m, int prop /* The icon name might ultimately be used as file * name, so better be safe than sorry */ - if (prop == PROP_ICON_NAME && !filename_is_safe(name)) + if (prop == PROP_ICON_NAME && !filename_is_valid(name)) return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid icon name '%s'", name); if (prop == PROP_PRETTY_HOSTNAME && string_has_cc(name, NULL)) return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid pretty host name '%s'", name); @@ -572,7 +572,7 @@ static int set_machine_info(Context *c, sd_bus *bus, sd_bus_message *m, int prop r = context_write_data_machine_info(c); if (r < 0) { - log_error("Failed to write machine info: %s", strerror(-r)); + log_error_errno(r, "Failed to write machine info: %m"); return sd_bus_error_set_errnof(error, r, "Failed to write machine info: %s", strerror(-r)); } @@ -613,7 +613,7 @@ static int method_set_location(sd_bus *bus, sd_bus_message *m, void *userdata, s static const sd_bus_vtable hostname_vtable[] = { SD_BUS_VTABLE_START(0), - SD_BUS_PROPERTY("Hostname", "s", NULL, offsetof(Context, data) + sizeof(char*) * PROP_HOSTNAME, 0), + SD_BUS_PROPERTY("Hostname", "s", NULL, offsetof(Context, data) + sizeof(char*) * PROP_HOSTNAME, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), SD_BUS_PROPERTY("StaticHostname", "s", NULL, offsetof(Context, data) + sizeof(char*) * PROP_STATIC_HOSTNAME, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), SD_BUS_PROPERTY("PrettyHostname", "s", NULL, offsetof(Context, data) + sizeof(char*) * PROP_PRETTY_HOSTNAME, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), SD_BUS_PROPERTY("IconName", "s", property_get_icon_name, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), @@ -644,28 +644,20 @@ static int connect_bus(Context *c, sd_event *event, sd_bus **_bus) { assert(_bus); r = sd_bus_default_system(&bus); - if (r < 0) { - log_error("Failed to get system bus connection: %s", strerror(-r)); - return r; - } + if (r < 0) + return log_error_errno(r, "Failed to get system bus connection: %m"); r = sd_bus_add_object_vtable(bus, NULL, "/org/freedesktop/hostname1", "org.freedesktop.hostname1", hostname_vtable, c); - if (r < 0) { - log_error("Failed to register object: %s", strerror(-r)); - return r; - } + if (r < 0) + return log_error_errno(r, "Failed to register object: %m"); r = sd_bus_request_name(bus, "org.freedesktop.hostname1", 0); - if (r < 0) { - log_error("Failed to register name: %s", strerror(-r)); - return r; - } + if (r < 0) + return log_error_errno(r, "Failed to register name: %m"); r = sd_bus_attach_event(bus, event, 0); - if (r < 0) { - log_error("Failed to attach bus to event loop: %s", strerror(-r)); - return r; - } + if (r < 0) + return log_error_errno(r, "Failed to attach bus to event loop: %m"); *_bus = bus; bus = NULL; @@ -684,7 +676,7 @@ int main(int argc, char *argv[]) { log_open(); umask(0022); - label_init("/etc"); + mac_selinux_init("/etc"); if (argc != 1) { log_error("This program takes no arguments."); @@ -700,7 +692,7 @@ int main(int argc, char *argv[]) { r = sd_event_default(&event); if (r < 0) { - log_error("Failed to allocate event loop: %s", strerror(-r)); + log_error_errno(r, "Failed to allocate event loop: %m"); goto finish; } @@ -712,18 +704,18 @@ int main(int argc, char *argv[]) { r = context_read_data(&context); if (r < 0) { - log_error("Failed to read hostname and machine information: %s", strerror(-r)); + log_error_errno(r, "Failed to read hostname and machine information: %m"); goto finish; } r = bus_event_loop_with_idle(event, bus, "org.freedesktop.hostname1", DEFAULT_EXIT_USEC, NULL, NULL); if (r < 0) { - log_error("Failed to run event loop: %s", strerror(-r)); + log_error_errno(r, "Failed to run event loop: %m"); goto finish; } finish: - context_free(&context, bus); + context_free(&context); return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; }