git: 1b5f832e268f - main - Revert "fetch: Stop setting an alarm"

From: Dag-Erling Smørgrav <des_at_FreeBSD.org>
Date: Mon, 29 Jun 2026 15:54:18 UTC
The branch main has been updated by des:

URL: https://cgit.FreeBSD.org/src/commit/?id=1b5f832e268f1f1092d0ebfc3bab4a7228aae20b

commit 1b5f832e268f1f1092d0ebfc3bab4a7228aae20b
Author:     Dag-Erling Smørgrav <des@FreeBSD.org>
AuthorDate: 2026-06-29 15:53:53 +0000
Commit:     Dag-Erling Smørgrav <des@FreeBSD.org>
CommitDate: 2026-06-29 15:53:53 +0000

    Revert "fetch: Stop setting an alarm"
    
    This reverts commit 23380f3e6ac9334e85489f92f7f20b700ede3368.
---
 usr.bin/fetch/fetch.1 | 10 ++++------
 usr.bin/fetch/fetch.c | 33 ++++++++++++++++++++++++---------
 2 files changed, 28 insertions(+), 15 deletions(-)

diff --git a/usr.bin/fetch/fetch.1 b/usr.bin/fetch/fetch.1
index 17b96d8d459b..7238226998fc 100644
--- a/usr.bin/fetch/fetch.1
+++ b/usr.bin/fetch/fetch.1
@@ -28,7 +28,7 @@
 .\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 .\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 .\"
-.Dd June 27, 2026
+.Dd October 7, 2023
 .Dt FETCH 1
 .Os
 .Sh NAME
@@ -283,7 +283,7 @@ ignored and the file is fetched unconditionally.
 .It Fl s , -print-size
 Print the size in bytes of each requested file, without fetching it.
 .It Fl T Ar seconds , Fl -timeout= Ns Ar seconds
-Set the network timeout to
+Set timeout value to
 .Ar seconds .
 Overrides the environment variables
 .Ev FTP_TIMEOUT
@@ -322,11 +322,9 @@ message.
 .Sh ENVIRONMENT
 .Bl -tag -width HTTP_TIMEOUT
 .It Ev FTP_TIMEOUT
-Maximum time, in seconds, to wait before giving up on a stalled FTP
-transfer.
+Maximum time, in seconds, to wait before aborting an FTP connection.
 .It Ev HTTP_TIMEOUT
-Maximum time, in seconds, to wait before giving up on a stalled HTTP
-transfer.
+Maximum time, in seconds, to wait before aborting an HTTP connection.
 .El
 .Pp
 See
diff --git a/usr.bin/fetch/fetch.c b/usr.bin/fetch/fetch.c
index 4e2a7596e796..5e4b14289ca7 100644
--- a/usr.bin/fetch/fetch.c
+++ b/usr.bin/fetch/fetch.c
@@ -87,6 +87,7 @@ static pid_t	 pgrp;		/*        our process group */
 static long	 w_secs;	/*    -w: retry delay */
 static int	 family = PF_UNSPEC;	/* -[46]: address family to use */
 
+static int	 sigalrm;	/* SIGALRM received */
 static int	 siginfo;	/* SIGINFO received */
 static int	 sigint;	/* SIGINT received */
 
@@ -172,6 +173,9 @@ static void
 sig_handler(int sig)
 {
 	switch (sig) {
+	case SIGALRM:
+		sigalrm = 1;
+		break;
 	case SIGINFO:
 		siginfo = 1;
 		break;
@@ -432,7 +436,6 @@ fetch(char *URL, const char *path, int *is_http)
 	char *tmppath;
 	int r, tries;
 	unsigned timeout;
-	int timedout = 0;
 	char *ptr;
 
 	f = of = NULL;
@@ -516,7 +519,13 @@ fetch(char *URL, const char *path, int *is_http)
 
 	/* just print size */
 	if (s_flag) {
+		if (timeout)
+			alarm(timeout);
 		r = fetchStat(url, &us, flags);
+		if (timeout)
+			alarm(0);
+		if (sigalrm || sigint)
+			goto signal;
 		if (r == -1) {
 			warnx("%s", fetchLastErrString);
 			goto failure;
@@ -565,7 +574,13 @@ again:
 	size_prev = sb.st_size;
 
 	/* start the transfer */
+	if (timeout)
+		alarm(timeout);
 	f = fetchXGet(url, &us, flags);
+	if (timeout)
+		alarm(0);
+	if (sigalrm || sigint)
+		goto signal;
 	if (f == NULL) {
 		if (i_flag && *is_http && fetchLastErrCode == FETCH_OK &&
 		    strcmp(fetchLastErrString, "Not Modified") == 0) {
@@ -733,7 +748,7 @@ again:
 	/* start the counter */
 	stat_start(&xs, path, us.size, count);
 
-	siginfo = sigint = 0;
+	sigalrm = siginfo = sigint = 0;
 
 	/* suck in the data */
 	setvbuf(f, NULL, _IOFBF, B_size);
@@ -770,7 +785,8 @@ again:
 		if (readcnt != 0)
 			break;
 	}
-	timedout = ferror(f) && errno == ETIMEDOUT;
+	if (!sigalrm)
+		sigalrm = ferror(f) && errno == ETIMEDOUT;
 	signal(SIGINFO, SIG_DFL);
 
 	stat_end(&xs);
@@ -795,20 +811,18 @@ again:
 	}
 
 	/* timed out or interrupted? */
-	if (timedout) {
+	if (sigalrm)
 		warnx("transfer timed out");
-		goto failure;
-	}
 	if (sigint) {
 		warnx("transfer interrupted");
 		goto failure;
 	}
 
-	/* timeout / interrupt before connection completely established? */
+	/* timeout / interrupt before connection completley established? */
 	if (f == NULL)
 		goto failure;
 
-	if (!timedout) {
+	if (!sigalrm) {
 		/* check the status of our files */
 		if (ferror(f))
 			warn("%s", URL);
@@ -836,7 +850,7 @@ again:
 	 * If the transfer timed out and we didn't know how much to
 	 * expect, assume the worst (i.e. we didn't get all of it)
 	 */
-	if (timedout && us.size == -1) {
+	if (sigalrm && us.size == -1) {
 		warnx("%s may be truncated", path);
 		goto failure_keep;
 	}
@@ -1112,6 +1126,7 @@ main(int argc, char *argv[])
 	sa.sa_flags = 0;
 	sa.sa_handler = sig_handler;
 	sigemptyset(&sa.sa_mask);
+	sigaction(SIGALRM, &sa, NULL);
 	sa.sa_flags = SA_RESETHAND;
 	sigaction(SIGINT, &sa, NULL);
 	fetchRestartCalls = 0;