popen-safer: prevent popen from clobbering std descriptors
[gnulib.git] / tests / test-popen.c
1 /* Test of opening a subcommand stream.
2    Copyright (C) 2009 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 of the License, or
7    (at your option) 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 /* Written by Eric Blake <ebb9@byu.net>, 2009.  */
18
19 #include <config.h>
20
21 /* Specification.  */
22 #include <stdio.h>
23
24 /* Helpers.  */
25 #include <stdlib.h>
26 #include <string.h>
27 #include <sys/wait.h>
28 #include <unistd.h>
29
30 #if GNULIB_POPEN_SAFER
31 # include "stdio--.h"
32 #endif
33
34 #define ASSERT(expr) \
35   do                                                                         \
36     {                                                                        \
37       if (!(expr))                                                           \
38         {                                                                    \
39           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
40           fflush (stderr);                                                   \
41           abort ();                                                          \
42         }                                                                    \
43     }                                                                        \
44   while (0)
45
46 int
47 main (int argc, char **argv)
48 {
49   size_t len;
50   char *cmd;
51   int i;
52
53   /* Children - use the pipe.  */
54   if (argc > 1)
55     {
56       if (*argv[1] == 'r') /* Parent is reading, so we write.  */
57         ASSERT (putchar ('c') == 'c');
58       else /* Parent is writing, so we read.  */
59         ASSERT (getchar () == 'p');
60       /* Test that parent can read non-zero status.  */
61       return 42;
62     }
63
64   /* Parent - create read and write child, once under normal
65      circumstances and once with stdin and stdout closed.  */
66   len = strlen (argv[0]);
67   cmd = malloc (len + 3); /* Adding " r" and NUL.  */
68   ASSERT (cmd);
69   /* We count on argv[0] not containing any shell metacharacters.  */
70   strcpy (cmd, argv[0]);
71   cmd[len] = ' ';
72   cmd[len + 2] = '\0';
73   for (i = 0; i < 2; i++)
74     {
75       FILE *child;
76       int status;
77
78       if (i)
79         {
80           ASSERT (fclose (stdin) == 0);
81           ASSERT (fclose (stdout) == 0);
82         }
83
84       cmd[len + 1] = 'r';
85       ASSERT (child = popen (cmd, "r"));
86       ASSERT (fgetc (child) == 'c');
87       status = pclose (child);
88       ASSERT (WIFEXITED (status));
89       ASSERT (WEXITSTATUS (status) == 42);
90       if (i)
91         {
92           ASSERT (dup2 (STDIN_FILENO, STDIN_FILENO) == -1);
93           ASSERT (dup2 (STDOUT_FILENO, STDOUT_FILENO) == -1);
94         }
95
96       cmd[len + 1] = 'w';
97       ASSERT (child = popen (cmd, "w"));
98       ASSERT (fputc ('p', child) == 'p');
99       status = pclose (child);
100       ASSERT (WIFEXITED (status));
101       ASSERT (WEXITSTATUS (status) == 42);
102       if (i)
103         {
104           ASSERT (dup2 (STDIN_FILENO, STDIN_FILENO) == -1);
105           ASSERT (dup2 (STDOUT_FILENO, STDOUT_FILENO) == -1);
106         }
107     }
108   free (cmd);
109   return 0;
110 }