File: pipe6.c

package info (click to toggle)
gdb-doc 16.3-1
  • links: PTS, VCS
  • area: non-free
  • in suites: forky, sid, trixie
  • size: 244,264 kB
  • sloc: ansic: 2,134,731; asm: 375,582; exp: 206,875; cpp: 73,639; makefile: 70,232; sh: 26,038; python: 13,697; yacc: 11,341; ada: 7,358; xml: 6,098; perl: 5,077; pascal: 3,389; tcl: 2,986; f90: 2,764; lisp: 1,984; cs: 879; lex: 738; sed: 228; awk: 181; objc: 137; fortran: 57
file content (111 lines) | stat: -rw-r--r-- 1,996 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/* Check that writing an inordinate amount of data works (somewhat).
#progos: linux
#output: got: a\nexit: 0\n
   This test-case will *not* work on host (or for real): the first
   pipemax+1 bytes will be successfully written.  It's just for
   exercising a rare execution path.  */

#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>

int pip[2];

int pipemax;

int
process (void *arg)
{
  char *s = arg;
  char *buf = calloc (pipemax * 100, 1);
  int ret;

  if (buf == NULL)
    abort ();

  *buf = *s;

  ret = write (pip[1], buf, pipemax * 100);
  if (ret != -1 || errno != EFBIG)
    {
      perror ("write");
      abort ();
    }

  return 0;
}

int
main (void)
{
  int retcode;
  int pid;
  int st = 0;
  long stack[16384];
  char buf[1];

  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]);

  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;
}