Back to index

glibc  2.9
system.c
Go to the documentation of this file.
00001 /* Copyright (C) 2002, 2003, 2005 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #include <sched.h>
00020 #include <signal.h>
00021 #include <string.h>  /* For the real memset prototype.  */
00022 #include <sysdep.h>
00023 #include <unistd.h>
00024 #include <sys/wait.h>
00025 #include <bits/libc-lock.h>
00026 #include <kernel-features.h>
00027 
00028 /* We have to and actually can handle cancelable system().  The big
00029    problem: we have to kill the child process if necessary.  To do
00030    this a cleanup handler has to be registered and is has to be able
00031    to find the PID of the child.  The main problem is to reliable have
00032    the PID when needed.  It is not necessary for the parent thread to
00033    return.  It might still be in the kernel when the cancellation
00034    request comes.  Therefore we have to use the clone() calls ability
00035    to have the kernel write the PID into the user-level variable.  */
00036 #if defined __ASSUME_CLONE_THREAD_FLAGS && !defined FORK
00037 # define FORK() \
00038   INLINE_SYSCALL (clone, 3, CLONE_PARENT_SETTID | SIGCHLD, 0, &pid)
00039 #endif
00040 
00041 #ifdef _LIBC_REENTRANT
00042 static void cancel_handler (void *arg);
00043 
00044 # define CLEANUP_HANDLER \
00045   __libc_cleanup_region_start (1, cancel_handler, &pid)
00046 
00047 # define CLEANUP_RESET \
00048   __libc_cleanup_region_end (0)
00049 #endif
00050 
00051 
00052 /* Linux has waitpid(), so override the generic unix version.  */
00053 #include <sysdeps/posix/system.c>
00054 
00055 
00056 #ifdef _LIBC_REENTRANT
00057 /* The cancellation handler.  */
00058 static void
00059 cancel_handler (void *arg)
00060 {
00061   pid_t child = *(pid_t *) arg;
00062 
00063   INTERNAL_SYSCALL_DECL (err);
00064   INTERNAL_SYSCALL (kill, err, 2, child, SIGKILL);
00065 
00066   TEMP_FAILURE_RETRY (__waitpid (child, NULL, 0));
00067 
00068   DO_LOCK ();
00069 
00070   if (SUB_REF () == 0)
00071     {
00072       (void) __sigaction (SIGQUIT, &quit, (struct sigaction *) NULL);
00073       (void) __sigaction (SIGINT, &intr, (struct sigaction *) NULL);
00074     }
00075 
00076   DO_UNLOCK ();
00077 }
00078 #endif