~ubuntu-branches/ubuntu/utopic/coreutils/utopic-proposed

« back to all changes in this revision

Viewing changes to gnulib-tests/test-spawn-pipe-main.c

  • Committer: Package Import Robot
  • Author(s): Colin Watson
  • Date: 2012-11-28 03:03:42 UTC
  • mfrom: (8.3.4 sid)
  • Revision ID: package-import@ubuntu.com-20121128030342-21zanj8354gas5gr
Tags: 8.20-3ubuntu1
* Resynchronise with Debian.  Remaining changes:
  - Make 'uname -i -p' return the real processor/hardware, instead of
    unknown.
  - Build-depend on gettext:any instead of on gettext, so that apt-get can
    properly resolve build-dependencies on the tool when cross-building.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Test of create_pipe_bidi/wait_subprocess.
 
2
   Copyright (C) 2009-2012 Free Software Foundation, Inc.
 
3
 
 
4
   This program is free software; you can redistribute it and/or modify
 
5
   it under the terms of the GNU General Public License as published by
 
6
   the Free Software Foundation; either version 3, or (at your option)
 
7
   any later version.
 
8
 
 
9
   This program is distributed in the hope that it will be useful,
 
10
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
   GNU General Public License for more details.
 
13
 
 
14
   You should have received a copy of the GNU General Public License
 
15
   along with this program; if not, see <http://www.gnu.org/licenses/>.  */
 
16
 
 
17
#include <config.h>
 
18
 
 
19
#include "spawn-pipe.h"
 
20
#include "wait-process.h"
 
21
#include "progname.h"
 
22
 
 
23
#include <stdbool.h>
 
24
#include <stdio.h>
 
25
#include <stdlib.h>
 
26
#include <string.h>
 
27
#include <unistd.h>
 
28
 
 
29
/* Depending on arguments, this test intentionally closes stderr or
 
30
   starts life with stderr closed.  So, we arrange to have fd 10
 
31
   (outside the range of interesting fd's during the test) set up to
 
32
   duplicate the original stderr.  */
 
33
 
 
34
#define BACKUP_STDERR_FILENO 10
 
35
#define ASSERT_STREAM myerr
 
36
#include "macros.h"
 
37
 
 
38
static FILE *myerr;
 
39
 
 
40
/* Create a bi-directional pipe to a test child, and validate that the
 
41
   child program returns the expected output.
 
42
   PROG is the program to run in the child process.
 
43
   STDERR_CLOSED is true if we have already closed fd 2.  */
 
44
static void
 
45
test_pipe (const char *prog, bool stderr_closed)
 
46
{
 
47
  int fd[2];
 
48
  char *argv[3];
 
49
  pid_t pid;
 
50
  char buffer[2] = { 'a', 't' };
 
51
 
 
52
  /* Set up child.  */
 
53
  argv[0] = (char *) prog;
 
54
  argv[1] = (char *) (stderr_closed ? "1" : "0");
 
55
  argv[2] = NULL;
 
56
  pid = create_pipe_bidi (prog, prog, argv, false, true, true, fd);
 
57
  ASSERT (0 <= pid);
 
58
  ASSERT (STDERR_FILENO < fd[0]);
 
59
  ASSERT (STDERR_FILENO < fd[1]);
 
60
 
 
61
  /* Push child's input.  */
 
62
  ASSERT (write (fd[1], buffer, 1) == 1);
 
63
  ASSERT (close (fd[1]) == 0);
 
64
 
 
65
  /* Get child's output.  */
 
66
  ASSERT (read (fd[0], buffer, 2) == 1);
 
67
 
 
68
  /* Wait for child.  */
 
69
  ASSERT (wait_subprocess (pid, prog, true, false, true, true, NULL) == 0);
 
70
  ASSERT (close (fd[0]) == 0);
 
71
 
 
72
  /* Check the result.  */
 
73
  ASSERT (buffer[0] == 'b');
 
74
  ASSERT (buffer[1] == 't');
 
75
}
 
76
 
 
77
int
 
78
main (int argc, char *argv[])
 
79
{
 
80
  int test;
 
81
  int fd;
 
82
 
 
83
  set_program_name (argv[0]);
 
84
 
 
85
  if (argc != 3)
 
86
    {
 
87
      fprintf (stderr, "%s: need 2 arguments\n", argv[0]);
 
88
      return 2;
 
89
    }
 
90
  /* We might close fd 2 later, so save it in fd 10.  */
 
91
  if (dup2 (STDERR_FILENO, BACKUP_STDERR_FILENO) != BACKUP_STDERR_FILENO
 
92
      || (myerr = fdopen (BACKUP_STDERR_FILENO, "w")) == NULL)
 
93
    return 2;
 
94
 
 
95
  /* Selectively close various standard fds, to verify the child process is
 
96
     not impacted by this.  */
 
97
  test = atoi (argv[2]);
 
98
  switch (test)
 
99
    {
 
100
    case 0:
 
101
      break;
 
102
    case 1:
 
103
      close (0);
 
104
      break;
 
105
    case 2:
 
106
      close (1);
 
107
      break;
 
108
    case 3:
 
109
      close (0);
 
110
      close (1);
 
111
      break;
 
112
    case 4:
 
113
      close (2);
 
114
      break;
 
115
    case 5:
 
116
      close (0);
 
117
      close (2);
 
118
      break;
 
119
    case 6:
 
120
      close (1);
 
121
      close (2);
 
122
      break;
 
123
    case 7:
 
124
      close (0);
 
125
      close (1);
 
126
      close (2);
 
127
      break;
 
128
    default:
 
129
      ASSERT (false);
 
130
    }
 
131
 
 
132
  /* Plug any file descriptor leaks inherited from outside world before
 
133
     starting, so that child has a clean slate (at least for the fds that we
 
134
     might be manipulating).  */
 
135
  for (fd = 3; fd < 7; fd++)
 
136
    close (fd);
 
137
 
 
138
  test_pipe (argv[1], test >= 4);
 
139
 
 
140
  return 0;
 
141
}