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.

Signed-off-by: Anthony G. Basile <blueness@gentoo.org>
This commit is contained in:
Tom Gundersen 2015-02-11 16:21:18 -05:00 committed by Anthony G. Basile
parent 691df3c5c3
commit b39a4a3aba
6 changed files with 60 additions and 20 deletions

5
NOTES
View File

@ -7,11 +7,6 @@ Newest entries are on top.
This follows on the privious skipped commit. Since we did not move the renaming, a
residual call to udev_device_set_syspath() remains in udev-event.c.
* We skip "udev: event - move renaming of udev_device to libudev"
http://cgit.freedesktop.org/systemd/systemd/commit/?id=243d182543c7edc3980e1ae41712bb0b96df46bd
The purpose of this commit is to make it easier for the kdbus cut-over later which we
will not do. It also intefers with the ENABLE_RULE_GENERATOR code.
2014-12-15
* We did not move builtin-hwdb - port to sd-hwdb:

View File

@ -1988,3 +1988,25 @@ struct udev_device *udev_device_new_from_nulstr(struct udev *udev, char *nulstr,
return device;
}
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 = strjoina(dirname, "/", name);
r = udev_device_set_syspath(udev_device, new_syspath);
if (r < 0)
return r;
return 0;
}

View File

@ -45,6 +45,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);

View File

@ -846,6 +846,24 @@ ssize_t loop_read(int fd, void *buf, size_t nbytes, bool do_poll) {
return n;
}
char* dirname_malloc(const char *path) {
char *d, *dir, *dir2;
d = strdup(path);
if (!d)
return NULL;
dir = dirname(d);
assert(dir);
if (dir != d) {
dir2 = strdup(dir);
free(d);
return dir2;
}
return dir;
}
int dev_urandom(void *p, size_t n) {
static int have_syscall = -1;
int r, fd;

View File

@ -275,6 +275,9 @@ int flush_fd(int fd);
int fopen_temporary(const char *path, FILE **_f, char **_temp_path);
ssize_t loop_read(int fd, void *buf, size_t nbytes, bool do_poll);
char* dirname_malloc(const char *path);
int chmod_and_chown(const char *path, mode_t mode, uid_t uid, gid_t gid);
bool null_or_empty(struct stat *st) _pure_;

View File

@ -862,26 +862,27 @@ void udev_event_execute_rules(struct udev_event *event,
sigmask);
/* rename a new network interface, if needed */
int r;
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;
char *finalifname = event->name;
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), finalifname);
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));
}
}