1 /*
2  * Copyright (c) 2002, 2006, 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 <sys/types.h>
27 #include <string.h>
28 #include "jni.h"
29 #include "jni_util.h"
30 #include "jvm.h"
31 #include "jlong.h"
32 #include "nio_util.h"
33 #include <nativehelper/JNIHelp.h>
34 
35 #ifdef __linux__
36   #include <pthread.h>
37   // Android-changed: Use correct include for signal.h
38   // #include <sys/signal.h>
39   #include <signal.h>
40   // Android-changed: Bionic (and AsynchronousCloseMonitor) expects libcore to use
41   // __SIGRTMIN + 2, not __SIGRTMAX - 2
42   // Also use SIGRTMAX instead of __SIGRTMAX, which is not defined by musl.
43   /* Also defined in net/linux_close.c */
44   #if !defined(__BIONIC__)
45     #define INTERRUPT_SIGNAL (SIGRTMAX - 2)
46   #else
47     #define INTERRUPT_SIGNAL (__SIGRTMIN + 2)
48   #endif
49 #elif __solaris__
50   #include <thread.h>
51   #include <signal.h>
52   #define INTERRUPT_SIGNAL (SIGRTMAX - 2)
53 #elif _ALLBSD_SOURCE
54   #include <pthread.h>
55   #include <signal.h>
56   /* Also defined in net/bsd_close.c */
57   #define INTERRUPT_SIGNAL SIGIO
58 // BEGIN Android-added: Fuchsia: Support for Fuchsia platform.
59 #elif __Fuchsia__
60   #include <pthread.h>
61   #include <signal.h>
62   /* Also defined in net/bsd_close.c */
63   #define INTERRUPT_SIGNAL SIGIO
64 // END Android-added: Fuchsia: Support for Fuchsia platform.
65 #else
66   #error "missing platform-specific definition here"
67 #endif
68 
69 #define NATIVE_METHOD(className, functionName, signature) \
70 { #functionName, signature, (void*)(className ## _ ## functionName) }
71 
72 static void
nullHandler(int sig)73 nullHandler(int sig)
74 {
75 }
76 
NativeThread_init(JNIEnv * env)77 static void  NativeThread_init(JNIEnv *env)
78 {
79     /* Install the null handler for INTERRUPT_SIGNAL.  This might overwrite the
80      * handler previously installed by java/net/linux_close.c, but that's okay
81      * since neither handler actually does anything.  We install our own
82      * handler here simply out of paranoia; ultimately the two mechanisms
83      * should somehow be unified, perhaps within the VM.
84      */
85 
86     sigset_t ss;
87     struct sigaction sa, osa;
88     sa.sa_handler = nullHandler;
89     sa.sa_flags = 0;
90     sigemptyset(&sa.sa_mask);
91     if (sigaction(INTERRUPT_SIGNAL, &sa, &osa) < 0)
92         JNU_ThrowIOExceptionWithLastError(env, "sigaction");
93 }
94 
95 JNIEXPORT jlong JNICALL
NativeThread_current(JNIEnv * env,jclass cl)96 NativeThread_current(JNIEnv *env, jclass cl)
97 {
98 #ifdef __solaris__
99     return (jlong)thr_self();
100 #else
101     return (jlong)pthread_self();
102 #endif
103 }
104 
105 JNIEXPORT void JNICALL
NativeThread_signal(JNIEnv * env,jclass cl,jlong thread)106 NativeThread_signal(JNIEnv *env, jclass cl, jlong thread)
107 {
108     int ret;
109 #ifdef __solaris__
110     ret = thr_kill((thread_t)thread, INTERRUPT_SIGNAL);
111 #else
112     ret = pthread_kill((pthread_t)thread, INTERRUPT_SIGNAL);
113 #endif
114     if (ret != 0)
115         JNU_ThrowIOExceptionWithLastError(env, "Thread signal failed");
116 }
117 
118 static JNINativeMethod gMethods[] = {
119   NATIVE_METHOD(NativeThread, current, "()J"),
120   NATIVE_METHOD(NativeThread, signal, "(J)V"),
121 };
122 
register_sun_nio_ch_NativeThread(JNIEnv * env)123 void register_sun_nio_ch_NativeThread(JNIEnv* env) {
124   jniRegisterNativeMethods(env, "sun/nio/ch/NativeThread", gMethods, NELEM(gMethods));
125   NativeThread_init(env);
126 }
127