[PATCH 5/8] posix: Do not clobber errno by atfork handlers
Florian Weimer
fweimer@redhat.com
Tue Mar 9 11:01:18 GMT 2021
* Adhemerval Zanella via Libc-alpha:
> Checked on x86_64-linux-gnu.
> ---
> posix/fork.c | 4 +++-
> 1 file changed, 3 insertions(+), 1 deletion(-)
>
> diff --git a/posix/fork.c b/posix/fork.c
> index 4c9e60f187..7f27fb8338 100644
> --- a/posix/fork.c
> +++ b/posix/fork.c
> @@ -68,7 +68,7 @@ __libc_fork (void)
> }
>
> pid_t pid = _Fork ();
> -
> + int save_errno = errno;
> if (pid == 0)
> {
> /* Reset the lock state in the multi-threaded case. */
> @@ -107,6 +107,8 @@ __libc_fork (void)
> __run_fork_handlers (pid == 0 ? atfork_run_child : atfork_run_parent,
> multiple_threads);
>
> + if (pid < 0)
> + __set_errno (save_errno);
> return pid;
> }
> weak_alias (__libc_fork, __fork)
Why is this condition correct? Shouldn't it be pid >= 0?
I wonder if this should be part of __run_fork_handlers, so that fork
handlers always observe the original errno value.
Thanks,
Florian
More information about the Libc-alpha
mailing list