gopkg.in/ro-ag/posix.v1@v1.0.6/example/_child.c (about) 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <stdint.h> 4 #include <libgen.h> 5 #include <sys/mman.h> 6 7 typedef struct { 8 union { 9 void *pVoid; 10 uintptr_t uintptr; 11 }; 12 size_t memSize; 13 } address_t; 14 15 typedef struct { 16 address_t address; 17 size_t textSize; 18 char *textPtr; 19 } handler_t; 20 21 const int FileDesc = 3; 22 23 int main(int argc, char *argv[]) { 24 printf("(%s) C Program\n", basename(argv[0])); 25 address_t *pAdd, add; 26 /* Get start address */ 27 if ((pAdd = mmap(0, sizeof(address_t), PROT_READ, MAP_SHARED, FileDesc, 0)) == MAP_FAILED) { 28 perror("unable to map file"); 29 exit(EXIT_FAILURE); 30 } 31 32 add = *pAdd; 33 if (munmap(pAdd, sizeof(address_t)) == -1) { 34 perror("unable to unmap address"); 35 exit(EXIT_FAILURE); 36 } 37 38 /* mmap again with fixed address */ 39 40 handler_t *h = NULL; 41 if ((h = mmap(add.pVoid, add.memSize, PROT_READ, MAP_SHARED | MAP_FIXED, FileDesc, 0)) == MAP_FAILED) { 42 perror("unable to map fixed"); 43 exit(EXIT_FAILURE); 44 } 45 46 printf("(%s) Got Text: %.*s\n", basename(argv[0]), (int) h->textSize, h->textPtr); 47 48 if (munmap(h, add.memSize) == -1) { 49 perror("unable to unmap handler"); 50 exit(EXIT_FAILURE); 51 } 52 return 0; 53 }