1 /*
2 * Copyright (C) 2024 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 #include <fcntl.h>
18 #include <string.h>
19 #include <sys/stat.h>
20 #include <sys/syscall.h>
21 #include <unistd.h>
22 #include <utils/misc.h>
23
24 #include <string>
25
26 #include "jni_helper.h"
27
28 // ---- Exception related ----
29
throwErrnoException(JNIEnv * env,const char * functionName)30 static void throwErrnoException(JNIEnv* env, const char* functionName) {
31 int error = errno;
32 jniThrowErrnoException(env, functionName, error);
33 }
34
35 template <typename rc_t>
throwIfMinusOne(JNIEnv * env,const char * name,rc_t rc)36 static rc_t throwIfMinusOne(JNIEnv* env, const char* name, rc_t rc) {
37 if (rc == rc_t(-1)) {
38 throwErrnoException(env, name);
39 }
40 return rc;
41 }
42
43 // ---- Helper functions ---
44
45 static jclass g_StructStat;
46 static jclass g_StructTimespecClass;
47
makeStructTimespec(JNIEnv * env,const struct timespec & ts)48 static jobject makeStructTimespec(JNIEnv* env, const struct timespec& ts) {
49 static jmethodID ctor = env->GetMethodID(g_StructTimespecClass, "<init>",
50 "(JJ)V");
51 if (ctor == NULL) {
52 return NULL;
53 }
54 return env->NewObject(g_StructTimespecClass, ctor,
55 static_cast<jlong>(ts.tv_sec), static_cast<jlong>(ts.tv_nsec));
56 }
57
makeStructStat(JNIEnv * env,const struct stat64 & sb)58 static jobject makeStructStat(JNIEnv* env, const struct stat64& sb) {
59 static jmethodID ctor = env->GetMethodID(g_StructStat, "<init>",
60 "(JJIJIIJJLandroid/system/StructTimespec;Landroid/system/StructTimespec;Landroid/system/StructTimespec;JJ)V");
61 if (ctor == NULL) {
62 return NULL;
63 }
64
65 jobject atim_timespec = makeStructTimespec(env, sb.st_atim);
66 if (atim_timespec == NULL) {
67 return NULL;
68 }
69 jobject mtim_timespec = makeStructTimespec(env, sb.st_mtim);
70 if (mtim_timespec == NULL) {
71 return NULL;
72 }
73 jobject ctim_timespec = makeStructTimespec(env, sb.st_ctim);
74 if (ctim_timespec == NULL) {
75 return NULL;
76 }
77
78 return env->NewObject(g_StructStat, ctor,
79 static_cast<jlong>(sb.st_dev), static_cast<jlong>(sb.st_ino),
80 static_cast<jint>(sb.st_mode), static_cast<jlong>(sb.st_nlink),
81 static_cast<jint>(sb.st_uid), static_cast<jint>(sb.st_gid),
82 static_cast<jlong>(sb.st_rdev), static_cast<jlong>(sb.st_size),
83 atim_timespec, mtim_timespec, ctim_timespec,
84 static_cast<jlong>(sb.st_blksize), static_cast<jlong>(sb.st_blocks));
85 }
86
doStat(JNIEnv * env,jstring javaPath,bool isLstat)87 static jobject doStat(JNIEnv* env, jstring javaPath, bool isLstat) {
88 ScopedRealUtf8Chars path(env, javaPath);
89 if (path.c_str() == NULL) {
90 return NULL;
91 }
92 struct stat64 sb;
93 int rc = isLstat ? TEMP_FAILURE_RETRY(lstat64(path.c_str(), &sb))
94 : TEMP_FAILURE_RETRY(stat64(path.c_str(), &sb));
95 if (rc == -1) {
96 throwErrnoException(env, isLstat ? "lstat" : "stat");
97 return NULL;
98 }
99 return makeStructStat(env, sb);
100 }
101
102 // ---- JNI methods ----
103
104 typedef void (*FreeFunction)(void*);
105
nApplyFreeFunction(JNIEnv *,jclass,jlong freeFunction,jlong ptr)106 static void nApplyFreeFunction(JNIEnv*, jclass, jlong freeFunction, jlong ptr) {
107 void* nativePtr = reinterpret_cast<void*>(static_cast<uintptr_t>(ptr));
108 FreeFunction nativeFreeFunction
109 = reinterpret_cast<FreeFunction>(static_cast<uintptr_t>(freeFunction));
110 nativeFreeFunction(nativePtr);
111 }
112
nFcntlInt(JNIEnv * env,jclass,jint fd,jint cmd,jint arg)113 static jint nFcntlInt(JNIEnv* env, jclass, jint fd, jint cmd, jint arg) {
114 return throwIfMinusOne(env, "fcntl", TEMP_FAILURE_RETRY(fcntl(fd, cmd, arg)));
115 }
116
nLseek(JNIEnv * env,jclass,jint fd,jlong offset,jint whence)117 static jlong nLseek(JNIEnv* env, jclass, jint fd, jlong offset, jint whence) {
118 return throwIfMinusOne(env, "lseek", TEMP_FAILURE_RETRY(lseek(fd, offset, whence)));
119 }
120
nPipe2(JNIEnv * env,jclass,jint flags)121 static jintArray nPipe2(JNIEnv* env, jclass, jint flags) {
122 int fds[2];
123 throwIfMinusOne(env, "pipe2", TEMP_FAILURE_RETRY(pipe2(fds, flags)));
124
125 jintArray result;
126 result = env->NewIntArray(2);
127 if (result == NULL) {
128 return NULL; /* out of memory error thrown */
129 }
130 env->SetIntArrayRegion(result, 0, 2, fds);
131 return result;
132 }
133
nDup(JNIEnv * env,jclass,jint fd)134 static jlong nDup(JNIEnv* env, jclass, jint fd) {
135 return throwIfMinusOne(env, "fcntl", TEMP_FAILURE_RETRY(fcntl(fd, F_DUPFD_CLOEXEC, 0)));
136 }
137
nFstat(JNIEnv * env,jobject,jint fd)138 static jobject nFstat(JNIEnv* env, jobject, jint fd) {
139 struct stat64 sb;
140 int rc = TEMP_FAILURE_RETRY(fstat64(fd, &sb));
141 if (rc == -1) {
142 throwErrnoException(env, "fstat");
143 return NULL;
144 }
145 return makeStructStat(env, sb);
146 }
147
Linux_lstat(JNIEnv * env,jobject,jstring javaPath)148 static jobject Linux_lstat(JNIEnv* env, jobject, jstring javaPath) {
149 return doStat(env, javaPath, true);
150 }
151
Linux_stat(JNIEnv * env,jobject,jstring javaPath)152 static jobject Linux_stat(JNIEnv* env, jobject, jstring javaPath) {
153 return doStat(env, javaPath, false);
154 }
155
Linux_open(JNIEnv * env,jobject,jstring javaPath,jint flags,jint mode)156 static jint Linux_open(JNIEnv* env, jobject, jstring javaPath, jint flags, jint mode) {
157 ScopedRealUtf8Chars path(env, javaPath);
158 if (path.c_str() == NULL) {
159 return -1;
160 }
161 return throwIfMinusOne(env, "open", TEMP_FAILURE_RETRY(open(path.c_str(), flags, mode)));
162 }
163
Linux_setenv(JNIEnv * env,jobject,jstring javaName,jstring javaValue,jboolean overwrite)164 static void Linux_setenv(JNIEnv* env, jobject, jstring javaName, jstring javaValue,
165 jboolean overwrite) {
166 ScopedRealUtf8Chars name(env, javaName);
167 if (name.c_str() == NULL) {
168 jniThrowNullPointerException(env);
169 }
170 ScopedRealUtf8Chars value(env, javaValue);
171 if (value.c_str() == NULL) {
172 jniThrowNullPointerException(env);
173 }
174 throwIfMinusOne(env, "setenv", setenv(name.c_str(), value.c_str(), overwrite ? 1 : 0));
175 }
176
Linux_getpid(JNIEnv * env,jobject)177 static jint Linux_getpid(JNIEnv* env, jobject) {
178 return getpid();
179 }
180
Linux_gettid(JNIEnv * env,jobject)181 static jint Linux_gettid(JNIEnv* env, jobject) {
182 // gettid(2() was added in glibc 2.30 but Android uses an older version in prebuilt.
183 return syscall(__NR_gettid);
184 }
185
186 // ---- Registration ----
187
188 extern void register_android_system_OsConstants(JNIEnv* env);
189
190 static const JNINativeMethod sMethods[] =
191 {
192 { "applyFreeFunction", "(JJ)V", (void*)nApplyFreeFunction },
193 { "nFcntlInt", "(III)I", (void*)nFcntlInt },
194 { "nLseek", "(IJI)J", (void*)nLseek },
195 { "nPipe2", "(I)[I", (void*)nPipe2 },
196 { "nDup", "(I)I", (void*)nDup },
197 { "nFstat", "(I)Landroid/system/StructStat;", (void*)nFstat },
198 { "lstat", "(Ljava/lang/String;)Landroid/system/StructStat;", (void*)Linux_lstat },
199 { "stat", "(Ljava/lang/String;)Landroid/system/StructStat;", (void*)Linux_stat },
200 { "nOpen", "(Ljava/lang/String;II)I", (void*)Linux_open },
201 { "setenv", "(Ljava/lang/String;Ljava/lang/String;Z)V", (void*)Linux_setenv },
202 { "getpid", "()I", (void*)Linux_getpid },
203 { "gettid", "()I", (void*)Linux_gettid },
204 };
205
JNI_OnLoad(JavaVM * vm,void *)206 extern "C" jint JNI_OnLoad(JavaVM* vm, void* /* reserved */) {
207 ALOGV("%s: JNI_OnLoad", __FILE__);
208
209 JNIEnv* env = GetJNIEnvOrDie(vm);
210 g_StructStat = FindGlobalClassOrDie(env, "android/system/StructStat");
211 g_StructTimespecClass = FindGlobalClassOrDie(env, "android/system/StructTimespec");
212
213 jint res = jniRegisterNativeMethods(env, kRuntimeNative, sMethods, NELEM(sMethods));
214 if (res < 0) {
215 return res;
216 }
217
218 register_android_system_OsConstants(env);
219
220 return JNI_VERSION_1_4;
221 }
222