[systemd-commits] 5 commits - src/libudev src/udev
Tom Gundersen
tomegun at kemper.freedesktop.org
Mon Jan 26 06:11:25 PST 2015
src/libudev/libudev-device.c | 170 +++++++++++++++++++++++++++++++-----------
src/libudev/libudev-monitor.c | 34 +-------
src/libudev/libudev-private.h | 7 -
src/udev/udev-event.c | 35 ++++----
4 files changed, 156 insertions(+), 90 deletions(-)
New commits:
commit 71ef8b3ac49fd8b5cff36ee7efc945a751ab9a09
Author: Tom Gundersen <teg at jklm.no>
Date: Mon Jan 26 14:48:04 2015 +0100
libudev: private - drop some functions from the internal API
diff --git a/src/libudev/libudev-device.c b/src/libudev/libudev-device.c
index 6031237..d5206aa 100644
--- a/src/libudev/libudev-device.c
+++ b/src/libudev/libudev-device.c
@@ -161,7 +161,7 @@ _public_ dev_t udev_device_get_devnum(struct udev_device *udev_device)
return udev_device->devnum;
}
-int udev_device_set_devnum(struct udev_device *udev_device, dev_t devnum)
+static int udev_device_set_devnum(struct udev_device *udev_device, dev_t devnum)
{
char num[32];
@@ -258,7 +258,7 @@ static int udev_device_set_devtype(struct udev_device *udev_device, const char *
return 0;
}
-int udev_device_set_subsystem(struct udev_device *udev_device, const char *subsystem)
+static int udev_device_set_subsystem(struct udev_device *udev_device, const char *subsystem)
{
free(udev_device->subsystem);
udev_device->subsystem = strdup(subsystem);
@@ -392,6 +392,44 @@ static struct udev_list_entry *udev_device_add_property_from_string(struct udev_
return udev_device_add_property(udev_device, name, val);
}
+static int udev_device_set_syspath(struct udev_device *udev_device, const char *syspath)
+{
+ const char *pos;
+ size_t len;
+
+ free(udev_device->syspath);
+ udev_device->syspath = strdup(syspath);
+ if (udev_device->syspath == NULL)
+ return -ENOMEM;
+ udev_device->devpath = udev_device->syspath + strlen("/sys");
+ udev_device_add_property(udev_device, "DEVPATH", udev_device->devpath);
+
+ pos = strrchr(udev_device->syspath, '/');
+ if (pos == NULL)
+ return -EINVAL;
+ udev_device->sysname = strdup(&pos[1]);
+ if (udev_device->sysname == NULL)
+ return -ENOMEM;
+
+ /* some devices have '!' in their name, change that to '/' */
+ len = 0;
+ while (udev_device->sysname[len] != '\0') {
+ if (udev_device->sysname[len] == '!')
+ udev_device->sysname[len] = '/';
+ len++;
+ }
+
+ /* trailing number */
+ while (len > 0 && isdigit(udev_device->sysname[--len]))
+ udev_device->sysnum = &udev_device->sysname[len];
+
+ /* sysname is completely numeric */
+ if (len == 0)
+ udev_device->sysnum = NULL;
+
+ return 0;
+}
+
/*
* parse property string, and if needed, update internal values accordingly
*
@@ -637,7 +675,7 @@ void udev_device_set_info_loaded(struct udev_device *device)
device->info_loaded = true;
}
-struct udev_device *udev_device_new(struct udev *udev)
+static struct udev_device *udev_device_new(struct udev *udev)
{
struct udev_device *udev_device;
@@ -1607,44 +1645,6 @@ _public_ struct udev_list_entry *udev_device_get_sysattr_list_entry(struct udev_
return udev_list_get_entry(&udev_device->sysattr_list);
}
-int udev_device_set_syspath(struct udev_device *udev_device, const char *syspath)
-{
- const char *pos;
- size_t len;
-
- free(udev_device->syspath);
- udev_device->syspath = strdup(syspath);
- if (udev_device->syspath == NULL)
- return -ENOMEM;
- udev_device->devpath = udev_device->syspath + strlen("/sys");
- udev_device_add_property(udev_device, "DEVPATH", udev_device->devpath);
-
- pos = strrchr(udev_device->syspath, '/');
- if (pos == NULL)
- return -EINVAL;
- udev_device->sysname = strdup(&pos[1]);
- if (udev_device->sysname == NULL)
- return -ENOMEM;
-
- /* some devices have '!' in their name, change that to '/' */
- len = 0;
- while (udev_device->sysname[len] != '\0') {
- if (udev_device->sysname[len] == '!')
- udev_device->sysname[len] = '/';
- len++;
- }
-
- /* trailing number */
- while (len > 0 && isdigit(udev_device->sysname[--len]))
- udev_device->sysnum = &udev_device->sysname[len];
-
- /* sysname is completely numeric */
- if (len == 0)
- udev_device->sysnum = NULL;
-
- return 0;
-}
-
static int udev_device_set_devnode(struct udev_device *udev_device, const char *devnode)
{
free(udev_device->devnode);
diff --git a/src/libudev/libudev-private.h b/src/libudev/libudev-private.h
index d8bc647..9636304 100644
--- a/src/libudev/libudev-private.h
+++ b/src/libudev/libudev-private.h
@@ -37,16 +37,12 @@
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);
gid_t udev_device_get_devnode_gid(struct udev_device *udev_device);
-int udev_device_set_subsystem(struct udev_device *udev_device, const char *subsystem);
-int udev_device_set_syspath(struct udev_device *udev_device, const char *syspath);
int udev_device_rename(struct udev_device *udev_device, const char *new_name);
-int udev_device_set_devnum(struct udev_device *udev_device, dev_t devnum);
int udev_device_add_devlink(struct udev_device *udev_device, const char *devlink);
void udev_device_cleanup_devlinks_list(struct udev_device *udev_device);
struct udev_list_entry *udev_device_add_property(struct udev_device *udev_device, const char *key, const char *value);
commit 2df959ec3b5128dfe4d9b996dc13b16a8f4c4233
Author: Tom Gundersen <teg at jklm.no>
Date: Mon Jan 26 14:45:12 2015 +0100
libudev: monitor - move nulstr parsing to libudev-device
Hide the details a bit.
diff --git a/src/libudev/libudev-device.c b/src/libudev/libudev-device.c
index 08331a2..6031237 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 4cfb2f6..08ddde8 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 31f150c..d8bc647 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);
commit 04ef387ea900ff59f801d7bf745f21e644711643
Author: Tom Gundersen <teg at jklm.no>
Date: Mon Jan 26 14:13:31 2015 +0100
udev: event - minor nit
Stay uniform and use 'dev' rather than 'event->dev', as these are aliases (and event->dev looks
like it may be a typo for event->dev_db).
diff --git a/src/udev/udev-event.c b/src/udev/udev-event.c
index d6c9cfc..a8dd462 100644
--- a/src/udev/udev-event.c
+++ b/src/udev/udev-event.c
@@ -830,7 +830,7 @@ void udev_event_execute_rules(struct udev_event *event,
key = udev_list_entry_get_name(entry);
value = udev_list_entry_get_value(entry);
- property = udev_device_add_property(event->dev, key, value);
+ property = udev_device_add_property(dev, key, value);
udev_list_entry_set_num(property, true);
}
}
commit fa639f3ae770ffccdd9f97430b0883d01bc821ce
Author: Tom Gundersen <teg at jklm.no>
Date: Mon Jan 26 14:12:45 2015 +0100
udev: event - introduce and use internal udev_device_shallow_clone()
diff --git a/src/libudev/libudev-device.c b/src/libudev/libudev-device.c
index 193c706..08331a2 100644
--- a/src/libudev/libudev-device.c
+++ b/src/libudev/libudev-device.c
@@ -1944,3 +1944,24 @@ int udev_device_rename(struct udev_device *udev_device, const char *name)
return 0;
}
+
+struct udev_device *udev_device_shallow_clone(struct udev_device *old_device)
+{
+ struct udev_device *device;
+
+ if (old_device == NULL)
+ return NULL;
+
+ device = udev_device_new(old_device->udev);
+ if (!device) {
+ errno = ENOMEM;
+
+ return NULL;
+ }
+
+ udev_device_set_syspath(device, udev_device_get_syspath(old_device));
+ udev_device_set_subsystem(device, udev_device_get_subsystem(old_device));
+ udev_device_set_devnum(device, udev_device_get_devnum(old_device));
+
+ return device;
+}
diff --git a/src/libudev/libudev-private.h b/src/libudev/libudev-private.h
index 017b6cc..31f150c 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_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);
gid_t udev_device_get_devnode_gid(struct udev_device *udev_device);
diff --git a/src/udev/udev-event.c b/src/udev/udev-event.c
index e5b2259..d6c9cfc 100644
--- a/src/udev/udev-event.c
+++ b/src/udev/udev-event.c
@@ -809,11 +809,8 @@ void udev_event_execute_rules(struct udev_event *event,
if (major(udev_device_get_devnum(dev)) != 0)
udev_node_remove(dev);
} else {
- event->dev_db = udev_device_new(event->udev);
+ event->dev_db = udev_device_shallow_clone(dev);
if (event->dev_db != NULL) {
- udev_device_set_syspath(event->dev_db, udev_device_get_syspath(dev));
- udev_device_set_subsystem(event->dev_db, udev_device_get_subsystem(dev));
- udev_device_set_devnum(event->dev_db, udev_device_get_devnum(dev));
udev_device_read_db(event->dev_db, NULL);
udev_device_set_info_loaded(event->dev_db);
commit 243d182543c7edc3980e1ae41712bb0b96df46bd
Author: Tom Gundersen <teg at jklm.no>
Date: Mon Jan 26 13:33:00 2015 +0100
udev: event - move renaming of udev_device to libudev
This is not exposed in the public API. We want to simplify the internal libudev-device API as much as possible
so that it will be simpler to rip the whole thing out in the future.
diff --git a/src/libudev/libudev-device.c b/src/libudev/libudev-device.c
index a7446bf..193c706 100644
--- a/src/libudev/libudev-device.c
+++ b/src/libudev/libudev-device.c
@@ -1922,3 +1922,25 @@ void udev_device_set_db_persist(struct udev_device *udev_device)
{
udev_device->db_persist = true;
}
+
+int udev_device_rename(struct udev_device *udev_device, const char *name)
+{
+ _cleanup_free_ char *dirname = NULL;
+ char *new_syspath;
+ int r;
+
+ if (udev_device == NULL || name == NULL)
+ return -EINVAL;
+
+ dirname = dirname_malloc(udev_device->syspath);
+ if (!dirname)
+ return -ENOMEM;
+
+ new_syspath = strappenda(dirname, "/", name);
+
+ r = udev_device_set_syspath(udev_device, new_syspath);
+ if (r < 0)
+ return r;
+
+ return 0;
+}
diff --git a/src/libudev/libudev-private.h b/src/libudev/libudev-private.h
index d188bda..017b6cc 100644
--- a/src/libudev/libudev-private.h
+++ b/src/libudev/libudev-private.h
@@ -43,6 +43,7 @@ uid_t udev_device_get_devnode_uid(struct udev_device *udev_device);
gid_t udev_device_get_devnode_gid(struct udev_device *udev_device);
int udev_device_set_subsystem(struct udev_device *udev_device, const char *subsystem);
int udev_device_set_syspath(struct udev_device *udev_device, const char *syspath);
+int udev_device_rename(struct udev_device *udev_device, const char *new_name);
int udev_device_set_devnum(struct udev_device *udev_device, dev_t devnum);
int udev_device_add_devlink(struct udev_device *udev_device, const char *devlink);
void udev_device_cleanup_devlinks_list(struct udev_device *udev_device);
diff --git a/src/udev/udev-event.c b/src/udev/udev-event.c
index beb0943..e5b2259 100644
--- a/src/udev/udev-event.c
+++ b/src/udev/udev-event.c
@@ -846,23 +846,25 @@ void udev_event_execute_rules(struct udev_event *event,
/* rename a new network interface, if needed */
if (udev_device_get_ifindex(dev) > 0 && streq(udev_device_get_action(dev), "add") &&
event->name != NULL && !streq(event->name, udev_device_get_sysname(dev))) {
- char syspath[UTIL_PATH_SIZE];
- char *pos;
int r;
r = rename_netif(event);
- if (r >= 0) {
- /* remember old name */
- udev_device_add_property(dev, "INTERFACE_OLD", udev_device_get_sysname(dev));
+ if (r < 0)
+ log_warning_errno(r, "could not rename interface '%d' from '%s' to '%s': %m", udev_device_get_ifindex(dev),
+ udev_device_get_sysname(dev), event->name);
+ else {
+ const char *interface_old;
- /* now change the devpath, because the kernel device name has changed */
- strscpy(syspath, sizeof(syspath), udev_device_get_syspath(dev));
- pos = strrchr(syspath, '/');
- if (pos != NULL) {
- pos++;
- strscpy(pos, sizeof(syspath) - (pos - syspath), event->name);
- udev_device_set_syspath(event->dev, syspath);
- udev_device_add_property(dev, "INTERFACE", udev_device_get_sysname(dev));
+ /* remember old name */
+ interface_old = udev_device_get_sysname(dev);
+
+ r = udev_device_rename(dev, event->name);
+ if (r < 0)
+ log_warning_errno(r, "renamed interface '%d' from '%s' to '%s', but could not update udev_device: %m",
+ udev_device_get_ifindex(dev), udev_device_get_sysname(dev), event->name);
+ else {
+ udev_device_add_property(dev, "INTERFACE_OLD", interface_old);
+ udev_device_add_property(dev, "INTERFACE", event->name);
log_debug("changed devpath to '%s'", udev_device_get_devpath(dev));
}
}
More information about the systemd-commits
mailing list