Blame libio/iopopen.c

Packit 6c4009
/* Copyright (C) 1993-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Written by Per Bothner <bothner@cygnus.com>.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.
Packit 6c4009
Packit 6c4009
   As a special exception, if you link the code in this file with
Packit 6c4009
   files compiled with a GNU compiler to produce an executable,
Packit 6c4009
   that does not cause the resulting executable to be covered by
Packit 6c4009
   the GNU Lesser General Public License.  This exception does not
Packit 6c4009
   however invalidate any other reasons why the executable file
Packit 6c4009
   might be covered by the GNU Lesser General Public License.
Packit 6c4009
   This exception applies to code released by its copyright holders
Packit 6c4009
   in files containing the exception.  */
Packit 6c4009
Packit 6c4009
#include "libioP.h"
Packit 6c4009
#include <fcntl.h>
Packit 6c4009
#include <signal.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <shlib-compat.h>
Packit 6c4009
#include <not-cancel.h>
Packit 6c4009
#include <sys/types.h>
Packit 6c4009
#include <sys/wait.h>
Packit 6c4009
#include <kernel-features.h>
Packit 6c4009
Packit 6c4009
struct _IO_proc_file
Packit 6c4009
{
Packit 6c4009
  struct _IO_FILE_plus file;
Packit 6c4009
  /* Following fields must match those in class procbuf (procbuf.h) */
Packit 6c4009
  pid_t pid;
Packit 6c4009
  struct _IO_proc_file *next;
Packit 6c4009
};
Packit 6c4009
typedef struct _IO_proc_file _IO_proc_file;
Packit 6c4009
Packit 6c4009
static const struct _IO_jump_t _IO_proc_jumps;
Packit 6c4009
Packit 6c4009
static struct _IO_proc_file *proc_file_chain;
Packit 6c4009
Packit 6c4009
#ifdef _IO_MTSAFE_IO
Packit 6c4009
static _IO_lock_t proc_file_chain_lock = _IO_lock_initializer;
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
unlock (void *not_used)
Packit 6c4009
{
Packit 6c4009
  _IO_lock_unlock (proc_file_chain_lock);
Packit 6c4009
}
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
FILE *
Packit 6c4009
_IO_new_proc_open (FILE *fp, const char *command, const char *mode)
Packit 6c4009
{
Packit 6c4009
  int read_or_write;
Packit 6c4009
  int parent_end, child_end;
Packit 6c4009
  int pipe_fds[2];
Packit 6c4009
  pid_t child_pid;
Packit 6c4009
Packit 6c4009
  int do_read = 0;
Packit 6c4009
  int do_write = 0;
Packit 6c4009
  int do_cloexec = 0;
Packit 6c4009
  while (*mode != '\0')
Packit 6c4009
    switch (*mode++)
Packit 6c4009
      {
Packit 6c4009
      case 'r':
Packit 6c4009
	do_read = 1;
Packit 6c4009
	break;
Packit 6c4009
      case 'w':
Packit 6c4009
	do_write = 1;
Packit 6c4009
	break;
Packit 6c4009
      case 'e':
Packit 6c4009
	do_cloexec = 1;
Packit 6c4009
	break;
Packit 6c4009
      default:
Packit 6c4009
      errout:
Packit 6c4009
	__set_errno (EINVAL);
Packit 6c4009
	return NULL;
Packit 6c4009
      }
Packit 6c4009
Packit 6c4009
  if ((do_read ^ do_write) == 0)
Packit 6c4009
    goto errout;
Packit 6c4009
Packit 6c4009
  if (_IO_file_is_open (fp))
Packit 6c4009
    return NULL;
Packit 6c4009
Packit 6c4009
  /* Atomically set the O_CLOEXEC flag for the pipe end used by the
Packit 6c4009
     child process (to avoid leaking the file descriptor in case of a
Packit 6c4009
     concurrent fork).  This is later reverted in the child process.
Packit 6c4009
     When popen returns, the parent pipe end can be O_CLOEXEC or not,
Packit 6c4009
     depending on the 'e' open mode, but there is only one flag which
Packit 6c4009
     controls both descriptors.  The parent end is adjusted below,
Packit 6c4009
     after creating the child process.  (In the child process, the
Packit 6c4009
     parent end should be closed on execve, so O_CLOEXEC remains set
Packit 6c4009
     there.)  */
Packit 6c4009
  if (__pipe2 (pipe_fds, O_CLOEXEC) < 0)
Packit 6c4009
    return NULL;
Packit 6c4009
Packit 6c4009
  if (do_read)
Packit 6c4009
    {
Packit 6c4009
      parent_end = pipe_fds[0];
Packit 6c4009
      child_end = pipe_fds[1];
Packit 6c4009
      read_or_write = _IO_NO_WRITES;
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      parent_end = pipe_fds[1];
Packit 6c4009
      child_end = pipe_fds[0];
Packit 6c4009
      read_or_write = _IO_NO_READS;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  ((_IO_proc_file *) fp)->pid = child_pid = __fork ();
Packit 6c4009
  if (child_pid == 0)
Packit 6c4009
    {
Packit 6c4009
      int child_std_end = do_read ? 1 : 0;
Packit 6c4009
      struct _IO_proc_file *p;
Packit 6c4009
Packit 6c4009
      if (child_end != child_std_end)
Packit 6c4009
	__dup2 (child_end, child_std_end);
Packit 6c4009
      else
Packit 6c4009
	/* The descriptor is already the one we will use.  But it must
Packit 6c4009
	   not be marked close-on-exec.  Undo the effects.  */
Packit 6c4009
	__fcntl (child_end, F_SETFD, 0);
Packit 6c4009
      /* POSIX.2:  "popen() shall ensure that any streams from previous
Packit 6c4009
         popen() calls that remain open in the parent process are closed
Packit 6c4009
	 in the new child process." */
Packit 6c4009
      for (p = proc_file_chain; p; p = p->next)
Packit 6c4009
	{
Packit 6c4009
	  int fd = _IO_fileno ((FILE *) p);
Packit 6c4009
Packit 6c4009
	  /* If any stream from previous popen() calls has fileno
Packit 6c4009
	     child_std_end, it has been already closed by the dup2 syscall
Packit 6c4009
	     above.  */
Packit 6c4009
	  if (fd != child_std_end)
Packit 6c4009
	    __close_nocancel (fd);
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      execl ("/bin/sh", "sh", "-c", command, (char *) 0);
Packit 6c4009
      _exit (127);
Packit 6c4009
    }
Packit 6c4009
  __close_nocancel (child_end);
Packit 6c4009
  if (child_pid < 0)
Packit 6c4009
    {
Packit 6c4009
      __close_nocancel (parent_end);
Packit 6c4009
      return NULL;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (!do_cloexec)
Packit 6c4009
    /* Undo the effects of the pipe2 call which set the
Packit 6c4009
       close-on-exec flag.  */
Packit 6c4009
    __fcntl (parent_end, F_SETFD, 0);
Packit 6c4009
Packit 6c4009
  _IO_fileno (fp) = parent_end;
Packit 6c4009
Packit 6c4009
  /* Link into proc_file_chain. */
Packit 6c4009
#ifdef _IO_MTSAFE_IO
Packit 6c4009
  _IO_cleanup_region_start_noarg (unlock);
Packit 6c4009
  _IO_lock_lock (proc_file_chain_lock);
Packit 6c4009
#endif
Packit 6c4009
  ((_IO_proc_file *) fp)->next = proc_file_chain;
Packit 6c4009
  proc_file_chain = (_IO_proc_file *) fp;
Packit 6c4009
#ifdef _IO_MTSAFE_IO
Packit 6c4009
  _IO_lock_unlock (proc_file_chain_lock);
Packit 6c4009
  _IO_cleanup_region_end (0);
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
  _IO_mask_flags (fp, read_or_write, _IO_NO_READS|_IO_NO_WRITES);
Packit 6c4009
  return fp;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
FILE *
Packit 6c4009
_IO_new_popen (const char *command, const char *mode)
Packit 6c4009
{
Packit 6c4009
  struct locked_FILE
Packit 6c4009
  {
Packit 6c4009
    struct _IO_proc_file fpx;
Packit 6c4009
#ifdef _IO_MTSAFE_IO
Packit 6c4009
    _IO_lock_t lock;
Packit 6c4009
#endif
Packit 6c4009
  } *new_f;
Packit 6c4009
  FILE *fp;
Packit 6c4009
Packit 6c4009
  new_f = (struct locked_FILE *) malloc (sizeof (struct locked_FILE));
Packit 6c4009
  if (new_f == NULL)
Packit 6c4009
    return NULL;
Packit 6c4009
#ifdef _IO_MTSAFE_IO
Packit 6c4009
  new_f->fpx.file.file._lock = &new_f->lock;
Packit 6c4009
#endif
Packit 6c4009
  fp = &new_f->fpx.file.file;
Packit 6c4009
  _IO_init_internal (fp, 0);
Packit 6c4009
  _IO_JUMPS (&new_f->fpx.file) = &_IO_proc_jumps;
Packit 6c4009
  _IO_new_file_init_internal (&new_f->fpx.file);
Packit 6c4009
  if (_IO_new_proc_open (fp, command, mode) != NULL)
Packit 6c4009
    return (FILE *) &new_f->fpx.file;
Packit 6c4009
  _IO_un_link (&new_f->fpx.file);
Packit 6c4009
  free (new_f);
Packit 6c4009
  return NULL;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
_IO_new_proc_close (FILE *fp)
Packit 6c4009
{
Packit 6c4009
  /* This is not name-space clean. FIXME! */
Packit 6c4009
  int wstatus;
Packit 6c4009
  _IO_proc_file **ptr = &proc_file_chain;
Packit 6c4009
  pid_t wait_pid;
Packit 6c4009
  int status = -1;
Packit 6c4009
Packit 6c4009
  /* Unlink from proc_file_chain. */
Packit 6c4009
#ifdef _IO_MTSAFE_IO
Packit 6c4009
  _IO_cleanup_region_start_noarg (unlock);
Packit 6c4009
  _IO_lock_lock (proc_file_chain_lock);
Packit 6c4009
#endif
Packit 6c4009
  for ( ; *ptr != NULL; ptr = &(*ptr)->next)
Packit 6c4009
    {
Packit 6c4009
      if (*ptr == (_IO_proc_file *) fp)
Packit 6c4009
	{
Packit 6c4009
	  *ptr = (*ptr)->next;
Packit 6c4009
	  status = 0;
Packit 6c4009
	  break;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
#ifdef _IO_MTSAFE_IO
Packit 6c4009
  _IO_lock_unlock (proc_file_chain_lock);
Packit 6c4009
  _IO_cleanup_region_end (0);
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
  if (status < 0 || __close_nocancel (_IO_fileno(fp)) < 0)
Packit 6c4009
    return -1;
Packit 6c4009
  /* POSIX.2 Rationale:  "Some historical implementations either block
Packit 6c4009
     or ignore the signals SIGINT, SIGQUIT, and SIGHUP while waiting
Packit 6c4009
     for the child process to terminate.  Since this behavior is not
Packit 6c4009
     described in POSIX.2, such implementations are not conforming." */
Packit 6c4009
  do
Packit 6c4009
    {
Packit 6c4009
      wait_pid = __waitpid_nocancel (((_IO_proc_file *) fp)->pid, &wstatus, 0);
Packit 6c4009
    }
Packit 6c4009
  while (wait_pid == -1 && errno == EINTR);
Packit 6c4009
  if (wait_pid == -1)
Packit 6c4009
    return -1;
Packit 6c4009
  return wstatus;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static const struct _IO_jump_t _IO_proc_jumps libio_vtable = {
Packit 6c4009
  JUMP_INIT_DUMMY,
Packit 6c4009
  JUMP_INIT(finish, _IO_new_file_finish),
Packit 6c4009
  JUMP_INIT(overflow, _IO_new_file_overflow),
Packit 6c4009
  JUMP_INIT(underflow, _IO_new_file_underflow),
Packit 6c4009
  JUMP_INIT(uflow, _IO_default_uflow),
Packit 6c4009
  JUMP_INIT(pbackfail, _IO_default_pbackfail),
Packit 6c4009
  JUMP_INIT(xsputn, _IO_new_file_xsputn),
Packit 6c4009
  JUMP_INIT(xsgetn, _IO_default_xsgetn),
Packit 6c4009
  JUMP_INIT(seekoff, _IO_new_file_seekoff),
Packit 6c4009
  JUMP_INIT(seekpos, _IO_default_seekpos),
Packit 6c4009
  JUMP_INIT(setbuf, _IO_new_file_setbuf),
Packit 6c4009
  JUMP_INIT(sync, _IO_new_file_sync),
Packit 6c4009
  JUMP_INIT(doallocate, _IO_file_doallocate),
Packit 6c4009
  JUMP_INIT(read, _IO_file_read),
Packit 6c4009
  JUMP_INIT(write, _IO_new_file_write),
Packit 6c4009
  JUMP_INIT(seek, _IO_file_seek),
Packit 6c4009
  JUMP_INIT(close, _IO_new_proc_close),
Packit 6c4009
  JUMP_INIT(stat, _IO_file_stat),
Packit 6c4009
  JUMP_INIT(showmanyc, _IO_default_showmanyc),
Packit 6c4009
  JUMP_INIT(imbue, _IO_default_imbue)
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
strong_alias (_IO_new_popen, __new_popen)
Packit 6c4009
versioned_symbol (libc, _IO_new_popen, _IO_popen, GLIBC_2_1);
Packit 6c4009
versioned_symbol (libc, __new_popen, popen, GLIBC_2_1);
Packit 6c4009
versioned_symbol (libc, _IO_new_proc_open, _IO_proc_open, GLIBC_2_1);
Packit 6c4009
versioned_symbol (libc, _IO_new_proc_close, _IO_proc_close, GLIBC_2_1);