Skip to contentSkip to conversation

kbuild: set EXIT trap before creating temporary directory

PATCH In review Masahiro Yamada 3 participants 4 messages Jul 28, 2022 – Aug 7, 2022

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kbuild: set EXIT trap before creating temporary directory

Swap the order of 'mkdir' and 'trap' just in case the subshell is interrupted between 'mkdir' and 'trap' although the effect might be subtle.

Please not this is not a perfect solution to avoid the left-over tmp directory. There are more cases that miss to remove the tmp directory, for example:

- When interrupted, dash does not invoke the EXIT trap (bash does)

- 'rm' command might be interrupted before removing the directory

I am not addressing all the cases since the tmp directory is harmless after all.

---

 scripts/Kconfig.include   | 2 +-
 scripts/Makefile.compiler | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
scripts/Kconfig.include +1 -1
index c1f4222d223d..a0ccceb22cf8 100644
@@ -25,7 +25,7 @@ failure = $(if-success,$(1),n,y)
 
 # $(cc-option,<flag>)
 # Return y if the compiler supports <flag>, n otherwise
-cc-option = $(success,mkdir .tmp_$$; trap "rm -rf .tmp_$$" EXIT; $(CC) -Werror $(CLANG_FLAGS) $(1) -c -x c /dev/null -o .tmp_$$/tmp.o)
+cc-option = $(success,trap "rm -rf .tmp_$$" EXIT; mkdir .tmp_$$; $(CC) -Werror $(CLANG_FLAGS) $(1) -c -x c /dev/null -o .tmp_$$/tmp.o)
 
 # $(ld-option,<flag>)
 # Return y if the linker supports <flag>, n otherwise
