diff options
author | Tom Gundersen <teg@jklm.no> | 2015-01-26 14:45:12 +0100 |
---|---|---|
committer | Tom Gundersen <teg@jklm.no> | 2015-01-26 14:45:12 +0100 |
commit | 2df959ec3b5128dfe4d9b996dc13b16a8f4c4233 (patch) | |
tree | bc17c582ad342a3f75d34d7e5ff00a1595c4d473 /src | |
parent | 04ef387ea900ff59f801d7bf745f21e644711643 (diff) |
libudev: monitor - move nulstr parsing to libudev-device
Hide the details a bit.
Diffstat (limited to 'src')
-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 08331a2329..6031237762 100644 --- a/src/libudev/libudev-device.c +++ b/src/libudev/libudev-device.c @@ -1965,3 +1965,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 4cfb2f617c..08ddde8fd7 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 31f150c764..d8bc647e9c 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); |