summaryrefslogtreecommitdiff
path: root/libudev/libudev-device-private.c
diff options
context:
space:
mode:
authorKay Sievers <kay.sievers@vrfy.org>2010-01-13 13:26:56 +0100
committerKay Sievers <kay.sievers@vrfy.org>2010-01-13 13:26:56 +0100
commit7c34949019720b3865c6d0080ae8c7cfdb313085 (patch)
tree8ad3cf76650059b4268ed88a7425b5f6c316da37 /libudev/libudev-device-private.c
parent889dd1061c2e09044a6c0450c06180b47288fe4f (diff)
libudev: device - create db file atomically
We need to prevent that libudev parses half-written database files. Also for "change" events, we need to make sure, that database files always exist to be read by libudev, and that they are not first deleted before they are re-created.
Diffstat (limited to 'libudev/libudev-device-private.c')
-rw-r--r--libudev/libudev-device-private.c28
1 files changed, 17 insertions, 11 deletions
diff --git a/libudev/libudev-device-private.c b/libudev/libudev-device-private.c
index 66f0328eb3..07249c3885 100644
--- a/libudev/libudev-device-private.c
+++ b/libudev/libudev-device-private.c
@@ -25,6 +25,7 @@ int udev_device_update_db(struct udev_device *udev_device)
{
struct udev *udev = udev_device_get_udev(udev_device);
char filename[UTIL_PATH_SIZE];
+ char filename_tmp[UTIL_PATH_SIZE];
FILE *f;
char target[232]; /* on 64bit, tmpfs inlines up to 239 bytes */
size_t devlen = strlen(udev_get_dev_path(udev))+1;
@@ -35,7 +36,7 @@ int udev_device_update_db(struct udev_device *udev_device)
util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev), "/.udev/db/",
udev_device_get_subsystem(udev_device), ":", udev_device_get_sysname(udev_device), NULL);
- unlink(filename);
+ util_strscpyl(filename_tmp, sizeof(filename_tmp), filename, ".tmp", NULL);
udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device))
if (udev_list_entry_get_flag(list_entry))
@@ -66,21 +67,24 @@ int udev_device_update_db(struct udev_device *udev_device)
goto file;
}
}
- info(udev, "create db link (%s)\n", target);
- udev_selinux_setfscreatecon(udev, filename, S_IFLNK);
- util_create_path(udev, filename);
- ret = symlink(target, filename);
+ udev_selinux_setfscreatecon(udev, filename_tmp, S_IFLNK);
+ util_create_path(udev, filename_tmp);
+ ret = symlink(target, filename_tmp);
udev_selinux_resetfscreatecon(udev);
- if (ret == 0)
- goto out;
+ if (ret != 0)
+ goto file;
+ ret = rename(filename_tmp, filename);
+ if (ret != 0)
+ goto file;
+ info(udev, "created db link (%s)\n", target);
+ goto out;
file:
- util_create_path(udev, filename);
- f = fopen(filename, "w");
+ util_create_path(udev, filename_tmp);
+ f = fopen(filename_tmp, "w");
if (f == NULL) {
- err(udev, "unable to create db file '%s': %m\n", filename);
+ err(udev, "unable to create temporary db file '%s': %m\n", filename_tmp);
return -1;
}
- info(udev, "created db file for '%s' in '%s'\n", udev_device_get_devpath(udev_device), filename);
if (udev_device_get_devnode(udev_device) != NULL) {
fprintf(f, "N:%s\n", &udev_device_get_devnode(udev_device)[devlen]);
@@ -105,6 +109,8 @@ file:
udev_list_entry_get_value(list_entry));
}
fclose(f);
+ rename(filename_tmp, filename);
+ info(udev, "created db file for '%s' in '%s'\n", udev_device_get_devpath(udev_device), filename);
out:
return 0;
}