PTRACE_DETACH fails after PTRACE_CONT with errno=ESRCH

Viewed 3484

In my project I need to attach to processes, resume them and later detach using ptrace. However, detaching fails with errno=ESRCH (No such process).

Detaching works fine if I don't resume the process with PTRACE_CONT, but in this case the process is stopped/unresponsive which is not acceptable in my project. Tested on Arch & Ubuntu 12.04 LTS with identical results.

#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/ptrace.h>
#include <sys/wait.h>

int main(int argc, char *argv[])
{
    pid_t pid = 21000;

    if (ptrace(PTRACE_ATTACH, pid, NULL, NULL) == -1) {
        perror("PTRACE_ATTACH");
        return 1;
    } 
    printf("attached\n");
    waitpid(pid, NULL, WUNTRACED);

    if (ptrace(PTRACE_CONT, pid, NULL, NULL) == -1) {
        perror("PTRACE_CONT");
        return 1;
    }
    printf("continued\n");

    if (ptrace(PTRACE_DETACH, pid, NULL, NULL) == -1) {
        perror("PTRACE_DETACH");
        return 1;
    }
    printf("detached\n");

    return 0;
}

The output:

attached
continued
PTRACE_DETACH: No such process
1 Answers
Related