scripts/Makefile.compiler +1 -1
index 86ecd2ac874c..94d0d40cddb3 100644
@@ -21,8 +21,8 @@ TMPOUT = $(if $(KBUILD_EXTMOD),$(firstword $(KBUILD_EXTMOD))/).tmp_$$$$
 # automatically cleaned up.
 try-run = $(shell set -e;		\
 	TMP=$(TMPOUT)/tmp;		\
-	mkdir -p $(TMPOUT);		\
 	trap "rm -rf $(TMPOUT)" EXIT;	\
+	mkdir -p $(TMPOUT);		\
 	if ($(1)) >/dev/null 2>&1;	\
 	then echo "$(2)";		\
 	else echo "$(3)";		\
Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 9 lines · Masahiro Yamada

Swap the order of 'mkdir' and 'trap' just in case the subshell is interrupted between 'mkdir' and 'trap' although the effect might be subtle.

Please not this is not a perfect solution to avoid the left-over tmp directory. There are more cases that miss to remove the tmp directory, for example:

Quoted 8 lines

- When interrupted, dash does not invoke the EXIT trap (bash does)

- 'rm' command might be interrupted before removing the directory

I am not addressing all the cases since the tmp directory is harmless after all.

Thanks for the patch!

Quoted 36 lines

---

 scripts/Kconfig.include   | 2 +-
 scripts/Makefile.compiler | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
scripts/Kconfig.include +1 -1
index c1f4222d223d..a0ccceb22cf8 100644
@@ -25,7 +25,7 @@ failure = $(if-success,$(1),n,y)
 
 # $(cc-option,<flag>)
 # Return y if the compiler supports <flag>, n otherwise
-cc-option = $(success,mkdir .tmp_$$; trap "rm -rf .tmp_$$" EXIT; $(CC) -Werror $(CLANG_FLAGS) $(1) -c -x c /dev/null -o .tmp_$$/tmp.o)
+cc-option = $(success,trap "rm -rf .tmp_$$" EXIT; mkdir .tmp_$$; $(CC) -Werror $(CLANG_FLAGS) $(1) -c -x c /dev/null -o .tmp_$$/tmp.o)
 
 # $(ld-option,<flag>)
 # Return y if the linker supports <flag>, n otherwise
scripts/Makefile.compiler +1 -2
index 86ecd2ac874c..94d0d40cddb3 100644
@@ -21,8 +21,8 @@ TMPOUT = $(if $(KBUILD_EXTMOD),$(firstword $(KBUILD_EXTMOD))/).tmp_$$$$
 # automatically cleaned up.
 try-run = $(shell set -e;              \
        TMP=$(TMPOUT)/tmp;              \
-       mkdir -p $(TMPOUT);             \
        trap "rm -rf $(TMPOUT)" EXIT;   \
+       mkdir -p $(TMPOUT);             \
        if ($(1)) >/dev/null 2>&1;      \
        then echo "$(2)";               \
        else echo "$(3)";               \
--

2.34.1

Signature · 2 lines
Thanks,
~Nick Desaulniers
Reviewed-by on v1 1/1
#2 lore raw
In reply to Masahiro Yamada
Quoted 5 lines · Masahiro Yamada

Swap the order of 'mkdir' and 'trap' just in case the subshell is interrupted between 'mkdir' and 'trap' although the effect might be subtle.

Please not this is not a perfect solution to avoid the left-over tmp

not -> note?

Quoted 10 lines

directory. There are more cases that miss to remove the tmp directory, for example:

- When interrupted, dash does not invoke the EXIT trap (bash does)

- 'rm' command might be interrupted before removing the directory

I am not addressing all the cases since the tmp directory is harmless after all.

Quoted 6 lines

---

 scripts/Kconfig.include   | 2 +-
 scripts/Makefile.compiler | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

Do you want to add the same for filechk macro in scripts/Kbuild.include?

Kind regards,
Nicolas

Quoted 30 lines
scripts/Kconfig.include +1 -1
index c1f4222d223d..a0ccceb22cf8 100644
@@ -25,7 +25,7 @@ failure = $(if-success,$(1),n,y)
 
 # $(cc-option,<flag>)
 # Return y if the compiler supports <flag>, n otherwise
-cc-option = $(success,mkdir .tmp_$$; trap "rm -rf .tmp_$$" EXIT; $(CC) -Werror $(CLANG_FLAGS) $(1) -c -x c /dev/null -o .tmp_$$/tmp.o)
+cc-option = $(success,trap "rm -rf .tmp_$$" EXIT; mkdir .tmp_$$; $(CC) -Werror $(CLANG_FLAGS) $(1) -c -x c /dev/null -o .tmp_$$/tmp.o)
 
 # $(ld-option,<flag>)
 # Return y if the linker supports <flag>, n otherwise
scripts/Makefile.compiler +1 -1
index 86ecd2ac874c..94d0d40cddb3 100644
@@ -21,8 +21,8 @@ TMPOUT = $(if $(KBUILD_EXTMOD),$(firstword $(KBUILD_EXTMOD))/).tmp_$$$$
 # automatically cleaned up.
 try-run = $(shell set -e;		\
 	TMP=$(TMPOUT)/tmp;		\
-	mkdir -p $(TMPOUT);		\
 	trap "rm -rf $(TMPOUT)" EXIT;	\
+	mkdir -p $(TMPOUT);		\
 	if ($(1)) >/dev/null 2>&1;	\
 	then echo "$(2)";		\
 	else echo "$(3)";		\
Signature · 1 line
2.34.1
Signature · 3 lines
epost|xmpp: [email protected]          irc://oftc.net/nsc
↳ gpg: 18ed 52db e34f 860e e9fb  c82b 7d97 0932 55a0 ce7f
     -- frykten for herren er opphav til kunnskap --
#3 lore raw
Quoted 9 lines · Nicolas Schier
Quoted 5 lines · Masahiro Yamada

Swap the order of 'mkdir' and 'trap' just in case the subshell is interrupted between 'mkdir' and 'trap' although the effect might be subtle.

Please not this is not a perfect solution to avoid the left-over tmp

not -> note?

Right.
It is a typo.

Quoted 11 lines
Quoted 11 lines

directory. There are more cases that miss to remove the tmp directory, for example:

- When interrupted, dash does not invoke the EXIT trap (bash does)

- 'rm' command might be interrupted before removing the directory

I am not addressing all the cases since the tmp directory is harmless after all.

Quoted 8 lines
Quoted 6 lines

---

 scripts/Kconfig.include   | 2 +-
 scripts/Makefile.compiler | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

Do you want to add the same for filechk macro in scripts/Kbuild.include?

No, I do not think so.

In filechk

   mkdir -p $(dir $@);

this is a directory for the target.
So, the trap does not delete it.

Otherwise, all call-sites must create the directory manually.

$(timeconst-file): kernel/time/timeconst.bc FORCE

        @mkdir -p $(dir $@)
        $(call filechk,gentimeconst)
Quoted 40 lines

Kind regards,
Nicolas

Quoted 29 lines
scripts/Kconfig.include +1 -1
index c1f4222d223d..a0ccceb22cf8 100644
@@ -25,7 +25,7 @@ failure = $(if-success,$(1),n,y)
 
 # $(cc-option,<flag>)
 # Return y if the compiler supports <flag>, n otherwise
-cc-option = $(success,mkdir .tmp_$$; trap "rm -rf .tmp_$$" EXIT; $(CC) -Werror $(CLANG_FLAGS) $(1) -c -x c /dev/null -o .tmp_$$/tmp.o)
+cc-option = $(success,trap "rm -rf .tmp_$$" EXIT; mkdir .tmp_$$; $(CC) -Werror $(CLANG_FLAGS) $(1) -c -x c /dev/null -o .tmp_$$/tmp.o)
 
 # $(ld-option,<flag>)
 # Return y if the linker supports <flag>, n otherwise
scripts/Makefile.compiler +1 -2
index 86ecd2ac874c..94d0d40cddb3 100644
@@ -21,8 +21,8 @@ TMPOUT = $(if $(KBUILD_EXTMOD),$(firstword $(KBUILD_EXTMOD))/).tmp_$$$$
 # automatically cleaned up.
 try-run = $(shell set -e;            \
      TMP=$(TMPOUT)/tmp;              \
-     mkdir -p $(TMPOUT);             \
      trap "rm -rf $(TMPOUT)" EXIT;   \
+     mkdir -p $(TMPOUT);             \
      if ($(1)) >/dev/null 2>&1;      \
      then echo "$(2)";               \
      else echo "$(3)";               \
--
Quoted 1 line

2.34.1

--
epost|xmpp: [email protected] irc://oftc.net/nsc
↳ gpg: 18ed 52db e34f 860e e9fb c82b 7d97 0932 55a0 ce7f
-- frykten for herren er opphav til kunnskap --

Signature · 2 lines
Best Regards
Masahiro Yamada