iopopen.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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>.
  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.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>.
  15. As a special exception, if you link the code in this file with
  16. files compiled with a GNU compiler to produce an executable,
  17. that does not cause the resulting executable to be covered by
  18. the GNU Lesser General Public License. This exception does not
  19. however invalidate any other reasons why the executable file
  20. might be covered by the GNU Lesser General Public License.
  21. This exception applies to code released by its copyright holders
  22. in files containing the exception. */
  23. #include "libioP.h"
  24. #include <fcntl.h>
  25. #include <signal.h>
  26. #include <unistd.h>
  27. #include <stdlib.h>
  28. #include <shlib-compat.h>
  29. #include <not-cancel.h>
  30. #include <sys/types.h>
  31. #include <sys/wait.h>
  32. #include <spawn.h>
  33. #include <paths.h>
  34. struct _IO_proc_file
  35. {
  36. struct _IO_FILE_plus file;
  37. /* Following fields must match those in class procbuf (procbuf.h) */
  38. pid_t pid;
  39. struct _IO_proc_file *next;
  40. };
  41. typedef struct _IO_proc_file _IO_proc_file;
  42. static const struct _IO_jump_t _IO_proc_jumps;
  43. static struct _IO_proc_file *proc_file_chain;
  44. #ifdef _IO_MTSAFE_IO
  45. static _IO_lock_t proc_file_chain_lock = _IO_lock_initializer;
  46. static void
  47. unlock (void *not_used)
  48. {
  49. _IO_lock_unlock (proc_file_chain_lock);
  50. }
  51. #endif
  52. /* POSIX states popen shall ensure that any streams from previous popen()
  53. calls that remain open in the parent process should be closed in the new
  54. child process.
  55. To avoid a race-condition between checking which file descriptors need to
  56. be close (by transversing the proc_file_chain list) and the insertion of a
  57. new one after a successful posix_spawn this function should be called
  58. with proc_file_chain_lock acquired. */
  59. static bool
  60. spawn_process (posix_spawn_file_actions_t *fa, FILE *fp, const char *command,
  61. int do_cloexec, int pipe_fds[2], int parent_end, int child_end,
  62. int child_pipe_fd)
  63. {
  64. for (struct _IO_proc_file *p = proc_file_chain; p; p = p->next)
  65. {
  66. int fd = _IO_fileno ((FILE *) p);
  67. /* If any stream from previous popen() calls has fileno
  68. child_pipe_fd, it has been already closed by the adddup2 action
  69. above. */
  70. if (fd != child_pipe_fd
  71. && __posix_spawn_file_actions_addclose (fa, fd) != 0)
  72. return false;
  73. }
  74. if (__posix_spawn (&((_IO_proc_file *) fp)->pid, _PATH_BSHELL, fa, 0,
  75. (char *const[]){ (char*) "sh", (char*) "-c",
  76. (char *) command, NULL }, __environ) != 0)
  77. return false;
  78. __close_nocancel (pipe_fds[child_end]);
  79. if (!do_cloexec)
  80. /* Undo the effects of the pipe2 call which set the
  81. close-on-exec flag. */
  82. __fcntl (pipe_fds[parent_end], F_SETFD, 0);
  83. _IO_fileno (fp) = pipe_fds[parent_end];
  84. ((_IO_proc_file *) fp)->next = proc_file_chain;
  85. proc_file_chain = (_IO_proc_file *) fp;
  86. return true;
  87. }
  88. FILE *
  89. _IO_new_proc_open (FILE *fp, const char *command, const char *mode)
  90. {
  91. int read_or_write;
  92. /* These are indexes for pipe_fds. */
  93. int parent_end, child_end;
  94. int pipe_fds[2];
  95. int child_pipe_fd;
  96. bool spawn_ok;
  97. int do_read = 0;
  98. int do_write = 0;
  99. int do_cloexec = 0;
  100. while (*mode != '\0')
  101. switch (*mode++)
  102. {
  103. case 'r':
  104. do_read = 1;
  105. break;
  106. case 'w':
  107. do_write = 1;
  108. break;
  109. case 'e':
  110. do_cloexec = 1;
  111. break;
  112. default:
  113. errout:
  114. __set_errno (EINVAL);
  115. return NULL;
  116. }
  117. if ((do_read ^ do_write) == 0)
  118. goto errout;
  119. if (_IO_file_is_open (fp))
  120. return NULL;
  121. /* Atomically set the O_CLOEXEC flag for the pipe end used by the
  122. child process (to avoid leaking the file descriptor in case of a
  123. concurrent fork). This is later reverted in the child process.
  124. When popen returns, the parent pipe end can be O_CLOEXEC or not,
  125. depending on the 'e' open mode, but there is only one flag which
  126. controls both descriptors. The parent end is adjusted below,
  127. after creating the child process. (In the child process, the
  128. parent end should be closed on execve, so O_CLOEXEC remains set
  129. there.) */
  130. if (__pipe2 (pipe_fds, O_CLOEXEC) < 0)
  131. return NULL;
  132. if (do_read)
  133. {
  134. parent_end = 0;
  135. child_end = 1;
  136. read_or_write = _IO_NO_WRITES;
  137. child_pipe_fd = 1;
  138. }
  139. else
  140. {
  141. parent_end = 1;
  142. child_end = 0;
  143. read_or_write = _IO_NO_READS;
  144. child_pipe_fd = 0;
  145. }
  146. posix_spawn_file_actions_t fa;
  147. /* posix_spawn_file_actions_init does not fail. */
  148. __posix_spawn_file_actions_init (&fa);
  149. /* The descriptor is already the one the child will use. In this case
  150. it must be moved to another one otherwise, there is no safe way to
  151. remove the close-on-exec flag in the child without creating a FD leak
  152. race in the parent. */
  153. if (pipe_fds[child_end] == child_pipe_fd)
  154. {
  155. int tmp = __fcntl (child_pipe_fd, F_DUPFD_CLOEXEC, 0);
  156. if (tmp < 0)
  157. goto spawn_failure;
  158. __close_nocancel (pipe_fds[child_end]);
  159. pipe_fds[child_end] = tmp;
  160. }
  161. if (__posix_spawn_file_actions_adddup2 (&fa, pipe_fds[child_end],
  162. child_pipe_fd) != 0)
  163. goto spawn_failure;
  164. #ifdef _IO_MTSAFE_IO
  165. _IO_cleanup_region_start_noarg (unlock);
  166. _IO_lock_lock (proc_file_chain_lock);
  167. #endif
  168. spawn_ok = spawn_process (&fa, fp, command, do_cloexec, pipe_fds,
  169. parent_end, child_end, child_pipe_fd);
  170. #ifdef _IO_MTSAFE_IO
  171. _IO_lock_unlock (proc_file_chain_lock);
  172. _IO_cleanup_region_end (0);
  173. #endif
  174. __posix_spawn_file_actions_destroy (&fa);
  175. if (!spawn_ok)
  176. {
  177. spawn_failure:
  178. __close_nocancel (pipe_fds[child_end]);
  179. __close_nocancel (pipe_fds[parent_end]);
  180. __set_errno (ENOMEM);
  181. return NULL;
  182. }
  183. _IO_mask_flags (fp, read_or_write, _IO_NO_READS|_IO_NO_WRITES);
  184. return fp;
  185. }
  186. FILE *
  187. _IO_new_popen (const char *command, const char *mode)
  188. {
  189. struct locked_FILE
  190. {
  191. struct _IO_proc_file fpx;
  192. #ifdef _IO_MTSAFE_IO
  193. _IO_lock_t lock;
  194. #endif
  195. } *new_f;
  196. FILE *fp;
  197. new_f = (struct locked_FILE *) malloc (sizeof (struct locked_FILE));
  198. if (new_f == NULL)
  199. return NULL;
  200. #ifdef _IO_MTSAFE_IO
  201. new_f->fpx.file.file._lock = &new_f->lock;
  202. #endif
  203. fp = &new_f->fpx.file.file;
  204. _IO_init_internal (fp, 0);
  205. _IO_JUMPS (&new_f->fpx.file) = &_IO_proc_jumps;
  206. _IO_new_file_init_internal (&new_f->fpx.file);
  207. if (_IO_new_proc_open (fp, command, mode) != NULL)
  208. return (FILE *) &new_f->fpx.file;
  209. _IO_un_link (&new_f->fpx.file);
  210. free (new_f);
  211. return NULL;
  212. }
  213. int
  214. _IO_new_proc_close (FILE *fp)
  215. {
  216. /* This is not name-space clean. FIXME! */
  217. int wstatus;
  218. _IO_proc_file **ptr = &proc_file_chain;
  219. pid_t wait_pid;
  220. int status = -1;
  221. /* Unlink from proc_file_chain. */
  222. #ifdef _IO_MTSAFE_IO
  223. _IO_cleanup_region_start_noarg (unlock);
  224. _IO_lock_lock (proc_file_chain_lock);
  225. #endif
  226. for ( ; *ptr != NULL; ptr = &(*ptr)->next)
  227. {
  228. if (*ptr == (_IO_proc_file *) fp)
  229. {
  230. *ptr = (*ptr)->next;
  231. status = 0;
  232. break;
  233. }
  234. }
  235. #ifdef _IO_MTSAFE_IO
  236. _IO_lock_unlock (proc_file_chain_lock);
  237. _IO_cleanup_region_end (0);
  238. #endif
  239. if (status < 0 || __close_nocancel (_IO_fileno(fp)) < 0)
  240. return -1;
  241. /* POSIX.2 Rationale: "Some historical implementations either block
  242. or ignore the signals SIGINT, SIGQUIT, and SIGHUP while waiting
  243. for the child process to terminate. Since this behavior is not
  244. described in POSIX.2, such implementations are not conforming." */
  245. do
  246. {
  247. wait_pid = __waitpid_nocancel (((_IO_proc_file *) fp)->pid, &wstatus, 0);
  248. }
  249. while (wait_pid == -1 && errno == EINTR);
  250. if (wait_pid == -1)
  251. return -1;
  252. return wstatus;
  253. }
  254. static const struct _IO_jump_t _IO_proc_jumps libio_vtable = {
  255. JUMP_INIT_DUMMY,
  256. JUMP_INIT(finish, _IO_new_file_finish),
  257. JUMP_INIT(overflow, _IO_new_file_overflow),
  258. JUMP_INIT(underflow, _IO_new_file_underflow),
  259. JUMP_INIT(uflow, _IO_default_uflow),
  260. JUMP_INIT(pbackfail, _IO_default_pbackfail),
  261. JUMP_INIT(xsputn, _IO_new_file_xsputn),
  262. JUMP_INIT(xsgetn, _IO_default_xsgetn),
  263. JUMP_INIT(seekoff, _IO_new_file_seekoff),
  264. JUMP_INIT(seekpos, _IO_default_seekpos),
  265. JUMP_INIT(setbuf, _IO_new_file_setbuf),
  266. JUMP_INIT(sync, _IO_new_file_sync),
  267. JUMP_INIT(doallocate, _IO_file_doallocate),
  268. JUMP_INIT(read, _IO_file_read),
  269. JUMP_INIT(write, _IO_new_file_write),
  270. JUMP_INIT(seek, _IO_file_seek),
  271. JUMP_INIT(close, _IO_new_proc_close),
  272. JUMP_INIT(stat, _IO_file_stat),
  273. JUMP_INIT(showmanyc, _IO_default_showmanyc),
  274. JUMP_INIT(imbue, _IO_default_imbue)
  275. };
  276. strong_alias (_IO_new_popen, __new_popen)
  277. versioned_symbol (libc, _IO_new_popen, _IO_popen, GLIBC_2_1);
  278. versioned_symbol (libc, __new_popen, popen, GLIBC_2_1);
  279. versioned_symbol (libc, _IO_new_proc_open, _IO_proc_open, GLIBC_2_1);
  280. versioned_symbol (libc, _IO_new_proc_close, _IO_proc_close, GLIBC_2_1);