Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/solaris/native/sun/nio/fs/LinuxWatchService.c
32288 views
/*1* Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425#include "jni.h"26#include "jni_util.h"27#include "jvm.h"28#include "jlong.h"2930#include <stdlib.h>31#include <dlfcn.h>32#include <sys/types.h>33#include <sys/socket.h>34#include <sys/poll.h>35#include <sys/inotify.h>3637#include "sun_nio_fs_LinuxWatchService.h"3839static void throwUnixException(JNIEnv* env, int errnum) {40jobject x = JNU_NewObjectByName(env, "sun/nio/fs/UnixException",41"(I)V", errnum);42if (x != NULL) {43(*env)->Throw(env, x);44}45}4647JNIEXPORT jint JNICALL48Java_sun_nio_fs_LinuxWatchService_eventSize(JNIEnv *env, jclass clazz)49{50return (jint)sizeof(struct inotify_event);51}5253JNIEXPORT jintArray JNICALL54Java_sun_nio_fs_LinuxWatchService_eventOffsets(JNIEnv *env, jclass clazz)55{56jintArray result = (*env)->NewIntArray(env, 5);57if (result != NULL) {58jint arr[5];59arr[0] = (jint)offsetof(struct inotify_event, wd);60arr[1] = (jint)offsetof(struct inotify_event, mask);61arr[2] = (jint)offsetof(struct inotify_event, cookie);62arr[3] = (jint)offsetof(struct inotify_event, len);63arr[4] = (jint)offsetof(struct inotify_event, name);64(*env)->SetIntArrayRegion(env, result, 0, 5, arr);65}66return result;67}686970JNIEXPORT jint JNICALL71Java_sun_nio_fs_LinuxWatchService_inotifyInit72(JNIEnv* env, jclass clazz)73{74int ifd = inotify_init();75if (ifd == -1) {76throwUnixException(env, errno);77}78return (jint)ifd;79}8081JNIEXPORT jint JNICALL82Java_sun_nio_fs_LinuxWatchService_inotifyAddWatch83(JNIEnv* env, jclass clazz, jint fd, jlong address, jint mask)84{85int wfd = -1;86const char* path = (const char*)jlong_to_ptr(address);8788wfd = inotify_add_watch((int)fd, path, mask);89if (wfd == -1) {90throwUnixException(env, errno);91}92return (jint)wfd;93}9495JNIEXPORT void JNICALL96Java_sun_nio_fs_LinuxWatchService_inotifyRmWatch97(JNIEnv* env, jclass clazz, jint fd, jint wd)98{99int err = inotify_rm_watch((int)fd, (int)wd);100if (err == -1)101throwUnixException(env, errno);102}103104JNIEXPORT void JNICALL105Java_sun_nio_fs_LinuxWatchService_configureBlocking106(JNIEnv* env, jclass clazz, jint fd, jboolean blocking)107{108int flags = fcntl(fd, F_GETFL);109110if ((blocking == JNI_FALSE) && !(flags & O_NONBLOCK))111fcntl(fd, F_SETFL, flags | O_NONBLOCK);112else if ((blocking == JNI_TRUE) && (flags & O_NONBLOCK))113fcntl(fd, F_SETFL, flags & ~O_NONBLOCK);114}115116JNIEXPORT void JNICALL117Java_sun_nio_fs_LinuxWatchService_socketpair118(JNIEnv* env, jclass clazz, jintArray sv)119{120int sp[2];121if (socketpair(PF_UNIX, SOCK_STREAM, 0, sp) == -1) {122throwUnixException(env, errno);123} else {124jint res[2];125res[0] = (jint)sp[0];126res[1] = (jint)sp[1];127(*env)->SetIntArrayRegion(env, sv, 0, 2, &res[0]);128}129}130131JNIEXPORT jint JNICALL132Java_sun_nio_fs_LinuxWatchService_poll133(JNIEnv* env, jclass clazz, jint fd1, jint fd2)134{135struct pollfd ufds[2];136int n;137138ufds[0].fd = fd1;139ufds[0].events = POLLIN;140ufds[1].fd = fd2;141ufds[1].events = POLLIN;142143n = poll(&ufds[0], 2, -1);144if (n == -1) {145if (errno == EINTR) {146n = 0;147} else {148throwUnixException(env, errno);149}150}151return (jint)n;152}153154155