Skip to content

Commit

Permalink
Always create object path that depend upon external ids validating th…
Browse files Browse the repository at this point in the history
…e strings. Fixes #86.

Signed-off-by: Federico Di Pierro <[email protected]>
  • Loading branch information
FedeDP committed Feb 5, 2022
1 parent 664fb26 commit 8f6784f
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 11 deletions.
5 changes: 3 additions & 2 deletions src/modules/backlight2.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <polkit.h>
#include <udev.h>
#include <bus_utils.h>
#include <math.h>
#include <module/map.h>
#include <linux/fb.h>
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
11 changes: 2 additions & 9 deletions src/modules/keyboard.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <polkit.h>
#include <udev.h>
#include <math.h>
#include <bus_utils.h>
#include <module/map.h>

#define KBD_SUBSYSTEM "leds"
Expand Down Expand Up @@ -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) {
Expand Down
21 changes: 21 additions & 0 deletions src/utils/bus_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -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] = '_';
}
}
2 changes: 2 additions & 0 deletions src/utils/bus_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

0 comments on commit 8f6784f

Please sign in to comment.