chiark / gitweb /
dbus: terminate mechanism daemons after a while
[elogind.git] / src / localed.c
index 5efee0b2ac99b747c786906c34b9f19dadedd6c1..f652110db49d3dec46ed5123495e7db6e8661b2d 100644 (file)
@@ -29,6 +29,7 @@
 #include "strv.h"
 #include "dbus-common.h"
 #include "polkit.h"
+#include "def.h"
 
 #define INTERFACE                                                       \
         " <interface name=\"org.freedesktop.locale1\">\n"               \
@@ -59,6 +60,7 @@ enum {
          * using LANG instead. */
 
         PROP_LANG,
+        PROP_LANGUAGE,
         PROP_LC_CTYPE,
         PROP_LC_NUMERIC,
         PROP_LC_TIME,
@@ -76,6 +78,7 @@ enum {
 
 static const char * const names[_PROP_MAX] = {
         [PROP_LANG] = "LANG",
+        [PROP_LANGUAGE] = "LANGUAGE",
         [PROP_LC_CTYPE] = "LC_CTYPE",
         [PROP_LC_NUMERIC] = "LC_NUMERIC",
         [PROP_LC_TIME] = "LC_TIME",
@@ -106,6 +109,8 @@ static char *data[_PROP_MAX] = {
         NULL
 };
 
+static usec_t remain_until = 0;
+
 static void free_data(void) {
         int p;
 
@@ -132,6 +137,7 @@ static int read_data(void) {
 
         r = parse_env_file("/etc/locale.conf", NEWLINE,
                            "LANG",              &data[PROP_LANG],
+                           "LANGUAGE",          &data[PROP_LANGUAGE],
                            "LC_CTYPE",          &data[PROP_LC_CTYPE],
                            "LC_NUMERIC",        &data[PROP_LC_NUMERIC],
                            "LC_TIME",           &data[PROP_LC_TIME],
@@ -534,14 +540,24 @@ static int connect_bus(DBusConnection **_bus) {
                 goto fail;
         }
 
-        if (!dbus_connection_register_object_path(bus, "/org/freedesktop/locale1", &locale_vtable, NULL)) {
+        dbus_connection_set_exit_on_disconnect(bus, FALSE);
+
+        if (!dbus_connection_register_object_path(bus, "/org/freedesktop/locale1", &locale_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.locale1", 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.locale1", 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;
         }
@@ -563,11 +579,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
                       "<node>\n", stdout);
@@ -582,8 +601,6 @@ int main(int argc, char *argv[]) {
                 goto finish;
         }
 
-        umask(0022);
-
         r = read_data();
         if (r < 0) {
                 log_error("Failed to read locale data: %s", strerror(-r));
@@ -594,8 +611,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.locale1");
+                }
+        }
 
         r = 0;