1/*
2 * Copyright (c) 2008, 2009, 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 <port.h>       // Solaris 10
35
36#include "sun_nio_fs_SolarisWatchService.h"
37
38static void throwUnixException(JNIEnv* env, int errnum) {
39    jobject x = JNU_NewObjectByName(env, "sun/nio/fs/UnixException",
40        "(I)V", errnum);
41    if (x != NULL) {
42        (*env)->Throw(env, x);
43    }
44}
45
46JNIEXPORT void JNICALL
47Java_sun_nio_fs_SolarisWatchService_init(JNIEnv *env, jclass clazz)
48{
49}
50
51JNIEXPORT jint JNICALL
52Java_sun_nio_fs_SolarisWatchService_portCreate
53    (JNIEnv* env, jclass clazz)
54{
55    int port = port_create();
56    if (port == -1) {
57        throwUnixException(env, errno);
58    }
59    return (jint)port;
60}
61
62JNIEXPORT void JNICALL
63Java_sun_nio_fs_SolarisWatchService_portAssociate
64    (JNIEnv* env, jclass clazz, jint port, jint source, jlong objectAddress, jint events)
65{
66    uintptr_t object = (uintptr_t)jlong_to_ptr(objectAddress);
67
68    if (port_associate((int)port, (int)source, object, (int)events, NULL) == -1) {
69        throwUnixException(env, errno);
70    }
71}
72
73JNIEXPORT void JNICALL
74Java_sun_nio_fs_SolarisWatchService_portDissociate
75    (JNIEnv* env, jclass clazz, jint port, jint source, jlong objectAddress)
76{
77    uintptr_t object = (uintptr_t)jlong_to_ptr(objectAddress);
78
79    if (port_dissociate((int)port, (int)source, object) == -1) {
80        throwUnixException(env, errno);
81    }
82}
83
84JNIEXPORT void JNICALL
85Java_sun_nio_fs_SolarisWatchService_portSend(JNIEnv* env, jclass clazz,
86    jint port, jint events)
87{
88    if (port_send((int)port, (int)events, NULL) == -1) {
89        throwUnixException(env, errno);
90    }
91}
92
93JNIEXPORT jint JNICALL
94Java_sun_nio_fs_SolarisWatchService_portGetn(JNIEnv* env, jclass clazz,
95    jint port, jlong arrayAddress, jint max)
96{
97    uint_t n = 1;
98    port_event_t* list = (port_event_t*)jlong_to_ptr(arrayAddress);
99
100    if (port_getn((int)port, list, (uint_t)max, &n, NULL) == -1) {
101        throwUnixException(env, errno);
102    }
103    return (jint)n;
104}
105