From c8ff2e55625d1c01eaf2a6b47bb958be3d58ecce Mon Sep 17 00:00:00 2001 From: Sami Kerola Date: Sun, 3 Dec 2017 15:43:25 +0000 Subject: rtcwake: wait stdin to settle down before entering a system sleep This can delay entering to system sleep up to 0.28 seconds while discarding input, when stdin is interactive device. [kzak@redhat.com: - add note to the man page] Reference: https://github.com/karelzak/util-linux/issues/527 See-also: a85c39013491713ac0d9e24fd0f07b4fabdcfc17 Signed-off-by: Sami Kerola Signed-off-by: Karel Zak --- sys-utils/rtcwake.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'sys-utils/rtcwake.c') diff --git a/sys-utils/rtcwake.c b/sys-utils/rtcwake.c index 20e40a07f..b63c64627 100644 --- a/sys-utils/rtcwake.c +++ b/sys-utils/rtcwake.c @@ -23,12 +23,14 @@ #include #include #include +#include #include #include #include #include #include #include +#include #include #include @@ -260,6 +262,22 @@ nothing: return NULL; } +static void wait_stdin(struct rtcwake_control *ctl) +{ + struct pollfd fd[] = { + {.fd = STDIN_FILENO, .events = POLLIN} + }; + int tries = 0; + + while (tries < 8 && poll(fd, 1, 10) == 1) { + if (ctl->verbose) + warnx(_("discarding stdin")); + xusleep(250000); + tcflush(STDIN_FILENO, TCIFLUSH); + tries++; + } +} + static void suspend_system(struct rtcwake_control *ctl) { FILE *f = fopen(SYS_POWER_STATE_PATH, "w"); @@ -270,6 +288,8 @@ static void suspend_system(struct rtcwake_control *ctl) } if (!ctl->dryrun) { + if (isatty(STDIN_FILENO)) + wait_stdin(ctl); fprintf(f, "%s\n", ctl->mode_str); fflush(f); } -- cgit v1.2.3-55-g7522