[v3,2/4] posix: Do not clobber errno by atfork handlers

Message ID 20210503205419.2890929-2-adhemerval.zanella@linaro.org
State New
Headers show
Series
  • [v3,1/4] posix: Consolidate fork implementation
Related show

Commit Message

Florian Weimer via Libc-alpha May 3, 2021, 8:54 p.m.
Changes from v2:
* Set/restore errno unconditionally.

Changes from v1:
* Move the errno set/restore to parent branch.

---

Checked on x86_64-linux-gnu.
---
 posix/fork.c | 5 +++++
 1 file changed, 5 insertions(+)

-- 
2.30.2

Patch

diff --git a/posix/fork.c b/posix/fork.c
index cc1bdc1232..ff527a74f2 100644
--- a/posix/fork.c
+++ b/posix/fork.c
@@ -99,6 +99,9 @@  __libc_fork (void)
     }
   else
     {
+      /* If _Fork failed, preserve its errno value.  */
+      int save_errno = errno;
+
       /* Release acquired locks in the multi-threaded case.  */
       if (multiple_threads)
 	{
@@ -111,6 +114,8 @@  __libc_fork (void)
 
       /* Run the handlers registered for the parent.  */
       __run_fork_handlers (atfork_run_parent, multiple_threads);
+
+      __set_errno (save_errno);
     }
 
   return pid;