1 /* Check that closing a pipe with a nonempty buffer works.
3 #output: got: a\ngot: b\nexit: 0\n
15 #include <sys/types.h>
26 int lots
= pipemax
+ 256;
27 char *buf
= malloc (lots
);
35 /* The first write should go straight through. */
36 if (write (pip
[1], buf
, 1) != 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
);
51 fprintf (stderr
, "ret: %d, %s, %d\n", ret
, strerror (errno
), pipemax
);
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
)
77 fprintf (stderr
, "Bad pipe %d\n", retcode
);
84 pipemax
= fpathconf (pip
[1], _PC_PIPE_BUF
);
89 fprintf (stderr
, "Bad pipemax %d\n", pipemax
);
93 pid
= clone (process
, (char *) stack
+ sizeof (stack
) - 64,
94 (CLONE_VM
| CLONE_FS
| CLONE_FILES
| CLONE_SIGHAND
)
98 fprintf (stderr
, "Bad clone %d\n", pid
);
102 while ((retcode
= read (pip
[0], buf
, 1)) == 0)
107 fprintf (stderr
, "Bad read 1: %d\n", retcode
);
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)
120 fprintf (stderr
, "Bad read 2: %d\n", retcode
);
124 printf ("got: %c\n", buf
[0]);
126 if (close (pip
[0]) != 0)
128 perror ("pip close");
132 retcode
= waitpid (pid
, &st
, __WALL
);
134 if (retcode
!= pid
|| !WIFEXITED (st
))
136 fprintf (stderr
, "Bad wait %d:%d %x\n", pid
, retcode
, st
);
141 printf ("exit: %d\n", WEXITSTATUS (st
));