1 /* Copyright (C) 1993-2019 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Written by Per Bothner <bothner@cygnus.com>.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>.
19 As a special exception, if you link the code in this file with
20 files compiled with a GNU compiler to produce an executable,
21 that does not cause the resulting executable to be covered by
22 the GNU Lesser General Public License. This exception does not
23 however invalidate any other reasons why the executable file
24 might be covered by the GNU Lesser General Public License.
25 This exception applies to code released by its copyright holders
26 in files containing the exception. */
33 #include <shlib-compat.h>
34 #include <not-cancel.h>
35 #include <sys/types.h>
42 struct _IO_FILE_plus file
;
43 /* Following fields must match those in class procbuf (procbuf.h) */
45 struct _IO_proc_file
*next
;
47 typedef struct _IO_proc_file _IO_proc_file
;
49 static const struct _IO_jump_t _IO_proc_jumps
;
51 static struct _IO_proc_file
*proc_file_chain
;
54 static _IO_lock_t proc_file_chain_lock
= _IO_lock_initializer
;
57 unlock (void *not_used
)
59 _IO_lock_unlock (proc_file_chain_lock
);
63 /* POSIX states popen shall ensure that any streams from previous popen()
64 calls that remain open in the parent process should be closed in the new
66 To avoid a race-condition between checking which file descriptors need to
67 be close (by transversing the proc_file_chain list) and the insertion of a
68 new one after a successful posix_spawn this function should be called
69 with proc_file_chain_lock acquired. */
71 spawn_process (posix_spawn_file_actions_t
*fa
, FILE *fp
, const char *command
,
72 int do_cloexec
, int pipe_fds
[2], int parent_end
, int child_end
,
76 for (struct _IO_proc_file
*p
= proc_file_chain
; p
; p
= p
->next
)
78 int fd
= _IO_fileno ((FILE *) p
);
80 /* If any stream from previous popen() calls has fileno
81 child_pipe_fd, it has been already closed by the adddup2 action
83 if (fd
!= child_pipe_fd
84 && __posix_spawn_file_actions_addclose (fa
, fd
) != 0)
88 if (__posix_spawn (&((_IO_proc_file
*) fp
)->pid
, _PATH_BSHELL
, fa
, 0,
89 (char *const[]){ (char*) "sh", (char*) "-c",
90 (char *) command
, NULL
}, __environ
) != 0)
93 __close_nocancel (pipe_fds
[child_end
]);
96 /* Undo the effects of the pipe2 call which set the
97 close-on-exec flag. */
98 __fcntl (pipe_fds
[parent_end
], F_SETFD
, 0);
100 _IO_fileno (fp
) = pipe_fds
[parent_end
];
102 ((_IO_proc_file
*) fp
)->next
= proc_file_chain
;
103 proc_file_chain
= (_IO_proc_file
*) fp
;
109 _IO_new_proc_open (FILE *fp
, const char *command
, const char *mode
)
112 /* These are indexes for pipe_fds. */
113 int parent_end
, child_end
;
121 while (*mode
!= '\0')
135 __set_errno (EINVAL
);
139 if ((do_read
^ do_write
) == 0)
142 if (_IO_file_is_open (fp
))
145 /* Atomically set the O_CLOEXEC flag for the pipe end used by the
146 child process (to avoid leaking the file descriptor in case of a
147 concurrent fork). This is later reverted in the child process.
148 When popen returns, the parent pipe end can be O_CLOEXEC or not,
149 depending on the 'e' open mode, but there is only one flag which
150 controls both descriptors. The parent end is adjusted below,
151 after creating the child process. (In the child process, the
152 parent end should be closed on execve, so O_CLOEXEC remains set
154 if (__pipe2 (pipe_fds
, O_CLOEXEC
) < 0)
161 read_or_write
= _IO_NO_WRITES
;
168 read_or_write
= _IO_NO_READS
;
172 posix_spawn_file_actions_t fa
;
173 /* posix_spawn_file_actions_init does not fail. */
174 __posix_spawn_file_actions_init (&fa
);
176 /* The descriptor is already the one the child will use. In this case
177 it must be moved to another one otherwise, there is no safe way to
178 remove the close-on-exec flag in the child without creating a FD leak
179 race in the parent. */
180 if (pipe_fds
[child_end
] == child_pipe_fd
)
182 int tmp
= __fcntl (child_pipe_fd
, F_DUPFD_CLOEXEC
, 0);
185 __close_nocancel (pipe_fds
[child_end
]);
186 pipe_fds
[child_end
] = tmp
;
189 if (__posix_spawn_file_actions_adddup2 (&fa
, pipe_fds
[child_end
],
194 _IO_cleanup_region_start_noarg (unlock
);
195 _IO_lock_lock (proc_file_chain_lock
);
197 spawn_ok
= spawn_process (&fa
, fp
, command
, do_cloexec
, pipe_fds
,
198 parent_end
, child_end
, child_pipe_fd
);
200 _IO_lock_unlock (proc_file_chain_lock
);
201 _IO_cleanup_region_end (0);
204 __posix_spawn_file_actions_destroy (&fa
);
209 __close_nocancel (pipe_fds
[child_end
]);
210 __close_nocancel (pipe_fds
[parent_end
]);
211 __set_errno (ENOMEM
);
215 _IO_mask_flags (fp
, read_or_write
, _IO_NO_READS
|_IO_NO_WRITES
);
220 _IO_new_popen (const char *command
, const char *mode
)
224 struct _IO_proc_file fpx
;
231 new_f
= (struct locked_FILE
*) malloc (sizeof (struct locked_FILE
));
235 new_f
->fpx
.file
.file
._lock
= &new_f
->lock
;
237 fp
= &new_f
->fpx
.file
.file
;
238 _IO_init_internal (fp
, 0);
239 _IO_JUMPS (&new_f
->fpx
.file
) = &_IO_proc_jumps
;
240 _IO_new_file_init_internal (&new_f
->fpx
.file
);
241 if (_IO_new_proc_open (fp
, command
, mode
) != NULL
)
242 return (FILE *) &new_f
->fpx
.file
;
243 _IO_un_link (&new_f
->fpx
.file
);
249 _IO_new_proc_close (FILE *fp
)
251 /* This is not name-space clean. FIXME! */
253 _IO_proc_file
**ptr
= &proc_file_chain
;
257 /* Unlink from proc_file_chain. */
259 _IO_cleanup_region_start_noarg (unlock
);
260 _IO_lock_lock (proc_file_chain_lock
);
262 for ( ; *ptr
!= NULL
; ptr
= &(*ptr
)->next
)
264 if (*ptr
== (_IO_proc_file
*) fp
)
272 _IO_lock_unlock (proc_file_chain_lock
);
273 _IO_cleanup_region_end (0);
276 if (status
< 0 || __close_nocancel (_IO_fileno(fp
)) < 0)
278 /* POSIX.2 Rationale: "Some historical implementations either block
279 or ignore the signals SIGINT, SIGQUIT, and SIGHUP while waiting
280 for the child process to terminate. Since this behavior is not
281 described in POSIX.2, such implementations are not conforming." */
284 wait_pid
= __waitpid_nocancel (((_IO_proc_file
*) fp
)->pid
, &wstatus
, 0);
286 while (wait_pid
== -1 && errno
== EINTR
);
292 static const struct _IO_jump_t _IO_proc_jumps libio_vtable
= {
294 JUMP_INIT(finish
, _IO_new_file_finish
),
295 JUMP_INIT(overflow
, _IO_new_file_overflow
),
296 JUMP_INIT(underflow
, _IO_new_file_underflow
),
297 JUMP_INIT(uflow
, _IO_default_uflow
),
298 JUMP_INIT(pbackfail
, _IO_default_pbackfail
),
299 JUMP_INIT(xsputn
, _IO_new_file_xsputn
),
300 JUMP_INIT(xsgetn
, _IO_default_xsgetn
),
301 JUMP_INIT(seekoff
, _IO_new_file_seekoff
),
302 JUMP_INIT(seekpos
, _IO_default_seekpos
),
303 JUMP_INIT(setbuf
, _IO_new_file_setbuf
),
304 JUMP_INIT(sync
, _IO_new_file_sync
),
305 JUMP_INIT(doallocate
, _IO_file_doallocate
),
306 JUMP_INIT(read
, _IO_file_read
),
307 JUMP_INIT(write
, _IO_new_file_write
),
308 JUMP_INIT(seek
, _IO_file_seek
),
309 JUMP_INIT(close
, _IO_new_proc_close
),
310 JUMP_INIT(stat
, _IO_file_stat
),
311 JUMP_INIT(showmanyc
, _IO_default_showmanyc
),
312 JUMP_INIT(imbue
, _IO_default_imbue
)
315 strong_alias (_IO_new_popen
, __new_popen
)
316 versioned_symbol (libc
, _IO_new_popen
, _IO_popen
, GLIBC_2_1
);
317 versioned_symbol (libc
, __new_popen
, popen
, GLIBC_2_1
);
318 versioned_symbol (libc
, _IO_new_proc_open
, _IO_proc_open
, GLIBC_2_1
);
319 versioned_symbol (libc
, _IO_new_proc_close
, _IO_proc_close
, GLIBC_2_1
);