X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fhostnamed.c;h=68c5715b0e1cc1ed7ab6df2cd510a7407c844d6b;hb=34ca941cec76bbfdfd02c705b76bc1b53ea2bcd1;hp=4ba1d4a57014a05ad882a9a56283db1f012063f8;hpb=7640a5de1b3ffe6547200ad204d14e4f067caf4f;p=elogind.git diff --git a/src/hostnamed.c b/src/hostnamed.c index 4ba1d4a57..68c5715b0 100644 --- a/src/hostnamed.c +++ b/src/hostnamed.c @@ -3,7 +3,7 @@ /*** This file is part of systemd. - Copyright 2010 Lennart Poettering + Copyright 2011 Lennart Poettering systemd is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -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__) @@ -134,7 +147,13 @@ static const char* fallback_icon_name(void) { /* We only list the really obvious cases here. The DMI data is unreliable enough, so let's not do any additional guesswork - on top of that. */ + on top of that. + + See the SMBIOS Specification 2.7.1 section 7.4.1 for + details about the values listed here: + + http://www.dmtf.org/sites/default/files/standards/documents/DSP0134_2.7.1.pdf + */ switch (t) { @@ -150,6 +169,7 @@ static const char* fallback_icon_name(void) { return "computer-laptop"; case 0x11: + case 0x1C: return "computer-server"; } @@ -181,10 +201,11 @@ static int write_data_static_hostname(void) { return 0; } - return write_one_line_file("/etc/hostname", data[PROP_STATIC_HOSTNAME]); + return write_one_line_file_atomic("/etc/hostname", data[PROP_STATIC_HOSTNAME]); } static int write_data_other(void) { + static const char * const name[_PROP_MAX] = { [PROP_PRETTY_HOSTNAME] = "PRETTY_HOSTNAME", [PROP_ICON_NAME] = "ICON_NAME" @@ -469,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", @@ -521,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", @@ -556,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); @@ -575,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", @@ -622,17 +655,55 @@ oom: return DBUS_HANDLER_RESULT_NEED_MEMORY; } -int main(int argc, char *argv[]) { - const DBusObjectPathVTable hostname_vtable = { +static int connect_bus(DBusConnection **_bus) { + static const DBusObjectPathVTable hostname_vtable = { .message_function = hostname_message_handler }; - - DBusConnection *bus = NULL; DBusError error; + DBusConnection *bus = NULL; int r; + assert(_bus); + dbus_error_init(&error); + bus = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error); + if (!bus) { + log_error("Failed to get system D-Bus connection: %s", error.message); + r = -ECONNREFUSED; + goto fail; + } + + if (!dbus_connection_register_object_path(bus, "/org/freedesktop/hostname1", &hostname_vtable, NULL)) { + log_error("Not enough memory"); + r = -ENOMEM; + goto fail; + } + + 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 fail; + } + + if (_bus) + *_bus = bus; + + return 0; + +fail: + dbus_connection_close(bus); + dbus_connection_unref(bus); + + dbus_error_free(&error); + + return r; +} + +int main(int argc, char *argv[]) { + int r; + DBusConnection *bus = NULL; + log_set_target(LOG_TARGET_AUTO); log_parse_environment(); log_open(); @@ -643,6 +714,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(); @@ -651,23 +725,9 @@ int main(int argc, char *argv[]) { goto finish; } - bus = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error); - if (!bus) { - log_error("Failed to get system D-Bus connection: %s", error.message); - r = -ECONNREFUSED; - goto finish; - } - - if (!dbus_connection_register_object_path(bus, "/org/freedesktop/hostname1", &hostname_vtable, NULL)) { - log_error("Not enough memory"); - r = -ENOMEM; - goto finish; - } - - 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 = connect_bus(&bus); + if (r < 0) goto finish; - } while (dbus_connection_read_write_dispatch(bus, -1)) ; @@ -683,7 +743,5 @@ finish: dbus_connection_unref(bus); } - dbus_error_free(&error); - return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; }