diff options
author | Sami Kerola | 2017-12-03 16:43:25 +0100 |
---|---|---|
committer | Karel Zak | 2018-01-02 15:14:37 +0100 |
commit | c8ff2e55625d1c01eaf2a6b47bb958be3d58ecce (patch) | |
tree | 9d354644f821675fd5d59337c45dced5292751e8 | |
parent | sfdisk: wait before re-read (diff) | |
download | kernel-qcow2-util-linux-c8ff2e55625d1c01eaf2a6b47bb958be3d58ecce.tar.gz kernel-qcow2-util-linux-c8ff2e55625d1c01eaf2a6b47bb958be3d58ecce.tar.xz kernel-qcow2-util-linux-c8ff2e55625d1c01eaf2a6b47bb958be3d58ecce.zip |
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 <kerolasa@iki.fi>
Signed-off-by: Karel Zak <kzak@redhat.com>
-rw-r--r-- | sys-utils/rtcwake.8.in | 7 | ||||
-rw-r--r-- | sys-utils/rtcwake.c | 20 |
2 files changed, 25 insertions, 2 deletions
diff --git a/sys-utils/rtcwake.8.in b/sys-utils/rtcwake.8.in index b35860113..72eb0e34f 100644 --- a/sys-utils/rtcwake.8.in +++ b/sys-utils/rtcwake.8.in @@ -48,8 +48,11 @@ an alarm up to 24 hours in the future. .PP The suspend setup maybe be interrupted by active hardware; for example wireless USB input devices that continue to send events for some fraction of a second after the -return key is pressed. In this case is better to use sleep command before invoking -suspend by rtcwake. +return key is pressed. +.BR rtcwake +tries to avoid this problem and it waits to terminal to settle down before +entering a system sleep. + .SH OPTIONS .TP .BR \-A , " \-\-adjfile " \fIfile 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 <fcntl.h> #include <getopt.h> #include <linux/rtc.h> +#include <poll.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/ioctl.h> #include <sys/time.h> #include <sys/types.h> +#include <termios.h> #include <time.h> #include <unistd.h> @@ -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); } |