The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* //device/libs/android_runtime/android_server_AlarmManagerService.cpp |
| 2 | ** |
| 3 | ** Copyright 2006, The Android Open Source Project |
| 4 | ** |
Elliott Hughes | dd66bcb | 2011-04-12 11:28:59 -0700 | [diff] [blame] | 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | ** you may not use this file except in compliance with the License. |
| 7 | ** You may obtain a copy of the License at |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8 | ** |
Elliott Hughes | dd66bcb | 2011-04-12 11:28:59 -0700 | [diff] [blame] | 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10 | ** |
Elliott Hughes | dd66bcb | 2011-04-12 11:28:59 -0700 | [diff] [blame] | 11 | ** Unless required by applicable law or agreed to in writing, software |
| 12 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | ** See the License for the specific language governing permissions and |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #define LOG_TAG "AlarmManagerService" |
| 19 | |
| 20 | #include "JNIHelp.h" |
| 21 | #include "jni.h" |
Mathias Agopian | 25ba5b6 | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 22 | #include <utils/Log.h> |
| 23 | #include <utils/misc.h> |
Greg Hackmann | 0eb5826 | 2014-02-26 12:22:13 -0800 | [diff] [blame] | 24 | #include <utils/String8.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | |
Greg Hackmann | 0eb5826 | 2014-02-26 12:22:13 -0800 | [diff] [blame] | 26 | #include <dirent.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | #include <fcntl.h> |
| 28 | #include <stdio.h> |
| 29 | #include <string.h> |
Greg Hackmann | a1d6f92 | 2013-12-09 16:56:53 -0800 | [diff] [blame] | 30 | #include <sys/epoll.h> |
| 31 | #include <sys/timerfd.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | #include <sys/types.h> |
| 33 | #include <sys/socket.h> |
| 34 | #include <arpa/inet.h> |
| 35 | #include <netinet/in.h> |
| 36 | #include <stdlib.h> |
| 37 | #include <errno.h> |
| 38 | #include <unistd.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | #include <linux/ioctl.h> |
Greg Hackmann | 38bf514 | 2014-02-19 16:39:36 -0800 | [diff] [blame] | 40 | #include <linux/rtc.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | |
Greg Hackmann | d7151c0 | 2016-04-25 13:30:34 -0700 | [diff] [blame] | 42 | #include <array> |
Mykola Kondratenko | e45abfa | 2016-01-18 12:43:30 +0100 | [diff] [blame] | 43 | #include <memory> |
| 44 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | namespace android { |
| 46 | |
Greg Hackmann | d7151c0 | 2016-04-25 13:30:34 -0700 | [diff] [blame] | 47 | static constexpr int ANDROID_ALARM_TIME_CHANGE_MASK = 1 << 16; |
| 48 | |
| 49 | /** |
| 50 | * The AlarmManager alarm constants: |
| 51 | * |
| 52 | * RTC_WAKEUP |
| 53 | * RTC |
| 54 | * REALTIME_WAKEUP |
| 55 | * REALTIME |
| 56 | * SYSTEMTIME (only defined in old alarm driver header, possibly unused?) |
| 57 | * |
| 58 | * We also need an extra CLOCK_REALTIME fd which exists specifically to be |
| 59 | * canceled on RTC changes. |
| 60 | */ |
| 61 | static const size_t ANDROID_ALARM_TYPE_COUNT = 5; |
Greg Hackmann | a1d6f92 | 2013-12-09 16:56:53 -0800 | [diff] [blame] | 62 | static const size_t N_ANDROID_TIMERFDS = ANDROID_ALARM_TYPE_COUNT + 1; |
| 63 | static const clockid_t android_alarm_to_clockid[N_ANDROID_TIMERFDS] = { |
| 64 | CLOCK_REALTIME_ALARM, |
| 65 | CLOCK_REALTIME, |
| 66 | CLOCK_BOOTTIME_ALARM, |
| 67 | CLOCK_BOOTTIME, |
| 68 | CLOCK_MONOTONIC, |
| 69 | CLOCK_REALTIME, |
| 70 | }; |
Greg Hackmann | d7151c0 | 2016-04-25 13:30:34 -0700 | [diff] [blame] | 71 | |
| 72 | typedef std::array<int, N_ANDROID_TIMERFDS> TimerFds; |
Greg Hackmann | a1d6f92 | 2013-12-09 16:56:53 -0800 | [diff] [blame] | 73 | |
| 74 | class AlarmImpl |
| 75 | { |
| 76 | public: |
Greg Hackmann | d7151c0 | 2016-04-25 13:30:34 -0700 | [diff] [blame] | 77 | AlarmImpl(const TimerFds &fds, int epollfd, int rtc_id) : |
| 78 | fds{fds}, epollfd{epollfd}, rtc_id{rtc_id} { } |
| 79 | ~AlarmImpl(); |
Greg Hackmann | a1d6f92 | 2013-12-09 16:56:53 -0800 | [diff] [blame] | 80 | |
| 81 | int set(int type, struct timespec *ts); |
Greg Hackmann | 38bf514 | 2014-02-19 16:39:36 -0800 | [diff] [blame] | 82 | int setTime(struct timeval *tv); |
Greg Hackmann | a1d6f92 | 2013-12-09 16:56:53 -0800 | [diff] [blame] | 83 | int waitForAlarm(); |
| 84 | |
| 85 | private: |
Greg Hackmann | d7151c0 | 2016-04-25 13:30:34 -0700 | [diff] [blame] | 86 | const TimerFds fds; |
| 87 | const int epollfd; |
| 88 | const int rtc_id; |
Greg Hackmann | a1d6f92 | 2013-12-09 16:56:53 -0800 | [diff] [blame] | 89 | }; |
| 90 | |
Greg Hackmann | a1d6f92 | 2013-12-09 16:56:53 -0800 | [diff] [blame] | 91 | AlarmImpl::~AlarmImpl() |
| 92 | { |
Greg Hackmann | d7151c0 | 2016-04-25 13:30:34 -0700 | [diff] [blame] | 93 | for (auto fd : fds) { |
| 94 | epoll_ctl(epollfd, EPOLL_CTL_DEL, fd, nullptr); |
| 95 | close(fd); |
Greg Hackmann | a1d6f92 | 2013-12-09 16:56:53 -0800 | [diff] [blame] | 96 | } |
Greg Hackmann | a1d6f92 | 2013-12-09 16:56:53 -0800 | [diff] [blame] | 97 | |
Greg Hackmann | a1d6f92 | 2013-12-09 16:56:53 -0800 | [diff] [blame] | 98 | close(epollfd); |
| 99 | } |
| 100 | |
Greg Hackmann | d7151c0 | 2016-04-25 13:30:34 -0700 | [diff] [blame] | 101 | int AlarmImpl::set(int type, struct timespec *ts) |
Greg Hackmann | a1d6f92 | 2013-12-09 16:56:53 -0800 | [diff] [blame] | 102 | { |
Greg Hackmann | d7151c0 | 2016-04-25 13:30:34 -0700 | [diff] [blame] | 103 | if (static_cast<size_t>(type) > ANDROID_ALARM_TYPE_COUNT) { |
Greg Hackmann | a1d6f92 | 2013-12-09 16:56:53 -0800 | [diff] [blame] | 104 | errno = EINVAL; |
| 105 | return -1; |
| 106 | } |
| 107 | |
| 108 | if (!ts->tv_nsec && !ts->tv_sec) { |
| 109 | ts->tv_nsec = 1; |
| 110 | } |
| 111 | /* timerfd interprets 0 = disarm, so replace with a practically |
| 112 | equivalent deadline of 1 ns */ |
| 113 | |
| 114 | struct itimerspec spec; |
| 115 | memset(&spec, 0, sizeof(spec)); |
| 116 | memcpy(&spec.it_value, ts, sizeof(spec.it_value)); |
| 117 | |
| 118 | return timerfd_settime(fds[type], TFD_TIMER_ABSTIME, &spec, NULL); |
| 119 | } |
| 120 | |
Greg Hackmann | d7151c0 | 2016-04-25 13:30:34 -0700 | [diff] [blame] | 121 | int AlarmImpl::setTime(struct timeval *tv) |
Greg Hackmann | 38bf514 | 2014-02-19 16:39:36 -0800 | [diff] [blame] | 122 | { |
| 123 | struct rtc_time rtc; |
| 124 | struct tm tm, *gmtime_res; |
| 125 | int fd; |
| 126 | int res; |
| 127 | |
Greg Hackmann | 38bf514 | 2014-02-19 16:39:36 -0800 | [diff] [blame] | 128 | res = settimeofday(tv, NULL); |
| 129 | if (res < 0) { |
| 130 | ALOGV("settimeofday() failed: %s\n", strerror(errno)); |
Greg Hackmann | c924472 | 2014-01-27 16:30:09 -0800 | [diff] [blame] | 131 | return -1; |
| 132 | } |
| 133 | |
Greg Hackmann | 0eb5826 | 2014-02-26 12:22:13 -0800 | [diff] [blame] | 134 | if (rtc_id < 0) { |
| 135 | ALOGV("Not setting RTC because wall clock RTC was not found"); |
| 136 | errno = ENODEV; |
| 137 | return -1; |
| 138 | } |
| 139 | |
| 140 | android::String8 rtc_dev = String8::format("/dev/rtc%d", rtc_id); |
| 141 | fd = open(rtc_dev.string(), O_RDWR); |
Greg Hackmann | c924472 | 2014-01-27 16:30:09 -0800 | [diff] [blame] | 142 | if (fd < 0) { |
Greg Hackmann | 0eb5826 | 2014-02-26 12:22:13 -0800 | [diff] [blame] | 143 | ALOGV("Unable to open %s: %s\n", rtc_dev.string(), strerror(errno)); |
Greg Hackmann | c924472 | 2014-01-27 16:30:09 -0800 | [diff] [blame] | 144 | return res; |
Greg Hackmann | 38bf514 | 2014-02-19 16:39:36 -0800 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | gmtime_res = gmtime_r(&tv->tv_sec, &tm); |
| 148 | if (!gmtime_res) { |
| 149 | ALOGV("gmtime_r() failed: %s\n", strerror(errno)); |
| 150 | res = -1; |
| 151 | goto done; |
| 152 | } |
| 153 | |
| 154 | memset(&rtc, 0, sizeof(rtc)); |
| 155 | rtc.tm_sec = tm.tm_sec; |
| 156 | rtc.tm_min = tm.tm_min; |
| 157 | rtc.tm_hour = tm.tm_hour; |
| 158 | rtc.tm_mday = tm.tm_mday; |
| 159 | rtc.tm_mon = tm.tm_mon; |
| 160 | rtc.tm_year = tm.tm_year; |
| 161 | rtc.tm_wday = tm.tm_wday; |
| 162 | rtc.tm_yday = tm.tm_yday; |
| 163 | rtc.tm_isdst = tm.tm_isdst; |
| 164 | res = ioctl(fd, RTC_SET_TIME, &rtc); |
| 165 | if (res < 0) |
| 166 | ALOGV("RTC_SET_TIME ioctl failed: %s\n", strerror(errno)); |
| 167 | done: |
| 168 | close(fd); |
| 169 | return res; |
| 170 | } |
| 171 | |
Greg Hackmann | d7151c0 | 2016-04-25 13:30:34 -0700 | [diff] [blame] | 172 | int AlarmImpl::waitForAlarm() |
Greg Hackmann | a1d6f92 | 2013-12-09 16:56:53 -0800 | [diff] [blame] | 173 | { |
| 174 | epoll_event events[N_ANDROID_TIMERFDS]; |
| 175 | |
| 176 | int nevents = epoll_wait(epollfd, events, N_ANDROID_TIMERFDS, -1); |
| 177 | if (nevents < 0) { |
| 178 | return nevents; |
| 179 | } |
| 180 | |
| 181 | int result = 0; |
| 182 | for (int i = 0; i < nevents; i++) { |
| 183 | uint32_t alarm_idx = events[i].data.u32; |
| 184 | uint64_t unused; |
| 185 | ssize_t err = read(fds[alarm_idx], &unused, sizeof(unused)); |
| 186 | if (err < 0) { |
| 187 | if (alarm_idx == ANDROID_ALARM_TYPE_COUNT && errno == ECANCELED) { |
| 188 | result |= ANDROID_ALARM_TIME_CHANGE_MASK; |
| 189 | } else { |
| 190 | return err; |
| 191 | } |
| 192 | } else { |
| 193 | result |= (1 << alarm_idx); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | return result; |
| 198 | } |
| 199 | |
Greg Hackmann | 38bf514 | 2014-02-19 16:39:36 -0800 | [diff] [blame] | 200 | static jint android_server_AlarmManagerService_setKernelTime(JNIEnv*, jobject, jlong nativeData, jlong millis) |
| 201 | { |
| 202 | AlarmImpl *impl = reinterpret_cast<AlarmImpl *>(nativeData); |
| 203 | struct timeval tv; |
| 204 | int ret; |
| 205 | |
| 206 | if (millis <= 0 || millis / 1000LL >= INT_MAX) { |
| 207 | return -1; |
| 208 | } |
| 209 | |
| 210 | tv.tv_sec = (time_t) (millis / 1000LL); |
| 211 | tv.tv_usec = (suseconds_t) ((millis % 1000LL) * 1000LL); |
| 212 | |
| 213 | ALOGD("Setting time of day to sec=%d\n", (int) tv.tv_sec); |
| 214 | |
| 215 | ret = impl->setTime(&tv); |
| 216 | |
| 217 | if(ret < 0) { |
| 218 | ALOGW("Unable to set rtc to %ld: %s\n", tv.tv_sec, strerror(errno)); |
| 219 | ret = -1; |
| 220 | } |
| 221 | return ret; |
| 222 | } |
| 223 | |
Greg Hackmann | a1d6f92 | 2013-12-09 16:56:53 -0800 | [diff] [blame] | 224 | static jint android_server_AlarmManagerService_setKernelTimezone(JNIEnv*, jobject, jlong, jint minswest) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 225 | { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 226 | struct timezone tz; |
| 227 | |
| 228 | tz.tz_minuteswest = minswest; |
| 229 | tz.tz_dsttime = 0; |
| 230 | |
| 231 | int result = settimeofday(NULL, &tz); |
| 232 | if (result < 0) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 233 | ALOGE("Unable to set kernel timezone to %d: %s\n", minswest, strerror(errno)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 234 | return -1; |
| 235 | } else { |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 236 | ALOGD("Kernel timezone updated to %d minutes west of GMT\n", minswest); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | return 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 240 | } |
| 241 | |
Greg Hackmann | 0eb5826 | 2014-02-26 12:22:13 -0800 | [diff] [blame] | 242 | static const char rtc_sysfs[] = "/sys/class/rtc"; |
| 243 | |
| 244 | static bool rtc_is_hctosys(unsigned int rtc_id) |
| 245 | { |
| 246 | android::String8 hctosys_path = String8::format("%s/rtc%u/hctosys", |
| 247 | rtc_sysfs, rtc_id); |
Greg Hackmann | 0eb5826 | 2014-02-26 12:22:13 -0800 | [diff] [blame] | 248 | FILE *file = fopen(hctosys_path.string(), "re"); |
| 249 | if (!file) { |
| 250 | ALOGE("failed to open %s: %s", hctosys_path.string(), strerror(errno)); |
| 251 | return false; |
| 252 | } |
| 253 | |
| 254 | unsigned int hctosys; |
| 255 | bool ret = false; |
| 256 | int err = fscanf(file, "%u", &hctosys); |
| 257 | if (err == EOF) |
| 258 | ALOGE("failed to read from %s: %s", hctosys_path.string(), |
| 259 | strerror(errno)); |
| 260 | else if (err == 0) |
| 261 | ALOGE("%s did not have expected contents", hctosys_path.string()); |
| 262 | else |
| 263 | ret = hctosys; |
| 264 | |
| 265 | fclose(file); |
| 266 | return ret; |
| 267 | } |
| 268 | |
| 269 | static int wall_clock_rtc() |
| 270 | { |
Mykola Kondratenko | e45abfa | 2016-01-18 12:43:30 +0100 | [diff] [blame] | 271 | std::unique_ptr<DIR, int(*)(DIR*)> dir(opendir(rtc_sysfs), closedir); |
| 272 | if (!dir.get()) { |
Greg Hackmann | 0eb5826 | 2014-02-26 12:22:13 -0800 | [diff] [blame] | 273 | ALOGE("failed to open %s: %s", rtc_sysfs, strerror(errno)); |
| 274 | return -1; |
| 275 | } |
| 276 | |
| 277 | struct dirent *dirent; |
Mykola Kondratenko | e45abfa | 2016-01-18 12:43:30 +0100 | [diff] [blame] | 278 | while (errno = 0, dirent = readdir(dir.get())) { |
Greg Hackmann | 0eb5826 | 2014-02-26 12:22:13 -0800 | [diff] [blame] | 279 | unsigned int rtc_id; |
| 280 | int matched = sscanf(dirent->d_name, "rtc%u", &rtc_id); |
| 281 | |
| 282 | if (matched < 0) |
| 283 | break; |
| 284 | else if (matched != 1) |
| 285 | continue; |
| 286 | |
| 287 | if (rtc_is_hctosys(rtc_id)) { |
| 288 | ALOGV("found wall clock RTC %u", rtc_id); |
| 289 | return rtc_id; |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | if (errno == 0) |
| 294 | ALOGW("no wall clock RTC found"); |
| 295 | else |
| 296 | ALOGE("failed to enumerate RTCs: %s", strerror(errno)); |
| 297 | |
| 298 | return -1; |
| 299 | } |
| 300 | |
Greg Hackmann | a0126e0 | 2016-05-02 13:22:51 -0700 | [diff] [blame] | 301 | static void log_timerfd_create_error(clockid_t id) |
| 302 | { |
| 303 | if (errno == EINVAL) { |
| 304 | switch (id) { |
| 305 | case CLOCK_REALTIME_ALARM: |
| 306 | case CLOCK_BOOTTIME_ALARM: |
| 307 | ALOGE("kernel missing required commits:"); |
| 308 | ALOGE("https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=6cffe00f7d4e24679eae6b7aae4caaf915288256"); |
| 309 | ALOGE("https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=11ffa9d6065f344a9bd769a2452f26f2f671e5f8"); |
| 310 | LOG_ALWAYS_FATAL("kernel does not support timerfd_create() with alarm timers"); |
| 311 | break; |
| 312 | |
| 313 | case CLOCK_BOOTTIME: |
| 314 | ALOGE("kernel missing required commit:"); |
| 315 | ALOGE("https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=4a2378a943f09907fb1ae35c15de917f60289c14"); |
| 316 | LOG_ALWAYS_FATAL("kernel does not support timerfd_create(CLOCK_BOOTTIME)"); |
| 317 | break; |
| 318 | |
| 319 | default: |
| 320 | break; |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | ALOGE("timerfd_create(%u) failed: %s", id, strerror(errno)); |
| 325 | } |
| 326 | |
Greg Hackmann | d7151c0 | 2016-04-25 13:30:34 -0700 | [diff] [blame] | 327 | static jlong android_server_AlarmManagerService_init(JNIEnv*, jobject) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 328 | { |
Greg Hackmann | a1d6f92 | 2013-12-09 16:56:53 -0800 | [diff] [blame] | 329 | int epollfd; |
Greg Hackmann | d7151c0 | 2016-04-25 13:30:34 -0700 | [diff] [blame] | 330 | TimerFds fds; |
Greg Hackmann | a1d6f92 | 2013-12-09 16:56:53 -0800 | [diff] [blame] | 331 | |
Greg Hackmann | d7151c0 | 2016-04-25 13:30:34 -0700 | [diff] [blame] | 332 | epollfd = epoll_create(fds.size()); |
Greg Hackmann | a1d6f92 | 2013-12-09 16:56:53 -0800 | [diff] [blame] | 333 | if (epollfd < 0) { |
Greg Hackmann | d7151c0 | 2016-04-25 13:30:34 -0700 | [diff] [blame] | 334 | ALOGE("epoll_create(%zu) failed: %s", fds.size(), |
Greg Hackmann | a1d6f92 | 2013-12-09 16:56:53 -0800 | [diff] [blame] | 335 | strerror(errno)); |
| 336 | return 0; |
| 337 | } |
| 338 | |
Greg Hackmann | d7151c0 | 2016-04-25 13:30:34 -0700 | [diff] [blame] | 339 | for (size_t i = 0; i < fds.size(); i++) { |
Greg Hackmann | a1d6f92 | 2013-12-09 16:56:53 -0800 | [diff] [blame] | 340 | fds[i] = timerfd_create(android_alarm_to_clockid[i], 0); |
| 341 | if (fds[i] < 0) { |
Greg Hackmann | a0126e0 | 2016-05-02 13:22:51 -0700 | [diff] [blame] | 342 | log_timerfd_create_error(android_alarm_to_clockid[i]); |
Greg Hackmann | a1d6f92 | 2013-12-09 16:56:53 -0800 | [diff] [blame] | 343 | close(epollfd); |
| 344 | for (size_t j = 0; j < i; j++) { |
| 345 | close(fds[j]); |
| 346 | } |
| 347 | return 0; |
| 348 | } |
| 349 | } |
| 350 | |
Greg Hackmann | d7151c0 | 2016-04-25 13:30:34 -0700 | [diff] [blame] | 351 | AlarmImpl *ret = new AlarmImpl(fds, epollfd, wall_clock_rtc()); |
Greg Hackmann | a1d6f92 | 2013-12-09 16:56:53 -0800 | [diff] [blame] | 352 | |
Greg Hackmann | d7151c0 | 2016-04-25 13:30:34 -0700 | [diff] [blame] | 353 | for (size_t i = 0; i < fds.size(); i++) { |
Greg Hackmann | a1d6f92 | 2013-12-09 16:56:53 -0800 | [diff] [blame] | 354 | epoll_event event; |
| 355 | event.events = EPOLLIN | EPOLLWAKEUP; |
| 356 | event.data.u32 = i; |
| 357 | |
| 358 | int err = epoll_ctl(epollfd, EPOLL_CTL_ADD, fds[i], &event); |
| 359 | if (err < 0) { |
Greg Hackmann | d7151c0 | 2016-04-25 13:30:34 -0700 | [diff] [blame] | 360 | ALOGE("epoll_ctl(EPOLL_CTL_ADD) failed: %s", strerror(errno)); |
Greg Hackmann | a1d6f92 | 2013-12-09 16:56:53 -0800 | [diff] [blame] | 361 | delete ret; |
| 362 | return 0; |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | struct itimerspec spec; |
| 367 | memset(&spec, 0, sizeof(spec)); |
| 368 | /* 0 = disarmed; the timerfd doesn't need to be armed to get |
| 369 | RTC change notifications, just set up as cancelable */ |
| 370 | |
| 371 | int err = timerfd_settime(fds[ANDROID_ALARM_TYPE_COUNT], |
| 372 | TFD_TIMER_ABSTIME | TFD_TIMER_CANCEL_ON_SET, &spec, NULL); |
| 373 | if (err < 0) { |
Greg Hackmann | d7151c0 | 2016-04-25 13:30:34 -0700 | [diff] [blame] | 374 | ALOGE("timerfd_settime() failed: %s", strerror(errno)); |
Greg Hackmann | a1d6f92 | 2013-12-09 16:56:53 -0800 | [diff] [blame] | 375 | delete ret; |
| 376 | return 0; |
| 377 | } |
| 378 | |
| 379 | return reinterpret_cast<jlong>(ret); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 380 | } |
| 381 | |
Greg Hackmann | a1d6f92 | 2013-12-09 16:56:53 -0800 | [diff] [blame] | 382 | static void android_server_AlarmManagerService_close(JNIEnv*, jobject, jlong nativeData) |
| 383 | { |
| 384 | AlarmImpl *impl = reinterpret_cast<AlarmImpl *>(nativeData); |
| 385 | delete impl; |
| 386 | } |
| 387 | |
| 388 | static void android_server_AlarmManagerService_set(JNIEnv*, jobject, jlong nativeData, jint type, jlong seconds, jlong nanoseconds) |
| 389 | { |
| 390 | AlarmImpl *impl = reinterpret_cast<AlarmImpl *>(nativeData); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 391 | struct timespec ts; |
Jeff Brown | 11c5f1a | 2010-03-31 15:29:40 -0700 | [diff] [blame] | 392 | ts.tv_sec = seconds; |
| 393 | ts.tv_nsec = nanoseconds; |
Elliott Hughes | dd66bcb | 2011-04-12 11:28:59 -0700 | [diff] [blame] | 394 | |
Greg Hackmann | a1d6f92 | 2013-12-09 16:56:53 -0800 | [diff] [blame] | 395 | int result = impl->set(type, &ts); |
Greg Hackmann | 32b4c07 | 2013-12-06 10:52:09 -0800 | [diff] [blame] | 396 | if (result < 0) |
| 397 | { |
Ashok Bhat | f5df700 | 2014-03-25 20:51:35 +0000 | [diff] [blame] | 398 | ALOGE("Unable to set alarm to %lld.%09lld: %s\n", |
| 399 | static_cast<long long>(seconds), |
| 400 | static_cast<long long>(nanoseconds), strerror(errno)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 401 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 402 | } |
| 403 | |
Greg Hackmann | a1d6f92 | 2013-12-09 16:56:53 -0800 | [diff] [blame] | 404 | static jint android_server_AlarmManagerService_waitForAlarm(JNIEnv*, jobject, jlong nativeData) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 405 | { |
Greg Hackmann | a1d6f92 | 2013-12-09 16:56:53 -0800 | [diff] [blame] | 406 | AlarmImpl *impl = reinterpret_cast<AlarmImpl *>(nativeData); |
Greg Hackmann | 32b4c07 | 2013-12-06 10:52:09 -0800 | [diff] [blame] | 407 | int result = 0; |
Elliott Hughes | dd66bcb | 2011-04-12 11:28:59 -0700 | [diff] [blame] | 408 | |
Greg Hackmann | 32b4c07 | 2013-12-06 10:52:09 -0800 | [diff] [blame] | 409 | do |
| 410 | { |
Greg Hackmann | a1d6f92 | 2013-12-09 16:56:53 -0800 | [diff] [blame] | 411 | result = impl->waitForAlarm(); |
Greg Hackmann | 32b4c07 | 2013-12-06 10:52:09 -0800 | [diff] [blame] | 412 | } while (result < 0 && errno == EINTR); |
Elliott Hughes | dd66bcb | 2011-04-12 11:28:59 -0700 | [diff] [blame] | 413 | |
Greg Hackmann | 32b4c07 | 2013-12-06 10:52:09 -0800 | [diff] [blame] | 414 | if (result < 0) |
| 415 | { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 416 | ALOGE("Unable to wait on alarm: %s\n", strerror(errno)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 417 | return 0; |
| 418 | } |
Elliott Hughes | dd66bcb | 2011-04-12 11:28:59 -0700 | [diff] [blame] | 419 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 420 | return result; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 421 | } |
| 422 | |
Daniel Micay | 76f6a86 | 2015-09-19 17:31:01 -0400 | [diff] [blame] | 423 | static const JNINativeMethod sMethods[] = { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 424 | /* name, signature, funcPtr */ |
Greg Hackmann | a1d6f92 | 2013-12-09 16:56:53 -0800 | [diff] [blame] | 425 | {"init", "()J", (void*)android_server_AlarmManagerService_init}, |
| 426 | {"close", "(J)V", (void*)android_server_AlarmManagerService_close}, |
| 427 | {"set", "(JIJJ)V", (void*)android_server_AlarmManagerService_set}, |
| 428 | {"waitForAlarm", "(J)I", (void*)android_server_AlarmManagerService_waitForAlarm}, |
Greg Hackmann | 38bf514 | 2014-02-19 16:39:36 -0800 | [diff] [blame] | 429 | {"setKernelTime", "(JJ)I", (void*)android_server_AlarmManagerService_setKernelTime}, |
Greg Hackmann | a1d6f92 | 2013-12-09 16:56:53 -0800 | [diff] [blame] | 430 | {"setKernelTimezone", "(JI)I", (void*)android_server_AlarmManagerService_setKernelTimezone}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 431 | }; |
| 432 | |
| 433 | int register_android_server_AlarmManagerService(JNIEnv* env) |
| 434 | { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 435 | return jniRegisterNativeMethods(env, "com/android/server/AlarmManagerService", |
| 436 | sMethods, NELEM(sMethods)); |
| 437 | } |
| 438 | |
| 439 | } /* namespace android */ |