aboutsummaryrefslogtreecommitdiff
path: root/networking/udhcp/dhcpd.c
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2018-03-10 19:01:48 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2018-03-10 19:34:39 +0100
commit3293bc146985c56790033409facc0ad64a471084 (patch)
treeb520ce32d3853a9126414b4be5607f50abecf217 /networking/udhcp/dhcpd.c
parent3936222a0ac793f94824fbcb054148ceb8898425 (diff)
downloadbusybox-w32-3293bc146985c56790033409facc0ad64a471084.tar.gz
busybox-w32-3293bc146985c56790033409facc0ad64a471084.tar.bz2
busybox-w32-3293bc146985c56790033409facc0ad64a471084.zip
udhcpd: fix "not dying on SIGTERM"
Fixes: commit 52a515d18724bbb34e3ccbbb0218efcc4eccc0a8 "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>
Diffstat (limited to 'networking/udhcp/dhcpd.c')
-rw-r--r--networking/udhcp/dhcpd.c34
1 files changed, 18 insertions, 16 deletions
diff --git a/networking/udhcp/dhcpd.c b/networking/udhcp/dhcpd.c
index 093239536..db3ab4f00 100644
--- a/networking/udhcp/dhcpd.c
+++ b/networking/udhcp/dhcpd.c
@@ -915,20 +915,18 @@ int udhcpd_main(int argc UNUSED_PARAM, char **argv)
915 915
916 udhcp_sp_fd_set(pfds, server_socket); 916 udhcp_sp_fd_set(pfds, server_socket);
917 tv = timeout_end - monotonic_sec(); 917 tv = timeout_end - monotonic_sec();
918 retval = 0; 918 /* Block here waiting for either signal or packet */
919 if (!server_config.auto_time || tv > 0) { 919 retval = safe_poll(pfds, 2, server_config.auto_time ? tv * 1000 : -1);
920 retval = poll(pfds, 2, server_config.auto_time ? tv * 1000 : -1); 920 if (retval <= 0) {
921 } 921 if (retval == 0) {
922 if (retval == 0) { 922 write_leases();
923 write_leases(); 923 goto continue_with_autotime;
924 goto continue_with_autotime; 924 }
925 } 925 /* < 0 and not EINTR: should not happen */
926 if (retval < 0 && errno != EINTR) { 926 bb_perror_msg_and_die("poll");
927 log1("error on select");
928 continue;
929 } 927 }
930 928
931 switch (udhcp_sp_read(pfds)) { 929 if (pfds[0].revents) switch (udhcp_sp_read()) {
932 case SIGUSR1: 930 case SIGUSR1:
933 bb_error_msg("received %s", "SIGUSR1"); 931 bb_error_msg("received %s", "SIGUSR1");
934 write_leases(); 932 write_leases();
@@ -938,12 +936,16 @@ int udhcpd_main(int argc UNUSED_PARAM, char **argv)
938 bb_error_msg("received %s", "SIGTERM"); 936 bb_error_msg("received %s", "SIGTERM");
939 write_leases(); 937 write_leases();
940 goto ret0; 938 goto ret0;
941 case 0: /* no signal: read a packet */
942 break;
943 default: /* signal or error (probably EINTR): back to select */
944 continue;
945 } 939 }
946 940
941 /* Is it a packet? */
942 if (!pfds[1].revents)
943 continue; /* no */
944
945 /* Note: we do not block here, we block on poll() instead.
946 * Blocking here would prevent SIGTERM from working:
947 * socket read inside this call is restarted on caught signals.
948 */
947 bytes = udhcp_recv_kernel_packet(&packet, server_socket); 949 bytes = udhcp_recv_kernel_packet(&packet, server_socket);
948 if (bytes < 0) { 950 if (bytes < 0) {
949 /* bytes can also be -2 ("bad packet data") */ 951 /* bytes can also be -2 ("bad packet data") */