Blame sysdeps/unix/sysv/linux/system.c

Packit 6c4009
/* Copyright (C) 2002-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 <sched.h>
Packit 6c4009
#include <signal.h>
Packit 6c4009
#include <string.h>	/* For the real memset prototype.  */
Packit 6c4009
#include <sysdep.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include <sys/wait.h>
Packit 6c4009
#include <libc-lock.h>
Packit 6c4009
Packit 6c4009
/* We have to and actually can handle cancelable system().  The big
Packit 6c4009
   problem: we have to kill the child process if necessary.  To do
Packit 6c4009
   this a cleanup handler has to be registered and is has to be able
Packit 6c4009
   to find the PID of the child.  The main problem is to reliable have
Packit 6c4009
   the PID when needed.  It is not necessary for the parent thread to
Packit 6c4009
   return.  It might still be in the kernel when the cancellation
Packit 6c4009
   request comes.  Therefore we have to use the clone() calls ability
Packit 6c4009
   to have the kernel write the PID into the user-level variable.  */
Packit 6c4009
#ifndef FORK
Packit 6c4009
# define FORK() \
Packit 6c4009
  INLINE_SYSCALL (clone, 3, CLONE_PARENT_SETTID | SIGCHLD, 0, &pid)
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
#ifdef _LIBC_REENTRANT
Packit 6c4009
static void cancel_handler (void *arg);
Packit 6c4009
Packit 6c4009
# define CLEANUP_HANDLER \
Packit 6c4009
  __libc_cleanup_region_start (1, cancel_handler, &pid)
Packit 6c4009
Packit 6c4009
# define CLEANUP_RESET \
Packit 6c4009
  __libc_cleanup_region_end (0)
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Linux has waitpid(), so override the generic unix version.  */
Packit 6c4009
#include <sysdeps/posix/system.c>
Packit 6c4009
Packit 6c4009
Packit 6c4009
#ifdef _LIBC_REENTRANT
Packit 6c4009
/* The cancellation handler.  */
Packit 6c4009
static void
Packit 6c4009
cancel_handler (void *arg)
Packit 6c4009
{
Packit 6c4009
  pid_t child = *(pid_t *) arg;
Packit 6c4009
Packit 6c4009
  INTERNAL_SYSCALL_DECL (err);
Packit 6c4009
  INTERNAL_SYSCALL (kill, err, 2, child, SIGKILL);
Packit 6c4009
Packit 6c4009
  TEMP_FAILURE_RETRY (__waitpid (child, NULL, 0));
Packit 6c4009
Packit 6c4009
  DO_LOCK ();
Packit 6c4009
Packit 6c4009
  if (SUB_REF () == 0)
Packit 6c4009
    {
Packit 6c4009
      (void) __sigaction (SIGQUIT, &quit, (struct sigaction *) NULL);
Packit 6c4009
      (void) __sigaction (SIGINT, &intr, (struct sigaction *) NULL);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  DO_UNLOCK ();
Packit 6c4009
}
Packit 6c4009
#endif