1 /* Check that writing an inordinate amount of data works (somewhat). 2 #progos: linux 3 #output: got: a\nexit: 0\n 4 This test-case will *not* work on host (or for real): the first 5 pipemax+1 bytes will be successfully written. It's just for 6 exercising a rare execution path. */ 7 8 #include <stddef.h> 9 #include <stdlib.h> 10 #include <stdio.h> 11 #include <limits.h> 12 #include <unistd.h> 13 #include <sched.h> 14 #include <signal.h> 15 #include <errno.h> 16 #include <sys/types.h> 17 #include <sys/wait.h> 18 19 int pip[2]; 20 21 int pipemax; 22 23 int 24 process (void *arg) 25 { 26 char *s = arg; 27 char *buf = calloc (pipemax * 100, 1); 28 int ret; 29 30 if (buf == NULL) 31 abort (); 32 33 *buf = *s; 34 35 ret = write (pip[1], buf, pipemax * 100); 36 if (ret != -1 || errno != EFBIG) 37 { 38 perror ("write"); 39 abort (); 40 } 41 42 return 0; 43 } 44 45 int 46 main (void) 47 { 48 int retcode; 49 int pid; 50 int st = 0; 51 long stack[16384]; 52 char buf[1]; 53 54 retcode = pipe (pip); 55 56 if (retcode != 0) 57 { 58 fprintf (stderr, "Bad pipe %d\n", retcode); 59 abort (); 60 } 61 62 #ifdef PIPE_MAX 63 pipemax = PIPE_MAX; 64 #else 65 pipemax = fpathconf (pip[1], _PC_PIPE_BUF); 66 #endif 67 68 if (pipemax <= 0) 69 { 70 fprintf (stderr, "Bad pipemax %d\n", pipemax); 71 abort (); 72 } 73 74 pid = clone (process, (char *) stack + sizeof (stack) - 64, 75 (CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND) 76 | SIGCHLD, "ab"); 77 if (pid <= 0) 78 { 79 fprintf (stderr, "Bad clone %d\n", pid); 80 abort (); 81 } 82 83 while ((retcode = read (pip[0], buf, 1)) == 0) 84 ; 85 86 if (retcode != 1) 87 { 88 fprintf (stderr, "Bad read 1: %d\n", retcode); 89 abort (); 90 } 91 92 printf ("got: %c\n", buf[0]); 93 94 if (close (pip[0]) != 0) 95 { 96 perror ("pip close"); 97 abort (); 98 } 99 100 retcode = waitpid (pid, &st, __WALL); 101 102 if (retcode != pid || !WIFEXITED (st)) 103 { 104 fprintf (stderr, "Bad wait %d:%d %x\n", pid, retcode, st); 105 perror ("errno"); 106 abort (); 107 } 108 109 printf ("exit: %d\n", WEXITSTATUS (st)); 110 return 0; 111 } 112