git: e3b4cb1b32c0 - main - tftpd: Use poll() instead of alarm() + setjmp().
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Fri, 10 Mar 2023 13:29:20 UTC
The branch main has been updated by des: URL: https://cgit.FreeBSD.org/src/commit/?id=e3b4cb1b32c05ec668b16fe4e858e78b61fe5805 commit e3b4cb1b32c05ec668b16fe4e858e78b61fe5805 Author: Dag-Erling Smørgrav <des@FreeBSD.org> AuthorDate: 2023-03-10 13:24:36 +0000 Commit: Dag-Erling Smørgrav <des@FreeBSD.org> CommitDate: 2023-03-10 13:25:16 +0000 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 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,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 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 &&