• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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: Fuchsia: Point to correct location of header. http://b/119426171
36 // #include <sys/poll.h>
37 #if defined(__Fuchsia__)
38 #include <poll.h>
39 #else
40 #include <sys/poll.h>
41 #include <sys/inotify.h>
42 #endif
43 
44 #include "sun_nio_fs_LinuxWatchService.h"
45 
throwUnixException(JNIEnv * env,int errnum)46 static void throwUnixException(JNIEnv* env, int errnum) {
47     jobject x = JNU_NewObjectByName(env, "sun/nio/fs/UnixException",
48         "(I)V", errnum);
49     if (x != NULL) {
50         (*env)->Throw(env, x);
51     }
52 }
53 
54 JNIEXPORT jint JNICALL
Java_sun_nio_fs_LinuxWatchService_eventSize(JNIEnv * env,jclass clazz)55 Java_sun_nio_fs_LinuxWatchService_eventSize(JNIEnv *env, jclass clazz)
56 {
57     return (jint)sizeof(struct inotify_event);
58 }
59 
60 JNIEXPORT jintArray JNICALL
Java_sun_nio_fs_LinuxWatchService_eventOffsets(JNIEnv * env,jclass clazz)61 Java_sun_nio_fs_LinuxWatchService_eventOffsets(JNIEnv *env, jclass clazz)
62 {
63     jintArray result = (*env)->NewIntArray(env, 5);
64     if (result != NULL) {
65         jint arr[5];
66         arr[0] = (jint)offsetof(struct inotify_event, wd);
67         arr[1] = (jint)offsetof(struct inotify_event, mask);
68         arr[2] = (jint)offsetof(struct inotify_event, cookie);
69         arr[3] = (jint)offsetof(struct inotify_event, len);
70         arr[4] = (jint)offsetof(struct inotify_event, name);
71         (*env)->SetIntArrayRegion(env, result, 0, 5, arr);
72     }
73     return result;
74 }
75 
76 
77 JNIEXPORT jint JNICALL
Java_sun_nio_fs_LinuxWatchService_inotifyInit(JNIEnv * env,jclass clazz)78 Java_sun_nio_fs_LinuxWatchService_inotifyInit
79     (JNIEnv* env, jclass clazz)
80 {
81     int ifd = inotify_init();
82     if (ifd == -1) {
83         throwUnixException(env, errno);
84     }
85     return (jint)ifd;
86 }
87 
88 JNIEXPORT jint JNICALL
Java_sun_nio_fs_LinuxWatchService_inotifyAddWatch(JNIEnv * env,jclass clazz,jint fd,jlong address,jint mask)89 Java_sun_nio_fs_LinuxWatchService_inotifyAddWatch
90     (JNIEnv* env, jclass clazz, jint fd, jlong address, jint mask)
91 {
92     int wfd = -1;
93     const char* path = (const char*)jlong_to_ptr(address);
94 
95     wfd = inotify_add_watch((int)fd, path, mask);
96     if (wfd == -1) {
97         throwUnixException(env, errno);
98     }
99     return (jint)wfd;
100 }
101 
102 JNIEXPORT void JNICALL
Java_sun_nio_fs_LinuxWatchService_inotifyRmWatch(JNIEnv * env,jclass clazz,jint fd,jint wd)103 Java_sun_nio_fs_LinuxWatchService_inotifyRmWatch
104     (JNIEnv* env, jclass clazz, jint fd, jint wd)
105 {
106     int err = inotify_rm_watch((int)fd, (int)wd);
107     if (err == -1)
108         throwUnixException(env, errno);
109 }
110 
111 JNIEXPORT void JNICALL
Java_sun_nio_fs_LinuxWatchService_configureBlocking(JNIEnv * env,jclass clazz,jint fd,jboolean blocking)112 Java_sun_nio_fs_LinuxWatchService_configureBlocking
113     (JNIEnv* env, jclass clazz, jint fd, jboolean blocking)
114 {
115     int flags = fcntl(fd, F_GETFL);
116 
117     if ((blocking == JNI_FALSE) && !(flags & O_NONBLOCK))
118         fcntl(fd, F_SETFL, flags | O_NONBLOCK);
119     else if ((blocking == JNI_TRUE) && (flags & O_NONBLOCK))
120         fcntl(fd, F_SETFL, flags & ~O_NONBLOCK);
121 }
122 
123 JNIEXPORT void JNICALL
Java_sun_nio_fs_LinuxWatchService_socketpair(JNIEnv * env,jclass clazz,jintArray sv)124 Java_sun_nio_fs_LinuxWatchService_socketpair
125     (JNIEnv* env, jclass clazz, jintArray sv)
126 {
127     int sp[2];
128     if (socketpair(PF_UNIX, SOCK_STREAM, 0, sp) == -1) {
129         throwUnixException(env, errno);
130     } else {
131         jint res[2];
132         res[0] = (jint)sp[0];
133         res[1] = (jint)sp[1];
134         (*env)->SetIntArrayRegion(env, sv, 0, 2, &res[0]);
135     }
136 }
137 
138 JNIEXPORT jint JNICALL
Java_sun_nio_fs_LinuxWatchService_poll(JNIEnv * env,jclass clazz,jint fd1,jint fd2)139 Java_sun_nio_fs_LinuxWatchService_poll
140     (JNIEnv* env, jclass clazz, jint fd1, jint fd2)
141 {
142     struct pollfd ufds[2];
143     int n;
144 
145     ufds[0].fd = fd1;
146     ufds[0].events = POLLIN;
147     ufds[1].fd = fd2;
148     ufds[1].events = POLLIN;
149 
150     n = poll(&ufds[0], 2, -1);
151     if (n == -1) {
152         if (errno == EINTR) {
153             n = 0;
154         } else {
155             throwUnixException(env, errno);
156         }
157      }
158     return (jint)n;
159 }
160