chiark / gitweb /
udev: event - move renaming of udev_device to libudev
authorTom Gundersen <teg@jklm.no>
Mon, 26 Jan 2015 12:33:00 +0000 (13:33 +0100)
committerTom Gundersen <teg@jklm.no>
Mon, 26 Jan 2015 12:33:00 +0000 (13:33 +0100)
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.

src/libudev/libudev-device.c
src/libudev/libudev-private.h
src/udev/udev-event.c

index a7446bff77c100d86873641a22c98f089c3d920e..193c706d89d185e2d65152005ccd45a0ea51af7a 100644 (file)
@@ -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;
+}
index d188bdad204d9a2e78c57d5305718834128f4e06..017b6cc63b605fc1101ecf31230280dbc9d49292 100644 (file)
@@ -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);
index beb09439c75ad27fa9ecf1bed00706d4dec80ee4..e5b2259fda1aa7806329287789201068e19f87fd 100644 (file)
@@ -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));
                                 }
                         }