Rename __atomic_fork to __locked_fork and give it &errno as argument.

rtld and libc use different storage, so the initial version would
incorrectly report the failure reason for fork().

There is still a small race condition inside ld.elf_so as it doesn't use
thread-safe errno internally, but that's a more contained internal
issue.
This commit is contained in:
joerg 2020-04-19 01:06:15 +00:00
parent 77abcafc06
commit 25a494ecc4
4 changed files with 13 additions and 11 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: pthread_atfork.c,v 1.13 2020/04/16 14:39:58 joerg Exp $ */
/* $NetBSD: pthread_atfork.c,v 1.14 2020/04/19 01:06:15 joerg Exp $ */
/*-
* Copyright (c) 2002 The NetBSD Foundation, Inc.
@ -31,7 +31,7 @@
#include <sys/cdefs.h>
#if defined(LIBC_SCCS) && !defined(lint)
__RCSID("$NetBSD: pthread_atfork.c,v 1.13 2020/04/16 14:39:58 joerg Exp $");
__RCSID("$NetBSD: pthread_atfork.c,v 1.14 2020/04/19 01:06:15 joerg Exp $");
#endif /* LIBC_SCCS and not lint */
#include "namespace.h"
@ -48,10 +48,10 @@ __weak_alias(fork, _fork)
#endif /* __weak_alias */
pid_t __fork(void); /* XXX */
pid_t __atomic_fork(void) __weak; /* XXX */
pid_t __locked_fork(int *) __weak; /* XXX */
pid_t
__atomic_fork(void)
__locked_fork(int *my_errno)
{
return __fork();
}
@ -164,7 +164,7 @@ fork(void)
SIMPLEQ_FOREACH(iter, &prepareq, next)
(*iter->fn)();
ret = __atomic_fork();
ret = __locked_fork(&errno);
if (ret != 0) {
/*

View File

@ -1,4 +1,4 @@
/* $NetBSD: rtld.c,v 1.204 2020/04/16 14:39:58 joerg Exp $ */
/* $NetBSD: rtld.c,v 1.205 2020/04/19 01:06:15 joerg Exp $ */
/*
* Copyright 1996 John D. Polstra.
@ -40,7 +40,7 @@
#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: rtld.c,v 1.204 2020/04/16 14:39:58 joerg Exp $");
__RCSID("$NetBSD: rtld.c,v 1.205 2020/04/19 01:06:15 joerg Exp $");
#endif /* not lint */
#include <sys/param.h>
@ -1535,13 +1535,15 @@ __dl_cxa_refcount(void *addr, ssize_t delta)
pid_t __fork(void);
__dso_public pid_t
__atomic_fork(void)
__locked_fork(int *my_errno)
{
sigset_t mask;
pid_t result;
_rtld_exclusive_enter(&mask);
result = __fork();
if (result == -1)
*my_errno = errno;
_rtld_exclusive_exit(&mask);
return result;

View File

@ -1,4 +1,4 @@
/* $NetBSD: rtld.h,v 1.139 2020/04/16 14:39:58 joerg Exp $ */
/* $NetBSD: rtld.h,v 1.140 2020/04/19 01:06:15 joerg Exp $ */
/*
* Copyright 1996 John D. Polstra.
@ -370,7 +370,7 @@ __dso_public int dl_iterate_phdr(int (*)(struct dl_phdr_info *, size_t, void *),
__dso_public void *_dlauxinfo(void) __pure;
__dso_public void __dl_cxa_refcount(void *addr, ssize_t delta);
__dso_public pid_t __atomic_fork(void);
__dso_public pid_t __locked_fork(int *);
#if defined(__ARM_EABI__) && !defined(__ARM_DWARF_EH__)
/*

View File

@ -23,6 +23,6 @@
___tls_get_addr;
__gnu_Unwind_Find_exidx;
__dl_cxa_refcount;
__atomic_fork;
__locked_fork;
local: *;
};