Search code examples
clinuxexecatexit

Execv register issue at atexit()


"No functions registered by atexit() in the calling process image are registered in the new process image".

Here is code:

pid = fork();
if (pid == 0) {
    atexit(check_mem);
    return execv(...);
}

check_mem function not getting called after execv(). Because of above "line". Any hacks to get the function registered after execv call ??

Thanks in advance for your help.


Solution

  • The perfect solution is using ptrace() as below :

    pid = fork();
    if (pid == 0) {
        ptrace(PTRACE_TRACEME, 0, 0, 0);
        return execve(...);
    }
    
    wait(NULL);
    ptrace(PTRACE_SETOPTIONS, pid, 0, PTRACE_O_TRACEEXIT);
    ptrace(PTRACE_CONT, pid, 0, (void*)0);
    
        while(1){
        waitpid(pid, &status, 0);
        if((WSTOPSIG(status) == SIGTRAP) && (status & (PTRACE_EVENT_EXIT << 8)))
            break;
    
        ptrace(PTRACE_CONT, pid, 0, WSTOPSIG(status));
    }
    
    check_mem();
    
    ptrace(PTRACE_CONT, pid, 0, 0);
    

    Acknowledgments : www.wienand.org/junkcode/linux/stopper.c