1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #define LOG_TAG "SystemClockTime"
18 
19 #include <android-base/file.h>
20 #include <android-base/unique_fd.h>
21 #include <linux/rtc.h>
22 #include <nativehelper/JNIHelp.h>
23 #include <utils/Log.h>
24 #include <utils/String8.h>
25 
26 #include "jni.h"
27 
28 namespace android {
29 
30 class SystemClockImpl {
31 public:
SystemClockImpl(const std::string & rtc_dev)32     SystemClockImpl(const std::string &rtc_dev) : rtc_dev{rtc_dev} {}
33 
34     int setTime(struct timeval *tv);
35 
36 private:
37     std::string rtc_dev;
38 };
39 
setTime(struct timeval * tv)40 int SystemClockImpl::setTime(struct timeval *tv) {
41     if (settimeofday(tv, NULL) == -1) {
42         ALOGV("settimeofday() failed: %s", strerror(errno));
43         return -1;
44     }
45 
46     android::base::unique_fd fd{open(rtc_dev.c_str(), O_RDWR)};
47     if (!fd.ok()) {
48         ALOGE("Unable to open %s: %s", rtc_dev.c_str(), strerror(errno));
49         return -1;
50     }
51 
52     struct tm tm;
53     if (!gmtime_r(&tv->tv_sec, &tm)) {
54         ALOGV("gmtime_r() failed: %s", strerror(errno));
55         return -1;
56     }
57 
58     struct rtc_time rtc = {};
59     rtc.tm_sec = tm.tm_sec;
60     rtc.tm_min = tm.tm_min;
61     rtc.tm_hour = tm.tm_hour;
62     rtc.tm_mday = tm.tm_mday;
63     rtc.tm_mon = tm.tm_mon;
64     rtc.tm_year = tm.tm_year;
65     rtc.tm_wday = tm.tm_wday;
66     rtc.tm_yday = tm.tm_yday;
67     rtc.tm_isdst = tm.tm_isdst;
68     if (ioctl(fd, RTC_SET_TIME, &rtc) == -1) {
69         ALOGV("RTC_SET_TIME ioctl failed: %s", strerror(errno));
70         return -1;
71     }
72 
73     return 0;
74 }
75 
com_android_server_SystemClockTime_init(JNIEnv *,jobject)76 static jlong com_android_server_SystemClockTime_init(JNIEnv *, jobject) {
77     // Find the wall clock RTC. We expect this always to be /dev/rtc0, but
78     // check the /dev/rtc symlink first so that legacy devices that don't use
79     // rtc0 can add a symlink rather than need to carry a local patch to this
80     // code.
81     //
82     // TODO: if you're reading this in a world where all devices are using the
83     // GKI, you can remove the readlink and just assume /dev/rtc0.
84     std::string dev_rtc;
85     if (!android::base::Readlink("/dev/rtc", &dev_rtc)) {
86         dev_rtc = "/dev/rtc0";
87     }
88 
89     std::unique_ptr<SystemClockImpl> system_clock{new SystemClockImpl(dev_rtc)};
90     return reinterpret_cast<jlong>(system_clock.release());
91 }
92 
com_android_server_SystemClockTime_setTime(JNIEnv *,jobject,jlong nativeData,jlong millis)93 static jint com_android_server_SystemClockTime_setTime(JNIEnv *, jobject, jlong nativeData,
94                                                        jlong millis) {
95     SystemClockImpl *impl = reinterpret_cast<SystemClockImpl *>(nativeData);
96 
97     if (millis <= 0 || millis / 1000LL >= std::numeric_limits<time_t>::max()) {
98         return -1;
99     }
100 
101     struct timeval tv;
102     tv.tv_sec = (millis / 1000LL);
103     tv.tv_usec = ((millis % 1000LL) * 1000LL);
104 
105     ALOGD("Setting time of day to sec=%ld", tv.tv_sec);
106 
107     int ret = impl->setTime(&tv);
108     if (ret < 0) {
109         ALOGW("Unable to set rtc to %ld: %s", tv.tv_sec, strerror(errno));
110         ret = -1;
111     }
112     return ret;
113 }
114 
115 static const JNINativeMethod sMethods[] = {
116         /* name, signature, funcPtr */
117         {"init", "()J", (void *)com_android_server_SystemClockTime_init},
118         {"setTime", "(JJ)I", (void *)com_android_server_SystemClockTime_setTime},
119 };
120 
register_com_android_server_SystemClockTime(JNIEnv * env)121 int register_com_android_server_SystemClockTime(JNIEnv *env) {
122     return jniRegisterNativeMethods(env, "com/android/server/SystemClockTime", sMethods,
123                                     NELEM(sMethods));
124 }
125 
126 } /* namespace android */
127