sys-apps/netplug: Attempt to fix zombie creation

Thanks-to: Lev Danilski <8o55kd+1v8xnjsby8b9k@pokemail.net>
Closes: https://bugs.gentoo.org/631316
Package-Manager: Portage-2.3.64, Repoman-2.3.12
Signed-off-by: Lars Wendler <polynomial-c@gentoo.org>
This commit is contained in:
Lars Wendler
2019-04-21 01:02:50 +02:00
parent b5e3f074c5
commit ca3c5c6b52
2 changed files with 138 additions and 0 deletions

View File

@@ -0,0 +1,65 @@
# Rework SIGCHLD handler to anticipate multiple children dying while the
# handler is being executed.
#
# Without the patch if multiple SIGCHLD signals are received while the signal
# handler is being executed, the first will be left in pending state and the
# extra discarded. Due to the children processing logic in netplugd, the ones
# which were missed will never be waited, left as zombies.
#
# Implementation of the signal handler is following suggested handling in
# https://www.gnu.org/software/libc/manual/html_node/Merged-Signals.html
#
# The patch strives to change only the children wait logic in the signal
# handler, it doesn't try to enhance write call error handling or the unsafe
# call to exit/do_log. Also the formatting is left as it was in the original
# code.
--- a/main.c
+++ b/main.c
@@ -153,17 +153,29 @@ static int child_handler_pipe[2];
static void
child_handler(int sig, siginfo_t *info, void *v)
{
- struct child_exit ce;
- int ret;
- ssize_t s = 0;
+ int old_errno = errno;
assert(sig == SIGCHLD);
- ce.pid = info->si_pid;
- ret = waitpid(info->si_pid, &ce.status, 0);
- if (ret == info->si_pid)
+ while (1)
{
- s = write(child_handler_pipe[1], &ce, sizeof(ce));
+ pid_t pid;
+ int status;
+
+ do
+ {
+ errno = 0;
+ pid = waitpid(WAIT_ANY, &status, WNOHANG);
+ } while (pid <= 0 && errno == EINTR);
+
+ if (pid <= 0)
+ {
+ break;
+ }
+
+ struct child_exit ce = { .pid = pid, .status = status };
+
+ ssize_t s = write(child_handler_pipe[1], &ce, sizeof(ce));
if (s == -1)
{
@@ -171,6 +183,9 @@ child_handler(int sig, siginfo_t *info, void *v)
exit(1);
}
}
+
+ errno = old_errno;
+ return;
}
/* Poll the existing interface state, so we can catch any state

View File

@@ -0,0 +1,73 @@
# Copyright 1999-2019 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2
EAPI=7
inherit toolchain-funcs
DESCRIPTION="Brings up/down ethernet ports automatically with cable detection"
HOMEPAGE="https://www.red-bean.com/~bos/"
SRC_URI="https://www.red-bean.com/~bos/netplug/${P}.tar.bz2"
LICENSE="GPL-2"
SLOT="0"
KEYWORDS="~amd64 ~arm ~mips ~ppc ~ppc64 ~sparc ~x86"
IUSE="debug doc"
DEPEND="doc? ( app-text/ghostscript-gpl
media-gfx/graphviz )"
RDEPEND=""
PATCHES=(
# Remove nested functions, #116140
"${FILESDIR}/${PN}-1.2.9-remove-nest.patch"
# Ignore wireless events
"${FILESDIR}/${PN}-1.2.9-ignore-wireless.patch"
# Fix DOWNANDOUT problem #599400
"${FILESDIR}/${P}-downandout.patch"
# Wait for multiple children in SIGCHLD handler #631316
"${FILESDIR}/${P}-multi-waitpid-sigchld.patch"
)
src_prepare() {
# Remove debug flags from CFLAGS
if ! use debug ; then
sed -i -e "s/ -ggdb3//" Makefile || die
fi
# Remove -O3 and -Werror from CFLAGS
sed -i -e "s/ -O3//" -e "s/ -Werror//" Makefile || die
default
}
src_compile() {
tc-export CC
emake CC="${CC}"
if use doc ; then
emake -C docs/
fi
}
src_install() {
into /
dosbin netplugd
doman man/man8/netplugd.8
dodir /etc/netplug.d
exeinto /etc/netplug.d
newexe "${FILESDIR}/netplug-2" netplug
dodir /etc/netplug
echo "eth*" > "${ED}"/etc/netplug/netplugd.conf || die
dodoc ChangeLog NEWS README TODO
if use doc; then
dodoc docs/state-machine.ps
fi
}