busybox/examples
Denys Vlasenko 3293bc1469 udhcpd: fix "not dying on SIGTERM"
Fixes:
	commit 52a515d187
	"udhcp: use poll() instead of select()"
	Feb 16 2017

udhcp_sp_read() is meant to check whether signal pipe indeed has some data to read.
In the above commit, it was changed as follows:

-	if (!FD_ISSET(signal_pipe.rd, rfds))
+	if (!pfds[0].revents)
		return 0;

The problem is, the check was working for select() purely by accident.
Caught signal interrupts select()/poll() syscalls, they return with EINTR
(regardless of SA_RESTART flag in sigaction). _Then_ signal handler is invoked.
IOW: they can't see any changes to fd state caused by signal haldler
(in our case, signal handler makes signal pipe ready to be read).

For select(), it means that rfds[] bit array is unmodified, bit of signal
pipe's read fd is still set, and the above check "works": it thinks select()
says there is data to read.

This accident does not work for poll(): .revents stays clear, and we do not
try reading signal pipe as we should. In udhcpd, we fall through and block
in socket read. Further SIGTERM signals simply cause socket read to be
interrupted and then restarted (since SIGTERM handler has SA_RESTART=1).

Fixing this as follows: remove the check altogether. Set signal pipe read fd
to nonblocking mode. Always read it in udhcp_sp_read().
If read fails, assume it's EAGAIN and return 0 ("no signal seen").

udhcpd avoids reading signal pipe on every recvd packet by looping if EINTR
(using safe_poll()) - thus ensuring we have correct .revents for all fds -
and calling udhcp_sp_read() only if pfds[0].revents!=0.

udhcpc performs much fewer reads (typically it sleeps >99.999% of the time),
there is no need to optimize it: can call udhcp_sp_read() after each poll
unconditionally.

To robustify socket reads, unconditionally set pfds[1].revents=0
in udhcp_sp_fd_set() (which is before poll), and check it before reading
network socket in udhcpd.

TODO:
This might still fail: if pfds[1].revents=POLLIN, socket read may still block.
There are rare cases when select/poll indicates that data can be read,
but then actual read still blocks (one such case is UDP packets with
wrong checksum). General advise is, if you use a poll/select loop,
keep all your fds nonblocking.
Maybe we should also do that to our network sockets?

function                                             old     new   delta
udhcp_sp_setup                                        55      65     +10
udhcp_sp_fd_set                                       54      60      +6
udhcp_sp_read                                         46      36     -10
udhcpd_main                                         1451    1437     -14
udhcpc_main                                         2723    2708     -15
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 2/3 up/down: 16/-39)            Total: -23 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-03-10 19:34:39 +01:00
..
bootfloppy *: trailing empty lines removed 2010-07-26 01:49:12 +02:00
udhcp examples: make udhcpc script handle /32 netmasks 2017-04-06 00:53:43 +02:00
var_service udhcpd: fix "not dying on SIGTERM" 2018-03-10 19:34:39 +01:00
android-build build system: make CONFIG_EXTRA_LDFLAGS go to LDFLAGS, not EXTRA_LDFLAGS 2015-04-14 14:15:15 +02:00
busybox.spec Fixes to usage text; updated busybox.spec 2009-10-02 11:37:17 +02:00
depmod remove "local" bashism from a few scripts 2017-01-24 20:52:42 +01:00
depmod.pl depmod.pl: output correct dep format according to kernel version 2011-09-09 18:58:08 +02:00
devfsd.conf nameif: extended matching (Nico Erfurth <masta@perlgolf.de>) 2007-12-24 14:09:19 +00:00
dnsd.conf new applet. Thanks Roberto A. Foglietta 2006-01-25 14:19:11 +00:00
inetd.conf Add an example inetd.conf file 2004-04-07 10:34:16 +00:00
inittab examples/inittab: fix a few incorrect statements about init 2013-03-29 14:35:44 +01:00
linux-2.6.30_proc_self_exe.patch Added kernel patch for /proc/self/exe to examples 2009-07-02 12:04:50 +02:00
mdev_fat.conf examples/mdev_fat.conf: document that newer mdev exposes path 2015-02-07 18:14:39 +01:00
mdev.conf mdev.conf: rename hw_random to hwrng 2016-12-09 16:12:15 -05:00
mdev.conf.change_blockdev.sh fix more instances of ": $((a++))" in shell scripts 2017-07-15 19:20:45 +02:00
mk2knr.pl Major rework of the directory structure and the entire build system. 2001-10-24 05:00:29 +00:00
undeb undeb: clean up 2016-04-04 01:35:34 -04:00
unrpm unrpm: clean up 2016-04-04 01:39:17 -04:00
zcip.script zcip.script: fix $ip environment variable 2011-08-09 04:41:00 +02:00