The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* //device/libs/android_runtime/android_util_FileObserver.cpp |
| 2 | ** |
| 3 | ** Copyright 2006, The Android Open Source Project |
| 4 | ** |
| 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 |
| 8 | ** |
| 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | ** |
| 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 |
| 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
Steven Moreland | 2279b25 | 2017-07-19 09:50:45 -0700 | [diff] [blame] | 18 | #include <nativehelper/JNIHelp.h> |
Sudheer Shanka | f7c79b4 | 2019-01-27 14:18:26 -0800 | [diff] [blame] | 19 | #include <nativehelper/ScopedPrimitiveArray.h> |
| 20 | #include <nativehelper/ScopedUtfChars.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | #include "jni.h" |
| 22 | #include "utils/Log.h" |
| 23 | #include "utils/misc.h" |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 24 | #include "core_jni_helpers.h" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | |
| 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
| 29 | #include <stdint.h> |
| 30 | #include <fcntl.h> |
| 31 | #include <sys/ioctl.h> |
| 32 | #include <errno.h> |
| 33 | |
Yabin Cui | 20a9cf4 | 2014-11-10 15:43:18 -0800 | [diff] [blame] | 34 | #if defined(__linux__) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | #include <sys/inotify.h> |
| 36 | #endif |
| 37 | |
| 38 | namespace android { |
| 39 | |
| 40 | static jmethodID method_onEvent; |
| 41 | |
| 42 | static jint android_os_fileobserver_init(JNIEnv* env, jobject object) |
| 43 | { |
Yabin Cui | 20a9cf4 | 2014-11-10 15:43:18 -0800 | [diff] [blame] | 44 | #if defined(__linux__) |
Nick Kralevich | 4b3a08c | 2019-01-28 10:39:10 -0800 | [diff] [blame] | 45 | return (jint)inotify_init1(IN_CLOEXEC); |
Yabin Cui | 20a9cf4 | 2014-11-10 15:43:18 -0800 | [diff] [blame] | 46 | #else |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | return -1; |
Yabin Cui | 20a9cf4 | 2014-11-10 15:43:18 -0800 | [diff] [blame] | 48 | #endif |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | static void android_os_fileobserver_observe(JNIEnv* env, jobject object, jint fd) |
| 52 | { |
Yabin Cui | 20a9cf4 | 2014-11-10 15:43:18 -0800 | [diff] [blame] | 53 | #if defined(__linux__) |
| 54 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | char event_buf[512]; |
| 56 | struct inotify_event* event; |
Yabin Cui | 20a9cf4 | 2014-11-10 15:43:18 -0800 | [diff] [blame] | 57 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | while (1) |
| 59 | { |
| 60 | int event_pos = 0; |
| 61 | int num_bytes = read(fd, event_buf, sizeof(event_buf)); |
Yabin Cui | 20a9cf4 | 2014-11-10 15:43:18 -0800 | [diff] [blame] | 62 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | if (num_bytes < (int)sizeof(*event)) |
| 64 | { |
| 65 | if (errno == EINTR) |
| 66 | continue; |
| 67 | |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 68 | ALOGE("***** ERROR! android_os_fileobserver_observe() got a short event!"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | return; |
| 70 | } |
Yabin Cui | 20a9cf4 | 2014-11-10 15:43:18 -0800 | [diff] [blame] | 71 | |
Mike Lockwood | 59d25d0 | 2010-02-18 07:01:28 -0500 | [diff] [blame] | 72 | while (num_bytes >= (int)sizeof(*event)) |
| 73 | { |
| 74 | int event_size; |
| 75 | event = (struct inotify_event *)(event_buf + event_pos); |
| 76 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | jstring path = NULL; |
Yabin Cui | 20a9cf4 | 2014-11-10 15:43:18 -0800 | [diff] [blame] | 78 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 79 | if (event->len > 0) |
| 80 | { |
| 81 | path = env->NewStringUTF(event->name); |
| 82 | } |
Mike Lockwood | 59d25d0 | 2010-02-18 07:01:28 -0500 | [diff] [blame] | 83 | |
| 84 | env->CallVoidMethod(object, method_onEvent, event->wd, event->mask, path); |
| 85 | if (env->ExceptionCheck()) { |
| 86 | env->ExceptionDescribe(); |
| 87 | env->ExceptionClear(); |
| 88 | } |
The Android Open Source Project | b22d55b | 2009-03-05 15:45:10 -0800 | [diff] [blame] | 89 | if (path != NULL) |
| 90 | { |
| 91 | env->DeleteLocalRef(path); |
| 92 | } |
Mike Lockwood | 59d25d0 | 2010-02-18 07:01:28 -0500 | [diff] [blame] | 93 | |
| 94 | event_size = sizeof(*event) + event->len; |
| 95 | num_bytes -= event_size; |
| 96 | event_pos += event_size; |
| 97 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 98 | } |
Yabin Cui | 20a9cf4 | 2014-11-10 15:43:18 -0800 | [diff] [blame] | 99 | |
| 100 | #endif |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 101 | } |
| 102 | |
Sudheer Shanka | f7c79b4 | 2019-01-27 14:18:26 -0800 | [diff] [blame] | 103 | static void android_os_fileobserver_startWatching(JNIEnv* env, jobject object, jint fd, |
| 104 | jobjectArray pathStrings, jint mask, |
| 105 | jintArray wfdArray) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 106 | { |
Sudheer Shanka | f7c79b4 | 2019-01-27 14:18:26 -0800 | [diff] [blame] | 107 | ScopedIntArrayRW wfds(env, wfdArray); |
| 108 | if (wfds.get() == nullptr) { |
| 109 | jniThrowException(env, "java/lang/IllegalStateException", "Failed to get ScopedIntArrayRW"); |
| 110 | } |
Yabin Cui | 20a9cf4 | 2014-11-10 15:43:18 -0800 | [diff] [blame] | 111 | |
| 112 | #if defined(__linux__) |
| 113 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 114 | if (fd >= 0) |
| 115 | { |
Sudheer Shanka | f7c79b4 | 2019-01-27 14:18:26 -0800 | [diff] [blame] | 116 | size_t count = wfds.size(); |
| 117 | for (jsize i = 0; i < count; ++i) { |
| 118 | jstring pathString = (jstring) env->GetObjectArrayElement(pathStrings, i); |
Yabin Cui | 20a9cf4 | 2014-11-10 15:43:18 -0800 | [diff] [blame] | 119 | |
Sudheer Shanka | f7c79b4 | 2019-01-27 14:18:26 -0800 | [diff] [blame] | 120 | ScopedUtfChars path(env, pathString); |
Yabin Cui | 20a9cf4 | 2014-11-10 15:43:18 -0800 | [diff] [blame] | 121 | |
Sudheer Shanka | f7c79b4 | 2019-01-27 14:18:26 -0800 | [diff] [blame] | 122 | wfds[i] = inotify_add_watch(fd, path.c_str(), mask); |
| 123 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 124 | } |
| 125 | |
Yabin Cui | 20a9cf4 | 2014-11-10 15:43:18 -0800 | [diff] [blame] | 126 | #endif |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 127 | } |
| 128 | |
Sudheer Shanka | f7c79b4 | 2019-01-27 14:18:26 -0800 | [diff] [blame] | 129 | static void android_os_fileobserver_stopWatching(JNIEnv* env, jobject object, |
| 130 | jint fd, jintArray wfdArray) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 131 | { |
Yabin Cui | 20a9cf4 | 2014-11-10 15:43:18 -0800 | [diff] [blame] | 132 | #if defined(__linux__) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 133 | |
Sudheer Shanka | f7c79b4 | 2019-01-27 14:18:26 -0800 | [diff] [blame] | 134 | ScopedIntArrayRO wfds(env, wfdArray); |
| 135 | if (wfds.get() == nullptr) { |
| 136 | jniThrowException(env, "java/lang/IllegalStateException", "Failed to get ScopedIntArrayRO"); |
| 137 | } |
| 138 | size_t count = wfds.size(); |
| 139 | for (size_t i = 0; i < count; ++i) { |
| 140 | inotify_rm_watch((int)fd, (uint32_t)wfds[i]); |
| 141 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 142 | |
Yabin Cui | 20a9cf4 | 2014-11-10 15:43:18 -0800 | [diff] [blame] | 143 | #endif |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 144 | } |
| 145 | |
Daniel Micay | 76f6a86 | 2015-09-19 17:31:01 -0400 | [diff] [blame] | 146 | static const JNINativeMethod sMethods[] = { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 147 | /* name, signature, funcPtr */ |
| 148 | { "init", "()I", (void*)android_os_fileobserver_init }, |
| 149 | { "observe", "(I)V", (void*)android_os_fileobserver_observe }, |
Sudheer Shanka | f7c79b4 | 2019-01-27 14:18:26 -0800 | [diff] [blame] | 150 | { "startWatching", "(I[Ljava/lang/String;I[I)V", (void*)android_os_fileobserver_startWatching }, |
| 151 | { "stopWatching", "(I[I)V", (void*)android_os_fileobserver_stopWatching } |
Yabin Cui | 20a9cf4 | 2014-11-10 15:43:18 -0800 | [diff] [blame] | 152 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 153 | }; |
| 154 | |
| 155 | int register_android_os_FileObserver(JNIEnv* env) |
| 156 | { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 157 | jclass clazz = FindClassOrDie(env, "android/os/FileObserver$ObserverThread"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 158 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 159 | method_onEvent = GetMethodIDOrDie(env, clazz, "onEvent", "(IILjava/lang/String;)V"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 160 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 161 | return RegisterMethodsOrDie(env, "android/os/FileObserver$ObserverThread", sMethods, |
| 162 | NELEM(sMethods)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | } /* namespace android */ |