Blame libio/oldiopopen.c

Packit 6c4009
/* Copyright (C) 1998-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
#define _IO_USE_OLD_IO_FILE
Packit 6c4009
#include "libioP.h"
Packit 6c4009
#include <signal.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include <sys/types.h>
Packit 6c4009
#include <sys/wait.h>
Packit 6c4009
Packit 6c4009
#include <shlib-compat.h>
Packit 6c4009
#if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_1)
Packit 6c4009
Packit 6c4009
struct _IO_proc_file
Packit 6c4009
{
Packit 6c4009
  struct _IO_FILE_complete_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 struct _IO_proc_file *old_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
attribute_compat_text_section
Packit 6c4009
_IO_old_proc_open (FILE *fp, const char *command, const char *mode)
Packit 6c4009
{
Packit 6c4009
  volatile int read_or_write;
Packit 6c4009
  volatile int parent_end, child_end;
Packit 6c4009
  int pipe_fds[2];
Packit 6c4009
  pid_t child_pid;
Packit 6c4009
  if (_IO_file_is_open (fp))
Packit 6c4009
    return NULL;
Packit 6c4009
  if (__pipe (pipe_fds) < 0)
Packit 6c4009
    return NULL;
Packit 6c4009
  if (mode[0] == 'r' && mode[1] == '\0')
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 if (mode[0] == 'w' && mode[1] == '\0')
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
  else
Packit 6c4009
    {
Packit 6c4009
      __close (pipe_fds[0]);
Packit 6c4009
      __close (pipe_fds[1]);
Packit 6c4009
      __set_errno (EINVAL);
Packit 6c4009
      return NULL;
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 = mode[0] == 'r' ? 1 : 0;
Packit 6c4009
      struct _IO_proc_file *p;
Packit 6c4009
Packit 6c4009
      __close (parent_end);
Packit 6c4009
      if (child_end != child_std_end)
Packit 6c4009
	{
Packit 6c4009
	  __dup2 (child_end, child_std_end);
Packit 6c4009
	  __close (child_end);
Packit 6c4009
	}
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 = old_proc_file_chain; p; p = p->next)
Packit 6c4009
	__close (_IO_fileno ((FILE *) p));
Packit 6c4009
Packit 6c4009
      execl ("/bin/sh", "sh", "-c", command, (char *) 0);
Packit 6c4009
      _exit (127);
Packit 6c4009
    }
Packit 6c4009
  __close (child_end);
Packit 6c4009
  if (child_pid < 0)
Packit 6c4009
    {
Packit 6c4009
      __close (parent_end);
Packit 6c4009
      return NULL;
Packit 6c4009
    }
Packit 6c4009
  _IO_fileno (fp) = parent_end;
Packit 6c4009
Packit 6c4009
  /* Link into old_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 = old_proc_file_chain;
Packit 6c4009
  old_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
attribute_compat_text_section
Packit 6c4009
_IO_old_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._file._lock = &new_f->lock;
Packit 6c4009
#endif
Packit 6c4009
  fp = &new_f->fpx.file.file._file;
Packit 6c4009
  _IO_old_init (fp, 0);
Packit 6c4009
  _IO_JUMPS_FILE_plus (&new_f->fpx.file) = &_IO_old_proc_jumps;
Packit 6c4009
  _IO_old_file_init_internal ((struct _IO_FILE_plus *) &new_f->fpx.file);
Packit 6c4009
  if (_IO_old_proc_open (fp, command, mode) != NULL)
Packit 6c4009
    return fp;
Packit 6c4009
  _IO_un_link ((struct _IO_FILE_plus *) &new_f->fpx.file);
Packit 6c4009
  free (new_f);
Packit 6c4009
  return NULL;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
attribute_compat_text_section
Packit 6c4009
_IO_old_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 = &old_proc_file_chain;
Packit 6c4009
  pid_t wait_pid;
Packit 6c4009
  int status = -1;
Packit 6c4009
Packit 6c4009
  /* Unlink from old_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 (_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 (((_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
const struct _IO_jump_t _IO_old_proc_jumps libio_vtable = {
Packit 6c4009
  JUMP_INIT_DUMMY,
Packit 6c4009
  JUMP_INIT(finish, _IO_old_file_finish),
Packit 6c4009
  JUMP_INIT(overflow, _IO_old_file_overflow),
Packit 6c4009
  JUMP_INIT(underflow, _IO_old_file_underflow),
Packit 6c4009
  JUMP_INIT(uflow, _IO_default_uflow),
Packit 6c4009
  JUMP_INIT(pbackfail, _IO_default_pbackfail),
Packit 6c4009
  JUMP_INIT(xsputn, _IO_old_file_xsputn),
Packit 6c4009
  JUMP_INIT(xsgetn, _IO_default_xsgetn),
Packit 6c4009
  JUMP_INIT(seekoff, _IO_old_file_seekoff),
Packit 6c4009
  JUMP_INIT(seekpos, _IO_default_seekpos),
Packit 6c4009
  JUMP_INIT(setbuf, _IO_old_file_setbuf),
Packit 6c4009
  JUMP_INIT(sync, _IO_old_file_sync),
Packit 6c4009
  JUMP_INIT(doallocate, _IO_file_doallocate),
Packit 6c4009
  JUMP_INIT(read, _IO_file_read),
Packit 6c4009
  JUMP_INIT(write, _IO_old_file_write),
Packit 6c4009
  JUMP_INIT(seek, _IO_file_seek),
Packit 6c4009
  JUMP_INIT(close, _IO_old_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_old_popen, __old_popen)
Packit 6c4009
compat_symbol (libc, _IO_old_popen, _IO_popen, GLIBC_2_0);
Packit 6c4009
compat_symbol (libc, __old_popen, popen, GLIBC_2_0);
Packit 6c4009
compat_symbol (libc, _IO_old_proc_open, _IO_proc_open, GLIBC_2_0);
Packit 6c4009
compat_symbol (libc, _IO_old_proc_close, _IO_proc_close, GLIBC_2_0);
Packit 6c4009
Packit 6c4009
#endif