]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/uclibc/lib/contrib/uclibc/libc/sysdeps/linux/x86_64/sigaction.c
update
[l4.git] / l4 / pkg / uclibc / lib / contrib / uclibc / 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, see
17    <http://www.gnu.org/licenses/>.  */
18
19
20 #include <errno.h>
21 #include <stddef.h>
22 #include <signal.h>
23 #include <string.h>
24
25 #include <sys/syscall.h>
26
27 #include <bits/kernel_sigaction.h>
28
29 /* We do not globally define the SA_RESTORER flag so do it here.  */
30 #define SA_RESTORER 0x04000000
31
32 #ifdef __NR_rt_sigaction
33
34 /* Using the hidden attribute here does not change the code but it
35    helps to avoid warnings.  */
36 extern void restore_rt(void) __asm__ ("__restore_rt") attribute_hidden;
37 extern void restore(void) __asm__ ("__restore") attribute_hidden;
38
39 /* If ACT is not NULL, change the action for SIG to *ACT.
40    If OACT is not NULL, put the old action for SIG in *OACT.  */
41 int
42 __libc_sigaction(int sig, const struct sigaction *act, struct sigaction *oact)
43 {
44         struct sigaction kact;
45
46         if (act) {
47                 memcpy(&kact, act, sizeof(kact));
48                 kact.sa_flags |= SA_RESTORER;
49                 kact.sa_restorer = &restore_rt;
50                 act = &kact;
51         }
52         /* NB: kernel (as of 2.6.25) will return EINVAL
53          * if sizeof(act->sa_mask) does not match kernel's sizeof(sigset_t) */
54         return INLINE_SYSCALL(rt_sigaction, 4, sig, act, oact, sizeof(act->sa_mask));
55 }
56
57 #else
58
59 extern void restore(void) __asm__ ("__restore") attribute_hidden;
60
61 /* If ACT is not NULL, change the action for SIG to *ACT.
62    If OACT is not NULL, put the old action for SIG in *OACT.  */
63 int
64 __libc_sigaction(int sig, const struct sigaction *act, struct sigaction *oact)
65 {
66         int result;
67         struct old_kernel_sigaction kact, koact;
68
69         if (act) {
70                 kact.k_sa_handler = act->sa_handler;
71                 kact.sa_mask = act->sa_mask.__val[0];
72                 kact.sa_flags = act->sa_flags | SA_RESTORER;
73                 kact.sa_restorer = &restore;
74         }
75         __asm__ __volatile__ (
76                 "syscall\n"
77                 : "=a" (result)
78                 : "0" (__NR_sigaction), "mr" (sig),
79                   "c" (act ? &kact : NULL),
80                   "d" (oact ? &koact : NULL));
81         if (result < 0) {
82                 __set_errno(-result);
83                 return -1;
84         }
85         if (oact) {
86                 oact->sa_handler = koact.k_sa_handler;
87                 oact->sa_mask.__val[0] = koact.sa_mask;
88                 oact->sa_flags = koact.sa_flags;
89                 oact->sa_restorer = koact.sa_restorer;
90         }
91         return result;
92 }
93
94 #endif
95
96
97 #ifndef LIBC_SIGACTION
98 # ifndef __UCLIBC_HAS_THREADS__
99 strong_alias(__libc_sigaction,sigaction)
100 libc_hidden_def(sigaction)
101 # else
102 weak_alias(__libc_sigaction,sigaction)
103 libc_hidden_weak(sigaction)
104 # endif
105 #endif
106
107
108 /* NOTE: Please think twice before making any changes to the bits of
109    code below.  GDB needs some intimate knowledge about it to
110    recognize them as signal trampolines, and make backtraces through
111    signal handlers work right.  Important are both the names
112    (__restore_rt) and the exact instruction sequence.
113    If you ever feel the need to make any changes, please notify the
114    appropriate GDB maintainer.
115
116    The unwind information starts a byte before __restore_rt, so that
117    it is found when unwinding, to get an address the unwinder assumes
118    will be in the middle of a call instruction.  See the Linux kernel
119    (the i386 vsyscall, in particular) for an explanation of the complex
120    unwind information used here in order to get the traditional CFA.
121  */
122
123 #define RESTORE(name, syscall) RESTORE2(name, syscall)
124 #define RESTORE2(name, syscall) \
125 __asm__ (                                               \
126         "nop\n"                                         \
127         ".text\n"                                       \
128         "__" #name ":\n"                                \
129         "       movq    $" #syscall ", %rax\n"          \
130         "       syscall\n"                              \
131 );
132
133 #ifdef __NR_rt_sigaction
134 /* The return code for realtime-signals.  */
135 RESTORE(restore_rt, __NR_rt_sigreturn)
136 #endif
137 #ifdef __NR_sigreturn
138 RESTORE(restore, __NR_sigreturn)
139 #endif