Home | History | Annotate | Line # | Download | only in c
      1 /* Check that TRT happens for pipe corner cases.
      2 #progos: linux
      3 */
      4 #include <stddef.h>
      5 #include <signal.h>
      6 #include <stdlib.h>
      7 #include <stdio.h>
      8 #include <unistd.h>
      9 #include <errno.h>
     10 #include <limits.h>
     11 
     12 void err (const char *s)
     13 {
     14   perror (s);
     15   abort ();
     16 }
     17 
     18 int main (void)
     19 {
     20   int pip[2];
     21   char c;
     22   int pipemax;
     23 
     24   if (pipe (pip) != 0)
     25     err ("pipe");
     26 
     27 #ifdef PIPE_MAX
     28   pipemax = PIPE_MAX;
     29 #else
     30   pipemax = fpathconf (pip[1], _PC_PIPE_BUF);
     31 #endif
     32 
     33   if (pipemax <= 0)
     34     {
     35       fprintf (stderr, "Bad pipemax %d\n", pipemax);
     36       abort ();
     37     }
     38 
     39   /* Writing to wrong end of pipe.  */
     40   if (write (pip[0], "argh", 1) != -1
     41       || errno != EBADF)
     42     err ("write pipe");
     43 
     44   errno = 0;
     45 
     46   /* Reading from wrong end of pipe.  */
     47   if (read (pip[1], &c, 1) != -1
     48       || errno != EBADF)
     49     err ("write pipe");
     50 
     51   errno = 0;
     52 
     53   if (close (pip[0]) != 0)
     54     err ("close");
     55 
     56   if (signal (SIGPIPE, SIG_IGN) != SIG_DFL)
     57     err ("signal");
     58 
     59   /* Writing to pipe with closed read end.  */
     60   if (write (pip[1], "argh", 1) != -1
     61       || errno != EPIPE)
     62     err ("write closed");
     63 
     64   printf ("pass\n");
     65   exit (0);
     66 }
     67