2.5-18.1
[jlayton/glibc.git] / sysdeps / unix / sysv / linux / x86_64 / sigaction.c
1 /* POSIX.1 `sigaction' call for Linux/x86-64.
2    Copyright (C) 2001, 2002, 2003, 2005 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
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, write to the Free
17    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18    02111-1307 USA.  */
19
20 #include <sysdep.h>
21 #include <errno.h>
22 #include <stddef.h>
23 #include <signal.h>
24 #include <string.h>
25
26 #include <sysdep.h>
27 #include <sys/syscall.h>
28
29 #include <kernel-features.h>
30
31 /* The difference here is that the sigaction structure used in the
32    kernel is not the same as we use in the libc.  Therefore we must
33    translate it here.  */
34 #include <kernel_sigaction.h>
35
36 /* We do not globally define the SA_RESTORER flag so do it here.  */
37 #define SA_RESTORER 0x04000000
38
39 /* Using the hidden attribute here does not change the code but it
40    helps to avoid warnings.  */
41 #if defined HAVE_HIDDEN && defined HAVE_VISIBILITY_ATTRIBUTE \
42     && !defined HAVE_BROKEN_VISIBILITY_ATTRIBUTE
43 extern void restore_rt (void) asm ("__restore_rt") attribute_hidden;
44 #else
45 static void restore_rt (void) asm ("__restore_rt");
46 #endif
47
48
49 /* If ACT is not NULL, change the action for SIG to *ACT.
50    If OACT is not NULL, put the old action for SIG in *OACT.  */
51 int
52 __libc_sigaction (int sig, const struct sigaction *act, struct sigaction *oact)
53 {
54   int result;
55   struct kernel_sigaction kact, koact;
56
57   if (act)
58     {
59       kact.k_sa_handler = act->sa_handler;
60       memcpy (&kact.sa_mask, &act->sa_mask, sizeof (sigset_t));
61       kact.sa_flags = act->sa_flags | SA_RESTORER;
62
63       kact.sa_restorer = &restore_rt;
64     }
65
66   /* XXX The size argument hopefully will have to be changed to the
67      real size of the user-level sigset_t.  */
68   result = INLINE_SYSCALL (rt_sigaction, 4,
69                            sig, act ? __ptrvalue (&kact) : NULL,
70                            oact ? __ptrvalue (&koact) : NULL, _NSIG / 8);
71   if (oact && result >= 0)
72     {
73       oact->sa_handler = koact.k_sa_handler;
74       memcpy (&oact->sa_mask, &koact.sa_mask, sizeof (sigset_t));
75       oact->sa_flags = koact.sa_flags;
76       oact->sa_restorer = koact.sa_restorer;
77     }
78   return result;
79 }
80 libc_hidden_def (__libc_sigaction)
81
82 #ifdef WRAPPER_INCLUDE
83 # include WRAPPER_INCLUDE
84 #endif
85
86 #ifndef LIBC_SIGACTION
87 weak_alias (__libc_sigaction, __sigaction)
88 libc_hidden_weak (__sigaction)
89 weak_alias (__libc_sigaction, sigaction)
90 #endif
91
92 /* NOTE: Please think twice before making any changes to the bits of
93    code below.  GDB needs some intimate knowledge about it to
94    recognize them as signal trampolines, and make backtraces through
95    signal handlers work right.  Important are both the names
96    (__restore_rt) and the exact instruction sequence.
97    If you ever feel the need to make any changes, please notify the
98    appropriate GDB maintainer.  */
99
100 #define RESTORE(name, syscall) RESTORE2 (name, syscall)
101 # define RESTORE2(name, syscall) \
102 asm                                             \
103   (                                             \
104    ".align 16\n"                                \
105    CFI_STARTPROC "\n"                           \
106    "__" #name ":\n"                             \
107    "    movq $" #syscall ", %rax\n"             \
108    "    syscall\n"                              \
109    CFI_ENDPROC "\n"                             \
110    );
111 /* The return code for realtime-signals.  */
112 RESTORE (restore_rt, __NR_rt_sigreturn)