Skip to content

Commit 8f6784f

Browse files
committed
Always create object path that depend upon external ids validating the strings. Fixes #86.
Signed-off-by: Federico Di Pierro <nierro92@gmail.com>
1 parent 664fb26 commit 8f6784f

File tree

4 files changed

+28
-11
lines changed

4 files changed

+28
-11
lines changed

src/modules/backlight2.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <polkit.h>
22
#include <udev.h>
3+
#include <bus_utils.h>
34
#include <math.h>
45
#include <module/map.h>
56
#include <linux/fb.h>
@@ -153,7 +154,7 @@ MODULE("BACKLIGHT2");
153154
d->sn = strdup(id);
154155
d->max = VALREC_MAX_VAL(valrec);
155156
d->cookie = curr_cookie;
156-
snprintf(d->obj_path, sizeof(d->obj_path) - 1, "%s/%s", object_path, d->sn);
157+
make_valid_obj_path(d->obj_path, sizeof(d->obj_path), object_path, d->sn);
157158
int r = sd_bus_add_object_vtable(bus, &d->slot, d->obj_path, bus_interface, vtable, d);
158159
if (r < 0) {
159160
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) {
365366
d->sn = strdup(id);
366367
// Unused. But receive() callback expects brightness value to be cached
367368
udev_device_get_sysattr_value(dev, "brightness");
368-
snprintf(d->obj_path, sizeof(d->obj_path) - 1, "%s/%s", object_path, d->sn);
369+
make_valid_obj_path(d->obj_path, sizeof(d->obj_path), object_path, d->sn);
369370
ret = sd_bus_add_object_vtable(bus, &d->slot, d->obj_path, bus_interface, vtable, d);
370371
if (ret < 0) {
371372
m_log("Failed to add object vtable on path '%s': %d\n", d->obj_path, ret);

src/modules/keyboard.c

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <polkit.h>
22
#include <udev.h>
33
#include <math.h>
4+
#include <bus_utils.h>
45
#include <module/map.h>
56

67
#define KBD_SUBSYSTEM "leds"
@@ -155,15 +156,7 @@ static int kbd_new(struct udev_device *dev, void *userdata) {
155156
k->max = atoi(udev_device_get_sysattr_value(dev, "max_brightness"));
156157
k->sysname = strdup(udev_device_get_sysname(dev));
157158

158-
/*
159-
* Substitute wrong chars, eg: dell::kbd_backlight -> dell__kbd_backlight
160-
* See spec: https://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-marshaling-object-path
161-
*/
162-
snprintf(k->obj_path, sizeof(k->obj_path) - 1, "%s/%s", object_path, k->sysname);
163-
char *ptr = NULL;
164-
while ((ptr = strchr(k->obj_path, ':'))) {
165-
*ptr = '_';
166-
}
159+
make_valid_obj_path(k->obj_path, sizeof(k->obj_path), object_path, k->sysname);
167160

168161
int r = sd_bus_add_object_vtable(bus, &k->slot, k->obj_path, bus_interface, vtable, k);
169162
if (r < 0) {

src/utils/bus_utils.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,24 @@ const char *bus_sender_xauth(void) {
4343
}
4444
return NULL;
4545
}
46+
47+
void make_valid_obj_path(char *storage, size_t size, const char *root, const char *basename) {
48+
/*
49+
* Substitute wrong chars, eg: dell::kbd_backlight -> dell__kbd_backlight
50+
* See spec: https://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-marshaling-object-path
51+
*/
52+
const char *valid_chars = "ABCDEFGHIJKLMNOPQRSTUWXYZabcdefghijklmnopqrstuvwxyz0123456789_";
53+
54+
snprintf(storage, size, "%s/%s", root, basename);
55+
56+
char *path = storage + strlen(root) + 1;
57+
const int full_len = strlen(path);
58+
59+
while (true) {
60+
int len = strspn(path, valid_chars);
61+
if (len == full_len) {
62+
break;
63+
}
64+
path[len] = '_';
65+
}
66+
}

src/utils/bus_utils.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@
33
int bus_sender_fill_creds(sd_bus_message *m);
44
const char *bus_sender_runtime_dir(void);
55
const char *bus_sender_xauth(void);
6+
7+
void make_valid_obj_path(char *storage, size_t size, const char *root, const char *basename);

0 commit comments

Comments
 (0)