diff options
-rw-r--r-- | src/libudev/libudev-device.c | 45 | ||||
-rw-r--r-- | src/libudev/libudev-monitor.c | 34 | ||||
-rw-r--r-- | src/libudev/libudev-private.h | 1 |
3 files changed, 53 insertions, 27 deletions
diff --git a/src/libudev/libudev-device.c b/src/libudev/libudev-device.c index 24c8d3b02c..32c80cc9ec 100644 --- a/src/libudev/libudev-device.c +++ b/src/libudev/libudev-device.c @@ -1943,3 +1943,48 @@ struct udev_device *udev_device_shallow_clone(struct udev_device *old_device) return device; } + +struct udev_device *udev_device_new_from_nulstr(struct udev *udev, char *nulstr, ssize_t buflen) { + struct udev_device *device; + ssize_t bufpos = 0; + + if (nulstr == NULL || buflen <= 0) { + errno = EINVAL; + + return NULL; + } + + device = udev_device_new(udev); + if (!device) { + errno = ENOMEM; + + return NULL; + } + + udev_device_set_info_loaded(device); + + while (bufpos < buflen) { + char *key; + size_t keylen; + + key = nulstr + bufpos; + keylen = strlen(key); + if (keylen == 0) + break; + + bufpos += keylen + 1; + udev_device_add_property_from_string_parse(device, key); + } + + if (udev_device_add_property_from_string_parse_finish(device) < 0) { + log_debug("missing values, invalid device"); + + udev_device_unref(device); + + errno = EINVAL; + + return NULL; + } + + return device; +} diff --git a/src/libudev/libudev-monitor.c b/src/libudev/libudev-monitor.c index 0a02ef153a..3f1fee7f7e 100644 --- a/src/libudev/libudev-monitor.c +++ b/src/libudev/libudev-monitor.c @@ -585,6 +585,7 @@ _public_ struct udev_device *udev_monitor_receive_device(struct udev_monitor *ud } buf; ssize_t buflen; ssize_t bufpos; + bool is_initialized = false; retry: if (udev_monitor == NULL) @@ -638,63 +639,42 @@ retry: return NULL; } - udev_device = udev_device_new(udev_monitor->udev); - if (udev_device == NULL) - return NULL; - if (memcmp(buf.raw, "libudev", 8) == 0) { /* udev message needs proper version magic */ if (buf.nlh.magic != htonl(UDEV_MONITOR_MAGIC)) { log_debug("unrecognized message signature (%x != %x)", buf.nlh.magic, htonl(UDEV_MONITOR_MAGIC)); - udev_device_unref(udev_device); return NULL; } if (buf.nlh.properties_off+32 > (size_t)buflen) { - udev_device_unref(udev_device); return NULL; } bufpos = buf.nlh.properties_off; /* devices received from udev are always initialized */ - udev_device_set_is_initialized(udev_device); + is_initialized = true; } else { /* kernel message with header */ bufpos = strlen(buf.raw) + 1; if ((size_t)bufpos < sizeof("a@/d") || bufpos >= buflen) { log_debug("invalid message length"); - udev_device_unref(udev_device); return NULL; } /* check message header */ if (strstr(buf.raw, "@/") == NULL) { log_debug("unrecognized message header"); - udev_device_unref(udev_device); return NULL; } } - udev_device_set_info_loaded(udev_device); - - while (bufpos < buflen) { - char *key; - size_t keylen; - - key = &buf.raw[bufpos]; - keylen = strlen(key); - if (keylen == 0) - break; - bufpos += keylen + 1; - udev_device_add_property_from_string_parse(udev_device, key); - } - - if (udev_device_add_property_from_string_parse_finish(udev_device) < 0) { - log_debug("missing values, invalid device"); - udev_device_unref(udev_device); + udev_device = udev_device_new_from_nulstr(udev_monitor->udev, &buf.raw[bufpos], buflen - bufpos); + if (!udev_device) return NULL; - } + + if (is_initialized) + udev_device_set_is_initialized(udev_device); /* skip device, if it does not pass the current filter */ if (!passes_filter(udev_monitor, udev_device)) { diff --git a/src/libudev/libudev-private.h b/src/libudev/libudev-private.h index 97f0cd68a4..29df8df6c4 100644 --- a/src/libudev/libudev-private.h +++ b/src/libudev/libudev-private.h @@ -38,6 +38,7 @@ int udev_get_rules_path(struct udev *udev, char **path[], usec_t *ts_usec[]); /* libudev-device.c */ struct udev_device *udev_device_new(struct udev *udev); +struct udev_device *udev_device_new_from_nulstr(struct udev *udev, char *nulstr, ssize_t buflen); struct udev_device *udev_device_shallow_clone(struct udev_device *old_device); mode_t udev_device_get_devnode_mode(struct udev_device *udev_device); uid_t udev_device_get_devnode_uid(struct udev_device *udev_device); |