X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fhostnamed.c;h=ce690453445ba7db8c8605cf483aa7733ce051c7;hb=572481b9094f7f1fbba69f1bd0c8f7db31550b01;hp=d7e553e6d139da00ed9d99ac8467570976c9e7ab;hpb=41550d40a271e85f632cfcef8d6e972d8f5b0bf7;p=elogind.git diff --git a/src/hostnamed.c b/src/hostnamed.c index d7e553e6d..ce6904534 100644 --- a/src/hostnamed.c +++ b/src/hostnamed.c @@ -24,6 +24,7 @@ #include #include #include +#include #include "util.h" #include "strv.h" @@ -110,6 +111,18 @@ static int read_data(void) { return 0; } +static bool check_nss(void) { + + void *dl; + + if ((dl = dlopen("libnss_myhostname.so.2", RTLD_LAZY))) { + dlclose(dl); + return true; + } + + return false; +} + static const char* fallback_icon_name(void) { #if defined(__i386__) || defined(__x86_64__) @@ -192,6 +205,7 @@ static int write_data_static_hostname(void) { } static int write_data_other(void) { + static const char * const name[_PROP_MAX] = { [PROP_PRETTY_HOSTNAME] = "PRETTY_HOSTNAME", [PROP_ICON_NAME] = "ICON_NAME" @@ -476,10 +490,12 @@ static DBusHandlerResult hostname_message_handler( data[PROP_HOSTNAME] = h; r = write_data_hostname(); - if (r < 0) + if (r < 0) { + log_error("Failed to set host name: %s", strerror(-r)); return bus_send_error_reply(connection, message, NULL, r); + } - log_info("Changed host name to '%s'", data[PROP_HOSTNAME]); + log_info("Changed host name to '%s'", strempty(data[PROP_HOSTNAME])); changed = bus_properties_changed_new( "/org/freedesktop/hostname1", @@ -528,10 +544,12 @@ static DBusHandlerResult hostname_message_handler( } r = write_data_static_hostname(); - if (r < 0) + if (r < 0) { + log_error("Failed to write static host name: %s", strerror(-r)); return bus_send_error_reply(connection, message, NULL, r); + } - log_info("Changed static host name to '%s'", data[PROP_HOSTNAME]); + log_info("Changed static host name to '%s'", strempty(data[PROP_HOSTNAME])); changed = bus_properties_changed_new( "/org/freedesktop/hostname1", @@ -563,7 +581,13 @@ static DBusHandlerResult hostname_message_handler( if (!streq_ptr(name, data[k])) { - r = verify_polkit(connection, message, "org.freedesktop.hostname1.set-machine-info", interactive, &error); + /* Since the pretty hostname should always be + * changed at the same time as the static one, + * use the same policy action for both... */ + + r = verify_polkit(connection, message, k == PROP_PRETTY_HOSTNAME ? + "org.freedesktop.hostname1.set-static-hostname" : + "org.freedesktop.hostname1.set-machine-info", interactive, &error); if (r < 0) return bus_send_error_reply(connection, message, &error, r); @@ -582,10 +606,12 @@ static DBusHandlerResult hostname_message_handler( } r = write_data_other(); - if (r < 0) + if (r < 0) { + log_error("Failed to write machine info: %s", strerror(-r)); return bus_send_error_reply(connection, message, NULL, r); + } - log_info("Changed %s to '%s'", k == PROP_PRETTY_HOSTNAME ? "pretty host name" : "icon name", data[k]); + log_info("Changed %s to '%s'", k == PROP_PRETTY_HOSTNAME ? "pretty host name" : "icon name", strempty(data[k])); changed = bus_properties_changed_new( "/org/freedesktop/hostname1", @@ -650,6 +676,9 @@ int main(int argc, char *argv[]) { goto finish; } + if (!check_nss()) + log_warning("Warning: nss-myhostname is not installed. Changing the local hostname might make it unresolveable. Please install nss-myhostname!"); + umask(0022); r = read_data(); @@ -673,6 +702,7 @@ int main(int argc, char *argv[]) { if (dbus_bus_request_name(bus, "org.freedesktop.hostname1", DBUS_NAME_FLAG_DO_NOT_QUEUE, &error) < 0) { log_error("Failed to register name on bus: %s", error.message); + r = -EEXIST; goto finish; }