/*************************** * Buffer aligné et protegé */ #define LEN (8*4096) #include #include int main() { ... buffer = memalign(4096, LEN); if (!buffer) { printf("failed to malloc\n"); exit(-1); } printf("buffer = %p\n", buffer); ret = mprotect(buffer, LEN, PROT_NONE); if (ret < 0) { printf("failed to mprotect\n"); exit(-1); } /********************** * Traitant de SIGSEGV */ #define _GNU_SOURCE /* a garder TOUT EN HAUT du fichier */ #include #include void segv_handler(int sig, siginfo_t *info, void *_context) { ucontext_t *context = _context; int write = (context->uc_mcontext.gregs[REG_ERR] & 2); #ifdef __x86_64__ void *addr = (void *)(context->uc_mcontext.gregs[REG_CR2]); #elif __i386__ void *addr = (void *)(context->uc_mcontext.cr2); #else #error Architecture non supportée #endif ... int main() { ... struct sigaction act; int ret; act.sa_flags = SA_SIGINFO; act.sa_sigaction = segv_handler; sigaction(SIGSEGV, &act, NULL); /********************** * Attributs des pages */ enum status { INVALID, READONLY, READWRITE, }; struct pageattr { void * address; enum status status; int owner; } /********************************************* * 4 tubes, 8 entrées, pour le cas symétrique */ static int fd_outreq; /* where I send my requests to the other process */ static int fd_inresp; /* where I receive the other process responses to my requests */ static int fd_inreq; /* where I receive requests from the other process */ static int fd_outresp; /* where I send responses to the other process requests */ int main() { ... int pipes[8]; ret = pipe(&pipes[0]); /* first pipe to send requests from first process */ if (ret < 0) { perror("pipe1"); exit(-1); } ret = pipe(&pipes[2]); /* second pipe to recv responses to first process requests */ if (ret < 0) { perror("pipe2"); exit(-1); } ret = pipe(&pipes[4]); /* third pipe to send requests from second process */ if (ret < 0) { perror("pipe3"); exit(-1); } ret = pipe(&pipes[6]); /* fourth pipe to recv responses to second process requests */ if (ret < 0) { perror("pipe4"); exit(-1); } pid = fork(); if (pid) { /* pere */ close(pipes[0]); fd_outreq = pipes[1]; fd_inresp = pipes[2]; close(pipes[3]); fd_inreq = pipes[4]; close(pipes[5]); close(pipes[6]); fd_outresp = pipes[7]; ... } else { /* fils */ fd_inreq = pipes[0]; close(pipes[1]); close(pipes[2]); fd_outresp = pipes[3]; close(pipes[4]); fd_outreq = pipes[5]; fd_inresp = pipes[6]; close(pipes[7]); ... }