OSDN Git Service

32b13c06497122ef0b0b13e709a0305d9111d5d9
[uclinux-h8/uClibc.git] / libc / sysdeps / linux / x86_64 / sigaction.c
1 /* POSIX.1 `sigaction' call for Linux/x86-64.
2    Copyright (C) 2001, 2002, 2003 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
21 #include <errno.h>
22 #include <stddef.h>
23 #include <signal.h>
24 #include <string.h>
25
26
27 #include <sys/syscall.h>
28
29
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 <bits/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 #if defined __NR_rt_sigaction
40 /* Using the hidden attribute here does not change the code but it
41    helps to avoid warnings.  */
42 extern void restore_rt (void) asm ("__restore_rt") attribute_hidden;
43 extern void restore (void) asm ("__restore") attribute_hidden;
44
45 /* If ACT is not NULL, change the action for SIG to *ACT.
46    If OACT is not NULL, put the old action for SIG in *OACT.  */
47 /* psm: couldn't use __sigaction, if building w/ disabled hidden,
48  *      it will conflict w/ the one in libpthread */
49 int attribute_hidden
50 __sigaction_internal (int sig, const struct sigaction *act, struct sigaction *oact)
51 {
52         int result;
53         struct kernel_sigaction kact, koact;
54
55         if (act) {
56                 kact.k_sa_handler = act->sa_handler;
57                 __memcpy (&kact.sa_mask, &act->sa_mask, sizeof (sigset_t));
58                 kact.sa_flags = act->sa_flags | SA_RESTORER;
59
60                 kact.sa_restorer = &restore_rt;
61         }
62
63         /* XXX The size argument hopefully will have to be changed to the
64            real size of the user-level sigset_t.  */
65         result = INLINE_SYSCALL (rt_sigaction, 4,
66                                  sig, act ? __ptrvalue (&kact) : NULL,
67                                  oact ? __ptrvalue (&koact) : NULL, _NSIG / 8);
68         if (oact && result >= 0) {
69                 oact->sa_handler = koact.k_sa_handler;
70                 __memcpy (&oact->sa_mask, &koact.sa_mask, sizeof (sigset_t));
71                 oact->sa_flags = koact.sa_flags;
72                 oact->sa_restorer = koact.sa_restorer;
73         }
74         return result;
75 }
76 #else
77
78 extern void restore (void) asm ("__restore") attribute_hidden;
79
80 /* If ACT is not NULL, change the action for SIG to *ACT.
81    If OACT is not NULL, put the old action for SIG in *OACT.  */
82 int attribute_hidden
83 __sigaction_internal (int sig, const struct sigaction *act, struct sigaction *oact)
84 {
85         int result;
86         struct old_kernel_sigaction kact, koact;
87
88 #ifdef SIGCANCEL
89         if (sig == SIGCANCEL) {
90                 __set_errno (EINVAL);
91                 return -1;
92         }
93 #endif
94
95         if (act) {
96                 kact.k_sa_handler = act->sa_handler;
97                 kact.sa_mask = act->sa_mask.__val[0];
98                 kact.sa_flags = act->sa_flags | SA_RESTORER;
99                 kact.sa_restorer = &restore;
100         }
101
102         asm volatile ("syscall\n"
103                       : "=a" (result)
104                       : "0" (__NR_sigaction), "mr" (sig),
105                         "c" (act ? __ptrvalue (&kact) : 0),
106                         "d" (oact ? __ptrvalue (&koact) : 0));
107
108         if (result < 0) {
109                 __set_errno(-result);
110                 return -1;
111         }
112
113         if (oact) {
114                 oact->sa_handler = koact.k_sa_handler;
115                 oact->sa_mask.__val[0] = koact.sa_mask;
116                 oact->sa_flags = koact.sa_flags;
117                 oact->sa_restorer = koact.sa_restorer;
118         }
119         return result;
120 }
121 #endif
122 strong_alias(__sigaction_internal,__libc_sigaction)
123 weak_alias(__sigaction_internal,sigaction)
124
125 /* NOTE: Please think twice before making any changes to the bits of
126    code below.  GDB needs some intimate knowledge about it to
127    recognize them as signal trampolines, and make backtraces through
128    signal handlers work right.  Important are both the names
129    (__restore_rt) and the exact instruction sequence.
130    If you ever feel the need to make any changes, please notify the
131    appropriate GDB maintainer.  */
132
133 #define RESTORE(name, syscall) RESTORE2 (name, syscall)
134 # define RESTORE2(name, syscall) \
135 asm                                             \
136   (                                             \
137    ".text\n" \
138    ".align 16\n"                                \
139    "__" #name ":\n"                             \
140    "    movq $" #syscall ", %rax\n"             \
141    "    syscall\n"                              \
142    );
143 #ifdef __NR_rt_sigaction
144 /* The return code for realtime-signals.  */
145 RESTORE (restore_rt, __NR_rt_sigreturn)
146 #endif
147 #ifdef __NR_sigreturn
148 RESTORE (restore, __NR_sigreturn)
149 #endif