linux-hotplug.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] improve atomicity of device creation
@ 2007-12-11 16:08 Scott James Remnant
  2007-12-11 16:25 ` [dm-devel] " Alasdair G Kergon
  2007-12-11 17:35 ` Kay Sievers
  0 siblings, 2 replies; 15+ messages in thread
From: Scott James Remnant @ 2007-12-11 16:08 UTC (permalink / raw)
  To: dm-devel; +Cc: linux-hotplug-devel


[-- Attachment #1.1.1: Type: text/plain, Size: 199 bytes --]

This is a patch we've written and applied in Ubuntu to improve the
atomicity of devmapper device creation somewhat, and avoid races with
udev.

Scott
-- 
Scott James Remnant
scott@ubuntu.com

[-- Attachment #1.1.2: devmapper-1.02.20-atomic-mknod.patch --]
[-- Type: text/x-patch, Size: 1762 bytes --]

--- devmapper-1.02.20.orig/lib/libdm-common.c
+++ devmapper-1.02.20/lib/libdm-common.c
@@ -252,12 +252,11 @@
 static int _add_dev_node(const char *dev_name, uint32_t major, uint32_t minor,
 			 uid_t uid, gid_t gid, mode_t mode)
 {
-	char path[PATH_MAX];
+	char path[PATH_MAX], tmppath[PATH_MAX + 7];
 	struct stat info;
 	dev_t dev = MKDEV(major, minor);
 	mode_t old_mask;
-
-	_build_dev_path(path, sizeof(path), dev_name);
+	int retval;
 
 	if (stat(path, &info) >= 0) {
 		if (!S_ISBLK(info.st_mode)) {
@@ -269,31 +268,39 @@
 		/* If right inode already exists we don't touch uid etc. */
 		if (info.st_rdev == dev)
 			return 1;
-
-		if (unlink(path) < 0) {
-			log_error("Unable to unlink device node for '%s'",
-				  dev_name);
-			return 0;
-		}
 	}
 
+	_build_dev_path(path, sizeof(path), dev_name);
+	strcpy (tmppath, path);
+	strcat (tmppath, ".dm-tmp");
+
 	old_mask = umask(0);
-	if (mknod(path, S_IFBLK | mode, dev) < 0) {
-		log_error("Unable to make device node for '%s'", dev_name);
+	retval = mknod(tmppath, S_IFBLK | mode, dev);
+	umask(old_mask);
+	if (retval < 0) {
+		log_error("Unable to make temporary device node for '%s'", dev_name);
 		return 0;
 	}
-	umask(old_mask);
 
-	if (chown(path, uid, gid) < 0) {
+	if (chown(tmppath, uid, gid) < 0) {
 		log_error("%s: chown failed: %s", path, strerror(errno));
+		unlink(tmppath);
 		return 0;
 	}
 
 #ifdef HAVE_SELINUX
-	if (!dm_set_selinux_context(path, S_IFBLK))
+	if (!dm_set_selinux_context(tmppath, S_IFBLK)) {
+		unlink(tmppath);
 		return 0;
+	}
 #endif
 
+	if (rename(tmppath, path) < 0) {
+		log_error("Unable to replace device node for '%s'", dev_name);
+		unlink(tmppath);
+		return 0;
+	}
+
 	return 1;
 }
 

[-- Attachment #1.2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 189 bytes --]

[-- Attachment #2: Type: text/plain, Size: 278 bytes --]

-------------------------------------------------------------------------
SF.Net email is sponsored by: 
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php

[-- Attachment #3: Type: text/plain, Size: 226 bytes --]

_______________________________________________
Linux-hotplug-devel mailing list  http://linux-hotplug.sourceforge.net
Linux-hotplug-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel

^ permalink raw reply	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2007-12-11 19:43 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-12-11 16:08 [PATCH] improve atomicity of device creation Scott James Remnant
2007-12-11 16:25 ` [dm-devel] " Alasdair G Kergon
2007-12-11 16:42   ` Scott James Remnant
2007-12-11 17:03     ` Alasdair G Kergon
2007-12-11 17:18       ` Scott James Remnant
2007-12-11 17:51         ` Kay Sievers
2007-12-11 18:08           ` Alasdair G Kergon
2007-12-11 18:25             ` Kay Sievers
2007-12-11 19:13               ` Alasdair G Kergon
2007-12-11 19:43               ` Alasdair G Kergon
2007-12-11 17:35 ` Kay Sievers
2007-12-11 17:40   ` Scott James Remnant
2007-12-11 17:53     ` Alasdair G Kergon
2007-12-11 19:09       ` Scott James Remnant
2007-12-11 18:03     ` Kay Sievers

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).