893N/A/*
2362N/A * Copyright (c) 2008, 2009, Oracle and/or its affiliates. All rights reserved.
893N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
893N/A *
893N/A * This code is free software; you can redistribute it and/or modify it
893N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
893N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
893N/A *
893N/A * This code is distributed in the hope that it will be useful, but WITHOUT
893N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
893N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
893N/A * version 2 for more details (a copy is included in the LICENSE file that
893N/A * accompanied this code).
893N/A *
893N/A * You should have received a copy of the GNU General Public License version
893N/A * 2 along with this work; if not, write to the Free Software Foundation,
893N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
893N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
893N/A */
893N/A
893N/A#include "jni.h"
893N/A#include "jni_util.h"
893N/A#include "jvm.h"
893N/A#include "jlong.h"
893N/A
893N/A#include <stdlib.h>
893N/A#include <dlfcn.h>
893N/A#include <sys/types.h>
893N/A#include <sys/socket.h>
893N/A#include <sys/poll.h>
4126N/A#include <sys/inotify.h>
893N/A
893N/A#include "sun_nio_fs_LinuxWatchService.h"
893N/A
893N/Astatic void throwUnixException(JNIEnv* env, int errnum) {
893N/A jobject x = JNU_NewObjectByName(env, "sun/nio/fs/UnixException",
893N/A "(I)V", errnum);
893N/A if (x != NULL) {
893N/A (*env)->Throw(env, x);
893N/A }
893N/A}
893N/A
893N/AJNIEXPORT jint JNICALL
893N/AJava_sun_nio_fs_LinuxWatchService_eventSize(JNIEnv *env, jclass clazz)
893N/A{
893N/A return (jint)sizeof(struct inotify_event);
893N/A}
893N/A
893N/AJNIEXPORT jintArray JNICALL
893N/AJava_sun_nio_fs_LinuxWatchService_eventOffsets(JNIEnv *env, jclass clazz)
893N/A{
893N/A jintArray result = (*env)->NewIntArray(env, 5);
893N/A if (result != NULL) {
893N/A jint arr[5];
893N/A arr[0] = (jint)offsetof(struct inotify_event, wd);
893N/A arr[1] = (jint)offsetof(struct inotify_event, mask);
893N/A arr[2] = (jint)offsetof(struct inotify_event, cookie);
893N/A arr[3] = (jint)offsetof(struct inotify_event, len);
893N/A arr[4] = (jint)offsetof(struct inotify_event, name);
893N/A (*env)->SetIntArrayRegion(env, result, 0, 5, arr);
893N/A }
893N/A return result;
893N/A}
893N/A
893N/A
893N/AJNIEXPORT jint JNICALL
893N/AJava_sun_nio_fs_LinuxWatchService_inotifyInit
893N/A (JNIEnv* env, jclass clazz)
893N/A{
4126N/A int ifd = inotify_init();
893N/A if (ifd == -1) {
893N/A throwUnixException(env, errno);
893N/A }
893N/A return (jint)ifd;
893N/A}
893N/A
893N/AJNIEXPORT jint JNICALL
893N/AJava_sun_nio_fs_LinuxWatchService_inotifyAddWatch
893N/A (JNIEnv* env, jclass clazz, jint fd, jlong address, jint mask)
893N/A{
893N/A int wfd = -1;
893N/A const char* path = (const char*)jlong_to_ptr(address);
893N/A
4126N/A wfd = inotify_add_watch((int)fd, path, mask);
893N/A if (wfd == -1) {
893N/A throwUnixException(env, errno);
893N/A }
893N/A return (jint)wfd;
893N/A}
893N/A
893N/AJNIEXPORT void JNICALL
893N/AJava_sun_nio_fs_LinuxWatchService_inotifyRmWatch
893N/A (JNIEnv* env, jclass clazz, jint fd, jint wd)
893N/A{
4126N/A int err = inotify_rm_watch((int)fd, (int)wd);
893N/A if (err == -1)
893N/A throwUnixException(env, errno);
893N/A}
893N/A
893N/AJNIEXPORT void JNICALL
893N/AJava_sun_nio_fs_LinuxWatchService_configureBlocking
893N/A (JNIEnv* env, jclass clazz, jint fd, jboolean blocking)
893N/A{
893N/A int flags = fcntl(fd, F_GETFL);
893N/A
893N/A if ((blocking == JNI_FALSE) && !(flags & O_NONBLOCK))
893N/A fcntl(fd, F_SETFL, flags | O_NONBLOCK);
893N/A else if ((blocking == JNI_TRUE) && (flags & O_NONBLOCK))
893N/A fcntl(fd, F_SETFL, flags & ~O_NONBLOCK);
893N/A}
893N/A
893N/AJNIEXPORT void JNICALL
893N/AJava_sun_nio_fs_LinuxWatchService_socketpair
893N/A (JNIEnv* env, jclass clazz, jintArray sv)
893N/A{
893N/A int sp[2];
893N/A if (socketpair(PF_UNIX, SOCK_STREAM, 0, sp) == -1) {
893N/A throwUnixException(env, errno);
893N/A } else {
893N/A jint res[2];
893N/A res[0] = (jint)sp[0];
893N/A res[1] = (jint)sp[1];
893N/A (*env)->SetIntArrayRegion(env, sv, 0, 2, &res[0]);
893N/A }
893N/A}
893N/A
893N/AJNIEXPORT jint JNICALL
893N/AJava_sun_nio_fs_LinuxWatchService_poll
893N/A (JNIEnv* env, jclass clazz, jint fd1, jint fd2)
893N/A{
893N/A struct pollfd ufds[2];
893N/A int n;
893N/A
893N/A ufds[0].fd = fd1;
893N/A ufds[0].events = POLLIN;
893N/A ufds[1].fd = fd2;
893N/A ufds[1].events = POLLIN;
893N/A
893N/A n = poll(&ufds[0], 2, -1);
893N/A if (n == -1) {
893N/A if (errno == EINTR) {
893N/A n = 0;
893N/A } else {
893N/A throwUnixException(env, errno);
893N/A }
893N/A }
893N/A return (jint)n;
893N/A}