]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/unix/sysv/linux/i386/sigaction.c
Update to LGPL v2.1.
[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 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 #ifdef __NR_rt_sigaction
50 static void restore_rt (void) asm ("__restore_rt");
51 #endif
52 static void restore (void) asm ("__restore");
53
54
55 /* If ACT is not NULL, change the action for SIG to *ACT.
56 If OACT is not NULL, put the old action for SIG in *OACT. */
57 int
58 __libc_sigaction (int sig, const struct sigaction *act, struct sigaction *oact)
59 {
60 #if __ASSUME_REALTIME_SIGNALS == 0
61 struct old_kernel_sigaction k_newact, k_oldact;
62 #endif
63 int result;
64
65 #ifdef __NR_rt_sigaction
66
67 /* First try the RT signals. */
68 # if __ASSUME_REALTIME_SIGNALS == 0
69 if (!__libc_missing_rt_sigs)
70 # endif
71 {
72 struct kernel_sigaction kact, koact;
73 # if __ASSUME_REALTIME_SIGNALS == 0
74 int saved_errno = errno;
75 # endif
76
77 if (act)
78 {
79 kact.k_sa_handler = act->sa_handler;
80 memcpy (&kact.sa_mask, &act->sa_mask, sizeof (sigset_t));
81 kact.sa_flags = act->sa_flags | SA_RESTORER;
82
83 kact.sa_restorer = ((act->sa_flags & SA_SIGINFO)
84 ? &restore_rt : &restore);
85 }
86
87 /* XXX The size argument hopefully will have to be changed to the
88 real size of the user-level sigset_t. */
89 result = INLINE_SYSCALL (rt_sigaction, 4,
90 sig, act ? __ptrvalue (&kact) : NULL,
91 oact ? __ptrvalue (&koact) : NULL, _NSIG / 8);
92
93 # if __ASSUME_REALTIME_SIGNALS == 0
94 if (result >= 0 || errno != ENOSYS)
95 # endif
96 {
97 if (oact && result >= 0)
98 {
99 oact->sa_handler = koact.k_sa_handler;
100 memcpy (&oact->sa_mask, &koact.sa_mask, sizeof (sigset_t));
101 oact->sa_flags = koact.sa_flags;
102 oact->sa_restorer = koact.sa_restorer;
103 }
104 return result;
105 }
106
107 # if __ASSUME_REALTIME_SIGNALS == 0
108 __set_errno (saved_errno);
109 __libc_missing_rt_sigs = 1;
110 # endif
111 }
112 #endif
113
114 #if __ASSUME_REALTIME_SIGNALS == 0
115 if (act)
116 {
117 k_newact.k_sa_handler = act->sa_handler;
118 k_newact.sa_mask = act->sa_mask.__val[0];
119 k_newact.sa_flags = act->sa_flags | SA_RESTORER;
120
121 k_newact.sa_restorer = &restore;
122 }
123
124 asm volatile ("pushl %%ebx\n"
125 "movl %2, %%ebx\n"
126 "int $0x80\n"
127 "popl %%ebx"
128 : "=a" (result)
129 : "0" (SYS_ify (sigaction)), "r" (sig),
130 "c" (act ? __ptrvalue (&k_newact) : 0),
131 "d" (oact ? __ptrvalue (&k_oldact) : 0));
132
133 if (result < 0)
134 {
135 __set_errno (-result);
136 return -1;
137 }
138
139 if (oact)
140 {
141 oact->sa_handler = k_oldact.k_sa_handler;
142 oact->sa_mask.__val[0] = k_oldact.sa_mask;
143 oact->sa_flags = k_oldact.sa_flags;
144 oact->sa_restorer = k_oldact.sa_restorer;
145 }
146
147 return 0;
148 #endif
149 }
150
151 weak_alias (__libc_sigaction, __sigaction)
152 weak_alias (__libc_sigaction, sigaction)
153
154 /* NOTE: Please think twice before making any changes to the bits of
155 code below. GDB needs some intimate knowledge about it to
156 recognize them as signal trampolines, and make backtraces through
157 signal handlers work right. Important are both the names
158 (__restore and __restore_rt) and the exact instruction sequence.
159 If you ever feel the need to make any changes, please notify the
160 appropriate GDB maintainer. */
161
162 #define RESTORE(name, syscall) RESTORE2 (name, syscall)
163 #define RESTORE2(name, syscall) \
164 asm \
165 ( \
166 ".align 16\n" \
167 "__" #name ":\n" \
168 " movl $" #syscall ", %eax\n" \
169 " int $0x80" \
170 );
171
172 #ifdef __NR_rt_sigaction
173 /* The return code for realtime-signals. */
174 RESTORE (restore_rt, __NR_rt_sigreturn)
175 #endif
176
177 /* For the boring old signals. */
178 # undef RESTORE2
179 # define RESTORE2(name, syscall) \
180 asm \
181 ( \
182 ".align 8\n" \
183 "__" #name ":\n" \
184 " popl %eax\n" \
185 " movl $" #syscall ", %eax\n" \
186 " int $0x80" \
187 );
188
189 RESTORE (restore, __NR_sigreturn)