busybox/networking/udhcp
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
..
arpping.c dhcp: merge leases.c and static_leases.c into dhcpd.c 2016-10-04 00:56:58 +02:00
common.c udhcpc6: make -O OPT work 2017-06-28 19:18:17 +02:00
common.h udhcpd: fix "not dying on SIGTERM" 2018-03-10 19:34:39 +01:00
Config.src udhcpc6: --help fixes 2018-01-16 22:23:38 +01:00
d6_common.h udhcpc6: add support of prefix delegation 2018-01-16 21:39:14 +01:00
d6_dhcpc.c udhcpd: fix "not dying on SIGTERM" 2018-03-10 19:34:39 +01:00
d6_packet.c udhcpc[6]: make log2 messages for chaddr field indented like the rest 2017-09-29 18:02:01 +02:00
d6_socket.c udhcpc6: additional fix for ppp interface type 2018-01-23 16:46:31 +01:00
dhcpc.c udhcpd: fix "not dying on SIGTERM" 2018-03-10 19:34:39 +01:00
dhcpc.h udhcpc6: make -O OPT work 2017-06-28 19:18:17 +02:00
dhcpd.c udhcpd: fix "not dying on SIGTERM" 2018-03-10 19:34:39 +01:00
dhcpd.h dhcp: merge leases.c and static_leases.c into dhcpd.c 2016-10-04 00:56:58 +02:00
dhcprelay.c dhcprelay: code shrink 2018-01-08 10:14:17 +01:00
domain_codec.c whitespace and comment format fixes, no code changes 2017-10-05 14:40:24 +02:00
dumpleases.c getopt32: remove opt_complementary 2017-08-08 21:55:02 +02:00
Kbuild.src fix breakage found by mass one-applet builds 2016-12-23 02:42:26 +01:00
packet.c udhcpc[6]: make log2 messages for chaddr field indented like the rest 2017-09-29 18:02:01 +02:00
signalpipe.c udhcpd: fix "not dying on SIGTERM" 2018-03-10 19:34:39 +01:00
socket.c udhcpc: downgrade "MAC X:X:X:X:X:X" message to log2 level 2017-07-24 13:37:38 +02:00