1 /* Copyright (C) 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3 
4    The GNU C Library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Library General Public License as
6    published by the Free Software Foundation; either version 2 of the
7    License, or (at your option) any later version.
8 
9    The GNU C Library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Library General Public License for more details.
13 
14    You should have received a copy of the GNU Library General Public
15    License along with the GNU C Library; see the file COPYING.LIB.  If not,
16    see <http://www.gnu.org/licenses/>.
17 
18    Totally hacked up for uClibc by Erik Andersen <andersen@codepoet.org>
19    */
20 
21 #include <errno.h>
22 #include <signal.h>
23 #include <string.h>
24 #include <sys/syscall.h>
25 #include <bits/kernel_sigaction.h>
26 
27 #define SA_RESTORER	0x04000000
28 
29 #ifdef __NR_rt_sigaction
30 
31 # if _MIPS_SIM != _ABIO32
32 #  ifdef __NR_rt_sigreturn
33 static void restore_rt(void) __asm__ ("__restore_rt");
34 #  endif
35 #  ifdef __NR_sigreturn
36 static void restore(void) __asm__ ("__restore");
37 #  endif
38 # endif
39 
40 /* If ACT is not NULL, change the action for SIG to *ACT.
41    If OACT is not NULL, put the old action for SIG in *OACT.  */
__libc_sigaction(int sig,const struct sigaction * act,struct sigaction * oact)42 int __libc_sigaction(int sig, const struct sigaction *act, struct sigaction *oact)
43 {
44 # if _MIPS_SIM != _ABIO32
45 	struct sigaction kact;
46 	if (act) {
47 		memcpy(&kact, act, sizeof(kact));
48 		kact.sa_restorer = &restore_rt;
49 		act = &kact;
50 	}
51 # endif
52 
53 	/* NB: kernel (as of 2.6.25) will return EINVAL
54 	 * if sizeof(act->sa_mask) does not match kernel's sizeof(sigset_t) */
55 	return __syscall_rt_sigaction(sig, act, oact, sizeof(act->sa_mask));
56 }
57 
58 #else
59 
60 extern void restore(void) __asm__ ("__restore") attribute_hidden;
61 
62 /* If ACT is not NULL, change the action for SIG to *ACT.
63    If OACT is not NULL, put the old action for SIG in *OACT.  */
__libc_sigaction(int sig,const struct sigaction * act,struct sigaction * oact)64 int __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;
73 # if _MIPS_SIM == _ABIO32
74 		kact.sa_restorer = act->sa_restorer;
75 # else
76 		kact.sa_restorer = &restore_rt;
77 # endif
78 	}
79 	result = __syscall_sigaction(sig,
80 			act ? &kact : NULL,
81 			oact ? &koact : NULL);
82 	if (result < 0) {
83 		__set_errno(-result);
84 		return -1;
85 	}
86 	if (oact) {
87 		oact->sa_handler = koact.k_sa_handler;
88 		oact->sa_mask.__val[0] = koact.sa_mask;
89 		oact->sa_flags = koact.sa_flags;
90 		oact->sa_restorer = koact.sa_restorer;
91 	}
92 	return result;
93 }
94 
95 #endif
96 
97 
98 #ifndef LIBC_SIGACTION
99 # ifndef __UCLIBC_HAS_THREADS__
100 strong_alias(__libc_sigaction,sigaction)
101 libc_hidden_def(sigaction)
102 # else
103 weak_alias(__libc_sigaction,sigaction)
104 libc_hidden_weak(sigaction)
105 # endif
106 #endif
107 
108 
109 /* NOTE: Please think twice before making any changes to the bits of
110    code below.  GDB needs some intimate knowledge about it to
111    recognize them as signal trampolines, and make backtraces through
112    signal handlers work right.  Important are both the names
113    (__restore_rt) and the exact instruction sequence.
114    If you ever feel the need to make any changes, please notify the
115    appropriate GDB maintainer.  */
116 
117 #define RESTORE(name, syscall) RESTORE2(name, syscall)
118 #define RESTORE2(name, syscall) \
119 __asm__ (						\
120 	".align 4\n"					\
121 	"__" #name ":\n"				\
122 	"	li	$2, " #syscall "\n"		\
123 	"	syscall\n"				\
124 );
125 
126 /* The return code for realtime-signals.  */
127 #if _MIPS_SIM != _ABIO32
128 # ifdef __NR_rt_sigreturn
129 RESTORE(restore_rt, __NR_rt_sigreturn)
130 # endif
131 # ifdef __NR_sigreturn
132 RESTORE(restore, __NR_sigreturn)
133 # endif
134 #endif
135