summaryrefslogtreecommitdiff
path: root/sim/testsuite/cris/c/clone1.c
blob: 163b18647a84478df7cc1ae2e071664599c54fd8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
#notarget: cris*-*-elf
#output: got: a\nthen: bc\nexit: 0\n
*/

/* This is a very limited subset of what ex1.c does; we just check that
   thread creation (clone syscall) and pipe writes and reads work. */

#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sched.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>

int pip[2];

int
process (void *arg)
{
  char *s = arg;
  if (write (pip[1], s+2, 1) != 1) abort ();
  if (write (pip[1], s+1, 1) != 1) abort ();
  if (write (pip[1], s, 1) != 1) abort ();
  return 0;
}

int
main (void)
{
  int retcode;
  int pid;
  int st;
  long stack[16384];
  char buf[10] = {0};

  retcode = pipe (pip);

  if (retcode != 0)
    {
      fprintf (stderr, "Bad pipe %d\n", retcode);
      abort ();
    }

  pid = clone (process, (char *) stack + sizeof (stack) - 64,
	       (CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND)
	       | SIGCHLD, "cba");
  if (pid <= 0)
    {
      fprintf (stderr, "Bad clone %d\n", pid);
      abort ();
    }

  if ((retcode = read (pip[0], buf, 1)) != 1)
    {
      fprintf (stderr, "Bad read 1: %d\n", retcode);
      abort ();
    }
  printf ("got: %c\n", buf[0]);
  retcode = read (pip[0], buf, 2);
  if (retcode == 1)
    {
      retcode = read (pip[0], buf+1, 1);
      if (retcode != 1)
	{
	  fprintf (stderr, "Bad read 1.5: %d\n", retcode);
	  abort ();
	}
      retcode = 2;
    }
  if (retcode != 2)
    {
      fprintf (stderr, "Bad read 2: %d\n", retcode);
      abort ();
    }

  printf ("then: %s\n", buf);
  retcode = wait4 (-1, &st, WNOHANG | __WCLONE, NULL);

  if (retcode != pid || !WIFEXITED (st))
    {
      fprintf (stderr, "Bad wait %d %x\n", retcode, st);
      abort ();
    }

  printf ("exit: %d\n", WEXITSTATUS (st));
  return 0;
}