问题
I created an NDK application that does the following successfully:
#include <string.h>
#include <stdio.h>
#include <android/log.h>
JNIEXPORT jstring JNICALL Java_my_package_NativeAccess_stringFromJNI(
JNIEnv* env, jobject thiz) {
char* str = "Native Code!";
FILE* file = fopen("sdcard/hello.txt","w+");
if (file == NULL) {
file = fopen("mnt/sdcard/hello.txt","w+");
if (file == NULL) {
file = fopen("storage/sdcard/hello.txt","w+");
}
}
if (file == NULL) {
str = "Native Code! fopen() did not work!";
__android_log_write(ANDROID_LOG_ERROR, "AAAAAAAA", "file null!");
}
else{
str = "Native Code! fopen() worked!";
__android_log_write(ANDROID_LOG_ERROR, "AAAAAAAA", "file exists!");
fputs("HELLO WORLD!\n", file);
fflush(file);
fclose(file);
}
return (*env)->NewStringUTF(env, str);
}
I start the app in a custom built source of me. It is the latest KitKat version. There I only changed
/bionic/libc/upstream-freebsd/lib/libc/stdio/fopen.c
in the following way:
FILE *
fopen(const char *file, const char *mode)
{
//====================
fputs ("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n", stdout);
int uid = getuid();
int pid = getpid();
int ppid = getppid();
fprintf(stdout, "uid = %d", uid);
fputs ("\n", stdout);
fprintf(stdout, "pid = %d", pid);
fputs ("\n", stdout);
fprintf(stdout, "ppid = %d", ppid);
fputs ("\n", stdout);
fputs ("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n", stdout);
//====================
// ... the usual fopen continues here
}
When launching the emulator with the custom built Android source, I only get the following output via LogCat:
01-15 06:18:18.400: I/stdout(1417): XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
01-15 06:18:18.400: I/stdout(1417): uid = 1001
01-15 06:18:18.400: I/stdout(1417): pid = 1417
01-15 06:18:18.400: I/stdout(1417): ppid = 1217
01-15 06:18:18.400: I/stdout(1417): XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
when doing
adb shell ps
I get
USER PID PPID VSIZE RSS WCHAN PC NAME
...
root 1217 1 205812 40176 ffffffff b6f395ec S zygote
...
radio 1417 1217 238984 25788 ffffffff b6f3a650 S com.android.phone
...
u0_a1 1697 1217 217744 20236 ffffffff b6f3a650 S com.android.providers.calendar
The uid 1001
(== USER u0_a1 ) is therefore not my app.
My app had the uid 10055
:
$ adb shell dumpsys package my.package | grep userId=
userId=10055 gids=[3003, 1028, 1015]
My question is: Why does my native call of fopen not get logged, but system internal calls do get logged?
Thanks!
回答1:
For system processes, stdout/stderr are not redirected to logcat, i.e. anything you send there is lost (goes to /dev/null technically), that's why you don't see anything.
来源:https://stackoverflow.com/questions/21136622/android-source-is-fopen-in-bionic-libc-only-used-for-system-internal-apps