aboutsummaryrefslogtreecommitdiff
path: root/sim/testsuite/cris/c/pipe2.c
diff options
context:
space:
mode:
authorMike Frysinger <vapier@gentoo.org>2021-01-05 22:09:57 -0500
committerMike Frysinger <vapier@gentoo.org>2021-01-15 19:18:34 -0500
commit1368b914e93a3af332f787d3d41c106d11bb90da (patch)
tree9893ccae5d2d8cbf2ce855e09d6b8f30b56a21bc /sim/testsuite/cris/c/pipe2.c
parente403a898b5893337baea73bcb001ece74042f351 (diff)
downloadbinutils-1368b914e93a3af332f787d3d41c106d11bb90da.zip
binutils-1368b914e93a3af332f787d3d41c106d11bb90da.tar.gz
binutils-1368b914e93a3af332f787d3d41c106d11bb90da.tar.bz2
sim: testsuite: flatten tree
Now that all port tests live under testsuite/sim/*/, and none live in testsuite/ directly, flatten the structure by moving all of the dirs under testsuite/sim/ to testsuite/ directly. We need to stop passing --tool to dejagnu so that it searches all dirs and not just ones that start with "sim". Since we have no other dirs in this tree, and no plans to add any, should be fine.
Diffstat (limited to 'sim/testsuite/cris/c/pipe2.c')
-rw-r--r--sim/testsuite/cris/c/pipe2.c143
1 files changed, 143 insertions, 0 deletions
diff --git a/sim/testsuite/cris/c/pipe2.c b/sim/testsuite/cris/c/pipe2.c
new file mode 100644
index 0000000..18ccf38
--- /dev/null
+++ b/sim/testsuite/cris/c/pipe2.c
@@ -0,0 +1,143 @@
+/* Check that closing a pipe with a nonempty buffer works.
+#notarget: cris*-*-elf
+#output: got: a\ngot: b\nexit: 0\n
+*/
+
+
+#include <stddef.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <limits.h>
+#include <unistd.h>
+#include <sched.h>
+#include <signal.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <string.h>
+int pip[2];
+
+int pipemax;
+
+int
+process (void *arg)
+{
+ char *s = arg;
+ int lots = pipemax + 256;
+ char *buf = malloc (lots);
+ int ret;
+
+ if (buf == NULL)
+ abort ();
+
+ *buf = *s;
+
+ /* The first write should go straight through. */
+ if (write (pip[1], buf, 1) != 1)
+ abort ();
+
+ *buf = s[1];
+
+ /* The second write may or may not be successful for the whole
+ write, but should be successful for at least the pipemax part.
+ As linux/limits.h clamps PIPE_BUF to 4096, but the page size is
+ actually 8k, we can get away with that much. There should be no
+ error, though. Doing this on host shows that for
+ x86_64-unknown-linux-gnu (2.6.14-1.1656_FC4) pipemax * 10 can be
+ successfully written, perhaps for similar reasons. */
+ ret = write (pip[1], buf, lots);
+ if (ret < pipemax)
+ {
+ fprintf (stderr, "ret: %d, %s, %d\n", ret, strerror (errno), pipemax);
+ fflush (0);
+ abort ();
+ }
+
+ return 0;
+}
+
+int
+main (void)
+{
+ int retcode;
+ int pid;
+ int st = 0;
+ long stack[16384];
+ char buf[1];
+
+ /* We need to turn this off because we don't want (to have to model) a
+ SIGPIPE resulting from the close. */
+ if (signal (SIGPIPE, SIG_IGN) != SIG_DFL)
+ abort ();
+
+ retcode = pipe (pip);
+
+ if (retcode != 0)
+ {
+ fprintf (stderr, "Bad pipe %d\n", retcode);
+ abort ();
+ }
+
+#ifdef PIPE_MAX
+ pipemax = PIPE_MAX;
+#else
+ pipemax = fpathconf (pip[1], _PC_PIPE_BUF);
+#endif
+
+ if (pipemax <= 0)
+ {
+ fprintf (stderr, "Bad pipemax %d\n", pipemax);
+ abort ();
+ }
+
+ pid = clone (process, (char *) stack + sizeof (stack) - 64,
+ (CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND)
+ | SIGCHLD, "ab");
+ if (pid <= 0)
+ {
+ fprintf (stderr, "Bad clone %d\n", pid);
+ abort ();
+ }
+
+ while ((retcode = read (pip[0], buf, 1)) == 0)
+ ;
+
+ if (retcode != 1)
+ {
+ fprintf (stderr, "Bad read 1: %d\n", retcode);
+ abort ();
+ }
+
+ printf ("got: %c\n", buf[0]);
+
+ /* Need to read out something from the second write too before
+ closing, or the writer can get EPIPE. */
+ while ((retcode = read (pip[0], buf, 1)) == 0)
+ ;
+
+ if (retcode != 1)
+ {
+ fprintf (stderr, "Bad read 2: %d\n", retcode);
+ abort ();
+ }
+
+ printf ("got: %c\n", buf[0]);
+
+ if (close (pip[0]) != 0)
+ {
+ perror ("pip close");
+ abort ();
+ }
+
+ retcode = waitpid (pid, &st, __WALL);
+
+ if (retcode != pid || !WIFEXITED (st))
+ {
+ fprintf (stderr, "Bad wait %d:%d %x\n", pid, retcode, st);
+ perror ("errno");
+ abort ();
+ }
+
+ printf ("exit: %d\n", WEXITSTATUS (st));
+ return 0;
+}