Back to index

glibc  2.9
pthread_kill.c
Go to the documentation of this file.
00001 /* Copyright (C) 2002, 2003, 2004, 2006 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <errno.h>
00021 #include <signal.h>
00022 #include <pthreadP.h>
00023 #include <tls.h>
00024 #include <sysdep.h>
00025 #include <kernel-features.h>
00026 
00027 
00028 int
00029 __pthread_kill (threadid, signo)
00030      pthread_t threadid;
00031      int signo;
00032 {
00033   struct pthread *pd = (struct pthread *) threadid;
00034 
00035   /* Make sure the descriptor is valid.  */
00036   if (DEBUGGING_P && INVALID_TD_P (pd))
00037     /* Not a valid thread handle.  */
00038     return ESRCH;
00039 
00040   /* Force load of pd->tid into local variable or register.  Otherwise
00041      if a thread exits between ESRCH test and tgkill, we might return
00042      EINVAL, because pd->tid would be cleared by the kernel.  */
00043   pid_t tid = atomic_forced_read (pd->tid);
00044   if (__builtin_expect (tid <= 0, 0))
00045     /* Not a valid thread handle.  */
00046     return ESRCH;
00047 
00048   /* Disallow sending the signal we use for cancellation, timers, for
00049      for the setxid implementation.  */
00050   if (signo == SIGCANCEL || signo == SIGTIMER || signo == SIGSETXID)
00051     return EINVAL;
00052 
00053   /* We have a special syscall to do the work.  */
00054   INTERNAL_SYSCALL_DECL (err);
00055 
00056   /* One comment: The PID field in the TCB can temporarily be changed
00057      (in fork).  But this must not affect this code here.  Since this
00058      function would have to be called while the thread is executing
00059      fork, it would have to happen in a signal handler.  But this is
00060      no allowed, pthread_kill is not guaranteed to be async-safe.  */
00061   int val;
00062 #if __ASSUME_TGKILL
00063   val = INTERNAL_SYSCALL (tgkill, err, 3, THREAD_GETMEM (THREAD_SELF, pid),
00064                        tid, signo);
00065 #else
00066 # ifdef __NR_tgkill
00067   val = INTERNAL_SYSCALL (tgkill, err, 3, THREAD_GETMEM (THREAD_SELF, pid),
00068                        tid, signo);
00069   if (INTERNAL_SYSCALL_ERROR_P (val, err)
00070       && INTERNAL_SYSCALL_ERRNO (val, err) == ENOSYS)
00071 # endif
00072     val = INTERNAL_SYSCALL (tkill, err, 2, tid, signo);
00073 #endif
00074 
00075   return (INTERNAL_SYSCALL_ERROR_P (val, err)
00076          ? INTERNAL_SYSCALL_ERRNO (val, err) : 0);
00077 }
00078 strong_alias (__pthread_kill, pthread_kill)