Skip to contentSkip to conversation

binman: ensure temp filenames don't collide

PATCH New Stephen Warren 2 participants 6 messages Jul 16, 2018 – Jul 21, 2018

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 binman: ensure temp filenames don't collide

From: Stephen Warren <[email protected]>

The U-Boot Makefile can invoke binman multiple times in parallel. This is problematic because binman uses a static hard-coded temporary file name. If two instances of binman use that filename at the same time, one writing one reading, they may silently read the wrong content or actively detect missing signatures and error out the build process. Fix this by using a PID-specific filename instead.

---
 tools/binman/control.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/binman/control.py +1 -1
index a40b300fdacb..515999278949 100644
@@ -121,7 +121,7 @@ def Binman(options, args):
             # output into a file in our output directly. Then scan it for use
             # in binman.
             dtb_fname = fdt_util.EnsureCompiled(dtb_fname)
-            fname = tools.GetOutputFilename('u-boot-out.dtb')
+            fname = tools.GetOutputFilename('u-boot-out.dtb') + str(os.getpid())
             with open(dtb_fname) as infd:
                 with open(fname, 'wb') as outfd:
                     outfd.write(infd.read())
Signature · 1 line
2.18.0
#1 lore raw

PATCH 1/1 binman: ensure temp filenames don't collide

Hi Stephen,

Quoted 28 lines · Stephen Warren

From: Stephen Warren <[email protected]>

The U-Boot Makefile can invoke binman multiple times in parallel. This is problematic because binman uses a static hard-coded temporary file name. If two instances of binman use that filename at the same time, one writing one reading, they may silently read the wrong content or actively detect missing signatures and error out the build process. Fix this by using a PID-specific filename instead.

---
 tools/binman/control.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/binman/control.py +1 -2
index a40b300fdacb..515999278949 100644
@@ -121,7 +121,7 @@ def Binman(options, args):
             # output into a file in our output directly. Then scan it for use
             # in binman.
             dtb_fname = fdt_util.EnsureCompiled(dtb_fname)
-            fname = tools.GetOutputFilename('u-boot-out.dtb')
+            fname = tools.GetOutputFilename('u-boot-out.dtb') + str(os.getpid())
             with open(dtb_fname) as infd:
                 with open(fname, 'wb') as outfd:
                     outfd.write(infd.read())
--
Quoted 2 lines

2.18.0

But the output directory is itself (normally) a temporary dir. That determines the directly which GetOutputFilename() uses. So I am not sure how this can happen in practice?

Regards,
Simon

#2 lore raw

PATCH 1/1 binman: ensure temp filenames don't collide

Quoted 29 lines · Simon Glass

Hi Stephen,

Quoted 26 lines · Stephen Warren

From: Stephen Warren <[email protected]>

The U-Boot Makefile can invoke binman multiple times in parallel. This is problematic because binman uses a static hard-coded temporary file name. If two instances of binman use that filename at the same time, one writing one reading, they may silently read the wrong content or actively detect missing signatures and error out the build process. Fix this by using a PID-specific filename instead.

---
 tools/binman/control.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/binman/control.py +1 -1
index a40b300fdacb..515999278949 100644
@@ -121,7 +121,7 @@ def Binman(options, args):
             # output into a file in our output directly. Then scan it for use
             # in binman.
             dtb_fname = fdt_util.EnsureCompiled(dtb_fname)
-            fname = tools.GetOutputFilename('u-boot-out.dtb')
+            fname = tools.GetOutputFilename('u-boot-out.dtb') + str(os.getpid())
             with open(dtb_fname) as infd:
                 with open(fname, 'wb') as outfd:
Quoted 8 lines
Quoted 4 lines
tools/binman/control.py +0 -1
                     outfd.write(infd.read())
--

2.18.0

But the output directory is itself (normally) a temporary dir. That determines the directly which GetOutputFilename() uses. So I am not sure how this can happen in practice?

IIRC, the output directory for all 3 files was the same; the root of the build output directory. Perhaps the fact I run "make O=build-xxx" rather than just "make" makes a difference?

#3 lore raw

PATCH 1/1 binman: ensure temp filenames don't collide

Hi Stephen,

Quoted 39 lines · Stephen Warren
Quoted 29 lines · Simon Glass

Hi Stephen,

Quoted 26 lines · Stephen Warren

From: Stephen Warren <[email protected]>

The U-Boot Makefile can invoke binman multiple times in parallel. This is problematic because binman uses a static hard-coded temporary file name. If two instances of binman use that filename at the same time, one writing one reading, they may silently read the wrong content or actively detect missing signatures and error out the build process. Fix this by using a PID-specific filename instead.

---
 tools/binman/control.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/binman/control.py +1 -1
index a40b300fdacb..515999278949 100644
@@ -121,7 +121,7 @@ def Binman(options, args):
             # output into a file in our output directly. Then scan it for use
             # in binman.
             dtb_fname = fdt_util.EnsureCompiled(dtb_fname)
-            fname = tools.GetOutputFilename('u-boot-out.dtb')
+            fname = tools.GetOutputFilename('u-boot-out.dtb') + str(os.getpid())
             with open(dtb_fname) as infd:
                 with open(fname, 'wb') as outfd:
Quoted 8 lines
Quoted 4 lines
tools/binman/control.py +0 -1
                     outfd.write(infd.read())
--

2.18.0

But the output directory is itself (normally) a temporary dir. That determines the directly which GetOutputFilename() uses. So I am not sure how this can happen in practice?

Quoted 3 lines

