--- a/libdm/libdm-common.c
+++ b/libdm/libdm-common.c
@@ -404,12 +404,19 @@
static int _add_dev_node(const char *dev_name, uint32_t major, uint32_t minor,
uid_t uid, gid_t gid, mode_t mode, int check_udev)
{
- char path[PATH_MAX];
+ char path[PATH_MAX], tmppath[PATH_MAX];
struct stat info;
dev_t dev = MKDEV(major, minor);
mode_t old_mask;
_build_dev_path(path, sizeof(path), dev_name);
+ _build_dev_path(tmppath, sizeof(tmppath), ".XXXXXX");
+
+ mktemp(tmppath);
+ if (strlen(tmppath) == 0) {
+ log_error("Unable to create name of temporary file");
+ return 0;
+ }
if (stat(path, &info) >= 0) {
if (!S_ISBLK(info.st_mode)) {
@@ -433,24 +440,33 @@
"node creation.", path);
old_mask = umask(0);
- if (mknod(path, S_IFBLK | mode, dev) < 0) {
+ if (mknod(tmppath, S_IFBLK | mode, dev) < 0) {
umask(old_mask);
log_error("Unable to make device node for '%s'", dev_name);
- return 0;
+ goto error;
}
umask(old_mask);
- if (chown(path, uid, gid) < 0) {
+ if (chown(tmppath, uid, gid) < 0) {
log_sys_error("chown", path);
- return 0;
+ goto error;
}
- log_debug("Created %s", path);
+ if (!dm_set_selinux_context(tmppath, S_IFBLK))
+ goto error;
- if (!dm_set_selinux_context(path, S_IFBLK))
- return 0;
+ if (rename(tmppath, path) < 0) {
+ log_error("Unable to replace device node for '%s'", dev_name);
+ goto error;
+ }
+
+ log_debug("Created %s", path);
return 1;
+
+error:
+ unlink(tmppath);
+ return 0;
}
static int _rm_dev_node(const char *dev_name, int check_udev)