* sim/cris: New directory with C and assembly tests for the CRIS
[deliverable/binutils-gdb.git] / sim / testsuite / sim / cris / c / pipe2.c
1 /* Check that closing a pipe with a nonempty buffer works.
2 #notarget: cris*-*-elf
3 #output: got: a\nexit: 0\n
4 */
5
6
7 #include <stddef.h>
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include <limits.h>
11 #include <unistd.h>
12 #include <sched.h>
13 #include <signal.h>
14 #include <errno.h>
15 #include <sys/types.h>
16 #include <sys/wait.h>
17
18 int pip[2];
19
20 int pipemax;
21
22 int
23 process (void *arg)
24 {
25 char *s = arg;
26 char *buf = malloc (pipemax * 100);
27 int ret;
28
29 if (buf == NULL)
30 abort ();
31
32 *buf = *s;
33
34 /* The first write should go straight through. */
35 if (write (pip[1], buf, 1) != 1)
36 abort ();
37
38 *buf = s[1];
39
40 /* The second write should only successful for at most the PIPE_MAX
41 part, but no error. */
42 ret = write (pip[1], buf, pipemax * 10);
43 if (ret != 0 && ret != pipemax - 1 && ret != pipemax)
44 {
45 fprintf (stderr, "ret: %d\n", ret);
46 fflush (0);
47 abort ();
48 }
49
50 return 0;
51 }
52
53 int
54 main (void)
55 {
56 int retcode;
57 int pid;
58 int st = 0;
59 long stack[16384];
60 char buf[1];
61
62 /* We need to turn this off because we don't want (to have to model) a
63 SIGPIPE resulting from the close. */
64 if (signal (SIGPIPE, SIG_IGN) != SIG_DFL)
65 abort ();
66
67 retcode = pipe (pip);
68
69 if (retcode != 0)
70 {
71 fprintf (stderr, "Bad pipe %d\n", retcode);
72 abort ();
73 }
74
75 #ifdef PIPE_MAX
76 pipemax = PIPE_MAX;
77 #else
78 pipemax = fpathconf (pip[1], _PC_PIPE_BUF);
79 #endif
80
81 if (pipemax <= 0)
82 {
83 fprintf (stderr, "Bad pipemax %d\n", pipemax);
84 abort ();
85 }
86
87 pid = clone (process, (char *) stack + sizeof (stack) - 64,
88 (CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND)
89 | SIGCHLD, "ab");
90 if (pid <= 0)
91 {
92 fprintf (stderr, "Bad clone %d\n", pid);
93 abort ();
94 }
95
96 while ((retcode = read (pip[0], buf, 1)) == 0)
97 ;
98
99 if (retcode != 1)
100 {
101 fprintf (stderr, "Bad read 1: %d\n", retcode);
102 abort ();
103 }
104
105 printf ("got: %c\n", buf[0]);
106
107 if (close (pip[0]) != 0)
108 {
109 perror ("pip close");
110 abort ();
111 }
112
113 retcode = waitpid (pid, &st, __WALL);
114
115 if (retcode != pid || !WIFEXITED (st))
116 {
117 fprintf (stderr, "Bad wait %d:%d %x\n", pid, retcode, st);
118 perror ("errno");
119 abort ();
120 }
121
122 printf ("exit: %d\n", WEXITSTATUS (st));
123 return 0;
124 }
This page took 0.032627 seconds and 4 git commands to generate.