Update.
[glibc.git] / sysdeps / unix / sysv / linux / i386 / sigaction.c
blobc7493de6bba233df9fddc713a7665a57741c93e9
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.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include <sysdep.h>
21 #include <errno.h>
22 #include <stddef.h>
23 #include <signal.h>
24 #include <string.h>
26 #include <sysdep.h>
27 #include <sys/syscall.h>
29 #include <kernel-features.h>
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>
36 /* We do not globally define the SA_RESTORER flag so do it here. */
37 #define SA_RESTORER 0x04000000
40 extern int __syscall_rt_sigaction (int, const struct kernel_sigaction *,
41 struct kernel_sigaction *, size_t);
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
49 #ifdef __NR_rt_sigaction
50 static void restore_rt (void) asm ("__restore_rt");
51 #endif
52 static void restore (void) asm ("__restore");
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)
60 #if __ASSUME_REALTIME_SIGNALS == 0
61 struct old_kernel_sigaction k_newact, k_oldact;
62 #endif
63 int result;
65 #ifdef __NR_rt_sigaction
67 /* First try the RT signals. */
68 # if __ASSUME_REALTIME_SIGNALS == 0
69 if (!__libc_missing_rt_sigs)
70 # endif
72 struct kernel_sigaction kact, koact;
73 # if __ASSUME_REALTIME_SIGNALS == 0
74 int saved_errno = errno;
75 # endif
77 if (act)
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;
83 kact.sa_restorer = ((act->sa_flags & SA_SIGINFO)
84 ? &restore_rt : &restore);
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, sig, act ? &kact : NULL,
90 oact ? &koact : NULL, _NSIG / 8);
92 # if __ASSUME_REALTIME_SIGNALS == 0
93 if (result >= 0 || errno != ENOSYS)
94 # endif
96 if (oact && result >= 0)
98 oact->sa_handler = koact.k_sa_handler;
99 memcpy (&oact->sa_mask, &koact.sa_mask, sizeof (sigset_t));
100 oact->sa_flags = koact.sa_flags;
101 oact->sa_restorer = koact.sa_restorer;
103 return result;
106 # if __ASSUME_REALTIME_SIGNALS == 0
107 __set_errno (saved_errno);
108 __libc_missing_rt_sigs = 1;
109 # endif
111 #endif
113 #if __ASSUME_REALTIME_SIGNALS == 0
114 if (act)
116 k_newact.k_sa_handler = act->sa_handler;
117 k_newact.sa_mask = act->sa_mask.__val[0];
118 k_newact.sa_flags = act->sa_flags | SA_RESTORER;
120 k_newact.sa_restorer = &restore;
123 asm volatile ("pushl %%ebx\n"
124 "movl %2, %%ebx\n"
125 "int $0x80\n"
126 "popl %%ebx"
127 : "=a" (result)
128 : "0" (SYS_ify (sigaction)), "r" (sig),
129 "c" (act ? &k_newact : 0), "d" (oact ? &k_oldact : 0));
131 if (result < 0)
133 __set_errno (-result);
134 return -1;
137 if (oact)
139 oact->sa_handler = k_oldact.k_sa_handler;
140 oact->sa_mask.__val[0] = k_oldact.sa_mask;
141 oact->sa_flags = k_oldact.sa_flags;
142 oact->sa_restorer = k_oldact.sa_restorer;
145 return 0;
146 #endif
149 strong_alias (__libc_sigaction, __sigaction)
150 weak_alias (__libc_sigaction, sigaction)
152 /* NOTE: Please think twice before making any changes to the bits of
153 code below. GDB needs some intimate knowledge about it to
154 recognize them as signal trampolines, and make backtraces through
155 signal handlers work right. Important are both the names
156 (__restore and __restore_rt) and the exact instruction sequence.
157 If you ever feel the need to make any changes, please notify the
158 appropriate GDB maintainer. */
160 #define RESTORE(name, syscall) RESTORE2 (name, syscall)
161 #define RESTORE2(name, syscall) \
162 asm \
164 ".align 16\n" \
165 "__" #name ":\n" \
166 " movl $" #syscall ", %eax\n" \
167 " int $0x80" \
170 #ifdef __NR_rt_sigaction
171 /* The return code for realtime-signals. */
172 RESTORE (restore_rt, __NR_rt_sigreturn)
173 #endif
175 /* For the boring old signals. */
176 # undef RESTORE2
177 # define RESTORE2(name, syscall) \
178 asm \
180 ".align 8\n" \
181 "__" #name ":\n" \
182 " popl %eax\n" \
183 " movl $" #syscall ", %eax\n" \
184 " int $0x80" \
187 RESTORE (restore, __NR_sigreturn)