summaryrefslogtreecommitdiffstats
path: root/sys-utils/rtcwake.c
diff options
context:
space:
mode:
authorSami Kerola2017-12-03 16:43:25 +0100
committerKarel Zak2018-01-02 15:14:37 +0100
commitc8ff2e55625d1c01eaf2a6b47bb958be3d58ecce (patch)
tree9d354644f821675fd5d59337c45dced5292751e8 /sys-utils/rtcwake.c
parentsfdisk: wait before re-read (diff)
downloadkernel-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>
Diffstat (limited to 'sys-utils/rtcwake.c')
-rw-r--r--sys-utils/rtcwake.c20
1 files changed, 20 insertions, 0 deletions
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);
}