Update release-README after completing the 2.43 release.
[binutils-gdb.git] / sim / testsuite / cris / c / pipe2.c
blob7f3de61211a16d3a3eae8dc55f719e3ebf390f54
1 /* Check that closing a pipe with a nonempty buffer works.
2 #progos: linux
3 #output: got: a\ngot: b\nexit: 0\n
4 */
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 #include <string.h>
18 int pip[2];
20 int pipemax;
22 int
23 process (void *arg)
25 char *s = arg;
26 int lots = pipemax + 256;
27 char *buf = malloc (lots);
28 int ret;
30 if (buf == NULL)
31 abort ();
33 *buf = *s;
35 /* The first write should go straight through. */
36 if (write (pip[1], buf, 1) != 1)
37 abort ();
39 *buf = s[1];
41 /* The second write may or may not be successful for the whole
42 write, but should be successful for at least the pipemax part.
43 As linux/limits.h clamps PIPE_BUF to 4096, but the page size is
44 actually 8k, we can get away with that much. There should be no
45 error, though. Doing this on host shows that for
46 x86_64-unknown-linux-gnu (2.6.14-1.1656_FC4) pipemax * 10 can be
47 successfully written, perhaps for similar reasons. */
48 ret = write (pip[1], buf, lots);
49 if (ret < pipemax)
51 fprintf (stderr, "ret: %d, %s, %d\n", ret, strerror (errno), pipemax);
52 fflush (0);
53 abort ();
56 return 0;
59 int
60 main (void)
62 int retcode;
63 int pid;
64 int st = 0;
65 long stack[16384];
66 char buf[1];
68 /* We need to turn this off because we don't want (to have to model) a
69 SIGPIPE resulting from the close. */
70 if (signal (SIGPIPE, SIG_IGN) != SIG_DFL)
71 abort ();
73 retcode = pipe (pip);
75 if (retcode != 0)
77 fprintf (stderr, "Bad pipe %d\n", retcode);
78 abort ();
81 #ifdef PIPE_MAX
82 pipemax = PIPE_MAX;
83 #else
84 pipemax = fpathconf (pip[1], _PC_PIPE_BUF);
85 #endif
87 if (pipemax <= 0)
89 fprintf (stderr, "Bad pipemax %d\n", pipemax);
90 abort ();
93 pid = clone (process, (char *) stack + sizeof (stack) - 64,
94 (CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND)
95 | SIGCHLD, "ab");
96 if (pid <= 0)
98 fprintf (stderr, "Bad clone %d\n", pid);
99 abort ();
102 while ((retcode = read (pip[0], buf, 1)) == 0)
105 if (retcode != 1)
107 fprintf (stderr, "Bad read 1: %d\n", retcode);
108 abort ();
111 printf ("got: %c\n", buf[0]);
113 /* Need to read out something from the second write too before
114 closing, or the writer can get EPIPE. */
115 while ((retcode = read (pip[0], buf, 1)) == 0)
118 if (retcode != 1)
120 fprintf (stderr, "Bad read 2: %d\n", retcode);
121 abort ();
124 printf ("got: %c\n", buf[0]);
126 if (close (pip[0]) != 0)
128 perror ("pip close");
129 abort ();
132 retcode = waitpid (pid, &st, __WALL);
134 if (retcode != pid || !WIFEXITED (st))
136 fprintf (stderr, "Bad wait %d:%d %x\n", pid, retcode, st);
137 perror ("errno");
138 abort ();
141 printf ("exit: %d\n", WEXITSTATUS (st));
142 return 0;