OSDN Git Service

sigaction overhaul as described in docs/sigaction.txt
[uclinux-h8/uClibc.git] / 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, write to the Free
17    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18    02111-1307 USA.  */
19
20
21 #include <errno.h>
22 #include <stddef.h>
23 #include <signal.h>
24 #include <string.h>
25
26 #include <sys/syscall.h>
27
28 #include <bits/kernel_sigaction.h>
29
30 /* We do not globally define the SA_RESTORER flag so do it here.  */
31 #define SA_RESTORER 0x04000000
32
33 extern __typeof(sigaction) __libc_sigaction;
34
35
36 #ifdef __NR_rt_sigaction
37
38 /* Using the hidden attribute here does not change the code but it
39    helps to avoid warnings.  */
40 extern void restore_rt(void) __asm__ ("__restore_rt") attribute_hidden;
41 extern void restore(void) __asm__ ("__restore") attribute_hidden;
42
43 /* If ACT is not NULL, change the action for SIG to *ACT.
44    If OACT is not NULL, put the old action for SIG in *OACT.  */
45 int
46 __libc_sigaction(int sig, const struct sigaction *act, struct sigaction *oact)
47 {
48         struct sigaction kact;
49
50         if (act) {
51                 memcpy(&kact, act, sizeof(kact));
52                 kact.sa_flags |= SA_RESTORER;
53                 kact.sa_restorer = &restore_rt;
54                 act = &kact;
55         }
56         /* NB: kernel (as of 2.6.25) will return EINVAL
57          * if sizeof(act->sa_mask) does not match kernel's sizeof(sigset_t) */
58         return INLINE_SYSCALL(rt_sigaction, 4, sig, act, oact, sizeof(act->sa_mask));
59 }
60
61 #else
62
63 extern void restore(void) __asm__ ("__restore") attribute_hidden;
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         int result;
71         struct old_kernel_sigaction kact, koact;
72
73 #ifdef SIGCANCEL
74         if (sig == SIGCANCEL) {
75                 __set_errno(EINVAL);
76                 return -1;
77         }
78 #endif
79
80         if (act) {
81                 kact.k_sa_handler = act->sa_handler;
82                 kact.sa_mask = act->sa_mask.__val[0];
83                 kact.sa_flags = act->sa_flags | SA_RESTORER;
84                 kact.sa_restorer = &restore;
85         }
86         __asm__ __volatile__ (
87                 "syscall\n"
88                 : "=a" (result)
89                 : "0" (__NR_sigaction), "mr" (sig),
90                   "c" (act ? &kact : NULL),
91                   "d" (oact ? &koact : NULL));
92         if (result < 0) {
93                 __set_errno(-result);
94                 return -1;
95         }
96         if (oact) {
97                 oact->sa_handler = koact.k_sa_handler;
98                 oact->sa_mask.__val[0] = koact.sa_mask;
99                 oact->sa_flags = koact.sa_flags;
100                 oact->sa_restorer = koact.sa_restorer;
101         }
102         return result;
103 }
104
105 #endif
106
107
108 #ifndef LIBC_SIGACTION
109 weak_alias(__libc_sigaction,sigaction)
110 libc_hidden_weak(sigaction)
111 #endif
112
113
114 /* NOTE: Please think twice before making any changes to the bits of
115    code below.  GDB needs some intimate knowledge about it to
116    recognize them as signal trampolines, and make backtraces through
117    signal handlers work right.  Important are both the names
118    (__restore_rt) and the exact instruction sequence.
119    If you ever feel the need to make any changes, please notify the
120    appropriate GDB maintainer.  */
121
122 #define RESTORE(name, syscall) RESTORE2(name, syscall)
123 #define RESTORE2(name, syscall) \
124 __asm__ (                                               \
125         ".text\n"                                       \
126         "__" #name ":\n"                                \
127         "       movq    $" #syscall ", %rax\n"          \
128         "       syscall\n"                              \
129 );
130
131 #ifdef __NR_rt_sigaction
132 /* The return code for realtime-signals.  */
133 RESTORE(restore_rt, __NR_rt_sigreturn)
134 #endif
135 #ifdef __NR_sigreturn
136 RESTORE(restore, __NR_sigreturn)
137 #endif