Update to LGPL v2.1.
[jlayton/glibc.git] / sysdeps / unix / sysv / linux / sparc / sparc32 / sigaction.c
1 /* POSIX.1 sigaction call for Linux/SPARC.
2    Copyright (C) 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4    Contributed by Miguel de Icaza (miguel@nuclecu.unam.mx), 1997.
5
6    The GNU C Library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 2.1 of the License, or (at your option) any later version.
10
11    The GNU C Library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
15
16    You should have received a copy of the GNU Lesser General Public
17    License along with the GNU C Library; if not, write to the Free
18    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19    02111-1307 USA.  */
20
21 #include <string.h>
22 #include <syscall.h>
23 #include <sys/signal.h>
24 #include <errno.h>
25 #include <kernel_sigaction.h>
26
27 extern int __syscall_rt_sigaction (int, const struct kernel_sigaction *,
28                                    struct kernel_sigaction *, unsigned long,
29                                    size_t);
30
31 static void __rt_sigreturn_stub (void);
32 static void __sigreturn_stub (void);
33
34 /* The variable is shared between all wrappers around signal handling
35    functions which have RT equivalents.  */
36 int __libc_missing_rt_sigs;
37
38 int
39 __libc_sigaction (int sig, __const struct sigaction *act,
40                   struct sigaction *oact)
41 {
42   struct old_kernel_sigaction k_sigact, k_osigact;
43   int ret;
44
45 #ifdef __NR_rt_sigaction
46   /* First try the RT signals.  */
47   if (!__libc_missing_rt_sigs)
48     {
49       struct kernel_sigaction kact, koact;
50       unsigned long stub = 0;
51       int saved_errno = errno;
52
53       if (act)
54         {
55           kact.k_sa_handler = act->sa_handler;
56           memcpy (&kact.sa_mask, &act->sa_mask, sizeof (sigset_t));
57           if (((kact.sa_flags = act->sa_flags) & SA_SIGINFO) != 0)
58             stub = (unsigned long) &__rt_sigreturn_stub;
59           else
60             stub = (unsigned long) &__sigreturn_stub;
61           stub -= 8;
62           kact.sa_restorer = NULL;
63         }
64
65       /* XXX The size argument hopefully will have to be changed to the
66          real size of the user-level sigset_t.  */
67       ret = __syscall_rt_sigaction (sig, act ? &kact : 0,
68                                     oact ? &koact : 0,
69                                     stub, _NSIG / 8);
70
71       if (ret >= 0 || errno != ENOSYS)
72         {
73           if (oact && ret >= 0)
74             {
75               oact->sa_handler = koact.k_sa_handler;
76               memcpy (&oact->sa_mask, &koact.sa_mask, sizeof (sigset_t));
77               oact->sa_flags = koact.sa_flags;
78               oact->sa_restorer = koact.sa_restorer;
79             }
80           return ret;
81         }
82
83       __set_errno (saved_errno);
84       __libc_missing_rt_sigs = 1;
85     }
86 #endif
87
88   /* Magic to tell the kernel we are using "new-style" signals, in that
89      the signal table is not kept in userspace.  Not the same as the
90      really-new-style rt signals.  */
91   sig = -sig;
92
93   if (act)
94     {
95       k_sigact.k_sa_handler = act->sa_handler;
96       k_sigact.sa_mask = act->sa_mask.__val[0];
97       k_sigact.sa_flags = act->sa_flags;
98       k_sigact.sa_restorer = NULL;
99     }
100
101   {
102     register int r_syscallnr __asm__("%g1") = __NR_sigaction;
103     register int r_sig __asm__("%o0") = sig;
104     register struct old_kernel_sigaction *r_act __asm__("%o1");
105     register struct old_kernel_sigaction *r_oact __asm__("%o2");
106
107     r_act = act ? &k_sigact : NULL;
108     r_oact = oact ? &k_osigact : NULL;
109
110     __asm__ __volatile__("t 0x10\n\t"
111                          "bcc 1f\n\t"
112                          " nop\n\t"
113                          "sub %%g0,%%o0,%%o0\n"
114                          "1:"
115                          : "=r"(r_sig)
116                          : "r"(r_syscallnr), "r"(r_act), "r"(r_oact),
117                            "0"(r_sig));
118
119     ret = r_sig;
120   }
121
122   if (ret >= 0)
123     {
124       if (oact)
125         {
126           oact->sa_handler = k_osigact.k_sa_handler;
127           oact->sa_mask.__val[0] = k_osigact.sa_mask;
128           oact->sa_flags = k_osigact.sa_flags;
129           oact->sa_restorer = NULL;
130         }
131       return ret;
132     }
133
134   __set_errno (-ret);
135   return -1;
136 }
137
138 weak_alias (__libc_sigaction, __sigaction);
139 weak_alias (__libc_sigaction, sigaction);
140
141 static void
142 __rt_sigreturn_stub (void)
143 {
144   __asm__ ("mov %0, %%g1\n\t"
145            "ta  0x10\n\t"
146            : /* no outputs */
147            : "i" (__NR_rt_sigreturn));
148 }
149
150 static void
151 __sigreturn_stub (void)
152 {
153   __asm__ ("mov %0, %%g1\n\t"
154            "ta  0x10\n\t"
155            : /* no outputs */
156            : "i" (__NR_sigreturn));
157 }