diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2014-05-02 12:46:15 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2014-05-02 12:46:15 +0200 |
commit | a4476eb6543505f8685b59b138cb868b32347d71 (patch) | |
tree | 821913af8bc1e0d570c7bf18c996e83459225991 | |
parent | d8290c2ef039c389b2f39704543277865f3693fb (diff) | |
download | busybox-w32-a4476eb6543505f8685b59b138cb868b32347d71.tar.gz busybox-w32-a4476eb6543505f8685b59b138cb868b32347d71.tar.bz2 busybox-w32-a4476eb6543505f8685b59b138cb868b32347d71.zip |
rtcwake: fix incorrect (reversed) rtc/sys adjuestment; code shrink
function old new delta
rtcwake_main 482 462 -20
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | util-linux/rtcwake.c | 55 |
1 files changed, 32 insertions, 23 deletions
diff --git a/util-linux/rtcwake.c b/util-linux/rtcwake.c index 735a29822..33cdbfad4 100644 --- a/util-linux/rtcwake.c +++ b/util-linux/rtcwake.c | |||
@@ -51,7 +51,6 @@ | |||
51 | 51 | ||
52 | #define SYS_RTC_PATH "/sys/class/rtc/%s/device/power/wakeup" | 52 | #define SYS_RTC_PATH "/sys/class/rtc/%s/device/power/wakeup" |
53 | #define SYS_POWER_PATH "/sys/power/state" | 53 | #define SYS_POWER_PATH "/sys/power/state" |
54 | #define DEFAULT_MODE "standby" | ||
55 | 54 | ||
56 | static NOINLINE bool may_wakeup(const char *rtcname) | 55 | static NOINLINE bool may_wakeup(const char *rtcname) |
57 | { | 56 | { |
@@ -122,17 +121,16 @@ static NOINLINE void setup_alarm(int fd, time_t *wakeup, time_t rtc_time) | |||
122 | int rtcwake_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | 121 | int rtcwake_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
123 | int rtcwake_main(int argc UNUSED_PARAM, char **argv) | 122 | int rtcwake_main(int argc UNUSED_PARAM, char **argv) |
124 | { | 123 | { |
125 | time_t rtc_time; | ||
126 | |||
127 | unsigned opt; | 124 | unsigned opt; |
128 | const char *rtcname = NULL; | 125 | const char *rtcname = NULL; |
129 | const char *suspend; | 126 | const char *suspend = "standby"; |
130 | const char *opt_seconds; | 127 | const char *opt_seconds; |
131 | const char *opt_time; | 128 | const char *opt_time; |
132 | 129 | ||
130 | time_t rtc_time; | ||
133 | time_t sys_time; | 131 | time_t sys_time; |
134 | time_t alarm_time = 0; | 132 | time_t alarm_time = alarm_time; |
135 | unsigned seconds = 0; | 133 | unsigned seconds = seconds; /* for compiler */ |
136 | int utc = -1; | 134 | int utc = -1; |
137 | int fd; | 135 | int fd; |
138 | 136 | ||
@@ -148,6 +146,8 @@ int rtcwake_main(int argc UNUSED_PARAM, char **argv) | |||
148 | ; | 146 | ; |
149 | applet_long_options = rtcwake_longopts; | 147 | applet_long_options = rtcwake_longopts; |
150 | #endif | 148 | #endif |
149 | /* Must have -s or -t, exclusive */ | ||
150 | opt_complementary = "s:t:s--t:t--s"; | ||
151 | opt = getopt32(argv, "alud:m:s:t:", &rtcname, &suspend, &opt_seconds, &opt_time); | 151 | opt = getopt32(argv, "alud:m:s:t:", &rtcname, &suspend, &opt_seconds, &opt_time); |
152 | 152 | ||
153 | /* this is the default | 153 | /* this is the default |
@@ -156,17 +156,17 @@ int rtcwake_main(int argc UNUSED_PARAM, char **argv) | |||
156 | */ | 156 | */ |
157 | if (opt & (RTCWAKE_OPT_UTC | RTCWAKE_OPT_LOCAL)) | 157 | if (opt & (RTCWAKE_OPT_UTC | RTCWAKE_OPT_LOCAL)) |
158 | utc = opt & RTCWAKE_OPT_UTC; | 158 | utc = opt & RTCWAKE_OPT_UTC; |
159 | if (!(opt & RTCWAKE_OPT_SUSPEND_MODE)) | 159 | if (opt & RTCWAKE_OPT_SECONDS) { |
160 | suspend = DEFAULT_MODE; | ||
161 | if (opt & RTCWAKE_OPT_SECONDS) | ||
162 | /* alarm time, seconds-to-sleep (relative) */ | 160 | /* alarm time, seconds-to-sleep (relative) */ |
163 | seconds = xatoi(opt_seconds); | 161 | seconds = xatou(opt_seconds); |
164 | if (opt & RTCWAKE_OPT_TIME) | 162 | } else { |
163 | /* RTCWAKE_OPT_TIME */ | ||
165 | /* alarm time, time_t (absolute, seconds since 1/1 1970 UTC) */ | 164 | /* alarm time, time_t (absolute, seconds since 1/1 1970 UTC) */ |
166 | alarm_time = xatol(opt_time); | 165 | if (sizeof(alarm_time) <= sizeof(long)) |
167 | 166 | alarm_time = xatol(opt_time); | |
168 | if (!alarm_time && !seconds) | 167 | else |
169 | bb_error_msg_and_die("must provide wake time"); | 168 | alarm_time = xatoll(opt_time); |
169 | } | ||
170 | 170 | ||
171 | if (utc == -1) | 171 | if (utc == -1) |
172 | utc = rtc_adjtime_is_utc(); | 172 | utc = rtc_adjtime_is_utc(); |
@@ -177,8 +177,9 @@ int rtcwake_main(int argc UNUSED_PARAM, char **argv) | |||
177 | /* this RTC must exist and (if we'll sleep) be wakeup-enabled */ | 177 | /* this RTC must exist and (if we'll sleep) be wakeup-enabled */ |
178 | fd = rtc_xopen(&rtcname, O_RDONLY); | 178 | fd = rtc_xopen(&rtcname, O_RDONLY); |
179 | 179 | ||
180 | if (strcmp(suspend, "on") && !may_wakeup(rtcname)) | 180 | if (strcmp(suspend, "on") != 0) |
181 | bb_error_msg_and_die("%s not enabled for wakeup events", rtcname); | 181 | if (!may_wakeup(rtcname)) |
182 | bb_error_msg_and_die("%s not enabled for wakeup events", rtcname); | ||
182 | 183 | ||
183 | /* relative or absolute alarm time, normalized to time_t */ | 184 | /* relative or absolute alarm time, normalized to time_t */ |
184 | sys_time = time(NULL); | 185 | sys_time = time(NULL); |
@@ -188,21 +189,29 @@ int rtcwake_main(int argc UNUSED_PARAM, char **argv) | |||
188 | rtc_time = rtc_tm2time(&tm_time, utc); | 189 | rtc_time = rtc_tm2time(&tm_time, utc); |
189 | } | 190 | } |
190 | 191 | ||
191 | 192 | if (opt & RTCWAKE_OPT_TIME) { | |
192 | if (alarm_time) { | 193 | /* Correct for RTC<->system clock difference */ |
193 | if (alarm_time < sys_time) | 194 | alarm_time += rtc_time - sys_time; |
195 | if (alarm_time < rtc_time) | ||
196 | /* | ||
197 | * Compat message text. | ||
198 | * I'd say "RTC time is already ahead of ..." instead. | ||
199 | */ | ||
194 | bb_error_msg_and_die("time doesn't go backward to %s", ctime(&alarm_time)); | 200 | bb_error_msg_and_die("time doesn't go backward to %s", ctime(&alarm_time)); |
195 | alarm_time += sys_time - rtc_time; | ||
196 | } else | 201 | } else |
197 | alarm_time = rtc_time + seconds + 1; | 202 | alarm_time = rtc_time + seconds + 1; |
198 | setup_alarm(fd, &alarm_time, rtc_time); | ||
199 | 203 | ||
204 | setup_alarm(fd, &alarm_time, rtc_time); | ||
200 | sync(); | 205 | sync(); |
206 | #if 0 /*debug*/ | ||
207 | printf("sys_time: %s", ctime(&sys_time)); | ||
208 | printf("rtc_time: %s", ctime(&rtc_time)); | ||
209 | #endif | ||
201 | printf("wakeup from \"%s\" at %s", suspend, ctime(&alarm_time)); | 210 | printf("wakeup from \"%s\" at %s", suspend, ctime(&alarm_time)); |
202 | fflush_all(); | 211 | fflush_all(); |
203 | usleep(10 * 1000); | 212 | usleep(10 * 1000); |
204 | 213 | ||
205 | if (strcmp(suspend, "on")) | 214 | if (strcmp(suspend, "on") != 0) |
206 | xopen_xwrite_close(SYS_POWER_PATH, suspend); | 215 | xopen_xwrite_close(SYS_POWER_PATH, suspend); |
207 | else { | 216 | else { |
208 | /* "fake" suspend ... we'll do the delay ourselves */ | 217 | /* "fake" suspend ... we'll do the delay ourselves */ |