Home | History | Annotate | Download | only in common
      1 /* Check that clone() is implemented and properly works
      2  */
      3 #define __GNU_SOURCE 1
      4 #include <stdio.h>
      5 #include <errno.h>
      6 #include <sched.h>
      7 #include <unistd.h>
      8 #include <signal.h>
      9 #include <stdlib.h>
     10 #include <sys/ptrace.h>
     11 #include <sys/wait.h>
     12 #include <stdarg.h>
     13 #include <string.h>
     14 
     15 static int
     16 clone_child (void *arg)
     17 {
     18  errno = 0;
     19  ptrace (PTRACE_TRACEME, 0, 0, 0);
     20  if (errno != 0)
     21    perror ("ptrace");
     22  if (kill (getpid (), SIGSTOP) < 0)
     23    perror ("kill");
     24  return 0;
     25 }
     26 
     27 #define PAGE_SIZE 4096
     28 #define STACK_SIZE (4 * PAGE_SIZE)
     29 
     30 char clone_stack[STACK_SIZE] __attribute__ ((aligned (PAGE_SIZE)));
     31 
     32 int
     33 main ()
     34 {
     35  int pid,child;
     36  int status;
     37 
     38  pid = clone (clone_child, clone_stack + 3 * PAGE_SIZE,
     39               CLONE_VM | SIGCHLD, NULL);
     40  if (pid < 0)
     41    {
     42      perror ("clone");
     43      exit (1);
     44    }
     45  printf ("child pid %d\n", pid);
     46 
     47  //sleep(20);
     48  child = waitpid (pid, &status, 0);
     49  printf("waitpid returned %d\n", child);
     50  if (child < 0) {
     51    perror ("waitpid");
     52    return 1;
     53  }
     54  printf ("child %d, status 0x%x\n", child, status);
     55  return 0;
     56 }
     57