popen-safer: prevent popen from clobbering std descriptors
[gnulib.git] / tests / test-popen-safer.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 <sys/wait.h>
27 #include <unistd.h>
28
29 /* This test intentionally closes stderr.  So, we arrange to have fd 10
30    (outside the range of interesting fd's during the test) set up to
31    duplicate the original stderr.  */
32
33 #define BACKUP_STDERR_FILENO 10
34 static FILE *myerr;
35
36 #define ASSERT(expr) \
37   do                                                                         \
38     {                                                                        \
39       if (!(expr))                                                           \
40         {                                                                    \
41           fprintf (myerr, "%s:%d: assertion failed\n", __FILE__, __LINE__);  \
42           fflush (myerr);                                                    \
43           abort ();                                                          \
44         }                                                                    \
45     }                                                                        \
46   while (0)
47
48 int
49 main (int argc, char **argv)
50 {
51   FILE *fp;
52   int status;
53
54   /* We close fd 2 later, so save it in fd 10.  */
55   if (dup2 (STDERR_FILENO, BACKUP_STDERR_FILENO) != BACKUP_STDERR_FILENO
56       || (myerr = fdopen (BACKUP_STDERR_FILENO, "w")) == NULL)
57     return 2;
58
59   ASSERT (fp = popen ("exit 0", "r"));
60   ASSERT (STDERR_FILENO < fileno (fp));
61   status = pclose (fp);
62   ASSERT (WIFEXITED (status));
63   ASSERT (!WEXITSTATUS (status));
64
65   ASSERT (fclose (stdin) == 0);
66   ASSERT (fp = popen ("exit 0", "r"));
67   ASSERT (STDERR_FILENO < fileno (fp));
68   status = pclose (fp);
69   ASSERT (WIFEXITED (status));
70   ASSERT (!WEXITSTATUS (status));
71
72   ASSERT (fclose (stdout) == 0);
73   ASSERT (fp = popen ("exit 0", "r"));
74   ASSERT (STDERR_FILENO < fileno (fp));
75   status = pclose (fp);
76   ASSERT (WIFEXITED (status));
77   ASSERT (!WEXITSTATUS (status));
78
79   ASSERT (fclose (stderr) == 0);
80   ASSERT (fp = popen ("exit 0", "r"));
81   ASSERT (STDERR_FILENO < fileno (fp));
82   status = pclose (fp);
83   ASSERT (WIFEXITED (status));
84   ASSERT (!WEXITSTATUS (status));
85   return 0;
86 }