root/mtcp/NOTES-x86_64/example2.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. test_mmap
  2. main

   1 // FIX __set_errno BELOW, AND 6 ARG CALL FOR i386
   2 //    mtcp_sy.h:  static int mtcp_sy_errno;
   3 //    and for inline, will be available in same file only --- okay for typical use
   4 // Doesn't handle __PIC__ (position independent code).
   5 // LOOK AT INTERNAL_SYSCALL  AND DEFINE MISSING ARGS:  EXTRAVAR_6, LOADARGS_6, etc.
   6 //
   7 // TESTING:  gcc -pie -fpie example.c
   8 // TESTING:  gcc -DSHARED=1 -shared -o shared.so -pie -fpie example.c
   9 //           gcc -DEXECUTABLE=1 -shared -o shared.so -pie -fpie example.c
  10 
  11 #include <sys/types.h>
  12 #include <unistd.h>
  13 #include <errno.h>
  14 
  15 #include <stdio.h> /* for printf */
  16 #include <sys/mman.h>
  17 #include "../mtcp_sy.h"
  18 
  19 #ifdef SHARED
  20 void test_mmap() {
  21   FILE *stream = fopen("/etc/passwd", "r");
  22   void * ptr = (void *)INLINE_SYSCALL(mmap, 6,
  23            0, 1024, PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS, fileno(stream), 0);
  24   if (ptr == MAP_FAILED) { printf("mmap: %s\n", strerror(mtcp_sy_errno)); exit(1); }
  25   fclose(stream);
  26 
  27   printf("mmap pointer: %x\n", ptr);
  28 }
  29 #endif
  30 
  31 #ifndef SHARED
  32 int main() {
  33   pid_t x = INLINE_SYSCALL (getpid, 0);
  34   FILE *stream = fopen("/etc/passwd", "r");
  35   void * ptr = (void *)INLINE_SYSCALL(mmap, 6,
  36            0, 1024, PROT_READ, MAP_PRIVATE | MAP_FIXED | MAP_ANONYMOUS, fileno(stream), 0);
  37   fclose(stream);
  38   if (ptr == MAP_FAILED) { printf("mmap: %s\n", strerror(mtcp_sy_errno)); exit(1); }
  39 
  40   printf("mmap pointer: %x\n", ptr);
  41   printf("pid: %d\n", x);
  42   printf("pid: %d\n", mtcp_inline_syscall(getpid, 0));
  43   // printf("pid: %d\n", getpid());
  44 
  45   return 0;
  46 }
  47 #endif

/* [<][>][^][v][top][bottom][index][help] */