OSDN Git Service

Replace FSF snail mail address with URLs
[uclinux-h8/uClibc.git] / libpthread / nptl / sysdeps / unix / sysv / linux / pthread_sigqueue.c
1 /* Copyright (C) 2009 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3    Contributed by Ulrich Drepper <drepper@redhat.com>, 2009.
4
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <http://www.gnu.org/licenses/>.  */
18
19 #include <errno.h>
20 #include <signal.h>
21 #include <string.h>
22 #include <unistd.h>
23 #include <pthreadP.h>
24 #include <tls.h>
25 #include <sysdep.h>
26 #include <bits/kernel-features.h>
27
28
29 int
30 pthread_sigqueue (
31      pthread_t threadid,
32      int signo,
33      const union sigval value)
34 {
35 #ifdef __NR_rt_tgsigqueueinfo
36   struct pthread *pd = (struct pthread *) threadid;
37
38   /* Make sure the descriptor is valid.  */
39   if (DEBUGGING_P && INVALID_TD_P (pd))
40     /* Not a valid thread handle.  */
41     return ESRCH;
42
43   /* Force load of pd->tid into local variable or register.  Otherwise
44      if a thread exits between ESRCH test and tgkill, we might return
45      EINVAL, because pd->tid would be cleared by the kernel.  */
46   pid_t tid = atomic_forced_read (pd->tid);
47   if (__builtin_expect (tid <= 0, 0))
48     /* Not a valid thread handle.  */
49     return ESRCH;
50
51   /* Disallow sending the signal we use for cancellation, timers, for
52      for the setxid implementation.  */
53   if (signo == SIGCANCEL || signo == SIGTIMER || signo == SIGSETXID)
54     return EINVAL;
55
56   /* Set up the siginfo_t structure.  */
57   siginfo_t info;
58   memset (&info, '\0', sizeof (siginfo_t));
59   info.si_signo = signo;
60   info.si_code = SI_QUEUE;
61   info.si_pid = THREAD_GETMEM (THREAD_SELF, pid);
62   info.si_uid = getuid ();
63   info.si_value = value;
64
65   /* We have a special syscall to do the work.  */
66   INTERNAL_SYSCALL_DECL (err);
67
68   /* One comment: The PID field in the TCB can temporarily be changed
69      (in fork).  But this must not affect this code here.  Since this
70      function would have to be called while the thread is executing
71      fork, it would have to happen in a signal handler.  But this is
72      no allowed, pthread_sigqueue is not guaranteed to be async-safe.  */
73   int val = INTERNAL_SYSCALL (rt_tgsigqueueinfo, err, 4,
74                               THREAD_GETMEM (THREAD_SELF, pid),
75                               tid, signo, &info);
76
77   return (INTERNAL_SYSCALL_ERROR_P (val, err)
78           ? INTERNAL_SYSCALL_ERRNO (val, err) : 0);
79 #else
80   return ENOSYS;
81 #endif
82 }