#include #include #include #include #include #include #include #include #include #include #include int main() { pid_t p; p = fork(); if (p < 0) { perror("fork"); exit(1); } if (p > 0) { int rc; int status; sleep(1); /* let the child do its setup */ rc = ptrace(PT_ATTACH, p, 0, 0); if (rc < 0) { perror("ptrace(PT_ATTACH)"); goto kill_exit; } #if 1 /* Wait for the child process to stop. */ if (waitpid(p, &status, WUNTRACED) == -1) { perror("waitpid(WUNTRACED)"); goto kill_exit; } /* Check for an unexpected status. */ if (WIFSTOPPED(status) == 0) fprintf(stderr, "child process status: 0x%x", status); #else sleep(20); /* XXX XXX XXX */ #endif rc = ptrace(PT_CONTINUE, p, (caddr_t)1, 0); if (rc < 0) { perror("ptrace(PT_CONTINUE)"); goto kill_exit; } sleep(1); /* let things settle down */ if (ptrace(PT_DETACH, p, 0, 0) == 0) { printf("the child is alive\n"); goto kill_exit; } else perror("ptrace(PT_DETACH)"); printf("the child is dead\n"); exit(0); kill_exit: kill(p, SIGKILL); exit(1); } else { struct pollfd pfd; struct sockaddr_in sa; int s; s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); if (s < 0) { perror("socket"); exit(1); } memset(&sa, 0, sizeof(sa)); sa.sin_family = AF_INET; sa.sin_port = htons(4444); /* XXX */ sa.sin_addr.s_addr = INADDR_ANY; if (bind(s, (struct sockaddr *)&sa, sizeof(sa)) < 0) { perror("bind"); exit(1); } if (listen(s, 1) < 0) { perror("listen"); exit(1); } pfd.fd = s; pfd.revents = 0; pfd.events = POLLIN; if (poll(&pfd, 1, INFTIM) < 0) { perror("poll"); exit(1); } printf("successfully accepted\n"); exit(0); } }