]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/unix/sysv/linux/i386/sigaction.c
Update.
[thirdparty/glibc.git] / sysdeps / unix / sysv / linux / i386 / sigaction.c
1 /* POSIX.1 `sigaction' call for Linux/i386.
2 Copyright (C) 1991,95,96,97,98,99,2000,02 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
40 extern int __syscall_rt_sigaction (int, const struct kernel_sigaction *__unbounded,
41 struct kernel_sigaction *__unbounded, size_t);
42
43 #if __ASSUME_REALTIME_SIGNALS == 0
44 /* The variable is shared between all wrappers around signal handling
45 functions which have RT equivalents. */
46 int __libc_missing_rt_sigs;
47 #endif
48
49 /* Using the hidden attribute here does not change the code but it
50 helps to avoid warnings. */
51 #if defined HAVE_HIDDEN && defined HAVE_VISIBILITY_ATTRIBUTE \
52 && !defined HAVE_BROKEN_VISIBILITY_ATTRIBUTE
53 # ifdef __NR_rt_sigaction
54 extern void restore_rt (void) asm ("__restore_rt") attribute_hidden;
55 # endif
56 extern void restore (void) asm ("__restore") attribute_hidden;
57 #else
58 # ifdef __NR_rt_sigaction
59 static void restore_rt (void) asm ("__restore_rt");
60 # endif
61 static void restore (void) asm ("__restore");
62 #endif
63
64
65 /* If ACT is not NULL, change the action for SIG to *ACT.
66 If OACT is not NULL, put the old action for SIG in *OACT. */
67 int
68 __libc_sigaction (int sig, const struct sigaction *act, struct sigaction *oact)
69 {
70 #if __ASSUME_REALTIME_SIGNALS == 0
71 struct old_kernel_sigaction k_newact, k_oldact;
72 #endif
73 int result;
74
75 #ifdef __NR_rt_sigaction
76
77 /* First try the RT signals. */
78 # if __ASSUME_REALTIME_SIGNALS == 0
79 if (!__libc_missing_rt_sigs)
80 # endif
81 {
82 struct kernel_sigaction kact, koact;
83 # if __ASSUME_REALTIME_SIGNALS == 0
84 int saved_errno = errno;
85 # endif
86
87 if (act)
88 {
89 kact.k_sa_handler = act->sa_handler;
90 memcpy (&kact.sa_mask, &act->sa_mask, sizeof (sigset_t));
91 kact.sa_flags = act->sa_flags | SA_RESTORER;
92
93 kact.sa_restorer = ((act->sa_flags & SA_SIGINFO)
94 ? &restore_rt : &restore);
95 }
96
97 /* XXX The size argument hopefully will have to be changed to the
98 real size of the user-level sigset_t. */
99 result = INLINE_SYSCALL (rt_sigaction, 4,
100 sig, act ? __ptrvalue (&kact) : NULL,
101 oact ? __ptrvalue (&koact) : NULL, _NSIG / 8);
102
103 # if __ASSUME_REALTIME_SIGNALS == 0
104 if (result >= 0 || errno != ENOSYS)
105 # endif
106 {
107 if (oact && result >= 0)
108 {
109 oact->sa_handler = koact.k_sa_handler;
110 memcpy (&oact->sa_mask, &koact.sa_mask, sizeof (sigset_t));
111 oact->sa_flags = koact.sa_flags;
112 oact->sa_restorer = koact.sa_restorer;
113 }
114 return result;
115 }
116
117 # if __ASSUME_REALTIME_SIGNALS == 0
118 __set_errno (saved_errno);
119 __libc_missing_rt_sigs = 1;
120 # endif
121 }
122 #endif
123
124 #if __ASSUME_REALTIME_SIGNALS == 0
125 if (act)
126 {
127 k_newact.k_sa_handler = act->sa_handler;
128 k_newact.sa_mask = act->sa_mask.__val[0];
129 k_newact.sa_flags = act->sa_flags | SA_RESTORER;
130
131 k_newact.sa_restorer = &restore;
132 }
133
134 result = INTERNAL_SYCALL (sigaction, 3, sig,
135 act ? __ptrvalue (&k_newact) : 0,
136 oact ? __ptrvalue (&k_oldact) : 0);
137
138 if (result < 0)
139 {
140 __set_errno (-result);
141 return -1;
142 }
143
144 if (oact)
145 {
146 oact->sa_handler = k_oldact.k_sa_handler;
147 oact->sa_mask.__val[0] = k_oldact.sa_mask;
148 oact->sa_flags = k_oldact.sa_flags;
149 oact->sa_restorer = k_oldact.sa_restorer;
150 }
151
152 return 0;
153 #endif
154 }
155 libc_hidden_def (__libc_sigaction)
156
157 #ifndef SIGCANCEL
158 weak_alias (__libc_sigaction, __sigaction)
159 libc_hidden_weak (__sigaction)
160 weak_alias (__libc_sigaction, sigaction)
161 #else
162 int
163 __sigaction (sig, act, oact)
164 int sig;
165 const struct sigaction *act;
166 struct sigaction *oact;
167 {
168 if (sig == SIGCANCEL)
169 {
170 __set_errno (EINVAL);
171 return -1;
172 }
173
174 return __libc_sigaction (sig, act, oact);
175 }
176 libc_hidden_weak (__sigaction)
177 weak_alias (__sigaction, sigaction)
178 #endif
179
180 /* NOTE: Please think twice before making any changes to the bits of
181 code below. GDB needs some intimate knowledge about it to
182 recognize them as signal trampolines, and make backtraces through
183 signal handlers work right. Important are both the names
184 (__restore and __restore_rt) and the exact instruction sequence.
185 If you ever feel the need to make any changes, please notify the
186 appropriate GDB maintainer. */
187
188 #define RESTORE(name, syscall) RESTORE2 (name, syscall)
189 #define RESTORE2(name, syscall) \
190 asm \
191 ( \
192 ".text\n" \
193 " .align 16\n" \
194 "__" #name ":\n" \
195 " movl $" #syscall ", %eax\n" \
196 " int $0x80" \
197 );
198
199 #ifdef __NR_rt_sigaction
200 /* The return code for realtime-signals. */
201 RESTORE (restore_rt, __NR_rt_sigreturn)
202 #endif
203
204 /* For the boring old signals. */
205 # undef RESTORE2
206 # define RESTORE2(name, syscall) \
207 asm \
208 ( \
209 ".text\n" \
210 " .align 8\n" \
211 "__" #name ":\n" \
212 " popl %eax\n" \
213 " movl $" #syscall ", %eax\n" \
214 " int $0x80" \
215 );
216
217 RESTORE (restore, __NR_sigreturn)