Is it possible to capture the exit code without forking the process on Android?

Viewed 70

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). atexit call can't give us a response with the error code, which is not good. Is there any other way (maybe bionic-exclusive) to accomplish this?

1 Answers

You could hook libc's exit() function to call yours instead (assuming exit() and not another variant is called).

Here is an example using 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;
}
Related