X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Fhostnamed.c;h=9e8825da75599e3db0b27421d37910fe424c0fa1;hp=d05c9023c75cd658f8fc52f8e693bc47d7934ba2;hb=ad740100d108282d0244d5739d4dcc86fe4c5fde;hpb=91f9dcaf9270fe465525638cc08bd94590273349 diff --git a/src/hostnamed.c b/src/hostnamed.c index d05c9023c..9e8825da7 100644 --- a/src/hostnamed.c +++ b/src/hostnamed.c @@ -30,6 +30,7 @@ #include "strv.h" #include "dbus-common.h" #include "polkit.h" +#include "def.h" #define INTERFACE \ " \n" \ @@ -85,6 +86,8 @@ static char *data[_PROP_MAX] = { NULL }; +static usec_t remain_until = 0; + static void free_data(void) { int p; @@ -513,19 +516,29 @@ static int connect_bus(DBusConnection **_bus) { bus = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error); if (!bus) { - log_error("Failed to get system D-Bus connection: %s", error.message); + log_error("Failed to get system D-Bus connection: %s", bus_error_message(&error)); r = -ECONNREFUSED; goto fail; } - if (!dbus_connection_register_object_path(bus, "/org/freedesktop/hostname1", &hostname_vtable, NULL)) { + dbus_connection_set_exit_on_disconnect(bus, FALSE); + + if (!dbus_connection_register_object_path(bus, "/org/freedesktop/hostname1", &hostname_vtable, NULL) || + !dbus_connection_add_filter(bus, bus_exit_idle_filter, &remain_until, 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 = dbus_bus_request_name(bus, "org.freedesktop.hostname1", DBUS_NAME_FLAG_DO_NOT_QUEUE, &error); + if (dbus_error_is_set(&error)) { + log_error("Failed to register name on bus: %s", bus_error_message(&error)); + r = -EEXIST; + goto fail; + } + + if (r != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) { + log_error("Failed to acquire name."); r = -EEXIST; goto fail; } @@ -547,11 +560,14 @@ fail: int main(int argc, char *argv[]) { int r; DBusConnection *bus = NULL; + bool exiting = false; log_set_target(LOG_TARGET_AUTO); log_parse_environment(); log_open(); + umask(0022); + if (argc == 2 && streq(argv[1], "--introspect")) { fputs(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE "\n", stdout); @@ -569,8 +585,6 @@ int main(int argc, char *argv[]) { 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(); if (r < 0) { log_error("Failed to read hostname data: %s", strerror(-r)); @@ -581,8 +595,17 @@ int main(int argc, char *argv[]) { if (r < 0) goto finish; - while (dbus_connection_read_write_dispatch(bus, -1)) - ; + remain_until = now(CLOCK_MONOTONIC) + DEFAULT_EXIT_USEC; + for (;;) { + + if (!dbus_connection_read_write_dispatch(bus, exiting ? -1 : (int) (DEFAULT_EXIT_USEC/USEC_PER_MSEC))) + break; + + if (!exiting && remain_until < now(CLOCK_MONOTONIC)) { + exiting = true; + bus_async_unregister_and_exit(bus, "org.freedesktop.hostname1"); + } + } r = 0;