diff options
| author | MarijnS95 <marijns95@gmail.com> | 2020-02-12 12:20:32 +0100 |
|---|---|---|
| committer | MarijnS95 <marijns95@gmail.com> | 2020-02-25 13:49:35 +0100 |
| commit | 708e4d63619981fab11b72bfe5d104a0cff7e64c (patch) | |
| tree | 8893de624cd97cb50d60dfcd53640099cfbc9dd2 | |
| parent | d0959b5b9cff4e66f6dd14b39a5099a7e7ab2289 (diff) | |
timekeep: Ensure ats_2 file is always 8 bytes.
`long` on 32-bit platforms is generally 4 bytes long instead of 8 on
64-bit platforms. Use uint64_t to ensure it is always exactly 8 bytes.
For consistency convert the other variables to `unsigned long long`,
which is guaranteed to hold _at least_ a 64-bit number regardless of the
target architecture.
Signed-off-by: MarijnS95 <marijns95@gmail.com>
| -rw-r--r-- | timekeep.c | 24 |
1 files changed, 13 insertions, 11 deletions
@@ -29,10 +29,11 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include <fcntl.h> +#include <stdint.h> #include <stdlib.h> -#include <unistd.h> #include <string.h> -#include <fcntl.h> +#include <unistd.h> #include <sys/stat.h> #include <time.h> @@ -55,7 +56,7 @@ #endif #define TIME_ADJUST_PROP "persist.vendor.timeadjust" -int read_epoch(unsigned long* epoch) { +int read_epoch(unsigned long long* epoch) { int res = 0; int fd = open(RTC_SYS_FILE, O_RDONLY); @@ -68,7 +69,7 @@ int read_epoch(unsigned long* epoch) { res = read(fd, buffer, 16); if (res > 0) { char *endp = NULL; - *epoch = strtoul(buffer, &endp, 10); + *epoch = strtoull(buffer, &endp, 10); // sysfs read returns newline, ok to end up at '\n' if (*endp != '\0' && *endp != '\n') { ALOGI("Read from " RTC_SYS_FILE " returned " @@ -81,7 +82,8 @@ int read_epoch(unsigned long* epoch) { return res; } -void restore_ats(unsigned long value) { +/* ats_2 contains the time offset as 8-byte binary representation */ +void restore_ats(uint64_t value) { FILE *fp = NULL; value *= 1000; @@ -97,8 +99,8 @@ void restore_ats(unsigned long value) { int store_time() { char prop[PROPERTY_VALUE_MAX]; - unsigned long seconds = 0; - unsigned long epoch_since = 0; + unsigned long long seconds = 0; + unsigned long long epoch_since = 0; int res = -1; struct tm tm; time_t t; @@ -114,7 +116,7 @@ int store_time() { ALOGI("Failed to read epoch while storing"); } else { seconds -= epoch_since; - snprintf(prop, PROPERTY_VALUE_MAX, "%lu", seconds); + snprintf(prop, PROPERTY_VALUE_MAX, "%llu", seconds); restore_ats(seconds); property_set(TIME_ADJUST_PROP, prop); ALOGI("Time adjustment stored to property"); @@ -128,8 +130,8 @@ int store_time() { int restore_time() { struct timeval tv; - unsigned long time_adjust = 0; - unsigned long epoch_since = 0; + unsigned long long time_adjust = 0; + unsigned long long epoch_since = 0; int res = -1; char prop[PROPERTY_VALUE_MAX]; memset(prop, 0x0, PROPERTY_VALUE_MAX); @@ -137,7 +139,7 @@ int restore_time() { if (strcmp(prop, "0") != 0) { char *endp = NULL; - time_adjust = strtoul(prop, &endp, 10); + time_adjust = strtoull(prop, &endp, 10); if (*endp != '\0') { ALOGI("Property in " TIME_ADJUST_PROP " is not valid: %s (%d)", prop, errno); |
