From: Lennart Poettering Date: Wed, 24 Oct 2018 10:45:06 +0000 (+0200) Subject: sleep: rework write_wakealarm() to take a numeric parameter X-Git-Tag: v240~475^2~6 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=f780e438fa9552809757b77b949b2a3204b65d9a;p=thirdparty%2Fsystemd.git sleep: rework write_wakealarm() to take a numeric parameter Also, let's rename it to rtc_write_wake_alarm(). Both changes together make sure rtc_write_wake_alarm() and rtc_read_time() are more alike in their naming and semantics. --- diff --git a/src/sleep/sleep.c b/src/sleep/sleep.c index f9b822bd93d..f3098a69478 100644 --- a/src/sleep/sleep.c +++ b/src/sleep/sleep.c @@ -199,12 +199,15 @@ static int rtc_read_time(uint64_t *ret_sec) { return safe_atou64(t, ret_sec); } -static int write_wakealarm(const char *str) { +static int rtc_write_wake_alarm(uint64_t sec) { + char buf[DECIMAL_STR_MAX(uint64_t)]; int r; - r = write_string_file("/sys/class/rtc/rtc0/wakealarm", str, 0); + xsprintf(buf, "%" PRIu64, sec); + + r = write_string_file("/sys/class/rtc/rtc0/wakealarm", buf, 0); if (r < 0) - return log_error_errno(r, "Failed to write '%s' to /sys/class/rtc/rtc0/wakealarm: %m", str); + return log_error_errno(r, "Failed to write '%s' to /sys/class/rtc/rtc0/wakealarm: %m", buf); return 0; } @@ -213,8 +216,7 @@ static int execute_s2h(usec_t hibernate_delay_sec) { _cleanup_strv_free_ char **hibernate_modes = NULL, **hibernate_states = NULL, **suspend_modes = NULL, **suspend_states = NULL; - usec_t orig_time, cmp_time; - char time_str[DECIMAL_STR_MAX(uint64_t)]; + usec_t original_time, wake_time, cmp_time; int r; r = parse_sleep_config("suspend", NULL, &suspend_modes, &suspend_states, NULL); @@ -225,18 +227,16 @@ static int execute_s2h(usec_t hibernate_delay_sec) { if (r < 0) return r; - r = rtc_read_time(&orig_time); + r = rtc_read_time(&original_time); if (r < 0) return log_error_errno(r, "Failed to read time: %d", r); - orig_time += hibernate_delay_sec / USEC_PER_SEC; - xsprintf(time_str, "%" PRIu64, orig_time); - - r = write_wakealarm(time_str); + wake_time = original_time + (hibernate_delay_sec / USEC_PER_SEC); + r = rtc_write_wake_alarm(wake_time); if (r < 0) return r; - log_debug("Set RTC wake alarm for %s", time_str); + log_debug("Set RTC wake alarm for %" PRIu64, wake_time); r = execute(suspend_modes, suspend_states); if (r < 0) @@ -247,14 +247,14 @@ static int execute_s2h(usec_t hibernate_delay_sec) { return log_error_errno(r, "Failed to read time: %d", r); /* reset RTC */ - r = write_wakealarm("0"); + r = rtc_write_wake_alarm(0); if (r < 0) return r; log_debug("Woke up at %"PRIu64, cmp_time); /* if woken up after alarm time, hibernate */ - if (cmp_time >= orig_time) + if (cmp_time >= wake_time) r = execute(hibernate_modes, hibernate_states); return r;