[英]Is it possible to capture the exit code without forking the process on Android?
We are writing an Android application, with mixed C and Java parts, mainly used to contain and control an external Java VM. We are writing an Android application, with mixed C and Java parts, mainly used to contain and control an external Java VM. And we need to somehow catch the moment when the VM decides to call exit() without modifying the VM or forking the process (because we also need some way to acess an OpenGL ES context).
我们需要以某种方式捕捉 VM 决定在不修改 VM 或分叉进程的情况下调用 exit() 的时刻(因为我们还需要某种方式来访问 OpenGL ES 上下文)。 atexit call can't give us a response with the error code, which is not good.
atexit 调用不能给我们一个错误代码的响应,这是不好的。 Is there any other way (maybe bionic-exclusive) to accomplish this?
有没有其他方法(可能是仿生独有的)来实现这一点?
You could hook libc's exit() function to call yours instead (assuming exit() and not another variant is called).您可以挂钩 libc 的 exit() function 来调用您的(假设 exit() 而不是调用另一个变体)。
Here is an example using funchook :这是一个使用funchook的示例:
#include <funchook.h>
#include <stdio.h>
#include <stdlib.h>
void exit(int status);
static void (*exit_func)(int status);
static void exit_hook(int status) {
printf("status is %d\n", status);
exit_func(status);
}
int main() {
funchook_t *funchook = funchook_create();
exit_func = exit;
// Check for errors
funchook_prepare(funchook, (void**)&exit_func, exit_hook);
funchook_install(funchook, 0);
return 123;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.