Skip to contentSkip to conversation

makedevs: resync device creation with upstream busybox

PATCH New Arnout Vandecappelle (Essensium/Mind) 4 participants 7 messages Nov 5, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 makedevs: resync device creation with upstream busybox

In upstream busbyox, the code to create devices has been simplified:
the code for a single and for multiple devices is no longer duplicated.

Since we are going to change the device creation code next, it's convenient to have only one copy to modify.

There are two behavioural changes with this, but they were introduced silently together with other commits in upstream busybox.

- When mknod() fails, the chmod was still done. This is pointless so it is no longer done now.

- There was a check for mode != -1; however, a mode of -1 would not have worked anyway because all bits would be set for mknod(), which would fail. So this check is redundant.

---
 package/makedevs/makedevs.c | 47 +++++++++++++++------------------------------
 1 file changed, 15 insertions(+), 32 deletions(-)
package/makedevs/makedevs.c +15 -32
index cacb144..7092b14 100644
@@ -599,6 +599,8 @@ int main(int argc, char **argv)
 		} else
 		{
 			dev_t rdev;
+			unsigned i;
+			char *full_name_inc;
 
 			if (type == 'p') {
 				mode |= S_IFIFO;
@@ -614,43 +616,24 @@ int main(int argc, char **argv)
 				goto loop;
 			}
 
-			if (count > 0) {
-				int i;
-				char *full_name_inc;
-
-				full_name_inc = xmalloc(strlen(full_name) + 8);
-				for (i = 0; i < count; i++) {
-					sprintf(full_name_inc, "%s%d", full_name, start + i);
-					rdev = makedev(major, minor + i * increment);
-					if (mknod(full_name_inc, mode, rdev) == -1) {
-						bb_perror_msg("line %d: Couldnt create node %s", linenum, full_name_inc);
-						ret = EXIT_FAILURE;
-					}
-					else if (chown(full_name_inc, uid, gid) == -1) {
-						bb_perror_msg("line %d: chown failed for %s", linenum, full_name_inc);
-						ret = EXIT_FAILURE;
-					}
-					if ((mode != -1) && (chmod(full_name_inc, mode) < 0)){
-						bb_perror_msg("line %d: chmod failed for %s", linenum, full_name_inc);
-						ret = EXIT_FAILURE;
-					}
-				}
-				free(full_name_inc);
-			} else {
-				rdev = makedev(major, minor);
-				if (mknod(full_name, mode, rdev) == -1) {
-					bb_perror_msg("line %d: Couldnt create node %s", linenum, full_name);
+			full_name_inc = xmalloc(strlen(full_name) + sizeof(int)*3 + 2);
+			if (count)
+				count--;
+			for (i = start; i <= start + count; i++) {
+				sprintf(full_name_inc, count ? "%s%u" : "%s", full_name, i);
+				rdev = makedev(major, minor + (i - start) * increment);
+				if (mknod(full_name_inc, mode, rdev) < 0) {
+					bb_perror_msg("line %d: can't create node %s", linenum, full_name_inc);
 					ret = EXIT_FAILURE;
-				}
-				else if (chown(full_name, uid, gid) == -1) {
-					bb_perror_msg("line %d: chown failed for %s", linenum, full_name);
+				} else if (chown(full_name_inc, uid, gid) < 0) {
+					bb_perror_msg("line %d: can't chown %s", linenum, full_name_inc);
 					ret = EXIT_FAILURE;
-				}
-				if ((mode != -1) && (chmod(full_name, mode) < 0)){
-					bb_perror_msg("line %d: chmod failed for %s", linenum, full_name);
+				} else if (chmod(full_name_inc, mode) < 0) {
+					bb_perror_msg("line %d: can't chmod %s", linenum, full_name_inc);
 					ret = EXIT_FAILURE;
 				}
 			}
+			free(full_name_inc);
 		}
 loop:
 		free(line);
Signature · 1 line
2.9.3
#1 lore raw

PATCH 2/2 makedevs: make device node creation idempotent

We use makedevs to create device nodes in the target rootfs. However, this can be called several times, e.g. when building several filesystem images or when rebuilding. When makedevs is called the second time, the device node already exists so mknod() errors out.

This wasn't noticed before because fakeroot's mknod() wrapper (incorrectly) does _not_ error out when the file exists already. Now we switched from fakeroot to pseudo, the problem becomes apparent.

Before creating the device node, check if it already exists and if so, if it has the correct device type and number. Change of mode and ownership is still done.

This approach was preferred over removing the target files before creating them, which would be simpler. However, when e.g. a file exists as a normal file and makedevs specifies it as a device node, that really is an error so we should detect it.

The other types don't have to be changed. The 'd' (directory) type is already OK because it already only creates directories if they don't exist yet. The 'f' (file mode) and 'r' (recursive) types only operate on files and directories that exist already.

Patch also sent upstream to busybox.

---
v2: check if pre-existing file is of correct type instead of simply
unlinking (Yann).

---
 package/makedevs/makedevs.c | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)
package/makedevs/makedevs.c +16 -2
index 7092b14..bcf0e0f 100644
@@ -601,6 +601,7 @@ int main(int argc, char **argv)
 			dev_t rdev;
 			unsigned i;
 			char *full_name_inc;
+			struct stat st;
 
 			if (type == 'p') {
 				mode |= S_IFIFO;
@@ -622,10 +623,23 @@ int main(int argc, char **argv)
 			for (i = start; i <= start + count; i++) {
 				sprintf(full_name_inc, count ? "%s%u" : "%s", full_name, i);
 				rdev = makedev(major, minor + (i - start) * increment);
-				if (mknod(full_name_inc, mode, rdev) < 0) {
+				if (stat(full_name_inc, &st) == 0) {
+					if ((mode & S_IFMT) != (st.st_mode & S_IFMT)) {
+						bb_error_msg("line %d: node %s exists but is of wrong file type", linenum, full_name_inc);
+						ret = EXIT_FAILURE;
+						continue;
+					}
+					if (st.st_rdev != rdev) {
+						bb_error_msg("line %d: node %s exists but is wrong device number", linenum, full_name_inc);
+						ret = EXIT_FAILURE;
+						continue;
+					}
+				} else if (mknod(full_name_inc, mode, rdev) < 0) {
 					bb_perror_msg("line %d: can't create node %s", linenum, full_name_inc);
 					ret = EXIT_FAILURE;
-				} else if (chown(full_name_inc, uid, gid) < 0) {
+					continue;
+				}
+				if (chown(full_name_inc, uid, gid) < 0) {
 					bb_perror_msg("line %d: can't chown %s", linenum, full_name_inc);
 					ret = EXIT_FAILURE;
 				} else if (chmod(full_name_inc, mode) < 0) {
Signature · 1 line
2.9.3
#2 lore raw

PATCH 2/2 makedevs: make device node creation idempotent

Hi Arnout,

Quoted 23 lines · Arnout Vandecappelle (Essensium/Mind)

We use makedevs to create device nodes in the target rootfs. However, this can be called several times, e.g. when building several filesystem images or when rebuilding. When makedevs is called the second time, the device node already exists so mknod() errors out.

This wasn't noticed before because fakeroot's mknod() wrapper (incorrectly) does _not_ error out when the file exists already. Now we switched from fakeroot to pseudo, the problem becomes apparent.

Before creating the device node, check if it already exists and if so, if it has the correct device type and number. Change of mode and ownership is still done.

This approach was preferred over removing the target files before creating them, which would be simpler. However, when e.g. a file exists as a normal file and makedevs specifies it as a device node, that really is an error so we should detect it.

The other types don't have to be changed. The 'd' (directory) type is already OK because it already only creates directories if they don't exist yet. The 'f' (file mode) and 'r' (recursive) types only operate on files and directories that exist already.

Quoted 4 lines

Patch also sent upstream to busybox.

This fixes the build error I was seeing, thanks:

#3 lore raw

PATCH 2/2 makedevs: make device node creation idempotent

In reply to Arnout Vandecappelle (Essensium/Mind)

Arnout, All,

On 2016-11-05 14:38 +0100, Arnout Vandecappelle (Essensium/Mind) spake thusly:

Quoted 23 lines

We use makedevs to create device nodes in the target rootfs. However, this can be called several times, e.g. when building several filesystem images or when rebuilding. When makedevs is called the second time, the device node already exists so mknod() errors out.

This wasn't noticed before because fakeroot's mknod() wrapper (incorrectly) does _not_ error out when the file exists already. Now we switched from fakeroot to pseudo, the problem becomes apparent.

Before creating the device node, check if it already exists and if so, if it has the correct device type and number. Change of mode and ownership is still done.

This approach was preferred over removing the target files before creating them, which would be simpler. However, when e.g. a file exists as a normal file and makedevs specifies it as a device node, that really is an error so we should detect it.

The other types don't have to be changed. The 'd' (directory) type is already OK because it already only creates directories if they don't exist yet. The 'f' (file mode) and 'r' (recursive) types only operate on files and directories that exist already.

Quoted 4 lines

Patch also sent upstream to busybox.

Thank you! :-)

Regards,
Yann E. MORIN.

Quoted 52 lines

---
v2: check if pre-existing file is of correct type instead of simply
unlinking (Yann).

---
 package/makedevs/makedevs.c | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)
package/makedevs/makedevs.c +16 -2
index 7092b14..bcf0e0f 100644
@@ -601,6 +601,7 @@ int main(int argc, char **argv)
 			dev_t rdev;
 			unsigned i;
 			char *full_name_inc;
+			struct stat st;
 
 			if (type == 'p') {
 				mode |= S_IFIFO;
@@ -622,10 +623,23 @@ int main(int argc, char **argv)
 			for (i = start; i <= start + count; i++) {
 				sprintf(full_name_inc, count ? "%s%u" : "%s", full_name, i);
 				rdev = makedev(major, minor + (i - start) * increment);
-				if (mknod(full_name_inc, mode, rdev) < 0) {
+				if (stat(full_name_inc, &st) == 0) {
+					if ((mode & S_IFMT) != (st.st_mode & S_IFMT)) {
+						bb_error_msg("line %d: node %s exists but is of wrong file type", linenum, full_name_inc);
+						ret = EXIT_FAILURE;
+						continue;
+					}
+					if (st.st_rdev != rdev) {
+						bb_error_msg("line %d: node %s exists but is wrong device number", linenum, full_name_inc);
+						ret = EXIT_FAILURE;
+						continue;
+					}
+				} else if (mknod(full_name_inc, mode, rdev) < 0) {
 					bb_perror_msg("line %d: can't create node %s", linenum, full_name_inc);
 					ret = EXIT_FAILURE;
-				} else if (chown(full_name_inc, uid, gid) < 0) {
+					continue;
+				}
+				if (chown(full_name_inc, uid, gid) < 0) {
 					bb_perror_msg("line %d: can't chown %s", linenum, full_name_inc);
 					ret = EXIT_FAILURE;
 				} else if (chmod(full_name_inc, mode) < 0) {
Signature · 6 lines
2.9.3

_______________________________________________
buildroot mailing list
buildroot at busybox.net
http://lists.busybox.net/mailman/listinfo/buildroot
Signature · 6 lines
.-----------------.--------------------.------------------.--------------------.
|  Yann E. MORIN  | Real-Time Embedded | /"\ ASCII RIBBON | Erics' conspiracy: |
| +33 662 376 056 | Software  Designer | \ / CAMPAIGN     |  ___               |
| +33 223 225 172 `------------.-------:  X  AGAINST      |  \e/  There is no  |
| http://ymorin.is-a-geek.org/ | _/*\_ | / \ HTML MAIL    |   v   conspiracy.  |
'------------------------------^-------^------------------^--------------------'
#4 lore raw

PATCH 1/2 makedevs: resync device creation with upstream busybox

In reply to Arnout Vandecappelle (Essensium/Mind)
Quoted 9 lines · Arnout Vandecappelle (Essensium/Mind)

In upstream busbyox, the code to create devices has been simplified:
the code for a single and for multiple devices is no longer duplicated.

Since we are going to change the device creation code next, it's convenient to have only one copy to modify.

There are two behavioural changes with this, but they were introduced silently together with other commits in upstream busybox.

Quoted 8 lines

- When mknod() fails, the chmod was still done. This is pointless so it is no longer done now.

- There was a check for mode != -1; however, a mode of -1 would not have worked anyway because all bits would be set for mknod(), which would fail. So this check is redundant.

#5 lore raw

PATCH 1/2 makedevs: resync device creation with upstream busybox

In reply to Arnout Vandecappelle (Essensium/Mind)

Arnout, All,

On 2016-11-05 14:38 +0100, Arnout Vandecappelle (Essensium/Mind) spake thusly:

Quoted 9 lines

In upstream busbyox, the code to create devices has been simplified:
the code for a single and for multiple devices is no longer duplicated.

Since we are going to change the device creation code next, it's convenient to have only one copy to modify.

There are two behavioural changes with this, but they were introduced silently together with other commits in upstream busybox.

Quoted 8 lines

- When mknod() fails, the chmod was still done. This is pointless so it is no longer done now.

- There was a check for mode != -1; however, a mode of -1 would not have worked anyway because all bits would be set for mknod(), which would fail. So this check is redundant.

Regards,
Yann E. MORIN.

Quoted 81 lines
---
 package/makedevs/makedevs.c | 47 +++++++++++++++------------------------------
 1 file changed, 15 insertions(+), 32 deletions(-)
package/makedevs/makedevs.c +15 -32
index cacb144..7092b14 100644
@@ -599,6 +599,8 @@ int main(int argc, char **argv)
 		} else
 		{
 			dev_t rdev;
+			unsigned i;
+			char *full_name_inc;
 
 			if (type == 'p') {
 				mode |= S_IFIFO;
@@ -614,43 +616,24 @@ int main(int argc, char **argv)
 				goto loop;
 			}
 
-			if (count > 0) {
-				int i;
-				char *full_name_inc;
-
-				full_name_inc = xmalloc(strlen(full_name) + 8);
-				for (i = 0; i < count; i++) {
-					sprintf(full_name_inc, "%s%d", full_name, start + i);
-					rdev = makedev(major, minor + i * increment);
-					if (mknod(full_name_inc, mode, rdev) == -1) {
-						bb_perror_msg("line %d: Couldnt create node %s", linenum, full_name_inc);
-						ret = EXIT_FAILURE;
-					}
-					else if (chown(full_name_inc, uid, gid) == -1) {
-						bb_perror_msg("line %d: chown failed for %s", linenum, full_name_inc);
-						ret = EXIT_FAILURE;
-					}
-					if ((mode != -1) && (chmod(full_name_inc, mode) < 0)){
-						bb_perror_msg("line %d: chmod failed for %s", linenum, full_name_inc);
-						ret = EXIT_FAILURE;
-					}
-				}
-				free(full_name_inc);
-			} else {
-				rdev = makedev(major, minor);
-				if (mknod(full_name, mode, rdev) == -1) {
-					bb_perror_msg("line %d: Couldnt create node %s", linenum, full_name);
+			full_name_inc = xmalloc(strlen(full_name) + sizeof(int)*3 + 2);
+			if (count)
+				count--;
+			for (i = start; i <= start + count; i++) {
+				sprintf(full_name_inc, count ? "%s%u" : "%s", full_name, i);
+				rdev = makedev(major, minor + (i - start) * increment);
+				if (mknod(full_name_inc, mode, rdev) < 0) {
+					bb_perror_msg("line %d: can't create node %s", linenum, full_name_inc);
 					ret = EXIT_FAILURE;
-				}
-				else if (chown(full_name, uid, gid) == -1) {
-					bb_perror_msg("line %d: chown failed for %s", linenum, full_name);
+				} else if (chown(full_name_inc, uid, gid) < 0) {
+					bb_perror_msg("line %d: can't chown %s", linenum, full_name_inc);
 					ret = EXIT_FAILURE;
-				}
-				if ((mode != -1) && (chmod(full_name, mode) < 0)){
-					bb_perror_msg("line %d: chmod failed for %s", linenum, full_name);
+				} else if (chmod(full_name_inc, mode) < 0) {
+					bb_perror_msg("line %d: can't chmod %s", linenum, full_name_inc);
 					ret = EXIT_FAILURE;
 				}
 			}
+			free(full_name_inc);
 		}
 loop:
 		free(line);
Signature · 6 lines
2.9.3

_______________________________________________
buildroot mailing list
buildroot at busybox.net
http://lists.busybox.net/mailman/listinfo/buildroot
Signature · 6 lines
.-----------------.--------------------.------------------.--------------------.
|  Yann E. MORIN  | Real-Time Embedded | /"\ ASCII RIBBON | Erics' conspiracy: |
| +33 662 376 056 | Software  Designer | \ / CAMPAIGN     |  ___               |
| +33 223 225 172 `------------.-------:  X  AGAINST      |  \e/  There is no  |
| http://ymorin.is-a-geek.org/ | _/*\_ | / \ HTML MAIL    |   v   conspiracy.  |
'------------------------------^-------^------------------^--------------------'
#6 lore raw

PATCH 1/2 makedevs: resync device creation with upstream busybox

In reply to Arnout Vandecappelle (Essensium/Mind)

Hello,

Quoted 12 lines · Arnout Vandecappelle (Essensium/Mind)

In upstream busbyox, the code to create devices has been simplified:
the code for a single and for multiple devices is no longer duplicated.

Since we are going to change the device creation code next, it's convenient to have only one copy to modify.

There are two behavioural changes with this, but they were introduced silently together with other commits in upstream busybox.

- When mknod() fails, the chmod was still done. This is pointless so it is no longer done now.

Quoted 8 lines

- There was a check for mode != -1; however, a mode of -1 would not have worked anyway because all bits would be set for mknod(), which would fail. So this check is redundant.

---
 package/makedevs/makedevs.c | 47 +++++++++++++++------------------------------
 1 file changed, 15 insertions(+), 32 deletions(-)

Both applied to master, thanks a lot!

Thomas

Signature · 3 lines
Thomas Petazzoni, CTO, Free Electrons
Embedded Linux, Kernel and Android engineering
http://free-electrons.com