chiark / gitweb /
bus: instead of exposing the dbus1 flags when acquiring a name use our own that are...
[elogind.git] / src / locale / localed.c
index 7e03d82575ab0f9cd823b6644c8f4e86e4ece153..65318b6067b6b2a6c8ab29ed37dbe5baed3e0649 100644 (file)
@@ -391,7 +391,7 @@ static int locale_update_system_manager(Context *c, sd_bus *bus) {
 
 static int vconsole_write_data(Context *c) {
         int r;
-        char **l = NULL;
+        _cleanup_strv_free_ char **l = NULL;
 
         r = load_env_file("/etc/vconsole.conf", NULL, &l);
         if (r < 0 && r != -ENOENT)
@@ -403,10 +403,8 @@ static int vconsole_write_data(Context *c) {
                 char *s, **u;
 
                 s = strappend("KEYMAP=", c->vc_keymap);
-                if (!s) {
-                        strv_free(l);
+                if (!s)
                         return -ENOMEM;
-                }
 
                 u = strv_env_set(l, s);
                 free(s);
@@ -424,10 +422,8 @@ static int vconsole_write_data(Context *c) {
                 char *s, **u;
 
                 s = strappend("KEYMAP_TOGGLE=", c->vc_keymap_toggle);
-                if (!s) {
-                        strv_free(l);
+                if (!s)
                         return -ENOMEM;
-                }
 
                 u = strv_env_set(l, s);
                 free(s);
@@ -440,8 +436,6 @@ static int vconsole_write_data(Context *c) {
         }
 
         if (strv_isempty(l)) {
-                strv_free(l);
-
                 if (unlink("/etc/vconsole.conf") < 0)
                         return errno == ENOENT ? 0 : -errno;
 
@@ -449,14 +443,12 @@ static int vconsole_write_data(Context *c) {
         }
 
         r = write_env_file_label("/etc/vconsole.conf", l);
-        strv_free(l);
-
         return r;
 }
 
 static int write_data_x11(Context *c) {
-        FILE *f;
-        char *temp_path;
+        _cleanup_fclose_ FILE *f = NULL;
+        _cleanup_free_ char *temp_path = NULL;
         int r;
 
         if (isempty(c->x11_layout) &&
@@ -503,13 +495,9 @@ static int write_data_x11(Context *c) {
                 r = -errno;
                 unlink("/etc/X11/xorg.conf.d/00-keyboard.conf");
                 unlink(temp_path);
+                return r;
         } else
-                r = 0;
-
-        fclose(f);
-        free(temp_path);
-
-        return r;
+                return 0;
 }
 
 static int vconsole_reload(sd_bus *bus) {
@@ -591,7 +579,7 @@ static int vconsole_convert_to_x11(Context *c, sd_bus *bus) {
 
                 context_free_x11(c);
         } else {
-                FILE *f;
+                _cleanup_fclose_ FILE *f = NULL;
                 unsigned n = 0;
 
                 f = fopen(SYSTEMD_KBD_MODEL_MAP, "re");
@@ -599,22 +587,17 @@ static int vconsole_convert_to_x11(Context *c, sd_bus *bus) {
                         return -errno;
 
                 for (;;) {
-                        char **a;
+                        _cleanup_strv_free_ char **a = NULL;
                         int r;
 
                         r = read_next_mapping(f, &n, &a);
-                        if (r < 0) {
-                                fclose(f);
+                        if (r < 0)
                                 return r;
-                        }
-
                         if (r == 0)
                                 break;
 
-                        if (!streq(c->vc_keymap, a[0])) {
-                                strv_free(a);
+                        if (!streq(c->vc_keymap, a[0]))
                                 continue;
-                        }
 
                         if (!streq_ptr(c->x11_layout, strnulldash(a[1])) ||
                             !streq_ptr(c->x11_model, strnulldash(a[2])) ||
@@ -624,20 +607,14 @@ static int vconsole_convert_to_x11(Context *c, sd_bus *bus) {
                                 if (free_and_copy(&c->x11_layout, strnulldash(a[1])) < 0 ||
                                     free_and_copy(&c->x11_model, strnulldash(a[2])) < 0 ||
                                     free_and_copy(&c->x11_variant, strnulldash(a[3])) < 0 ||
-                                    free_and_copy(&c->x11_options, strnulldash(a[4])) < 0) {
-                                        strv_free(a);
-                                        fclose(f);
+                                    free_and_copy(&c->x11_options, strnulldash(a[4])) < 0)
                                         return -ENOMEM;
-                                }
 
                                 modified = true;
                         }
 
-                        strv_free(a);
                         break;
                 }
-
-                fclose(f);
         }
 
         if (modified) {
@@ -656,8 +633,114 @@ static int vconsole_convert_to_x11(Context *c, sd_bus *bus) {
         return 0;
 }
 
+static int find_converted_keymap(Context *c, char **new_keymap) {
+        const char *dir;
+        _cleanup_free_ char *n;
+
+        if (c->x11_variant)
+                n = strjoin(c->x11_layout, "-", c->x11_variant, NULL);
+        else
+                n = strdup(c->x11_layout);
+        if (!n)
+                return -ENOMEM;
+
+        NULSTR_FOREACH(dir, KBD_KEYMAP_DIRS) {
+                _cleanup_free_ char *p = NULL, *pz = NULL;
+
+                p = strjoin(dir, "xkb/", n, ".map", NULL);
+                pz = strjoin(dir, "xkb/", n, ".map.gz", NULL);
+                if (!p || !pz)
+                        return -ENOMEM;
+
+                if (access(p, F_OK) == 0 || access(pz, F_OK) == 0) {
+                        *new_keymap = n;
+                        n = NULL;
+                        return 1;
+                }
+        }
+
+        return 0;
+}
+
+static int find_legacy_keymap(Context *c, char **new_keymap) {
+        _cleanup_fclose_ FILE *f;
+        unsigned n = 0;
+        unsigned best_matching = 0;
+
+
+        f = fopen(SYSTEMD_KBD_MODEL_MAP, "re");
+        if (!f)
+                return -errno;
+
+        for (;;) {
+                _cleanup_strv_free_ char **a = NULL;
+                unsigned matching = 0;
+                int r;
+
+                r = read_next_mapping(f, &n, &a);
+                if (r < 0)
+                        return r;
+                if (r == 0)
+                        break;
+
+                /* Determine how well matching this entry is */
+                if (streq_ptr(c->x11_layout, a[1]))
+                        /* If we got an exact match, this is best */
+                        matching = 10;
+                else {
+                        size_t x;
+
+                        x = strcspn(c->x11_layout, ",");
+
+                        /* We have multiple X layouts, look for an
+                         * entry that matches our key with everything
+                         * but the first layout stripped off. */
+                        if (x > 0 &&
+                            strlen(a[1]) == x &&
+                            strneq(c->x11_layout, a[1], x))
+                                matching = 5;
+                        else  {
+                                size_t w;
+
+                                /* If that didn't work, strip off the
+                                 * other layouts from the entry, too */
+                                w = strcspn(a[1], ",");
+
+                                if (x > 0 && x == w &&
+                                    memcmp(c->x11_layout, a[1], x) == 0)
+                                        matching = 1;
+                        }
+                }
+
+                if (matching > 0 &&
+                    streq_ptr(c->x11_model, a[2])) {
+                        matching++;
+
+                        if (streq_ptr(c->x11_variant, a[3])) {
+                                matching++;
+
+                                if (streq_ptr(c->x11_options, a[4]))
+                                        matching++;
+                        }
+                }
+
+                /* The best matching entry so far, then let's save that */
+                if (matching > best_matching) {
+                        best_matching = matching;
+
+                        free(*new_keymap);
+                        *new_keymap = strdup(a[0]);
+                        if (!*new_keymap)
+                                return -ENOMEM;
+                }
+        }
+
+        return 0;
+}
+
 static int x11_convert_to_vconsole(Context *c, sd_bus *bus) {
         bool modified = false;
+        int r;
 
         assert(bus);
 
@@ -669,79 +752,15 @@ static int x11_convert_to_vconsole(Context *c, sd_bus *bus) {
 
                 context_free_x11(c);
         } else {
-                _cleanup_fclose_ FILE *f;
-                unsigned n = 0;
-                unsigned best_matching = 0;
                 char *new_keymap = NULL;
 
-                f = fopen(SYSTEMD_KBD_MODEL_MAP, "re");
-                if (!f)
-                        return -errno;
-
-                for (;;) {
-                        _cleanup_strv_free_ char **a = NULL;
-                        unsigned matching = 0;
-                        int r;
-
-                        r = read_next_mapping(f, &n, &a);
+                r = find_converted_keymap(c, &new_keymap);
+                if (r < 0)
+                        return r;
+                else if (r == 0) {
+                        r = find_legacy_keymap(c, &new_keymap);
                         if (r < 0)
                                 return r;
-                        if (r == 0)
-                                break;
-
-                        /* Determine how well matching this entry is */
-                        if (streq_ptr(c->x11_layout, a[1]))
-                                /* If we got an exact match, this is best */
-                                matching = 10;
-                        else {
-                                size_t x;
-
-                                x = strcspn(c->x11_layout, ",");
-
-                                /* We have multiple X layouts, look
-                                 * for an entry that matches our key
-                                 * with the everything but the first
-                                 * layout stripped off. */
-                                if (x > 0 &&
-                                    strlen(a[1]) == x &&
-                                    strneq(c->x11_layout, a[1], x))
-                                        matching = 5;
-                                else  {
-                                        size_t w;
-
-                                        /* If that didn't work, strip
-                                         * off the other layouts from
-                                         * the entry, too */
-                                        w = strcspn(a[1], ",");
-
-                                        if (x > 0 && x == w &&
-                                            memcmp(c->x11_layout, a[1], x) == 0)
-                                                matching = 1;
-                                }
-                        }
-
-                        if (matching > 0 &&
-                            streq_ptr(c->x11_model, a[2])) {
-                                matching++;
-
-                                if (streq_ptr(c->x11_variant, a[3])) {
-                                        matching++;
-
-                                        if (streq_ptr(c->x11_options, a[4]))
-                                                matching++;
-                                }
-                        }
-
-                        /* The best matching entry so far, then let's
-                         * save that */
-                        if (matching > best_matching) {
-                                best_matching = matching;
-
-                                free(new_keymap);
-                                new_keymap = strdup(a[0]);
-                                if (!new_keymap)
-                                        return -ENOMEM;
-                        }
                 }
 
                 if (!streq_ptr(c->vc_keymap, new_keymap)) {
@@ -753,8 +772,6 @@ static int x11_convert_to_vconsole(Context *c, sd_bus *bus) {
         }
 
         if (modified) {
-                int r;
-
                 r = vconsole_write_data(c);
                 if (r < 0)
                         log_error("Failed to set virtual console keymap: %s", strerror(-r));
@@ -770,8 +787,15 @@ static int x11_convert_to_vconsole(Context *c, sd_bus *bus) {
         return 0;
 }
 
-static int property_get_locale(sd_bus *bus, const char *path, const char *interface,
-                               const char *property, sd_bus_message *reply, sd_bus_error *error, void *userdata) {
+static int property_get_locale(
+                sd_bus *bus,
+                const char *path,
+                const char *interface,
+                const char *property,
+                sd_bus_message *reply,
+                void *userdata,
+                sd_bus_error *error) {
+
         Context *c = userdata;
         _cleanup_strv_free_ char **l = NULL;
         int p, q;
@@ -795,9 +819,8 @@ static int property_get_locale(sd_bus *bus, const char *path, const char *interf
         return sd_bus_message_append_strv(reply, l);
 }
 
-static int method_set_locale(sd_bus *bus, sd_bus_message *m, void *userdata) {
+static int method_set_locale(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
         Context *c = userdata;
-        _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
         _cleanup_strv_free_ char **l = NULL;
         char **i;
         int interactive;
@@ -808,11 +831,11 @@ static int method_set_locale(sd_bus *bus, sd_bus_message *m, void *userdata) {
 
         r = bus_message_read_strv_extend(m, &l);
         if (r < 0)
-                return sd_bus_reply_method_errno(bus, m, r, NULL);
+                return r;
 
         r = sd_bus_message_read_basic(m, 'b', &interactive);
         if (r < 0)
-                return sd_bus_reply_method_errno(bus, m, r, NULL);
+                return r;
 
         /* Check whether a variable changed and if so valid */
         STRV_FOREACH(i, l) {
@@ -836,7 +859,7 @@ static int method_set_locale(sd_bus *bus, sd_bus_message *m, void *userdata) {
                 }
 
                 if (!valid)
-                        sd_bus_reply_method_errorf(bus, m, SD_BUS_ERROR_INVALID_ARGS, "Invalid Locale data.");
+                        return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid Locale data.");
         }
 
         /* Check whether a variable is unset */
@@ -851,9 +874,9 @@ static int method_set_locale(sd_bus *bus, sd_bus_message *m, void *userdata) {
         if (modified) {
                 r = bus_verify_polkit_async(bus, &c->polkit_registry, m,
                                             "org.freedesktop.locale1.set-locale", interactive,
-                                            &error, method_set_locale, c);
+                                            error, method_set_locale, c);
                 if (r < 0)
-                        return sd_bus_reply_method_errno(bus, m, r, &error);
+                        return r;
                 if (r == 0)
                         return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
 
@@ -888,7 +911,7 @@ static int method_set_locale(sd_bus *bus, sd_bus_message *m, void *userdata) {
                 r = locale_write_data(c);
                 if (r < 0) {
                         log_error("Failed to set locale: %s", strerror(-r));
-                        return sd_bus_reply_method_errnof(bus, m, r, "Failed to set locale: %s", strerror(-r));
+                        return sd_bus_error_set_errnof(error, r, "Failed to set locale: %s", strerror(-r));
                 }
 
                 locale_update_system_manager(c, bus);
@@ -901,19 +924,18 @@ static int method_set_locale(sd_bus *bus, sd_bus_message *m, void *userdata) {
                                 "Locale", NULL);
         }
 
-        return sd_bus_reply_method_return(bus, m, NULL);
+        return sd_bus_reply_method_return(m, NULL);
 }
 
-static int method_set_vc_keyboard(sd_bus *bus, sd_bus_message *m, void *userdata) {
+static int method_set_vc_keyboard(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
         Context *c = userdata;
-        _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
         const char *keymap, *keymap_toggle;
         int convert, interactive;
         int r;
 
         r = sd_bus_message_read(m, "ssbb", &keymap, &keymap_toggle, &convert, &interactive);
         if (r < 0)
-                return sd_bus_reply_method_errno(bus, m, r, NULL);
+                return r;
 
         if (isempty(keymap))
                 keymap = NULL;
@@ -926,13 +948,13 @@ static int method_set_vc_keyboard(sd_bus *bus, sd_bus_message *m, void *userdata
 
                 if ((keymap && (!filename_is_safe(keymap) || !string_is_safe(keymap))) ||
                     (keymap_toggle && (!filename_is_safe(keymap_toggle) || !string_is_safe(keymap_toggle))))
-                        return sd_bus_reply_method_errnof(bus, m, r, "Received invalid keymap data: %s", -EINVAL);
+                        return sd_bus_error_set_errnof(error, r, "Received invalid keymap data: %s", -EINVAL);
 
                 r = bus_verify_polkit_async(bus, &c->polkit_registry, m,
                                 "org.freedesktop.locale1.set-keyboard",
-                                interactive, &error, method_set_vc_keyboard, c);
+                                interactive, error, method_set_vc_keyboard, c);
                 if (r < 0)
-                        return sd_bus_reply_method_errno(bus, m, r, &error);
+                        return r;
                 if (r == 0)
                         return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
 
@@ -943,7 +965,7 @@ static int method_set_vc_keyboard(sd_bus *bus, sd_bus_message *m, void *userdata
                 r = vconsole_write_data(c);
                 if (r < 0) {
                         log_error("Failed to set virtual console keymap: %s", strerror(-r));
-                        return sd_bus_reply_method_errnof(bus, m, r, "Failed to set virtual console keymap: %s", strerror(-r));
+                        return sd_bus_error_set_errnof(error, r, "Failed to set virtual console keymap: %s", strerror(-r));
                 }
 
                 log_info("Changed virtual console keymap to '%s'", strempty(c->vc_keymap));
@@ -964,19 +986,18 @@ static int method_set_vc_keyboard(sd_bus *bus, sd_bus_message *m, void *userdata
                 }
         }
 
-        return sd_bus_reply_method_return(bus, m, NULL);
+        return sd_bus_reply_method_return(m, NULL);
 }
 
-static int method_set_x11_keyboard(sd_bus *bus, sd_bus_message *m, void *userdata) {
+static int method_set_x11_keyboard(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
         Context *c = userdata;
-        _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
         const char *layout, *model, *variant, *options;
         int convert, interactive;
         int r;
 
         r = sd_bus_message_read(m, "ssssbb", &layout, &model, &variant, &options, &convert, &interactive);
         if (r < 0)
-                return sd_bus_reply_method_errno(bus, m, r, NULL);
+                return r;
 
         if (isempty(layout))
                 layout = NULL;
@@ -999,13 +1020,13 @@ static int method_set_x11_keyboard(sd_bus *bus, sd_bus_message *m, void *userdat
                     (model && !string_is_safe(model)) ||
                     (variant && !string_is_safe(variant)) ||
                     (options && !string_is_safe(options)))
-                        return sd_bus_reply_method_errnof(bus, m, r, "Received invalid keyboard data: %s", -EINVAL);
+                        return sd_bus_error_set_errnof(error, r, "Received invalid keyboard data: %s", -EINVAL);
 
                 r = bus_verify_polkit_async(bus, &c->polkit_registry, m,
                                 "org.freedesktop.locale1.set-keyboard",
-                                interactive, &error, method_set_x11_keyboard, c);
+                                interactive, error, method_set_x11_keyboard, c);
                 if (r < 0)
-                        return sd_bus_reply_method_errno(bus, m, r, &error);
+                        return r;
                 if (r == 0)
                         return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
 
@@ -1018,7 +1039,7 @@ static int method_set_x11_keyboard(sd_bus *bus, sd_bus_message *m, void *userdat
                 r = write_data_x11(c);
                 if (r < 0) {
                         log_error("Failed to set X11 keyboard layout: %s", strerror(-r));
-                        return sd_bus_reply_method_errnof(bus, m, r, "Failed to set X11 keyboard layout: %s", strerror(-r));
+                        return sd_bus_error_set_errnof(error, r, "Failed to set X11 keyboard layout: %s", strerror(-r));
                 }
 
                 log_info("Changed X11 keyboard layout to '%s'", strempty(c->x11_layout));
@@ -1035,7 +1056,7 @@ static int method_set_x11_keyboard(sd_bus *bus, sd_bus_message *m, void *userdat
                 }
         }
 
-        return sd_bus_reply_method_return(bus, m, NULL);
+        return sd_bus_reply_method_return(m, NULL);
 }
 
 static const sd_bus_vtable locale_vtable[] = {
@@ -1047,9 +1068,9 @@ static const sd_bus_vtable locale_vtable[] = {
         SD_BUS_PROPERTY("X11Options", "s", NULL, offsetof(Context, x11_options), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
         SD_BUS_PROPERTY("VConsoleKeymap", "s", NULL, offsetof(Context, vc_keymap), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
         SD_BUS_PROPERTY("VConsoleKeymapToggle", "s", NULL, offsetof(Context, vc_keymap_toggle), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
-        SD_BUS_METHOD("SetLocale", "asb", NULL, method_set_locale, 0),
-        SD_BUS_METHOD("SetVConsoleKeyboard", "ssbb", NULL, method_set_vc_keyboard, 0),
-        SD_BUS_METHOD("SetX11Keyboard", "ssssbb", NULL, method_set_x11_keyboard, 0),
+        SD_BUS_METHOD("SetLocale", "asb", NULL, method_set_locale, SD_BUS_VTABLE_UNPRIVILEGED),
+        SD_BUS_METHOD("SetVConsoleKeyboard", "ssbb", NULL, method_set_vc_keyboard, SD_BUS_VTABLE_UNPRIVILEGED),
+        SD_BUS_METHOD("SetX11Keyboard", "ssssbb", NULL, method_set_x11_keyboard, SD_BUS_VTABLE_UNPRIVILEGED),
         SD_BUS_VTABLE_END
 };
 
@@ -1061,7 +1082,7 @@ static int connect_bus(Context *c, sd_event *event, sd_bus **_bus) {
         assert(event);
         assert(_bus);
 
-        r = sd_bus_open_system(&bus);
+        r = sd_bus_default_system(&bus);
         if (r < 0) {
                 log_error("Failed to get system bus connection: %s", strerror(-r));
                 return r;
@@ -1073,17 +1094,12 @@ static int connect_bus(Context *c, sd_event *event, sd_bus **_bus) {
                 return r;
         }
 
-        r = sd_bus_request_name(bus, "org.freedesktop.locale1", SD_BUS_NAME_DO_NOT_QUEUE);
+        r = sd_bus_request_name(bus, "org.freedesktop.locale1", 0);
         if (r < 0) {
                 log_error("Failed to register name: %s", strerror(-r));
                 return r;
         }
 
-        if (r != SD_BUS_NAME_PRIMARY_OWNER) {
-                log_error("Failed to acquire name.");
-                return -EEXIST;
-        }
-
         r = sd_bus_attach_event(bus, event, 0);
         if (r < 0) {
                 log_error("Failed to attach bus to event loop: %s", strerror(-r));
@@ -1115,12 +1131,14 @@ int main(int argc, char *argv[]) {
                 goto finish;
         }
 
-        r = sd_event_new(&event);
+        r = sd_event_default(&event);
         if (r < 0) {
                 log_error("Failed to allocate event loop: %s", strerror(-r));
                 goto finish;
         }
 
+        sd_event_set_watchdog(event, true);
+
         r = connect_bus(&context, event, &bus);
         if (r < 0)
                 goto finish;