1 /*
2 ** Copyright 2006, 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 "AlarmManagerService"
18 
19 #include <android-base/file.h>
20 #include <android-base/unique_fd.h>
21 #include <nativehelper/JNIHelp.h>
22 #include <utils/Log.h>
23 #include <utils/String8.h>
24 #include <utils/misc.h>
25 #include "jni.h"
26 
27 #include <dirent.h>
28 #include <fcntl.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <sys/epoll.h>
32 #include <sys/timerfd.h>
33 #include <sys/types.h>
34 #include <sys/socket.h>
35 #include <arpa/inet.h>
36 #include <netinet/in.h>
37 #include <stdlib.h>
38 #include <errno.h>
39 #include <unistd.h>
40 #include <linux/ioctl.h>
41 #include <linux/rtc.h>
42 
43 #include <array>
44 #include <limits>
45 #include <memory>
46 
47 namespace android {
48 
49 static constexpr int ANDROID_ALARM_TIME_CHANGE_MASK = 1 << 16;
50 
51 /**
52  * The AlarmManager alarm constants:
53  *
54  *   RTC_WAKEUP
55  *   RTC
56  *   REALTIME_WAKEUP
57  *   REALTIME
58  *   SYSTEMTIME (only defined in old alarm driver header, possibly unused?)
59  *
60  * We also need an extra CLOCK_REALTIME fd which exists specifically to be
61  * canceled on RTC changes.
62  */
63 static const size_t ANDROID_ALARM_TYPE_COUNT = 5;
64 static const size_t N_ANDROID_TIMERFDS = ANDROID_ALARM_TYPE_COUNT + 1;
65 static const clockid_t android_alarm_to_clockid[N_ANDROID_TIMERFDS] = {
66     CLOCK_REALTIME_ALARM,
67     CLOCK_REALTIME,
68     CLOCK_BOOTTIME_ALARM,
69     CLOCK_BOOTTIME,
70     CLOCK_MONOTONIC,
71     CLOCK_REALTIME,
72 };
73 
74 typedef std::array<int, N_ANDROID_TIMERFDS> TimerFds;
75 
76 class AlarmImpl
77 {
78 public:
AlarmImpl(const TimerFds & fds,int epollfd)79     AlarmImpl(const TimerFds &fds, int epollfd)
80           : fds{fds}, epollfd{epollfd} {}
81     ~AlarmImpl();
82 
83     int set(int type, struct timespec *ts);
84     int waitForAlarm();
85     int getTime(int type, struct itimerspec *spec);
86 
87 private:
88     const TimerFds fds;
89     const int epollfd;
90 };
91 
~AlarmImpl()92 AlarmImpl::~AlarmImpl()
93 {
94     for (auto fd : fds) {
95         epoll_ctl(epollfd, EPOLL_CTL_DEL, fd, nullptr);
96         close(fd);
97     }
98 
99     close(epollfd);
100 }
101 
set(int type,struct timespec * ts)102 int AlarmImpl::set(int type, struct timespec *ts)
103 {
104     if (static_cast<size_t>(type) > ANDROID_ALARM_TYPE_COUNT) {
105         errno = EINVAL;
106         return -1;
107     }
108 
109     if (!ts->tv_nsec && !ts->tv_sec) {
110         ts->tv_nsec = 1;
111     }
112     /* timerfd interprets 0 = disarm, so replace with a practically
113        equivalent deadline of 1 ns */
114 
115     struct itimerspec spec;
116     memset(&spec, 0, sizeof(spec));
117     memcpy(&spec.it_value, ts, sizeof(spec.it_value));
118 
119     return timerfd_settime(fds[type], TFD_TIMER_ABSTIME, &spec, NULL);
120 }
121 
getTime(int type,struct itimerspec * spec)122 int AlarmImpl::getTime(int type, struct itimerspec *spec)
123 {
124     if (static_cast<size_t>(type) > ANDROID_ALARM_TYPE_COUNT) {
125         errno = EINVAL;
126         return -1;
127     }
128 
129     return timerfd_gettime(fds[type], spec);
130 }
131 
waitForAlarm()132 int AlarmImpl::waitForAlarm()
133 {
134     epoll_event events[N_ANDROID_TIMERFDS];
135 
136     int nevents = epoll_wait(epollfd, events, N_ANDROID_TIMERFDS, -1);
137     if (nevents < 0) {
138         return nevents;
139     }
140 
141     int result = 0;
142     for (int i = 0; i < nevents; i++) {
143         uint32_t alarm_idx = events[i].data.u32;
144         uint64_t unused;
145         ssize_t err = read(fds[alarm_idx], &unused, sizeof(unused));
146         // Worth evaluating even if read fails with EAGAIN, since epoll_wait
147         // returned. (see b/78560047#comment34)
148         if (err < 0 && errno != EAGAIN) {
149             if (alarm_idx == ANDROID_ALARM_TYPE_COUNT && errno == ECANCELED) {
150                 result |= ANDROID_ALARM_TIME_CHANGE_MASK;
151             } else {
152                 return err;
153             }
154         } else {
155             result |= (1 << alarm_idx);
156         }
157     }
158 
159     return result;
160 }
161 
log_timerfd_create_error(clockid_t id)162 static void log_timerfd_create_error(clockid_t id)
163 {
164     if (errno == EINVAL) {
165         switch (id) {
166         case CLOCK_REALTIME_ALARM:
167         case CLOCK_BOOTTIME_ALARM:
168             ALOGE("kernel missing required commits:");
169             ALOGE("https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=6cffe00f7d4e24679eae6b7aae4caaf915288256");
170             ALOGE("https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=11ffa9d6065f344a9bd769a2452f26f2f671e5f8");
171             LOG_ALWAYS_FATAL("kernel does not support timerfd_create() with alarm timers");
172             break;
173 
174         case CLOCK_BOOTTIME:
175             ALOGE("kernel missing required commit:");
176             ALOGE("https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=4a2378a943f09907fb1ae35c15de917f60289c14");
177             LOG_ALWAYS_FATAL("kernel does not support timerfd_create(CLOCK_BOOTTIME)");
178             break;
179 
180         default:
181             break;
182         }
183     }
184 
185     ALOGE("timerfd_create(%u) failed: %s", id, strerror(errno));
186 }
187 
android_server_alarm_AlarmManagerService_init(JNIEnv *,jobject)188 static jlong android_server_alarm_AlarmManagerService_init(JNIEnv*, jobject)
189 {
190     int epollfd;
191     TimerFds fds;
192 
193     epollfd = epoll_create(fds.size());
194     if (epollfd < 0) {
195         ALOGE("epoll_create(%zu) failed: %s", fds.size(), strerror(errno));
196         return 0;
197     }
198 
199     for (size_t i = 0; i < fds.size(); i++) {
200         fds[i] = timerfd_create(android_alarm_to_clockid[i], TFD_NONBLOCK);
201         if (fds[i] < 0) {
202             log_timerfd_create_error(android_alarm_to_clockid[i]);
203             close(epollfd);
204             for (size_t j = 0; j < i; j++) {
205                 close(fds[j]);
206             }
207             return 0;
208         }
209     }
210 
211     std::unique_ptr<AlarmImpl> alarm{new AlarmImpl(fds, epollfd)};
212 
213     for (size_t i = 0; i < fds.size(); i++) {
214         epoll_event event;
215         event.events = EPOLLIN | EPOLLWAKEUP;
216         event.data.u32 = i;
217 
218         int err = epoll_ctl(epollfd, EPOLL_CTL_ADD, fds[i], &event);
219         if (err < 0) {
220             ALOGE("epoll_ctl(EPOLL_CTL_ADD) failed: %s", strerror(errno));
221             return 0;
222         }
223     }
224 
225     struct itimerspec spec = {};
226     /* 0 = disarmed; the timerfd doesn't need to be armed to get
227        RTC change notifications, just set up as cancelable */
228 
229     int err = timerfd_settime(fds[ANDROID_ALARM_TYPE_COUNT],
230             TFD_TIMER_ABSTIME | TFD_TIMER_CANCEL_ON_SET, &spec, NULL);
231     if (err < 0) {
232         ALOGE("timerfd_settime() failed: %s", strerror(errno));
233         return 0;
234     }
235 
236     return reinterpret_cast<jlong>(alarm.release());
237 }
238 
android_server_alarm_AlarmManagerService_getNextAlarm(JNIEnv *,jobject,jlong nativeData,jint type)239 static jlong android_server_alarm_AlarmManagerService_getNextAlarm(JNIEnv*, jobject, jlong nativeData, jint type)
240 {
241     AlarmImpl *impl = reinterpret_cast<AlarmImpl *>(nativeData);
242     struct itimerspec spec;
243     memset(&spec, 0, sizeof(spec));
244     const int result = impl->getTime(type, &spec);
245     if (result < 0)
246     {
247         ALOGE("timerfd_gettime() failed for fd %d: %s\n", static_cast<int>(type), strerror(errno));
248         return result;
249     }
250     struct timespec nextTimespec = spec.it_value;
251     long long millis = nextTimespec.tv_sec * 1000LL;
252     millis += (nextTimespec.tv_nsec / 1000000LL);
253     return static_cast<jlong>(millis);
254 }
255 
android_server_alarm_AlarmManagerService_close(JNIEnv *,jobject,jlong nativeData)256 static void android_server_alarm_AlarmManagerService_close(JNIEnv*, jobject, jlong nativeData)
257 {
258     AlarmImpl *impl = reinterpret_cast<AlarmImpl *>(nativeData);
259     delete impl;
260 }
261 
android_server_alarm_AlarmManagerService_set(JNIEnv *,jobject,jlong nativeData,jint type,jlong seconds,jlong nanoseconds)262 static jint android_server_alarm_AlarmManagerService_set(JNIEnv*, jobject, jlong nativeData, jint type, jlong seconds, jlong nanoseconds)
263 {
264     AlarmImpl *impl = reinterpret_cast<AlarmImpl *>(nativeData);
265     struct timespec ts;
266     ts.tv_sec = seconds;
267     ts.tv_nsec = nanoseconds;
268 
269     const int result = impl->set(type, &ts);
270     if (result < 0)
271     {
272         ALOGE("Unable to set alarm to %lld.%09lld: %s\n",
273               static_cast<long long>(seconds),
274               static_cast<long long>(nanoseconds), strerror(errno));
275     }
276     return result >= 0 ? 0 : errno;
277 }
278 
android_server_alarm_AlarmManagerService_waitForAlarm(JNIEnv *,jobject,jlong nativeData)279 static jint android_server_alarm_AlarmManagerService_waitForAlarm(JNIEnv*, jobject, jlong nativeData)
280 {
281     AlarmImpl *impl = reinterpret_cast<AlarmImpl *>(nativeData);
282     int result = 0;
283 
284     do
285     {
286         result = impl->waitForAlarm();
287     } while (result < 0 && errno == EINTR);
288 
289     if (result < 0)
290     {
291         ALOGE("Unable to wait on alarm: %s\n", strerror(errno));
292         return 0;
293     }
294 
295     return result;
296 }
297 
298 static const JNINativeMethod sMethods[] = {
299      /* name, signature, funcPtr */
300     {"init", "()J", (void*)android_server_alarm_AlarmManagerService_init},
301     {"close", "(J)V", (void*)android_server_alarm_AlarmManagerService_close},
302     {"set", "(JIJJ)I", (void*)android_server_alarm_AlarmManagerService_set},
303     {"waitForAlarm", "(J)I", (void*)android_server_alarm_AlarmManagerService_waitForAlarm},
304     {"getNextAlarm", "(JI)J", (void*)android_server_alarm_AlarmManagerService_getNextAlarm},
305 };
306 
register_android_server_alarm_AlarmManagerService(JNIEnv * env)307 int register_android_server_alarm_AlarmManagerService(JNIEnv* env)
308 {
309     return jniRegisterNativeMethods(env, "com/android/server/alarm/AlarmManagerService",
310                                     sMethods, NELEM(sMethods));
311 }
312 
313 } /* namespace android */
314