Skip to content

Instantly share code, notes, and snippets.

@upsuper
Created April 19, 2014 11:47
Show Gist options
  • Save upsuper/11082112 to your computer and use it in GitHub Desktop.
Save upsuper/11082112 to your computer and use it in GitHub Desktop.
Wait for another process
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/ptrace.h>
const char *name;
void usage()
{
printf("usage: %s pid\n", name);
exit(2);
}
void report_error()
{
perror(name);
exit(1);
}
int main(int argc, char *argv[])
{
name = argv[0];
if (argc < 2)
usage();
pid_t pid = atoi(argv[1]);
if (pid <= 0)
usage();
if (ptrace(PT_ATTACH, pid, (caddr_t)0, 0) < 0)
report_error();
while (1) {
int status;
pid_t pid2 = waitpid(pid, &status, 0);
if (!WIFSTOPPED(status))
break;
if (ptrace(PT_CONTINUE, pid, (caddr_t)1, SIGCONT) < 0) {
ptrace(PT_DETACH, pid, (caddr_t)1, SIGCONT);
report_error();
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment