unix exec: avoid atexit handlers when child exits
authorPatrick Steinhardt <ps@pks.im>
Mon, 28 Aug 2017 18:57:19 +0000 (20:57 +0200)
committerVladimir Serbinenko <phcoder@gmail.com>
Wed, 30 Aug 2017 13:20:13 +0000 (15:20 +0200)
commite75cf4a58b5eaf482804e5e1b2cc7d4399df350e
tree0ccd756bd2a8c7f975ca06923adf3ed5815aca0c
parent3d86efda0074285fb86aa6ffe5d97327ebd134a4
unix exec: avoid atexit handlers when child exits

The `grub_util_exec_redirect_all` helper function can be used to
spawn an executable and redirect its output to some files. After calling
`fork()`, the parent will wait for the child to terminate with
`waitpid()` while the child prepares its file descriptors, environment
and finally calls `execvp()`. If something in the children's setup
fails, it will stop by calling `exit(127)`.

Calling `exit()` will cause any function registered via `atexit()` to be
executed, which is usually the wrong thing to do in a child. And
actually, one can easily observe faulty behaviour on musl-based systems
without modprobe(8) installed: executing `grub-install --help` will call
`grub_util_exec_redirect_all` with "modprobe", which obviously fails if
modprobe(8) is not installed. Due to the child now exiting and invoking
the `atexit()` handlers, it will clean up some data structures of the
parent and cause it to be deadlocked in the `waitpid()` syscall.

The issue can easily be fixed by calling `_exit(127)` instead, which is
especially designed to be called when the atexit-handlers should not be
executed.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
grub-core/osdep/unix/exec.c