IIRC, the output directory for all 3 files was the same; the root of the build output directory. Perhaps the fact I run "make O=build-xxx" rather than just "make" makes a difference?

Yes you are right - the -O flag sets the output directory and it no-longer uses a temporary directory.

But if we add a PID to every filename then we end up with multiple output files and we don't know which is right.

I think the correct fix is to change the Makefile to only run binman once. It makes no sense to run it multiple times.

Regards,
Simon

#4 lore raw

PATCH 1/1 binman: ensure temp filenames don't collide

Quoted 46 lines · Simon Glass

Hi Stephen,

Quoted 39 lines · Stephen Warren
Quoted 29 lines · Simon Glass

Hi Stephen,

Quoted 26 lines · Stephen Warren

From: Stephen Warren <[email protected]>

The U-Boot Makefile can invoke binman multiple times in parallel. This is problematic because binman uses a static hard-coded temporary file name. If two instances of binman use that filename at the same time, one writing one reading, they may silently read the wrong content or actively detect missing signatures and error out the build process. Fix this by using a PID-specific filename instead.

---
 tools/binman/control.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/binman/control.py +1 -1
index a40b300fdacb..515999278949 100644
@@ -121,7 +121,7 @@ def Binman(options, args):
             # output into a file in our output directly. Then scan it for use
             # in binman.
             dtb_fname = fdt_util.EnsureCompiled(dtb_fname)
-            fname = tools.GetOutputFilename('u-boot-out.dtb')
+            fname = tools.GetOutputFilename('u-boot-out.dtb') + str(os.getpid())
             with open(dtb_fname) as infd:
                 with open(fname, 'wb') as outfd:
Quoted 8 lines
Quoted 4 lines
tools/binman/control.py +0 -1
                     outfd.write(infd.read())
--

2.18.0

But the output directory is itself (normally) a temporary dir. That determines the directly which GetOutputFilename() uses. So I am not sure how this can happen in practice?

Quoted 3 lines

IIRC, the output directory for all 3 files was the same; the root of the build output directory. Perhaps the fact I run "make O=build-xxx" rather than just "make" makes a difference?

Quoted 8 lines

Yes you are right - the -O flag sets the output directory and it no-longer uses a temporary directory.

But if we add a PID to every filename then we end up with multiple output files and we don't know which is right.

I think the correct fix is to change the Makefile to only run binman once. It makes no sense to run it multiple times.

IIRC, this filename is just a temp file that eventually gets removed after binman runs. Certainly there are not *.${pid} files left around after the build has completed.

#5 lore raw

PATCH 1/1 binman: ensure temp filenames don't collide

Hi Stephen,

Quoted 58 lines · Stephen Warren
Quoted 7 lines · Simon Glass

Hi Stephen,

Quoted 4 lines · Stephen Warren
Quoted 3 lines · Simon Glass

Hi Stephen,

On 16 July 2018 at 16:51, Stephen Warren <[email protected]>

Quoted 5 lines · wrote:
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

From: Stephen Warren <[email protected]>

The U-Boot Makefile can invoke binman multiple times in parallel. This is problematic because binman uses a static hard-coded temporary file name. If two instances of binman use that filename at the same time,

one

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

writing one reading, they may silently read the wrong content or

actively

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

detect missing signatures and error out the build process. Fix this by using a PID-specific filename instead.

---
 tools/binman/control.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

tools/binman/control.py def Binman(options, args): · line 121

Quoted 6 lines
tools/binman/control.py +0 -0
index a40b300fdacb..515999278949 100644
@@ -121,7 +121,7 @@ def Binman(options, args):
             # output into a file in our output directly. Then scan

it for use

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

tools/binman/control.py def Binman(options, args):

Quoted 4 lines
tools/binman/control.py +1 -1
             # in binman.
             dtb_fname = fdt_util.EnsureCompiled(dtb_fname)
-            fname = tools.GetOutputFilename('u-boot-out.dtb')
+            fname = tools.GetOutputFilename('u-boot-out.dtb') +

str(os.getpid())

Quoted 21 lines
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
tools/binman/control.py +0 -1
             with open(dtb_fname) as infd:
                 with open(fname, 'wb') as outfd:
                     outfd.write(infd.read())
--

2.18.0

Quoted 7 lines
Quoted 3 lines

But the output directory is itself (normally) a temporary dir. That determines the directly which GetOutputFilename() uses. So I am not sure how this can happen in practice?

IIRC, the output directory for all 3 files was the same; the root of the build output directory. Perhaps the fact I run "make O=build-xxx" rather than just "make" makes a difference?

Yes you are right - the -O flag sets the output directory and it no-longer uses a temporary directory.

But if we add a PID to every filename then we end up with multiple output files and we don't know which is right.

Quoted 7 lines
Quoted 2 lines

I think the correct fix is to change the Makefile to only run binman once. It makes no sense to run it multiple times.

IIRC, this filename is just a temp file that eventually gets removed after binman runs. Certainly there are not *.${pid} files left around after the build has completed.

This is what I see with your patch:

u-boot-out.dtb198273
u-boot-out.dtb198277
u-boot-out.dtb198280

I think there are two options:

1. Add an arg to binman to indicate which output image to create, so that binman runs three times but only one of those generates the .dtb output file.

2. Use a GNU make pattern rule, as described here:
https://www.gnu.org/software/make/manual/html_node/Pattern-Examples.html#Pattern-Examples

I'll send a patch for the latter which seems easier to me. But I will probably have to update binman to support (1) too.

Regards,
Simon