aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDag-Erling Smørgrav <des@FreeBSD.org>2023-03-10 13:24:36 +0000
committerDag-Erling Smørgrav <des@FreeBSD.org>2023-03-10 13:25:16 +0000
commite3b4cb1b32c05ec668b16fe4e858e78b61fe5805 (patch)
tree1c3dd39fe5eb682cc11f19ee5088f7723b19a2ed
parent9f6f64941c02596b01367fd99abdd2c91ba0fe79 (diff)
downloadsrc-e3b4cb1b32c05ec668b16fe4e858e78b61fe5805.tar.gz
src-e3b4cb1b32c05ec668b16fe4e858e78b61fe5805.zip
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
-rw-r--r--libexec/tftpd/tftp-io.c38
-rw-r--r--libexec/tftpd/tftp-transfer.c2
2 files changed, 11 insertions, 29 deletions
diff --git a/libexec/tftpd/tftp-io.c b/libexec/tftpd/tftp-io.c
index 4504946e910f..a39b7cfe1fa3 100644
--- a/libexec/tftpd/tftp-io.c
+++ b/libexec/tftpd/tftp-io.c
@@ -38,8 +38,7 @@ __FBSDID("$FreeBSD$");
#include <assert.h>
#include <errno.h>
-#include <setjmp.h>
-#include <signal.h>
+#include <poll.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
@@ -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,16 +408,9 @@ 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));
return (RP_RECVFROM);
diff --git a/libexec/tftpd/tftp-transfer.c b/libexec/tftpd/tftp-transfer.c
index 91f87a1159a4..1949d00657b6 100644
--- a/libexec/tftpd/tftp-transfer.c
+++ b/libexec/tftpd/tftp-transfer.c
@@ -436,7 +436,7 @@ send_ack:
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 &&