Mercurial > hg > octave-jordi > gnulib-hg
annotate lib/popen.c @ 14079:97fc9a21a8fb
maint: update almost all copyright ranges to include 2011
Run the new "make update-copyright" rule.
author | Jim Meyering <meyering@redhat.com> |
---|---|
date | Sat, 01 Jan 2011 20:17:23 +0100 |
parents | c2cbabec01dd |
children | 5f0f5820c414 |
rev | line source |
---|---|
11848
9e3299dad578
popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
1 /* Open a stream to a sub-process. |
14079
97fc9a21a8fb
maint: update almost all copyright ranges to include 2011
Jim Meyering <meyering@redhat.com>
parents:
12559
diff
changeset
|
2 Copyright (C) 2009-2011 Free Software Foundation, Inc. |
11848
9e3299dad578
popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
3 |
9e3299dad578
popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
4 This program is free software: you can redistribute it and/or modify |
9e3299dad578
popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
5 it under the terms of the GNU General Public License as published by |
9e3299dad578
popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
6 the Free Software Foundation; either version 3 of the License, or |
9e3299dad578
popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
7 (at your option) any later version. |
9e3299dad578
popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
8 |
9e3299dad578
popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
9 This program is distributed in the hope that it will be useful, |
9e3299dad578
popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
10 but WITHOUT ANY WARRANTY; without even the implied warranty of |
9e3299dad578
popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
9e3299dad578
popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
12 GNU General Public License for more details. |
9e3299dad578
popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
13 |
9e3299dad578
popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
14 You should have received a copy of the GNU General Public License |
9e3299dad578
popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
9e3299dad578
popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
16 |
9e3299dad578
popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
17 /* Written by Eric Blake <ebb9@byu.net>, 2009. */ |
9e3299dad578
popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
18 |
9e3299dad578
popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
19 #include <config.h> |
9e3299dad578
popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
20 |
9e3299dad578
popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
21 /* Specification. */ |
9e3299dad578
popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
22 #include <stdio.h> |
9e3299dad578
popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
23 |
9e3299dad578
popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
24 #include <errno.h> |
9e3299dad578
popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
25 #include <fcntl.h> |
9e3299dad578
popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
26 #include <stdlib.h> |
9e3299dad578
popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
27 #include <unistd.h> |
9e3299dad578
popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
28 |
11857
f17a5ed578bd
popen: simplify access to original popen
Eric Blake <ebb9@byu.net>
parents:
11848
diff
changeset
|
29 #undef popen |
f17a5ed578bd
popen: simplify access to original popen
Eric Blake <ebb9@byu.net>
parents:
11848
diff
changeset
|
30 |
11848
9e3299dad578
popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
31 FILE * |
9e3299dad578
popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
32 rpl_popen (const char *filename, const char *mode) |
9e3299dad578
popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
33 { |
9e3299dad578
popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
34 /* The mingw popen works fine, and all other platforms have fcntl. |
9e3299dad578
popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
35 The bug of the child clobbering its own file descriptors if stdin |
9e3299dad578
popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
36 or stdout was closed in the parent can be worked around by |
9e3299dad578
popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
37 opening those two fds as close-on-exec to begin with. */ |
9e3299dad578
popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
38 /* Cygwin 1.5.x also has a bug where the popen fd is improperly |
9e3299dad578
popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
39 marked close-on-exec, and if the application undoes this, then |
9e3299dad578
popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
40 the fd leaks into subsequent popen calls. We could work around |
9e3299dad578
popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
41 this by maintaining a list of all fd's opened by popen, and |
9e3299dad578
popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
42 temporarily marking them cloexec around the real popen call, but |
9e3299dad578
popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
43 we would also have to override pclose, and the bookkeepping seems |
9e3299dad578
popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
44 extreme given that cygwin 1.7 no longer has the bug. */ |
9e3299dad578
popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
45 FILE *result; |
9e3299dad578
popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
46 int cloexec0 = fcntl (STDIN_FILENO, F_GETFD); |
9e3299dad578
popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
47 int cloexec1 = fcntl (STDOUT_FILENO, F_GETFD); |
9e3299dad578
popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
48 int saved_errno; |
9e3299dad578
popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
49 |
11857
f17a5ed578bd
popen: simplify access to original popen
Eric Blake <ebb9@byu.net>
parents:
11848
diff
changeset
|
50 /* If either stdin or stdout was closed (that is, fcntl failed), |
f17a5ed578bd
popen: simplify access to original popen
Eric Blake <ebb9@byu.net>
parents:
11848
diff
changeset
|
51 then we open a dummy close-on-exec fd to occupy that slot. That |
f17a5ed578bd
popen: simplify access to original popen
Eric Blake <ebb9@byu.net>
parents:
11848
diff
changeset
|
52 way, popen's internal use of pipe() will not contain either fd 0 |
f17a5ed578bd
popen: simplify access to original popen
Eric Blake <ebb9@byu.net>
parents:
11848
diff
changeset
|
53 or 1, overcoming the fact that the child process blindly calls |
f17a5ed578bd
popen: simplify access to original popen
Eric Blake <ebb9@byu.net>
parents:
11848
diff
changeset
|
54 close() on the parent's end of the pipe without first checking |
f17a5ed578bd
popen: simplify access to original popen
Eric Blake <ebb9@byu.net>
parents:
11848
diff
changeset
|
55 whether it is clobbering the fd just placed there via dup2(); the |
f17a5ed578bd
popen: simplify access to original popen
Eric Blake <ebb9@byu.net>
parents:
11848
diff
changeset
|
56 exec will get rid of the dummy fd's in the child. Fortunately, |
f17a5ed578bd
popen: simplify access to original popen
Eric Blake <ebb9@byu.net>
parents:
11848
diff
changeset
|
57 closed stderr in the parent does not cause problems in the |
f17a5ed578bd
popen: simplify access to original popen
Eric Blake <ebb9@byu.net>
parents:
11848
diff
changeset
|
58 child. */ |
11848
9e3299dad578
popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
59 if (cloexec0 < 0) |
9e3299dad578
popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
60 { |
9e3299dad578
popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
61 if (open ("/dev/null", O_RDONLY) != STDIN_FILENO |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11857
diff
changeset
|
62 || fcntl (STDIN_FILENO, F_SETFD, |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11857
diff
changeset
|
63 fcntl (STDIN_FILENO, F_GETFD) | FD_CLOEXEC) == -1) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11857
diff
changeset
|
64 abort (); |
11848
9e3299dad578
popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
65 } |
9e3299dad578
popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
66 if (cloexec1 < 0) |
9e3299dad578
popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
67 { |
9e3299dad578
popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
68 if (open ("/dev/null", O_RDONLY) != STDOUT_FILENO |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11857
diff
changeset
|
69 || fcntl (STDOUT_FILENO, F_SETFD, |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11857
diff
changeset
|
70 fcntl (STDOUT_FILENO, F_GETFD) | FD_CLOEXEC) == -1) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11857
diff
changeset
|
71 abort (); |
11848
9e3299dad578
popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
72 } |
11857
f17a5ed578bd
popen: simplify access to original popen
Eric Blake <ebb9@byu.net>
parents:
11848
diff
changeset
|
73 result = popen (filename, mode); |
f17a5ed578bd
popen: simplify access to original popen
Eric Blake <ebb9@byu.net>
parents:
11848
diff
changeset
|
74 /* Now, close any dummy fd's created in the parent. */ |
11848
9e3299dad578
popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
75 saved_errno = errno; |
9e3299dad578
popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
76 if (cloexec0 < 0) |
9e3299dad578
popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
77 close (STDIN_FILENO); |
9e3299dad578
popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
78 if (cloexec1 < 0) |
9e3299dad578
popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
79 close (STDOUT_FILENO); |
9e3299dad578
popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
80 errno = saved_errno; |
9e3299dad578
popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
81 return result; |
9e3299dad578
popen: fix cygwin 1.5 bug when stdin closed
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
82 } |