-
Notifications
You must be signed in to change notification settings - Fork 0
/
timekeep.c
162 lines (140 loc) · 4.19 KB
/
timekeep.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
* Copyright (C) 2015 Sony Mobile Communications Inc.
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names
* of its contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/time.h>
#define LOG_TAG "TimeKeep"
#include <cutils/properties.h>
#include <cutils/log.h>
#include <errno.h>
#define RTC_SYS_FILE "/sys/class/rtc/rtc0/since_epoch"
#define TIME_ADJUST_PROP "persist.sys.timeadjust"
int read_epoch(unsigned long* epoch) {
int res = 0;
int fd = open(RTC_SYS_FILE, O_RDONLY);
if (fd == -1) {
res = -errno;
ALOGD("Failed to open RTC sys path");
} else {
char buffer[16];
memset(buffer, 0, 16);
res = read(fd, buffer, 16);
if (res > 0) {
char *endp = NULL;
*epoch = strtoul(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 "
"invalid string %s (%s)", buffer, endp);
res = -1;
}
}
}
return res;
}
int store_time() {
char prop[PROPERTY_VALUE_MAX];
unsigned long seconds = 0;
unsigned long epoch_since = 0;
int res = -1;
struct tm tm;
time_t t;
// Get time and adjust for local time
time(&t);
localtime_r(&t, &tm);
seconds = mktime(&tm);
if (seconds > 0) {
res = read_epoch(&epoch_since);
if (res < 0) {
ALOGI("Failed to read epoch while storing");
} else {
seconds -= epoch_since;
snprintf(prop, PROPERTY_VALUE_MAX, "%lu", seconds);
property_set(TIME_ADJUST_PROP, prop);
ALOGI("Time adjustment stored to property");
res = 0;
}
}
return res;
}
int restore_time() {
struct timeval tv;
unsigned long time_adjust = 0;
unsigned long epoch_since = 0;
int res = -1;
char prop[PROPERTY_VALUE_MAX];
memset(prop, 0x0, PROPERTY_VALUE_MAX);
property_get(TIME_ADJUST_PROP, prop, "0");
if (strcmp(prop, "0") != 0) {
char *endp = NULL;
time_adjust = strtoul(prop, &endp, 10);
if (*endp != '\0') {
ALOGI("Property in " TIME_ADJUST_PROP
" is not valid: %s (%d)", prop, errno);
return res;
}
} else {
ALOGI("No time adjust value found for restore");
return res;
}
res = read_epoch(&epoch_since);
if (res < 0) {
ALOGI("Failed to read from " RTC_SYS_FILE
" (%d), bailing out", res);
} else {
tv.tv_sec = epoch_since + time_adjust;
tv.tv_usec = 0;
res = settimeofday(&tv, NULL);
if (res != 0) {
ALOGI("Failed to restore time (%d), root?",
res);
} else {
ALOGI("Time restored!");
}
}
return res;
}
int main(int argc, char* argv[]) {
int res = -1;
if (argc != 2) {
ALOGI("usage: timekeep store|restore");
return res;
}
// Keep CAP_SYS_TIME and drop to system user
if (strcmp(argv[1], "store") == 0) {
res = store_time();
}
if (strcmp(argv[1], "restore") == 0) {
res = restore_time();
}
return res;
}