Android源:是fopen()函数在仿生libc中仅用于内部系统的应用程序? [英] Android Source: Is fopen() in bionic libc only used for system internal apps?
问题描述
我创建了一个应用程序NDK,做了以下成功:
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
以下列方式:
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
}
在推出内置Android源定制的模拟器,我只能通过LogCat中得到以下的输出:
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
在做的时候
adb shell ps
我得到
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
在UID 1001
(== USER u0_a1),因此是不是我的应用程序。
The uid 1001
(== USER u0_a1 ) is therefore not my app.
我的应用程序有UID 10055
:
My app had the uid 10055
:
$ adb shell dumpsys package my.package | grep userId=
userId=10055 gids=[3003, 1028, 1015]
我的问题是:为什么我的母语的fopen调用不会被记录,但系统内部通话不被记录到日志
My question is: Why does my native call of fopen not get logged, but system internal calls do get logged?
谢谢!
推荐答案
有关系统进程,标准输出/标准错误不会被重定向到logcat的,也就是说任何你发送有丢失(去到/ dev / null的技术),这就是为什么你什么也没看到。
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.
这篇关于Android源:是fopen()函数在仿生libc中仅用于内部系统的应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!