Package: parted / 3.2-7

udevadm-settle.patch Patch series | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
From 0dd58cc5ac1cd5419bc1a6b45bf3acf4e13308de Mon Sep 17 00:00:00 2001
From: Colin Watson <cjwatson@ubuntu.com>
Date: Fri, 28 Mar 2014 17:10:16 +0000
Subject: udev handling

Run udevadm settle around partition table rereads, to avoid races.

This should be replaced by a proper completion-notification mechanism
between the kernel and udev.

Forwarded: no
Last-Update: 2014-08-05

Patch-Name: udevadm-settle.patch
---
 libparted/arch/linux.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 54 insertions(+), 2 deletions(-)

diff --git a/libparted/arch/linux.c b/libparted/arch/linux.c
index 0f18904..703294e 100644
--- a/libparted/arch/linux.c
+++ b/libparted/arch/linux.c
@@ -26,6 +26,7 @@
 #include <parted/fdasd.h>
 #endif
 
+#include <stdlib.h>
 #include <ctype.h>
 #include <errno.h>
 #include <fcntl.h>
@@ -3105,10 +3106,52 @@ _have_blkpg ()
         return have_blkpg = kver >= KERNEL_VERSION (2,4,0) ? 1 : 0;
 }
 
+static int
+_chrooted ()
+{
+        static int cached = -1;
+        struct stat root, init_root;
+
+        if (cached != -1)
+                return cached;
+
+        if (stat ("/", &root) || stat ("/proc/1/root", &init_root))
+                /* We can't tell, but are unlikely to be able to tell in the
+                 * future either.
+                 */
+                cached = 0;
+        else if (root.st_dev == init_root.st_dev &&
+                 root.st_ino == init_root.st_ino)
+                /* / has the same dev/ino as /sbin/init's root, so we're not
+                 * in a chroot.
+                 */
+                cached = 0;
+        else
+                /* We must be in a chroot. */
+                cached = 1;
+
+        return cached;
+}
+
 /* Return nonzero upon success, 0 if something fails.  */
 static int
 linux_disk_commit (PedDisk* disk)
 {
+        int ret = 1;
+
+        /* Modern versions of udev may notice the write activity on
+         * partition devices caused by _flush_cache, and may decide to
+         * synthesise some change events as a result. These may in turn run
+         * programs that open partition devices, which will race with us
+         * trying to remove those devices. To avoid this, we need to wait
+         * until udevd has finished processing its event queue.
+         * TODO: for upstream submission, this should check whether udevadm
+         * exists on $PATH.
+         */
+        if (!_chrooted () && system ("udevadm settle") != 0) {
+                /* ignore failures */
+        }
+
         if (disk->dev->type != PED_DEVICE_FILE) {
 
 		/* We now require BLKPG support.  If this assertion fails,
@@ -3118,10 +3161,19 @@ linux_disk_commit (PedDisk* disk)
 		assert (_have_blkpg ());
 
 		if (!_disk_sync_part_table (disk))
-			return 0;
+			ret = 0;
         }
 
-        return 1;
+        /* Now we wait for udevd to finish creating device nodes based on
+         * the above activity, so that callers can reliably use them.
+         * TODO: for upstream submission, this should check whether udevadm
+         * exists on $PATH.
+         */
+        if (!_chrooted () && system ("udevadm settle") != 0) {
+                /* ignore failures */
+        }
+
+        return ret;
 }
 
 #if USE_BLKID