#include #include #include #include #include #include #define SIZE 17313792 void xperror(char *error_string) { perror(error_string); exit(EXIT_FAILURE); } int main(int argc, char **argv) { int shmid, *addr; char buffer[64]; if ((shmid = shmget(21466751, SIZE, IPC_CREAT | IPC_EXCL | 0640)) < 0) xperror("shmget"); addr = (int *) shmat(shmid, (char *) 0x50000000, 0); if (addr == (int *) -1) xperror("shmat 1"); if (mprotect((char *) 0x51000000, 4096, PROT_READ) < 0) xperror("mprotect 1"); if (mprotect((char *) 0x51081000, 4096, PROT_READ) < 0) xperror("mprotect 2"); printf("First shmat & protects done: %08lx\n", (unsigned long) addr); sprintf(buffer, "cat /proc/%d/maps | grep /SYSV", getpid()); system(buffer); if (shmdt(addr) < 0) xperror("shmdt 1"); addr = (int *) shmat(shmid, (char *) 0x50000000, 0); if (addr == (int *) -1) { perror("shmat 2"); shmctl(shmid, IPC_RMID, NULL); exit(EXIT_FAILURE); } printf("Second shmat done: %08lx\n", (unsigned long) addr); system(buffer); if (shmdt(addr) < 0) xperror("shmdt 2"); shmctl(shmid, IPC_RMID, NULL); exit(EXIT_SUCCESS); }