Update.
[glibc.git] / sysdeps / unix / sysv / linux / i386 / sigaction.c
blob773101fe6cd3fa0b7385d405ddebcebe7a8a4048
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 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.
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.
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. */
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 *__unbounded,
41 struct kernel_sigaction *__unbounded, 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,
90 sig, act ? __ptrvalue (&kact) : NULL,
91 oact ? __ptrvalue (&koact) : NULL, _NSIG / 8);
93 # if __ASSUME_REALTIME_SIGNALS == 0
94 if (result >= 0 || errno != ENOSYS)
95 # endif
97 if (oact && result >= 0)
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;
104 return result;
107 # if __ASSUME_REALTIME_SIGNALS == 0
108 __set_errno (saved_errno);
109 __libc_missing_rt_sigs = 1;
110 # endif
112 #endif
114 #if __ASSUME_REALTIME_SIGNALS == 0
115 if (act)
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;
121 k_newact.sa_restorer = &restore;
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));
133 if (result < 0)
135 __set_errno (-result);
136 return -1;
139 if (oact)
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;
147 return 0;
148 #endif
151 weak_alias (__libc_sigaction, __sigaction)
152 weak_alias (__libc_sigaction, sigaction)
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. */
162 #define RESTORE(name, syscall) RESTORE2 (name, syscall)
163 #define RESTORE2(name, syscall) \
164 asm \
166 ".text\n" \
167 " .align 16\n" \
168 "__" #name ":\n" \
169 " movl $" #syscall ", %eax\n" \
170 " int $0x80" \
173 #ifdef __NR_rt_sigaction
174 /* The return code for realtime-signals. */
175 RESTORE (restore_rt, __NR_rt_sigreturn)
176 #endif
178 /* For the boring old signals. */
179 # undef RESTORE2
180 # define RESTORE2(name, syscall) \
181 asm \
183 ".text\n" \
184 " .align 8\n" \
185 "__" #name ":\n" \
186 " popl %eax\n" \
187 " movl $" #syscall ", %eax\n" \
188 " int $0x80" \
191 RESTORE (restore, __NR_sigreturn)