Util-Linux Archive mirror
 help / color / mirror / Atom feed
From: Goldwyn Rodrigues <rgoldwyn@suse.de>
To: util-linux@vger.kernel.org
Subject: [PATCH] more: exit if POLLHUP or POLLERR on stdin is received
Date: Wed, 6 Dec 2023 13:19:17 -0600	[thread overview]
Message-ID: <2eb7rt74ljbdfj2t4psk7qteiwgpab45nyyuzozvy4omxt4xxe@hfbfxuqaeq3x> (raw)


more command continues to run in case stdin have closed the file and it
takes 100% of CPU. This is because revents on stdin send
POLLIN | POLLHUP | POLLERR once stdin is closed. more receives it even
though it is not requested in events. This is common Linux behaviour to
never mask out POLLHUP or POLLERR. The loop in more_key_command() runs
infinitely because more_poll() returns 0 and read_command() reads 0
bytes.

Steps to reproduce:
1. Setup /etc/systemd/logind.conf with KillUserProcesses=no
2. Add config "Defaults use_pty" in /etc/sudoers
3. Start an ssh session to the machine
4. # sudo su -
5. # more <big enough file>
6. kill the parent ssh process, say close the tab

At this time "more" runs with 100% CPU utilization.

Signed-off-by: Goldwyn Rodrigues <rgoldwyn@suse.com>

diff --git a/text-utils/more.c b/text-utils/more.c
index d4db3d5eb..6ab9dfe40 100644
--- a/text-utils/more.c
+++ b/text-utils/more.c
@@ -1392,6 +1392,11 @@ static int more_poll(struct more_control *ctl, int timeout)
 			abort();
 		}
 	}
+
+	/* Check for POLLERR or POLLHUP in revents */
+	if (pfd[1].revents & (POLLHUP | POLLERR))
+		more_exit(ctl);
+
 	if (pfd[1].revents == 0)
 		return 1;
 	return 0;

-- 
Goldwyn

             reply	other threads:[~2023-12-06 19:18 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-06 19:19 Goldwyn Rodrigues [this message]
2023-12-08 11:45 ` [PATCH] more: exit if POLLHUP or POLLERR on stdin is received Karel Zak
2023-12-08 12:31   ` Karel Zak
2023-12-08 15:08     ` Goldwyn Rodrigues

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=2eb7rt74ljbdfj2t4psk7qteiwgpab45nyyuzozvy4omxt4xxe@hfbfxuqaeq3x \
    --to=rgoldwyn@suse.de \
    --cc=util-linux@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).