blob: 4810da308ce0e06b99a1d330c3dfb5c413c32432 [file] [log] [blame]
njnde62cbf2005-06-10 22:08:14 +00001
2/*--------------------------------------------------------------------*/
3/*--- Signal-related libc stuff. m_libcsignal.c ---*/
4/*--------------------------------------------------------------------*/
5
6/*
7 This file is part of Valgrind, a dynamic binary instrumentation
8 framework.
9
10 Copyright (C) 2000-2005 Julian Seward
11 jseward@acm.org
12
13 This program is free software; you can redistribute it and/or
14 modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation; either version 2 of the
16 License, or (at your option) any later version.
17
18 This program is distributed in the hope that it will be useful, but
19 WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
26 02111-1307, USA.
27
28 The GNU General Public License is contained in the file COPYING.
29*/
30
njnc7561b92005-06-19 01:24:32 +000031#include "pub_core_basics.h"
njnde62cbf2005-06-10 22:08:14 +000032#include "pub_core_libcbase.h"
33#include "pub_core_libcassert.h"
34#include "pub_core_libcsignal.h"
njn9abd6082005-06-17 21:31:45 +000035#include "pub_core_syscall.h"
njnde62cbf2005-06-10 22:08:14 +000036#include "vki_unistd.h"
37
38/* sigemptyset, sigfullset, sigaddset and sigdelset return 0 on
39 success and -1 on error. */
40
41Int VG_(sigfillset)( vki_sigset_t* set )
42{
43 Int i;
44 if (set == NULL)
45 return -1;
46 for (i = 0; i < _VKI_NSIG_WORDS; i++)
47 set->sig[i] = ~(UWord)0x0;
48 return 0;
49}
50
51Int VG_(sigemptyset)( vki_sigset_t* set )
52{
53 Int i;
54 if (set == NULL)
55 return -1;
56 for (i = 0; i < _VKI_NSIG_WORDS; i++)
57 set->sig[i] = 0x0;
58 return 0;
59}
60
61Bool VG_(isemptysigset)( const vki_sigset_t* set )
62{
63 Int i;
64 vg_assert(set != NULL);
65 for (i = 0; i < _VKI_NSIG_WORDS; i++)
66 if (set->sig[i] != 0x0) return False;
67 return True;
68}
69
70Bool VG_(isfullsigset)( const vki_sigset_t* set )
71{
72 Int i;
73 vg_assert(set != NULL);
74 for (i = 0; i < _VKI_NSIG_WORDS; i++)
75 if (set->sig[i] != ~(UWord)0x0) return False;
76 return True;
77}
78
79Bool VG_(iseqsigset)( const vki_sigset_t* set1, const vki_sigset_t* set2 )
80{
81 Int i;
82 vg_assert(set1 != NULL && set2 != NULL);
83 for (i = 0; i < _VKI_NSIG_WORDS; i++)
84 if (set1->sig[i] != set2->sig[i]) return False;
85 return True;
86}
87
88
89Int VG_(sigaddset)( vki_sigset_t* set, Int signum )
90{
91 if (set == NULL)
92 return -1;
93 if (signum < 1 || signum > _VKI_NSIG)
94 return -1;
95 signum--;
96 set->sig[signum / _VKI_NSIG_BPW] |= (1UL << (signum % _VKI_NSIG_BPW));
97 return 0;
98}
99
100Int VG_(sigdelset)( vki_sigset_t* set, Int signum )
101{
102 if (set == NULL)
103 return -1;
104 if (signum < 1 || signum > _VKI_NSIG)
105 return -1;
106 signum--;
107 set->sig[signum / _VKI_NSIG_BPW] &= ~(1UL << (signum % _VKI_NSIG_BPW));
108 return 0;
109}
110
111Int VG_(sigismember) ( const vki_sigset_t* set, Int signum )
112{
113 if (set == NULL)
114 return 0;
115 if (signum < 1 || signum > _VKI_NSIG)
116 return 0;
117 signum--;
118 if (1 & ((set->sig[signum / _VKI_NSIG_BPW]) >> (signum % _VKI_NSIG_BPW)))
119 return 1;
120 else
121 return 0;
122}
123
124
125/* Add all signals in src to dst. */
126void VG_(sigaddset_from_set)( vki_sigset_t* dst, vki_sigset_t* src )
127{
128 Int i;
129 vg_assert(dst != NULL && src != NULL);
130 for (i = 0; i < _VKI_NSIG_WORDS; i++)
131 dst->sig[i] |= src->sig[i];
132}
133
134/* Remove all signals in src from dst. */
135void VG_(sigdelset_from_set)( vki_sigset_t* dst, vki_sigset_t* src )
136{
137 Int i;
138 vg_assert(dst != NULL && src != NULL);
139 for (i = 0; i < _VKI_NSIG_WORDS; i++)
140 dst->sig[i] &= ~(src->sig[i]);
141}
142
143
144/* The functions sigaction, sigprocmask, sigpending and sigsuspend
145 return 0 on success and -1 on error.
146*/
147Int VG_(sigprocmask)( Int how, const vki_sigset_t* set, vki_sigset_t* oldset)
148{
149 SysRes res = VG_(do_syscall4)(__NR_rt_sigprocmask,
150 how, (UWord)set, (UWord)oldset,
151 _VKI_NSIG_WORDS * sizeof(UWord));
152 return res.isError ? -1 : 0;
153}
154
155
156Int VG_(sigaction) ( Int signum, const struct vki_sigaction* act,
157 struct vki_sigaction* oldact)
158{
159 SysRes res = VG_(do_syscall4)(__NR_rt_sigaction,
160 signum, (UWord)act, (UWord)oldact,
161 _VKI_NSIG_WORDS * sizeof(UWord));
162 return res.isError ? -1 : 0;
163}
164
165
166Int VG_(sigaltstack)( const vki_stack_t* ss, vki_stack_t* oss )
167{
168 SysRes res = VG_(do_syscall2)(__NR_sigaltstack, (UWord)ss, (UWord)oss);
169 return res.isError ? -1 : 0;
170}
171
172Int VG_(sigtimedwait)( const vki_sigset_t *set, vki_siginfo_t *info,
173 const struct vki_timespec *timeout )
174{
175 SysRes res = VG_(do_syscall4)(__NR_rt_sigtimedwait, (UWord)set, (UWord)info,
176 (UWord)timeout, sizeof(*set));
177 return res.isError ? -1 : res.val;
178}
179
180Int VG_(signal)(Int signum, void (*sighandler)(Int))
181{
182 SysRes res;
183 Int n;
184 struct vki_sigaction sa;
185 sa.ksa_handler = sighandler;
186 sa.sa_flags = VKI_SA_ONSTACK | VKI_SA_RESTART;
187 sa.sa_restorer = NULL;
188 n = VG_(sigemptyset)( &sa.sa_mask );
189 vg_assert(n == 0);
190 res = VG_(do_syscall4)(__NR_rt_sigaction, signum, (UWord)&sa, (UWord)NULL,
191 _VKI_NSIG_WORDS * sizeof(UWord));
192 return res.isError ? -1 : 0;
193}
194
195
196Int VG_(kill)( Int pid, Int signo )
197{
198 SysRes res = VG_(do_syscall2)(__NR_kill, pid, signo);
199 return res.isError ? -1 : 0;
200}
201
202
203Int VG_(tkill)( ThreadId tid, Int signo )
204{
205 SysRes res = VG_(mk_SysRes_Error)(VKI_ENOSYS);
206
207#if 0
208 /* This isn't right because the client may create a process
209 structure with multiple thread groups */
njn94c6ace2005-06-11 17:19:18 +0000210 res = VG_(do_syscall3)(__NR_tgkill, VG_(getpid)(), tid, signo);
njnde62cbf2005-06-10 22:08:14 +0000211#endif
212
213 res = VG_(do_syscall2)(__NR_tkill, tid, signo);
214
215 if (res.isError && res.val == VKI_ENOSYS)
216 res = VG_(do_syscall2)(__NR_kill, tid, signo);
217
218 return res.isError ? -1 : 0;
219}
220
221Int VG_(sigpending) ( vki_sigset_t* set )
222{
223// Nb: AMD64/Linux doesn't have __NR_sigpending; it only provides
224// __NR_rt_sigpending. This function will have to be abstracted in some
225// way to account for this. In the meantime, the easy option is to forget
226// about it for AMD64 until it's needed.
njnf8c78a52005-06-18 15:09:28 +0000227#if defined(VGA_amd64)
njnde62cbf2005-06-10 22:08:14 +0000228 I_die_here;
229#else
230 SysRes res = VG_(do_syscall1)(__NR_sigpending, (UWord)set);
231 return res.isError ? -1 : 0;
232#endif
233}
234
235/*--------------------------------------------------------------------*/
236/*--- end ---*/
237/*--------------------------------------------------------------------*/