From e3b4cb1b32c05ec668b16fe4e858e78b61fe5805 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Dag-Erling=20Sm=C3=B8rgrav?= Date: Fri, 10 Mar 2023 13:24:36 +0000 Subject: [PATCH] tftpd: Use poll() instead of alarm() + setjmp(). While there, don't log an error when timing out waiting for a possible retransmit after a successful transfer. Sponsored by: Klara, Inc. Reviewed by: markj Differential Revision: https://reviews.freebsd.org/D38966 --- libexec/tftpd/tftp-io.c | 38 +++++++++-------------------------- libexec/tftpd/tftp-transfer.c | 2 +- 2 files changed, 11 insertions(+), 29 deletions(-) diff --git a/libexec/tftpd/tftp-io.c b/libexec/tftpd/tftp-io.c index 4504946e910..a39b7cfe1fa 100644 --- a/libexec/tftpd/tftp-io.c +++ b/libexec/tftpd/tftp-io.c @@ -38,8 +38,7 @@ __FBSDID("$FreeBSD$"); #include #include -#include -#include +#include #include #include #include @@ -377,27 +376,19 @@ send_data(int peer, uint16_t block, char *data, int size) /* * Receive a packet + * + * If timeout is negative, no error will be logged on timeout. */ -static jmp_buf timeoutbuf; - -static void -timeout(int sig __unused) -{ - - /* tftp_log(LOG_DEBUG, "Timeout\n"); Inside a signal handler... */ - longjmp(timeoutbuf, 1); -} - int receive_packet(int peer, char *data, int size, struct sockaddr_storage *from, - int thistimeout) + int timeout) { + struct pollfd pfd; struct tftphdr *pkt; struct sockaddr_storage from_local; struct sockaddr_storage *pfrom; socklen_t fromlen; int n; - static int timed_out; if (debug & DEBUG_PACKETS) tftp_log(LOG_DEBUG, @@ -405,13 +396,11 @@ receive_packet(int peer, char *data, int size, struct sockaddr_storage *from, pkt = (struct tftphdr *)data; - signal(SIGALRM, timeout); - timed_out = setjmp(timeoutbuf); - alarm(thistimeout); - - if (timed_out != 0) { - tftp_log(LOG_ERR, "receive_packet: timeout"); - alarm(0); + pfd.fd = peer; + pfd.events = POLLIN; + if (poll(&pfd, 1, 1000 * (timeout < 0 ? -timeout : timeout)) < 1) { + if (timeout > 0) + tftp_log(LOG_ERR, "receive_packet: timeout"); return (RP_TIMEOUT); } @@ -419,15 +408,8 @@ receive_packet(int peer, char *data, int size, struct sockaddr_storage *from, fromlen = sizeof(*pfrom); n = recvfrom(peer, data, size, 0, (struct sockaddr *)pfrom, &fromlen); - alarm(0); - DROPPACKETn("receive_packet", RP_TIMEOUT); - if (n < 0) { - tftp_log(LOG_ERR, "receive_packet: timeout"); - return (RP_TIMEOUT); - } - if (n < 0) { /* No idea what could have happened if it isn't a timeout */ tftp_log(LOG_ERR, "receive_packet: %s", strerror(errno)); diff --git a/libexec/tftpd/tftp-transfer.c b/libexec/tftpd/tftp-transfer.c index 91f87a1159a..1949d00657b 100644 --- a/libexec/tftpd/tftp-transfer.c +++ b/libexec/tftpd/tftp-transfer.c @@ -436,7 +436,7 @@ tftp_receive(int peer, uint16_t *block, struct tftp_stats *ts, for (i = 0; ; i++) { n_data = receive_packet(peer, (char *)rp, pktsize, - NULL, timeoutpacket); + NULL, -timeoutpacket); if (n_data <= 0) break; if (n_data > 0 && -- 2.45.0