1 /*
2 * Copyright (C) 2013 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 <errno.h>
18 #include <jni.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <sys/stat.h>
23
24 /*
25 * Native methods used by
26 * cts/tests/tests/permission/src/android/security/cts/LinuxRngTest.java
27 */
28
throwIOException(JNIEnv * env,const char * format,...)29 static void throwIOException(JNIEnv* env, const char *format, ...) {
30 va_list ap;
31 va_start(ap, format);
32
33 char *message;
34 vasprintf(&message, format, ap);
35
36 va_end(ap);
37
38 jclass cls = env->FindClass("java/io/IOException");
39 env->ThrowNew(cls, message);
40
41 free(message);
42 }
43
android_security_cts_LinuxRngTest_getCharDeviceMajor(JNIEnv * env,jobject thiz,jstring name)44 jint android_security_cts_LinuxRngTest_getCharDeviceMajor(JNIEnv* env,
45 jobject thiz, jstring name)
46 {
47 const char* nameStr = env->GetStringUTFChars(name, NULL);
48
49 jint result = -1;
50 struct stat st;
51 if (stat(nameStr, &st) == -1) {
52 throwIOException(env, "Failed to stat %s: %s", nameStr, strerror(errno));
53 goto ret;
54 }
55
56 if (!S_ISCHR(st.st_mode)) {
57 throwIOException(env, "%s is not a character device: mode is 0%o", nameStr, st.st_mode);
58 goto ret;
59 }
60
61 result = major(st.st_rdev);
62
63 ret:
64 if (nameStr != NULL) {
65 env->ReleaseStringUTFChars(name, nameStr);
66 }
67 return result;
68 }
69
android_security_cts_LinuxRngTest_getCharDeviceMinor(JNIEnv * env,jobject thiz,jstring name)70 jint android_security_cts_LinuxRngTest_getCharDeviceMinor(JNIEnv* env,
71 jobject thiz, jstring name)
72 {
73 const char* nameStr = env->GetStringUTFChars(name, NULL);
74
75 jint result = -1;
76 struct stat st;
77 if (stat(nameStr, &st) == -1) {
78 throwIOException(env, "Failed to stat %s: %s", nameStr, strerror(errno));
79 goto ret;
80 }
81
82 if (!S_ISCHR(st.st_mode)) {
83 throwIOException(env, "%s is not a character device: mode is 0%o", nameStr, st.st_mode);
84 goto ret;
85 }
86
87 result = minor(st.st_rdev);
88
89 ret:
90 if (nameStr != NULL) {
91 env->ReleaseStringUTFChars(name, nameStr);
92 }
93 return result;
94 }
95
96 static JNINativeMethod gMethods[] = {
97 { "getCharDeviceMajor", "(Ljava/lang/String;)I",
98 (void *) android_security_cts_LinuxRngTest_getCharDeviceMajor },
99 { "getCharDeviceMinor", "(Ljava/lang/String;)I",
100 (void *) android_security_cts_LinuxRngTest_getCharDeviceMinor },
101 };
102
register_android_security_cts_LinuxRngTest(JNIEnv * env)103 int register_android_security_cts_LinuxRngTest(JNIEnv* env)
104 {
105 jclass clazz = env->FindClass("android/security/cts/LinuxRngTest");
106 return env->RegisterNatives(clazz, gMethods,
107 sizeof(gMethods) / sizeof(JNINativeMethod));
108 }
109