tech-toolchain archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

ptrace code crash



Hi,

why does this crash?
#include <sys/types.h>
#include <sys/ptrace.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>

int main(int argc, const char *argv[])
{	   
	int from_child[2] = {-1, -1};
	int pid;
	int msg;

	pid = fork();
	if (pid == 0) {
		int ppid = getppid();
		if (ptrace(PT_ATTACH, ppid, NULL, 0) == 0) {
			printf("PT_ATTACH success\n");
			waitpid(ppid, NULL, 0);
			close(from_child[0]);
			write(from_child[1], &msg, sizeof(msg));
			ptrace(PT_DETACH, ppid, NULL, 0);
		}
	} else {
		while ((read(from_child[0], &msg, sizeof(msg)) < 0) && (errno == EINTR));
		close(from_child[1]);
		close(from_child[0]);
		waitpid(pid, NULL, 0);
	}
	return 0;
}


Home | Main Index | Thread Index | Old Index