linux-stable/scripts
Masahiro Yamada 42bb1e6b7f scripts: handle BrokenPipeError for python scripts
[ Upstream commit 87c7ee67de ]

In the follow-up of commit fb3041d61f ("kbuild: fix SIGPIPE error
message for AR=gcc-ar and AR=llvm-ar"), Kees Cook pointed out that
tools should _not_ catch their own SIGPIPEs [1] [2].

Based on his feedback, LLVM was fixed [3].

However, Python's default behavior is to show noisy bracktrace when
SIGPIPE is sent. So, scripts written in Python are basically in the
same situation as the buggy llvm tools.

Example:

  $ make -s allnoconfig
  $ make -s allmodconfig
  $ scripts/diffconfig .config.old .config | head -n1
  -ALIX n
  Traceback (most recent call last):
    File "/home/masahiro/linux/scripts/diffconfig", line 132, in <module>
      main()
    File "/home/masahiro/linux/scripts/diffconfig", line 130, in main
      print_config("+", config, None, b[config])
    File "/home/masahiro/linux/scripts/diffconfig", line 64, in print_config
      print("+%s %s" % (config, new_value))
  BrokenPipeError: [Errno 32] Broken pipe

Python documentation [4] notes how to make scripts die immediately and
silently:

  """
  Piping output of your program to tools like head(1) will cause a
  SIGPIPE signal to be sent to your process when the receiver of its
  standard output closes early. This results in an exception like
  BrokenPipeError: [Errno 32] Broken pipe. To handle this case,
  wrap your entry point to catch this exception as follows:

    import os
    import sys

    def main():
        try:
            # simulate large output (your code replaces this loop)
            for x in range(10000):
                print("y")
            # flush output here to force SIGPIPE to be triggered
            # while inside this try block.
            sys.stdout.flush()
        except BrokenPipeError:
            # Python flushes standard streams on exit; redirect remaining output
            # to devnull to avoid another BrokenPipeError at shutdown
            devnull = os.open(os.devnull, os.O_WRONLY)
            os.dup2(devnull, sys.stdout.fileno())
            sys.exit(1)  # Python exits with error code 1 on EPIPE

    if __name__ == '__main__':
        main()

  Do not set SIGPIPE’s disposition to SIG_DFL in order to avoid
  BrokenPipeError. Doing that would cause your program to exit
  unexpectedly whenever any socket connection is interrupted while
  your program is still writing to it.
  """

Currently, tools/perf/scripts/python/intel-pt-events.py seems to be the
only script that fixes the issue that way.

tools/perf/scripts/python/compaction-times.py uses another approach
signal.signal(signal.SIGPIPE, signal.SIG_DFL) but the Python
documentation clearly says "Don't do it".

I cannot fix all Python scripts since there are so many.
I fixed some in the scripts/ directory.

[1]: https://lore.kernel.org/all/202211161056.1B9611A@keescook/
[2]: https://github.com/llvm/llvm-project/issues/59037
[3]: 4787efa380
[4]: https://docs.python.org/3/library/signal.html#note-on-sigpipe

Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
Reviewed-by: Nick Desaulniers <ndesaulniers@google.com>
Reviewed-by: Nicolas Schier <nicolas@fjasle.eu>
Signed-off-by: Sasha Levin <sashal@kernel.org>
2023-03-17 08:45:15 +01:00
..
atomic Merge branch 'kcsan' of git://git.kernel.org/pub/scm/linux/kernel/git/paulmck/linux-rcu into locking/core 2020-10-09 08:56:02 +02:00
basic kbuild: introduce hostprogs-always-y and userprogs-always-y 2020-08-10 01:32:59 +09:00
clang-tools scripts: handle BrokenPipeError for python scripts 2023-03-17 08:45:15 +01:00
coccinelle Merge branch 'for-5.10' of git://git.kernel.org/pub/scm/linux/kernel/git/jlawall/linux 2020-10-18 14:20:35 -07:00
dtc scripts/dtc: Call pkg-config POSIXly correct 2022-04-08 14:40:15 +02:00
dummy-tools kbuild: dummy-tools: avoid tmpdir leak in dummy gcc 2022-08-29 11:29:54 +02:00
gcc-plugins gcc-plugins: latent_entropy: use /dev/urandom 2022-04-20 09:23:26 +02:00
gdb scripts/gdb: change kernel config dumping method 2022-06-14 18:32:45 +02:00
genksyms genksyms: keywords: Use __restrict not _restrict 2020-08-18 20:16:46 +09:00
kconfig kconfig: fix failing to generate auto.conf 2022-02-23 12:01:07 +01:00
ksymoops
mod modpost: fix section mismatch check for exported init/exit sections 2022-06-29 08:59:54 +02:00
package builddeb: clean generated package content 2023-03-11 16:39:46 +01:00
selinux selinux: use "grep -E" instead of "egrep" 2022-10-26 13:25:17 +02:00
tracing ftrace/scripts: Update the instructions for ftrace-bisect.sh 2023-02-01 08:23:22 +01:00
.gitignore kbuild: preprocess module linker script 2020-09-25 00:36:41 +09:00
Kbuild.include kbuild: remove the target in signal traps when interrupted 2022-10-26 13:25:43 +02:00
Kconfig.include kconfig: unify cc-option and as-option 2020-06-17 10:38:42 +09:00
Lindent
Makefile certs: Add ability to preload revocation certs 2021-06-30 08:47:30 -04:00
Makefile.asm-generic
Makefile.build x86/retbleed: Add fine grained Kconfig knobs 2022-07-25 11:26:50 +02:00
Makefile.clean kbuild: introduce hostprogs-always-y and userprogs-always-y 2020-08-10 01:32:59 +09:00
Makefile.dtbinst
Makefile.extrawarn Makefile.extrawarn: Move -Wcast-function-type-strict to W=1 2022-10-15 07:55:50 +02:00
Makefile.gcc-plugins gcc-plugins: Undefine LATENT_ENTROPY_PLUGIN when plugin disabled for a file 2022-08-25 11:38:10 +02:00
Makefile.headersinst
Makefile.host kbuild: sort hostprogs before passing it to ifneq 2020-08-10 01:32:59 +09:00
Makefile.kasan kasan: fix hwasan build for gcc 2021-04-28 13:40:02 +02:00
Makefile.kcov kbuild: include scripts/Makefile.* only when relevant CONFIG is enabled 2020-08-10 01:32:59 +09:00
Makefile.kcsan Kbuild updates for v5.10 2020-10-22 13:13:57 -07:00
Makefile.lib dt-bindings: Use json for processed-schema* 2020-08-19 14:31:57 -06:00
Makefile.modfinal kbuild: preprocess module linker script 2020-09-25 00:36:41 +09:00
Makefile.modinst
Makefile.modpost kbuild: Fix include path in scripts/Makefile.modpost 2022-09-05 10:28:55 +02:00
Makefile.modsign
Makefile.package kbuild: fix broken builds because of GZIP,BZIP2,LZOP variables 2020-06-11 20:14:41 +09:00
Makefile.ubsan ubsan: remove CONFIG_UBSAN_OBJECT_SIZE 2022-04-13 21:01:10 +02:00
Makefile.userprogs kbuild: add infrastructure to build userspace programs 2020-05-17 18:52:01 +09:00
adjust_autoksyms.sh
asn1_compiler.c
bin2c.c
bloat-o-meter scripts: switch explicitly to Python 3 2021-05-22 11:40:55 +02:00
bootgraph.pl
bpf_helpers_doc.py bpf: Add struct bpf_redir_neigh forward declaration to BPF helper defs 2020-10-29 15:19:04 +01:00
cc-can-link.sh
check-sysctl-docs
check_extable.sh
checkincludes.pl
checkkconfigsymbols.py scripts: handle BrokenPipeError for python scripts 2023-03-17 08:45:15 +01:00
checkpatch.pl checkpatch: fix unescaped left brace 2020-12-30 11:53:56 +01:00
checkstack.pl scripts/checkstack.pl: fix arm sp regex 2020-05-26 00:03:16 +09:00
checksyscalls.sh
checkversion.pl
clang-version.sh
cleanfile
cleanpatch
coccicheck scripts: coccicheck: Change default condition for parallelism 2020-10-12 10:37:56 +02:00
config tweewide: Fix most Shebang lines 2021-05-22 11:40:55 +02:00
const_structs.checkpatch const_structs.checkpatch: add pinctrl_ops and pinmux_ops 2020-10-16 11:11:21 -07:00
decode_stacktrace.sh scripts/decode_stacktrace.sh: guess path to vmlinux by release name 2020-08-07 11:33:21 -07:00
decodecode scripts/decodecode: add the capability to supply the program counter 2020-10-13 18:38:26 -07:00
depmod.sh depmod: handle the case of /sbin/depmod without /sbin in PATH 2021-01-12 20:18:16 +01:00
dev-needs.sh scripts/dev-needs: Add script to list device dependencies 2020-09-04 18:19:37 +02:00
diffconfig scripts: handle BrokenPipeError for python scripts 2023-03-17 08:45:15 +01:00
documentation-file-ref-check scripts: documentation-file-ref-check: Add line break before exit 2020-04-15 15:13:13 -06:00
export_report.pl
extract-cert.c cert host tools: Stop complaining about deprecated OpenSSL functions 2022-11-16 09:57:17 +01:00
extract-ikconfig
extract-module-sig.pl
extract-sys-certs.pl
extract-vmlinux
extract_xc3028.pl
faddr2line scripts/faddr2line: Fix regression in name resolution on ppc64le 2022-12-08 11:23:54 +01:00
file-size.sh
find-unused-docs.sh
gcc-goto.sh
gcc-ld
gcc-version.sh
gcc-x86_32-has-stack-protector.sh
gcc-x86_64-has-stack-protector.sh
gen_autoksyms.sh kbuild: fix CONFIG_TRIM_UNUSED_KSYMS build for ppc64 2021-02-26 10:13:01 +01:00
gen_ksymdeps.sh kbuild: Fix 'no symbols' warning when CONFIG_TRIM_UNUSD_KSYMS=y 2021-09-18 13:40:16 +02:00
get_abi.pl tweewide: Fix most Shebang lines 2021-05-22 11:40:55 +02:00
get_dvb_firmware
get_maintainer.pl get_maintainer: exclude MAINTAINERS file(s) from --git-fallback 2020-10-16 11:11:19 -07:00
gfp-translate
headerdep.pl
headers_check.pl
headers_install.sh Merge branch 'work.fdpic' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs 2020-08-07 13:29:39 -07:00
insert-sys-cert.c
jobserver-exec
kallsyms.c kallsyms: fix nonconverging kallsyms table with lld 2021-02-17 11:02:24 +01:00
kernel-doc stddef: Introduce struct_group() helper macro 2022-11-25 17:45:54 +01:00
ld-version.sh
leaking_addresses.pl leaking_addresses: Always print a trailing newline 2021-11-18 14:03:57 +01:00
link-vmlinux.sh kbuild: Unify options for BTF generation for vmlinux and modules 2022-10-28 12:57:12 +02:00
lld-version.sh scripts/lld-version.sh: Rewrite based on upstream ld-version.sh 2021-11-21 13:46:37 +01:00
makelst
markup_oops.pl
mkcompile_h kbuild: mkcompile_h: consider timestamp if KBUILD_BUILD_TIMESTAMP is set 2021-07-25 14:36:16 +02:00
mkmakefile
mksysmap mksysmap: Fix the mismatch of 'L0' symbols in System.map 2022-09-23 14:17:00 +02:00
mkuboot.sh
module.lds.S modules: Ensure natural alignment for .altinstructions and __bug_table sections 2022-08-25 11:38:19 +02:00
modules-check.sh kbuild: make module name conflict fatal error 2020-05-26 00:03:16 +09:00
nsdeps scripts: add dummy report mode to add_namespace.cocci 2020-07-10 14:19:58 +02:00
objdiff
pahole-flags.sh kbuild: Add skip_encoding_btf_enum64 option to pahole 2022-10-28 12:57:12 +02:00
parse-maintainers.pl
patch-kernel
profile2linkerlist.pl
prune-kernel
recordmcount.c ftrace: Have recordmcount use w8 to read relp->r_info in arm64_is_fake_mcount 2021-03-09 11:11:14 +01:00
recordmcount.h recordmcount: Correct st_shndx handling 2021-06-30 08:47:23 -04:00
recordmcount.pl recordmcount.pl: fix typo in s390 mcount regex 2022-01-05 12:40:29 +01:00
setlocalversion scripts/setlocalversion: make git describe output more reliable 2020-09-25 02:28:12 +09:00
show_delta tweewide: Fix most Shebang lines 2021-05-22 11:40:55 +02:00
sign-file.c cert host tools: Stop complaining about deprecated OpenSSL functions 2022-11-16 09:57:17 +01:00
sorttable.c s390/kernel: expand exception table logic to allow new handling options 2020-07-20 10:55:50 +02:00
sorttable.h
spdxcheck-test.sh
spdxcheck.py scripts/spdxcheck.py: handle license identifiers in XML comments 2020-10-02 11:31:26 +02:00
spelling.txt Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jikos/trivial 2020-10-15 15:11:56 -07:00
sphinx-pre-install scripts: sphinx-pre-install: Fix ctex support on Debian 2022-01-27 10:54:36 +01:00
split-man.pl tweewide: Fix most Shebang lines 2021-05-22 11:40:55 +02:00
stackdelta
stackusage
subarch.include
tags.sh scripts/tags.sh: fix incompatibility with PCRE2 2023-03-03 11:44:51 +01:00
tools-support-relr.sh Makefile: fix GDB warning with CONFIG_RELR 2021-07-14 16:55:53 +02:00
unifdef.c
ver_linux
xen-hypercalls.sh
xz_wrap.sh kbuild: add variables for compression tools 2020-06-06 23:42:01 +09:00