前面的一篇记录中,http://blog.csdn.net/aaajj/article/details/51854015
谈到了采用发信号的方式和system_server进行交互,至于传递数据,可以用读写文件,socket等方式
这里讨论另外一种方式来进行尝试,采用组合信号的方式传递数据
比如5,二级制为101
从1到5的转化步骤为 左移1位,左移1位,加1
采用几个标记信号的按序组合就可以传递数据了
信号组合的方法进行文件关闭
vi ./frameworks/base/core/jni/com_android_internal_os_Zygote.cpp
int data = 1;
87 //test
88 void sigTest(int sig)
89 {
90 if (sig == 38)
91 {
92 data *= 2;
93 ALOGI("=====test system_server===== sig = %d", sig);
94 ALOGI("=====get fd = %d", data);
95 }
96 else if (sig == 39)
97 {
98 data += 1;
99 ALOGI("=====else test system_server===== sig = %d", sig);
100 ALOGI("=====get fd = %d", data);
101 }
102 else if (sig == 40)
103 {
104 ALOGI("=====get fd = %d, close", data);
105 close(data);
106
107 data = 1;
108 }
109
110
111 }
// Utility routine to fork zygote and specialize the child process.
static pid_t ForkAndSpecializeCommon(JNIEnv* env, uid_t uid, gid_t gid, jintArray javaGids,
jint debug_flags, jobjectArray javaRlimits,
jlong permittedCapabilities, jlong effectiveCapabilities,
jint mount_external,
jstring java_se_info, jstring java_se_name,
bool is_system_server, jintArray fdsToClose,
jstring instructionSet, jstring dataDir) {
SetSigChldHandler();
pid_t pid = fork();
if (pid == 0) {
// The child process.
gMallocLeakZygoteChild = 1;
// Clean up any descriptors which must be closed immediately
DetachDescriptors(env, fdsToClose);
// Keep capabilities across UID change, unless we're staying root.
if (uid != 0) {
EnableKeepCapabilities(env);
}
DropCapabilitiesBoundingSet(env);
471 //test
472 signal(38, sigTest);
473 signal(39, sigTest);
474 signal(40, sigTest);
475 //
测试过程
打开gmail,进行关闭
查找system_server的pid
ps | grep system_server
system 1097 496 2756148 203776 SyS_epoll_ 7f8aa45260 S system_server
得到1097
进入目录/proc/1097/fd
查找gmail的文件描述符fd
root@TEST:/proc/1097/fd # ls -l | grep mail
ls -l | grep mail
lr-x------ root root 2016-01-01 20:28 196 -> /system/app/Gmail2/Gmail2.apk
得到其fd为196
196 = 11000100
操作步骤为 ABAAAABAAE
A kill -38 1097
B kill -39 1097
E kill -40 1097
root@TEST:/proc/1097/fd # kill -38 1097
kill -38 1097
root@TEST:/proc/1097/fd # kill -39 1097
LOG
01-01 20:33:20.623: I/Zygote(1097): =====test system_server===== sig = 38
01-01 20:33:20.624: I/Zygote(1097): =====get fd = 2
01-01 20:33:27.655: I/Zygote(1097): =====else test system_server===== sig = 39
01-01 20:33:27.655: I/Zygote(1097): =====get fd = 3
01-01 20:33:32.974: I/Zygote(1097): =====test system_server===== sig = 38
01-01 20:33:32.975: I/Zygote(1097): =====get fd = 6
01-01 20:33:38.622: I/Zygote(1097): =====test system_server===== sig = 38
01-01 20:33:38.623: I/Zygote(1097): =====get fd = 12
01-01 20:33:42.526: I/Zygote(1097): =====test system_server===== sig = 38
01-01 20:33:42.526: I/Zygote(1097): =====get fd = 24
01-01 20:33:45.638: I/Zygote(1097): =====test system_server===== sig = 38
01-01 20:33:45.638: I/Zygote(1097): =====get fd = 48
01-01 20:33:54.814: I/Zygote(1097): =====else test system_server===== sig = 39
01-01 20:33:54.814: I/Zygote(1097): =====get fd = 49
01-01 20:33:58.942: I/Zygote(1097): =====test system_server===== sig = 38
01-01 20:33:58.942: I/Zygote(1097): =====get fd = 98
01-01 20:34:03.007: I/Zygote(1097): =====test system_server===== sig = 38
01-01 20:34:03.007: I/Zygote(1097): =====get fd = 196
01-01 20:34:11.396: I/Zygote(1097): =====get fd = 196, close
再查看
root@TEST:/proc/1097/fd # ls -l | grep mail
ls -l | grep mail
已经关闭了,没有找到mail的fd内容
说明了这种方法关闭文件是可行的