Blame sysdeps/posix/system.c

Packit 6c4009
/* Copyright (C) 1991-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
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
#include <errno.h>
Packit 6c4009
#include <signal.h>
Packit 6c4009
#include <stddef.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
#include <libc-lock.h>
Packit 6c4009
#include <sysdep-cancel.h>
Packit 6c4009
#include <sigsetops.h>
Packit 6c4009
Packit 6c4009
Packit 6c4009
#define	SHELL_PATH	"/bin/sh"	/* Path of the shell.  */
Packit 6c4009
#define	SHELL_NAME	"sh"		/* Name to give it.  */
Packit 6c4009
Packit 6c4009
Packit 6c4009
#ifdef _LIBC_REENTRANT
Packit 6c4009
static struct sigaction intr, quit;
Packit 6c4009
static int sa_refcntr;
Packit 6c4009
__libc_lock_define_initialized (static, lock);
Packit 6c4009
Packit 6c4009
# define DO_LOCK() __libc_lock_lock (lock)
Packit 6c4009
# define DO_UNLOCK() __libc_lock_unlock (lock)
Packit 6c4009
# define INIT_LOCK() ({ __libc_lock_init (lock); sa_refcntr = 0; })
Packit 6c4009
# define ADD_REF() sa_refcntr++
Packit 6c4009
# define SUB_REF() --sa_refcntr
Packit 6c4009
#else
Packit 6c4009
# define DO_LOCK()
Packit 6c4009
# define DO_UNLOCK()
Packit 6c4009
# define INIT_LOCK()
Packit 6c4009
# define ADD_REF() 0
Packit 6c4009
# define SUB_REF() 0
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Execute LINE as a shell command, returning its status.  */
Packit 6c4009
static int
Packit 6c4009
do_system (const char *line)
Packit 6c4009
{
Packit 6c4009
  int status, save;
Packit 6c4009
  pid_t pid;
Packit 6c4009
  struct sigaction sa;
Packit 6c4009
#ifndef _LIBC_REENTRANT
Packit 6c4009
  struct sigaction intr, quit;
Packit 6c4009
#endif
Packit 6c4009
  sigset_t omask;
Packit 6c4009
Packit 6c4009
  sa.sa_handler = SIG_IGN;
Packit 6c4009
  sa.sa_flags = 0;
Packit 6c4009
  __sigemptyset (&sa.sa_mask);
Packit 6c4009
Packit 6c4009
  DO_LOCK ();
Packit 6c4009
  if (ADD_REF () == 0)
Packit 6c4009
    {
Packit 6c4009
      if (__sigaction (SIGINT, &sa, &intr) < 0)
Packit 6c4009
	{
Packit 6c4009
	  (void) SUB_REF ();
Packit 6c4009
	  goto out;
Packit 6c4009
	}
Packit 6c4009
      if (__sigaction (SIGQUIT, &sa, &quit) < 0)
Packit 6c4009
	{
Packit 6c4009
	  save = errno;
Packit 6c4009
	  (void) SUB_REF ();
Packit 6c4009
	  goto out_restore_sigint;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
  DO_UNLOCK ();
Packit 6c4009
Packit 6c4009
  /* We reuse the bitmap in the 'sa' structure.  */
Packit 6c4009
  __sigaddset (&sa.sa_mask, SIGCHLD);
Packit 6c4009
  save = errno;
Packit 6c4009
  if (__sigprocmask (SIG_BLOCK, &sa.sa_mask, &omask) < 0)
Packit 6c4009
    {
Packit 6c4009
#ifndef _LIBC
Packit 6c4009
      if (errno == ENOSYS)
Packit 6c4009
	__set_errno (save);
Packit 6c4009
      else
Packit 6c4009
#endif
Packit 6c4009
	{
Packit 6c4009
	  DO_LOCK ();
Packit 6c4009
	  if (SUB_REF () == 0)
Packit 6c4009
	    {
Packit 6c4009
	      save = errno;
Packit 6c4009
	      (void) __sigaction (SIGQUIT, &quit, (struct sigaction *) NULL);
Packit 6c4009
	    out_restore_sigint:
Packit 6c4009
	      (void) __sigaction (SIGINT, &intr, (struct sigaction *) NULL);
Packit 6c4009
	      __set_errno (save);
Packit 6c4009
	    }
Packit 6c4009
	out:
Packit 6c4009
	  DO_UNLOCK ();
Packit 6c4009
	  return -1;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
#ifdef CLEANUP_HANDLER
Packit 6c4009
  CLEANUP_HANDLER;
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
#ifdef FORK
Packit 6c4009
  pid = FORK ();
Packit 6c4009
#else
Packit 6c4009
  pid = __fork ();
Packit 6c4009
#endif
Packit 6c4009
  if (pid == (pid_t) 0)
Packit 6c4009
    {
Packit 6c4009
      /* Child side.  */
Packit 6c4009
      const char *new_argv[4];
Packit 6c4009
      new_argv[0] = SHELL_NAME;
Packit 6c4009
      new_argv[1] = "-c";
Packit 6c4009
      new_argv[2] = line;
Packit 6c4009
      new_argv[3] = NULL;
Packit 6c4009
Packit 6c4009
      /* Restore the signals.  */
Packit 6c4009
      (void) __sigaction (SIGINT, &intr, (struct sigaction *) NULL);
Packit 6c4009
      (void) __sigaction (SIGQUIT, &quit, (struct sigaction *) NULL);
Packit 6c4009
      (void) __sigprocmask (SIG_SETMASK, &omask, (sigset_t *) NULL);
Packit 6c4009
      INIT_LOCK ();
Packit 6c4009
Packit 6c4009
      /* Exec the shell.  */
Packit 6c4009
      (void) __execve (SHELL_PATH, (char *const *) new_argv, __environ);
Packit 6c4009
      _exit (127);
Packit 6c4009
    }
Packit 6c4009
  else if (pid < (pid_t) 0)
Packit 6c4009
    /* The fork failed.  */
Packit 6c4009
    status = -1;
Packit 6c4009
  else
Packit 6c4009
    /* Parent side.  */
Packit 6c4009
    {
Packit 6c4009
      /* Note the system() is a cancellation point.  But since we call
Packit 6c4009
	 waitpid() which itself is a cancellation point we do not
Packit 6c4009
	 have to do anything here.  */
Packit 6c4009
      if (TEMP_FAILURE_RETRY (__waitpid (pid, &status, 0)) != pid)
Packit 6c4009
	status = -1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
#ifdef CLEANUP_HANDLER
Packit 6c4009
  CLEANUP_RESET;
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
  save = errno;
Packit 6c4009
  DO_LOCK ();
Packit 6c4009
  if ((SUB_REF () == 0
Packit 6c4009
       && (__sigaction (SIGINT, &intr, (struct sigaction *) NULL)
Packit 6c4009
	   | __sigaction (SIGQUIT, &quit, (struct sigaction *) NULL)) != 0)
Packit 6c4009
      || __sigprocmask (SIG_SETMASK, &omask, (sigset_t *) NULL) != 0)
Packit 6c4009
    {
Packit 6c4009
#ifndef _LIBC
Packit 6c4009
      /* glibc cannot be used on systems without waitpid.  */
Packit 6c4009
      if (errno == ENOSYS)
Packit 6c4009
	__set_errno (save);
Packit 6c4009
      else
Packit 6c4009
#endif
Packit 6c4009
	status = -1;
Packit 6c4009
    }
Packit 6c4009
  DO_UNLOCK ();
Packit 6c4009
Packit 6c4009
  return status;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
__libc_system (const char *line)
Packit 6c4009
{
Packit 6c4009
  if (line == NULL)
Packit 6c4009
    /* Check that we have a command processor available.  It might
Packit 6c4009
       not be available after a chroot(), for example.  */
Packit 6c4009
    return do_system ("exit 0") == 0;
Packit 6c4009
Packit 6c4009
  return do_system (line);
Packit 6c4009
}
Packit 6c4009
weak_alias (__libc_system, system)