1 /*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 /*
18 * JNI helper functions.
19 *
20 * This file may be included by C or C++ code, which is trouble because jni.h
21 * uses different typedefs for JNIEnv in each language.
22 */
23 #ifndef NATIVEHELPER_JNIHELP_H_
24 #define NATIVEHELPER_JNIHELP_H_
25
26 #include "jni.h"
27 #include "cutils/log.h"
28 #include <unistd.h>
29
30 #ifndef NELEM
31 # define NELEM(x) ((int) (sizeof(x) / sizeof((x)[0])))
32 #endif
33
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37
38 /*
39 * Register one or more native methods with a particular class.
40 * "className" looks like "java/lang/String". Aborts on failure.
41 * TODO: fix all callers and change the return type to void.
42 */
43 int jniRegisterNativeMethods(C_JNIEnv* env, const char* className, const JNINativeMethod* gMethods, int numMethods);
44
45 /*
46 * Throw an exception with the specified class and an optional message.
47 *
48 * The "className" argument will be passed directly to FindClass, which
49 * takes strings with slashes (e.g. "java/lang/Object").
50 *
51 * If an exception is currently pending, we log a warning message and
52 * clear it.
53 *
54 * Returns 0 on success, nonzero if something failed (e.g. the exception
55 * class couldn't be found, so *an* exception will still be pending).
56 *
57 * Currently aborts the VM if it can't throw the exception.
58 */
59 int jniThrowException(C_JNIEnv* env, const char* className, const char* msg);
60
61 /*
62 * Throw a java.lang.NullPointerException, with an optional message.
63 */
64 int jniThrowNullPointerException(C_JNIEnv* env, const char* msg);
65
66 /*
67 * Throw a java.lang.RuntimeException, with an optional message.
68 */
69 int jniThrowRuntimeException(C_JNIEnv* env, const char* msg);
70
71 /*
72 * Throw a java.io.IOException, generating the message from errno.
73 */
74 int jniThrowIOException(C_JNIEnv* env, int errnum);
75
76 /*
77 * Return a pointer to a locale-dependent error string explaining errno
78 * value 'errnum'. The returned pointer may or may not be equal to 'buf'.
79 * This function is thread-safe (unlike strerror) and portable (unlike
80 * strerror_r).
81 */
82 const char* jniStrError(int errnum, char* buf, size_t buflen);
83
84 /*
85 * Returns a new java.io.FileDescriptor for the given int fd.
86 */
87 jobject jniCreateFileDescriptor(C_JNIEnv* env, int fd);
88
89 /*
90 * Returns the int fd from a java.io.FileDescriptor.
91 */
92 int jniGetFDFromFileDescriptor(C_JNIEnv* env, jobject fileDescriptor);
93
94 /*
95 * Sets the int fd in a java.io.FileDescriptor.
96 */
97 void jniSetFileDescriptorOfFD(C_JNIEnv* env, jobject fileDescriptor, int value);
98
99 /*
100 * Log a message and an exception.
101 * If exception is NULL, logs the current exception in the JNI environment.
102 */
103 void jniLogException(C_JNIEnv* env, int priority, const char* tag, jthrowable exception);
104
105 #ifdef __cplusplus
106 }
107 #endif
108
109
110 /*
111 * For C++ code, we provide inlines that map to the C functions. g++ always
112 * inlines these, even on non-optimized builds.
113 */
114 #if defined(__cplusplus)
jniRegisterNativeMethods(JNIEnv * env,const char * className,const JNINativeMethod * gMethods,int numMethods)115 inline int jniRegisterNativeMethods(JNIEnv* env, const char* className, const JNINativeMethod* gMethods, int numMethods) {
116 return jniRegisterNativeMethods(&env->functions, className, gMethods, numMethods);
117 }
118
jniThrowException(JNIEnv * env,const char * className,const char * msg)119 inline int jniThrowException(JNIEnv* env, const char* className, const char* msg) {
120 return jniThrowException(&env->functions, className, msg);
121 }
122
123 extern "C" int jniThrowExceptionFmt(C_JNIEnv* env, const char* className, const char* fmt, va_list args);
124
125 /*
126 * Equivalent to jniThrowException but with a printf-like format string and
127 * variable-length argument list. This is only available in C++.
128 */
jniThrowExceptionFmt(JNIEnv * env,const char * className,const char * fmt,...)129 inline int jniThrowExceptionFmt(JNIEnv* env, const char* className, const char* fmt, ...) {
130 va_list args;
131 va_start(args, fmt);
132 return jniThrowExceptionFmt(&env->functions, className, fmt, args);
133 va_end(args);
134 }
135
jniThrowNullPointerException(JNIEnv * env,const char * msg)136 inline int jniThrowNullPointerException(JNIEnv* env, const char* msg) {
137 return jniThrowNullPointerException(&env->functions, msg);
138 }
139
jniThrowRuntimeException(JNIEnv * env,const char * msg)140 inline int jniThrowRuntimeException(JNIEnv* env, const char* msg) {
141 return jniThrowRuntimeException(&env->functions, msg);
142 }
143
jniThrowIOException(JNIEnv * env,int errnum)144 inline int jniThrowIOException(JNIEnv* env, int errnum) {
145 return jniThrowIOException(&env->functions, errnum);
146 }
147
jniCreateFileDescriptor(JNIEnv * env,int fd)148 inline jobject jniCreateFileDescriptor(JNIEnv* env, int fd) {
149 return jniCreateFileDescriptor(&env->functions, fd);
150 }
151
jniGetFDFromFileDescriptor(JNIEnv * env,jobject fileDescriptor)152 inline int jniGetFDFromFileDescriptor(JNIEnv* env, jobject fileDescriptor) {
153 return jniGetFDFromFileDescriptor(&env->functions, fileDescriptor);
154 }
155
jniSetFileDescriptorOfFD(JNIEnv * env,jobject fileDescriptor,int value)156 inline void jniSetFileDescriptorOfFD(JNIEnv* env, jobject fileDescriptor, int value) {
157 jniSetFileDescriptorOfFD(&env->functions, fileDescriptor, value);
158 }
159
160 inline void jniLogException(JNIEnv* env, int priority, const char* tag, jthrowable exception = NULL) {
161 jniLogException(&env->functions, priority, tag, exception);
162 }
163 #endif
164
165 /* Logging macros.
166 *
167 * Logs an exception. If the exception is omitted or NULL, logs the current exception
168 * from the JNI environment, if any.
169 */
170 #define LOG_EX(env, priority, tag, ...) \
171 IF_ALOG(priority, tag) jniLogException(env, ANDROID_##priority, tag, ##__VA_ARGS__)
172 #define LOGV_EX(env, ...) LOG_EX(env, LOG_VERBOSE, LOG_TAG, ##__VA_ARGS__)
173 #define LOGD_EX(env, ...) LOG_EX(env, LOG_DEBUG, LOG_TAG, ##__VA_ARGS__)
174 #define LOGI_EX(env, ...) LOG_EX(env, LOG_INFO, LOG_TAG, ##__VA_ARGS__)
175 #define LOGW_EX(env, ...) LOG_EX(env, LOG_WARN, LOG_TAG, ##__VA_ARGS__)
176 #define LOGE_EX(env, ...) LOG_EX(env, LOG_ERROR, LOG_TAG, ##__VA_ARGS__)
177
178 /*
179 * TEMP_FAILURE_RETRY is defined by some, but not all, versions of
180 * <unistd.h>. (Alas, it is not as standard as we'd hoped!) So, if it's
181 * not already defined, then define it here.
182 */
183 #ifndef TEMP_FAILURE_RETRY
184 /* Used to retry syscalls that can return EINTR. */
185 #define TEMP_FAILURE_RETRY(exp) ({ \
186 typeof (exp) _rc; \
187 do { \
188 _rc = (exp); \
189 } while (_rc == -1 && errno == EINTR); \
190 _rc; })
191 #endif
192
193 #endif /* NATIVEHELPER_JNIHELP_H_ */
194