diff --git a/src/modules/backlight2.c b/src/modules/backlight2.c index ffae2fd..157dd3e 100644 --- a/src/modules/backlight2.c +++ b/src/modules/backlight2.c @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -153,7 +154,7 @@ MODULE("BACKLIGHT2"); d->sn = strdup(id); d->max = VALREC_MAX_VAL(valrec); d->cookie = curr_cookie; - snprintf(d->obj_path, sizeof(d->obj_path) - 1, "%s/%s", object_path, d->sn); + make_valid_obj_path(d->obj_path, sizeof(d->obj_path), object_path, d->sn); int r = sd_bus_add_object_vtable(bus, &d->slot, d->obj_path, bus_interface, vtable, d); if (r < 0) { m_log("Failed to add object vtable on path '%s': %d\n", d->obj_path, r); @@ -365,7 +366,7 @@ static int store_internal_device(struct udev_device *dev, void *userdata) { d->sn = strdup(id); // Unused. But receive() callback expects brightness value to be cached udev_device_get_sysattr_value(dev, "brightness"); - snprintf(d->obj_path, sizeof(d->obj_path) - 1, "%s/%s", object_path, d->sn); + make_valid_obj_path(d->obj_path, sizeof(d->obj_path), object_path, d->sn); ret = sd_bus_add_object_vtable(bus, &d->slot, d->obj_path, bus_interface, vtable, d); if (ret < 0) { m_log("Failed to add object vtable on path '%s': %d\n", d->obj_path, ret); diff --git a/src/modules/keyboard.c b/src/modules/keyboard.c index 3e25fdf..df8986e 100644 --- a/src/modules/keyboard.c +++ b/src/modules/keyboard.c @@ -1,6 +1,7 @@ #include #include #include +#include #include #define KBD_SUBSYSTEM "leds" @@ -155,15 +156,7 @@ static int kbd_new(struct udev_device *dev, void *userdata) { k->max = atoi(udev_device_get_sysattr_value(dev, "max_brightness")); k->sysname = strdup(udev_device_get_sysname(dev)); - /* - * Substitute wrong chars, eg: dell::kbd_backlight -> dell__kbd_backlight - * See spec: https://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-marshaling-object-path - */ - snprintf(k->obj_path, sizeof(k->obj_path) - 1, "%s/%s", object_path, k->sysname); - char *ptr = NULL; - while ((ptr = strchr(k->obj_path, ':'))) { - *ptr = '_'; - } + make_valid_obj_path(k->obj_path, sizeof(k->obj_path), object_path, k->sysname); int r = sd_bus_add_object_vtable(bus, &k->slot, k->obj_path, bus_interface, vtable, k); if (r < 0) { diff --git a/src/utils/bus_utils.c b/src/utils/bus_utils.c index 1575ad6..fdb6a34 100644 --- a/src/utils/bus_utils.c +++ b/src/utils/bus_utils.c @@ -43,3 +43,24 @@ const char *bus_sender_xauth(void) { } return NULL; } + +void make_valid_obj_path(char *storage, size_t size, const char *root, const char *basename) { + /* + * Substitute wrong chars, eg: dell::kbd_backlight -> dell__kbd_backlight + * See spec: https://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-marshaling-object-path + */ + const char *valid_chars = "ABCDEFGHIJKLMNOPQRSTUWXYZabcdefghijklmnopqrstuvwxyz0123456789_"; + + snprintf(storage, size, "%s/%s", root, basename); + + char *path = storage + strlen(root) + 1; + const int full_len = strlen(path); + + while (true) { + int len = strspn(path, valid_chars); + if (len == full_len) { + break; + } + path[len] = '_'; + } +} diff --git a/src/utils/bus_utils.h b/src/utils/bus_utils.h index ecd7268..b6027b0 100644 --- a/src/utils/bus_utils.h +++ b/src/utils/bus_utils.h @@ -3,3 +3,5 @@ int bus_sender_fill_creds(sd_bus_message *m); const char *bus_sender_runtime_dir(void); const char *bus_sender_xauth(void); + +void make_valid_obj_path(char *storage, size_t size, const char *root, const char *basename);