Merge commit 'b572c3a256e7bf1e4eecc8c36448c08093240a6a' into stable
[gnulib.git] / lib / popen.c
1 /* Open a stream to a sub-process.
2    Copyright (C) 2009-2011 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 #if HAVE_POPEN
25
26 # include <errno.h>
27 # include <fcntl.h>
28 # include <stdlib.h>
29 # include <unistd.h>
30
31 # undef popen
32
33 FILE *
34 rpl_popen (const char *filename, const char *mode)
35 {
36   /* The mingw popen works fine, and all other platforms have fcntl.
37      The bug of the child clobbering its own file descriptors if stdin
38      or stdout was closed in the parent can be worked around by
39      opening those two fds as close-on-exec to begin with.  */
40   /* Cygwin 1.5.x also has a bug where the popen fd is improperly
41      marked close-on-exec, and if the application undoes this, then
42      the fd leaks into subsequent popen calls.  We could work around
43      this by maintaining a list of all fd's opened by popen, and
44      temporarily marking them cloexec around the real popen call, but
45      we would also have to override pclose, and the bookkeepping seems
46      extreme given that cygwin 1.7 no longer has the bug.  */
47   FILE *result;
48   int cloexec0 = fcntl (STDIN_FILENO, F_GETFD);
49   int cloexec1 = fcntl (STDOUT_FILENO, F_GETFD);
50   int saved_errno;
51
52   /* If either stdin or stdout was closed (that is, fcntl failed),
53      then we open a dummy close-on-exec fd to occupy that slot.  That
54      way, popen's internal use of pipe() will not contain either fd 0
55      or 1, overcoming the fact that the child process blindly calls
56      close() on the parent's end of the pipe without first checking
57      whether it is clobbering the fd just placed there via dup2(); the
58      exec will get rid of the dummy fd's in the child.  Fortunately,
59      closed stderr in the parent does not cause problems in the
60      child.  */
61   if (cloexec0 < 0)
62     {
63       if (open ("/dev/null", O_RDONLY) != STDIN_FILENO
64           || fcntl (STDIN_FILENO, F_SETFD,
65                     fcntl (STDIN_FILENO, F_GETFD) | FD_CLOEXEC) == -1)
66         abort ();
67     }
68   if (cloexec1 < 0)
69     {
70       if (open ("/dev/null", O_RDONLY) != STDOUT_FILENO
71           || fcntl (STDOUT_FILENO, F_SETFD,
72                     fcntl (STDOUT_FILENO, F_GETFD) | FD_CLOEXEC) == -1)
73         abort ();
74     }
75   result = popen (filename, mode);
76   /* Now, close any dummy fd's created in the parent.  */
77   saved_errno = errno;
78   if (cloexec0 < 0)
79     close (STDIN_FILENO);
80   if (cloexec1 < 0)
81     close (STDOUT_FILENO);
82   errno = saved_errno;
83   return result;
84 }
85
86 #else
87 /* Native Woe32 API.  */
88
89 # include <string.h>
90
91 FILE *
92 popen (const char *filename, const char *mode)
93 {
94   /* Use binary mode by default.  */
95   if (strcmp (mode, "r") == 0)
96     mode = "rb";
97   else if (strcmp (mode, "w") == 0)
98     mode = "wb";
99
100   return _popen (filename, mode);
101 }
102
103 #endif