1 /*
2 * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 #include "jni.h"
27 #include "jni_util.h"
28 #include "jvm.h"
29 #include "jlong.h"
30
31 #include <stdlib.h>
32 #include <dlfcn.h>
33 #include <sys/types.h>
34 #include <sys/socket.h>
35 // Android-changed: Point to correct location of header. http://b/119426171
36 // #include <sys/poll.h>
37 #include <poll.h>
38 #if !defined(__Fuchsia__)
39 #include <sys/inotify.h>
40 #endif
41
42 #include "sun_nio_fs_LinuxWatchService.h"
43
throwUnixException(JNIEnv * env,int errnum)44 static void throwUnixException(JNIEnv* env, int errnum) {
45 jobject x = JNU_NewObjectByName(env, "sun/nio/fs/UnixException",
46 "(I)V", errnum);
47 if (x != NULL) {
48 (*env)->Throw(env, x);
49 }
50 }
51
52 JNIEXPORT jint JNICALL
Java_sun_nio_fs_LinuxWatchService_eventSize(JNIEnv * env,jclass clazz)53 Java_sun_nio_fs_LinuxWatchService_eventSize(JNIEnv *env, jclass clazz)
54 {
55 return (jint)sizeof(struct inotify_event);
56 }
57
58 JNIEXPORT jintArray JNICALL
Java_sun_nio_fs_LinuxWatchService_eventOffsets(JNIEnv * env,jclass clazz)59 Java_sun_nio_fs_LinuxWatchService_eventOffsets(JNIEnv *env, jclass clazz)
60 {
61 jintArray result = (*env)->NewIntArray(env, 5);
62 if (result != NULL) {
63 jint arr[5];
64 arr[0] = (jint)offsetof(struct inotify_event, wd);
65 arr[1] = (jint)offsetof(struct inotify_event, mask);
66 arr[2] = (jint)offsetof(struct inotify_event, cookie);
67 arr[3] = (jint)offsetof(struct inotify_event, len);
68 arr[4] = (jint)offsetof(struct inotify_event, name);
69 (*env)->SetIntArrayRegion(env, result, 0, 5, arr);
70 }
71 return result;
72 }
73
74
75 JNIEXPORT jint JNICALL
Java_sun_nio_fs_LinuxWatchService_inotifyInit(JNIEnv * env,jclass clazz)76 Java_sun_nio_fs_LinuxWatchService_inotifyInit
77 (JNIEnv* env, jclass clazz)
78 {
79 int ifd = inotify_init();
80 if (ifd == -1) {
81 throwUnixException(env, errno);
82 }
83 return (jint)ifd;
84 }
85
86 JNIEXPORT jint JNICALL
Java_sun_nio_fs_LinuxWatchService_inotifyAddWatch(JNIEnv * env,jclass clazz,jint fd,jlong address,jint mask)87 Java_sun_nio_fs_LinuxWatchService_inotifyAddWatch
88 (JNIEnv* env, jclass clazz, jint fd, jlong address, jint mask)
89 {
90 int wfd = -1;
91 const char* path = (const char*)jlong_to_ptr(address);
92
93 wfd = inotify_add_watch((int)fd, path, mask);
94 if (wfd == -1) {
95 throwUnixException(env, errno);
96 }
97 return (jint)wfd;
98 }
99
100 JNIEXPORT void JNICALL
Java_sun_nio_fs_LinuxWatchService_inotifyRmWatch(JNIEnv * env,jclass clazz,jint fd,jint wd)101 Java_sun_nio_fs_LinuxWatchService_inotifyRmWatch
102 (JNIEnv* env, jclass clazz, jint fd, jint wd)
103 {
104 int err = inotify_rm_watch((int)fd, (int)wd);
105 if (err == -1)
106 throwUnixException(env, errno);
107 }
108
109 JNIEXPORT void JNICALL
Java_sun_nio_fs_LinuxWatchService_configureBlocking(JNIEnv * env,jclass clazz,jint fd,jboolean blocking)110 Java_sun_nio_fs_LinuxWatchService_configureBlocking
111 (JNIEnv* env, jclass clazz, jint fd, jboolean blocking)
112 {
113 int flags = fcntl(fd, F_GETFL);
114
115 if ((blocking == JNI_FALSE) && !(flags & O_NONBLOCK))
116 fcntl(fd, F_SETFL, flags | O_NONBLOCK);
117 else if ((blocking == JNI_TRUE) && (flags & O_NONBLOCK))
118 fcntl(fd, F_SETFL, flags & ~O_NONBLOCK);
119 }
120
121 JNIEXPORT void JNICALL
Java_sun_nio_fs_LinuxWatchService_socketpair(JNIEnv * env,jclass clazz,jintArray sv)122 Java_sun_nio_fs_LinuxWatchService_socketpair
123 (JNIEnv* env, jclass clazz, jintArray sv)
124 {
125 int sp[2];
126 if (socketpair(PF_UNIX, SOCK_STREAM, 0, sp) == -1) {
127 throwUnixException(env, errno);
128 } else {
129 jint res[2];
130 res[0] = (jint)sp[0];
131 res[1] = (jint)sp[1];
132 (*env)->SetIntArrayRegion(env, sv, 0, 2, &res[0]);
133 }
134 }
135
136 JNIEXPORT jint JNICALL
Java_sun_nio_fs_LinuxWatchService_poll(JNIEnv * env,jclass clazz,jint fd1,jint fd2)137 Java_sun_nio_fs_LinuxWatchService_poll
138 (JNIEnv* env, jclass clazz, jint fd1, jint fd2)
139 {
140 struct pollfd ufds[2];
141 int n;
142
143 ufds[0].fd = fd1;
144 ufds[0].events = POLLIN;
145 ufds[1].fd = fd2;
146 ufds[1].events = POLLIN;
147
148 n = poll(&ufds[0], 2, -1);
149 if (n == -1) {
150 if (errno == EINTR) {
151 n = 0;
152 } else {
153 throwUnixException(env, errno);
154 }
155 }
156 return (jint)n;
157 }
158