1 /* Copyright (C) 1993-2024 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library 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 GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>.
18 As a special exception, if you link the code in this file with
19 files compiled with a GNU compiler to produce an executable,
20 that does not cause the resulting executable to be covered by
21 the GNU Lesser General Public License. This exception does not
22 however invalidate any other reasons why the executable file
23 might be covered by the GNU Lesser General Public License.
24 This exception applies to code released by its copyright holders
25 in files containing the exception. */
32 #include <shlib-compat.h>
33 #include <not-cancel.h>
34 #include <sys/types.h>
41 struct _IO_FILE_plus file
;
42 /* Following fields must match those in class procbuf (procbuf.h) */
44 struct _IO_proc_file
*next
;
46 typedef struct _IO_proc_file _IO_proc_file
;
48 static struct _IO_proc_file
*proc_file_chain
;
51 static _IO_lock_t proc_file_chain_lock
= _IO_lock_initializer
;
54 unlock (void *not_used
)
56 _IO_lock_unlock (proc_file_chain_lock
);
60 /* POSIX states popen shall ensure that any streams from previous popen()
61 calls that remain open in the parent process should be closed in the new
63 To avoid a race-condition between checking which file descriptors need to
64 be close (by transversing the proc_file_chain list) and the insertion of a
65 new one after a successful posix_spawn this function should be called
66 with proc_file_chain_lock acquired. */
68 spawn_process (posix_spawn_file_actions_t
*fa
, FILE *fp
, const char *command
,
69 int do_cloexec
, int pipe_fds
[2], int parent_end
, int child_end
,
74 for (struct _IO_proc_file
*p
= proc_file_chain
; p
; p
= p
->next
)
76 int fd
= _IO_fileno ((FILE *) p
);
78 /* If any stream from previous popen() calls has fileno
79 child_pipe_fd, it has been already closed by the adddup2 action
81 if (fd
!= child_pipe_fd
)
83 err
= __posix_spawn_file_actions_addclose (fa
, fd
);
89 err
= __posix_spawn (&((_IO_proc_file
*) fp
)->pid
, _PATH_BSHELL
, fa
, 0,
90 (char *const[]){ (char*) "sh", (char*) "-c", (char*) "--",
91 (char *) command
, NULL
}, __environ
);
95 __close_nocancel (pipe_fds
[child_end
]);
98 /* Undo the effects of the pipe2 call which set the
99 close-on-exec flag. */
100 __fcntl (pipe_fds
[parent_end
], F_SETFD
, 0);
102 _IO_fileno (fp
) = pipe_fds
[parent_end
];
104 ((_IO_proc_file
*) fp
)->next
= proc_file_chain
;
105 proc_file_chain
= (_IO_proc_file
*) fp
;
111 _IO_new_proc_open (FILE *fp
, const char *command
, const char *mode
)
114 /* These are indexes for pipe_fds. */
115 int parent_end
, child_end
;
123 while (*mode
!= '\0')
137 __set_errno (EINVAL
);
141 if ((do_read
^ do_write
) == 0)
144 if (_IO_file_is_open (fp
))
147 /* Atomically set the O_CLOEXEC flag for the pipe end used by the
148 child process (to avoid leaking the file descriptor in case of a
149 concurrent fork). This is later reverted in the child process.
150 When popen returns, the parent pipe end can be O_CLOEXEC or not,
151 depending on the 'e' open mode, but there is only one flag which
152 controls both descriptors. The parent end is adjusted below,
153 after creating the child process. (In the child process, the
154 parent end should be closed on execve, so O_CLOEXEC remains set
156 if (__pipe2 (pipe_fds
, O_CLOEXEC
) < 0)
163 read_or_write
= _IO_NO_WRITES
;
170 read_or_write
= _IO_NO_READS
;
174 posix_spawn_file_actions_t fa
;
175 /* posix_spawn_file_actions_init does not fail. */
176 __posix_spawn_file_actions_init (&fa
);
178 /* The descriptor is already the one the child will use. In this case
179 it must be moved to another one otherwise, there is no safe way to
180 remove the close-on-exec flag in the child without creating a FD leak
181 race in the parent. */
182 if (pipe_fds
[child_end
] == child_pipe_fd
)
184 int tmp
= __fcntl (child_pipe_fd
, F_DUPFD_CLOEXEC
, 0);
187 __close_nocancel (pipe_fds
[child_end
]);
188 pipe_fds
[child_end
] = tmp
;
191 err
= __posix_spawn_file_actions_adddup2 (&fa
, pipe_fds
[child_end
],
197 _IO_cleanup_region_start_noarg (unlock
);
198 _IO_lock_lock (proc_file_chain_lock
);
200 err
= spawn_process (&fa
, fp
, command
, do_cloexec
, pipe_fds
, parent_end
,
201 child_end
, child_pipe_fd
);
203 _IO_lock_unlock (proc_file_chain_lock
);
204 _IO_cleanup_region_end (0);
207 __posix_spawn_file_actions_destroy (&fa
);
213 __close_nocancel (pipe_fds
[child_end
]);
214 __close_nocancel (pipe_fds
[parent_end
]);
218 _IO_mask_flags (fp
, read_or_write
, _IO_NO_READS
|_IO_NO_WRITES
);
223 _IO_new_popen (const char *command
, const char *mode
)
227 struct _IO_proc_file fpx
;
234 new_f
= (struct locked_FILE
*) malloc (sizeof (struct locked_FILE
));
238 new_f
->fpx
.file
.file
._lock
= &new_f
->lock
;
240 fp
= &new_f
->fpx
.file
.file
;
241 _IO_init_internal (fp
, 0);
242 _IO_JUMPS (&new_f
->fpx
.file
) = &_IO_proc_jumps
;
243 _IO_new_file_init_internal (&new_f
->fpx
.file
);
244 if (_IO_new_proc_open (fp
, command
, mode
) != NULL
)
245 return (FILE *) &new_f
->fpx
.file
;
246 _IO_un_link (&new_f
->fpx
.file
);
252 _IO_new_proc_close (FILE *fp
)
254 /* This is not name-space clean. FIXME! */
256 _IO_proc_file
**ptr
= &proc_file_chain
;
260 /* Unlink from proc_file_chain. */
262 _IO_cleanup_region_start_noarg (unlock
);
263 _IO_lock_lock (proc_file_chain_lock
);
265 for ( ; *ptr
!= NULL
; ptr
= &(*ptr
)->next
)
267 if (*ptr
== (_IO_proc_file
*) fp
)
275 _IO_lock_unlock (proc_file_chain_lock
);
276 _IO_cleanup_region_end (0);
279 if (status
< 0 || __close_nocancel (_IO_fileno(fp
)) < 0)
281 /* POSIX.2 Rationale: "Some historical implementations either block
282 or ignore the signals SIGINT, SIGQUIT, and SIGHUP while waiting
283 for the child process to terminate. Since this behavior is not
284 described in POSIX.2, such implementations are not conforming." */
288 __pthread_setcancelstate (PTHREAD_CANCEL_DISABLE
, &state
);
289 wait_pid
= __waitpid (((_IO_proc_file
*) fp
)->pid
, &wstatus
, 0);
290 __pthread_setcancelstate (state
, NULL
);
292 while (wait_pid
== -1 && errno
== EINTR
);
298 strong_alias (_IO_new_popen
, __new_popen
)
299 versioned_symbol (libc
, _IO_new_popen
, _IO_popen
, GLIBC_2_1
);
300 versioned_symbol (libc
, __new_popen
, popen
, GLIBC_2_1
);
301 versioned_symbol (libc
, _IO_new_proc_open
, _IO_proc_open
, GLIBC_2_1
);
302 versioned_symbol (libc
, _IO_new_proc_close
, _IO_proc_close
, GLIBC_2_1
);