blob: 3e1beccf97a6bfcf0ef2278c51912e54fde2c84b [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
31#include "core.h"
32#include "pub_core_libcbase.h"
33#include "pub_core_libcassert.h"
34#include "pub_core_libcsignal.h"
35#include "vki_unistd.h"
36
37/* sigemptyset, sigfullset, sigaddset and sigdelset return 0 on
38 success and -1 on error. */
39
40Int VG_(sigfillset)( vki_sigset_t* set )
41{
42 Int i;
43 if (set == NULL)
44 return -1;
45 for (i = 0; i < _VKI_NSIG_WORDS; i++)
46 set->sig[i] = ~(UWord)0x0;
47 return 0;
48}
49
50Int VG_(sigemptyset)( vki_sigset_t* set )
51{
52 Int i;
53 if (set == NULL)
54 return -1;
55 for (i = 0; i < _VKI_NSIG_WORDS; i++)
56 set->sig[i] = 0x0;
57 return 0;
58}
59
60Bool VG_(isemptysigset)( const vki_sigset_t* set )
61{
62 Int i;
63 vg_assert(set != NULL);
64 for (i = 0; i < _VKI_NSIG_WORDS; i++)
65 if (set->sig[i] != 0x0) return False;
66 return True;
67}
68
69Bool VG_(isfullsigset)( const vki_sigset_t* set )
70{
71 Int i;
72 vg_assert(set != NULL);
73 for (i = 0; i < _VKI_NSIG_WORDS; i++)
74 if (set->sig[i] != ~(UWord)0x0) return False;
75 return True;
76}
77
78Bool VG_(iseqsigset)( const vki_sigset_t* set1, const vki_sigset_t* set2 )
79{
80 Int i;
81 vg_assert(set1 != NULL && set2 != NULL);
82 for (i = 0; i < _VKI_NSIG_WORDS; i++)
83 if (set1->sig[i] != set2->sig[i]) return False;
84 return True;
85}
86
87
88Int VG_(sigaddset)( vki_sigset_t* set, Int signum )
89{
90 if (set == NULL)
91 return -1;
92 if (signum < 1 || signum > _VKI_NSIG)
93 return -1;
94 signum--;
95 set->sig[signum / _VKI_NSIG_BPW] |= (1UL << (signum % _VKI_NSIG_BPW));
96 return 0;
97}
98
99Int VG_(sigdelset)( vki_sigset_t* set, Int signum )
100{
101 if (set == NULL)
102 return -1;
103 if (signum < 1 || signum > _VKI_NSIG)
104 return -1;
105 signum--;
106 set->sig[signum / _VKI_NSIG_BPW] &= ~(1UL << (signum % _VKI_NSIG_BPW));
107 return 0;
108}
109
110Int VG_(sigismember) ( const vki_sigset_t* set, Int signum )
111{
112 if (set == NULL)
113 return 0;
114 if (signum < 1 || signum > _VKI_NSIG)
115 return 0;
116 signum--;
117 if (1 & ((set->sig[signum / _VKI_NSIG_BPW]) >> (signum % _VKI_NSIG_BPW)))
118 return 1;
119 else
120 return 0;
121}
122
123
124/* Add all signals in src to dst. */
125void VG_(sigaddset_from_set)( vki_sigset_t* dst, vki_sigset_t* src )
126{
127 Int i;
128 vg_assert(dst != NULL && src != NULL);
129 for (i = 0; i < _VKI_NSIG_WORDS; i++)
130 dst->sig[i] |= src->sig[i];
131}
132
133/* Remove all signals in src from dst. */
134void VG_(sigdelset_from_set)( vki_sigset_t* dst, vki_sigset_t* src )
135{
136 Int i;
137 vg_assert(dst != NULL && src != NULL);
138 for (i = 0; i < _VKI_NSIG_WORDS; i++)
139 dst->sig[i] &= ~(src->sig[i]);
140}
141
142
143/* The functions sigaction, sigprocmask, sigpending and sigsuspend
144 return 0 on success and -1 on error.
145*/
146Int VG_(sigprocmask)( Int how, const vki_sigset_t* set, vki_sigset_t* oldset)
147{
148 SysRes res = VG_(do_syscall4)(__NR_rt_sigprocmask,
149 how, (UWord)set, (UWord)oldset,
150 _VKI_NSIG_WORDS * sizeof(UWord));
151 return res.isError ? -1 : 0;
152}
153
154
155Int VG_(sigaction) ( Int signum, const struct vki_sigaction* act,
156 struct vki_sigaction* oldact)
157{
158 SysRes res = VG_(do_syscall4)(__NR_rt_sigaction,
159 signum, (UWord)act, (UWord)oldact,
160 _VKI_NSIG_WORDS * sizeof(UWord));
161 return res.isError ? -1 : 0;
162}
163
164
165Int VG_(sigaltstack)( const vki_stack_t* ss, vki_stack_t* oss )
166{
167 SysRes res = VG_(do_syscall2)(__NR_sigaltstack, (UWord)ss, (UWord)oss);
168 return res.isError ? -1 : 0;
169}
170
171Int VG_(sigtimedwait)( const vki_sigset_t *set, vki_siginfo_t *info,
172 const struct vki_timespec *timeout )
173{
174 SysRes res = VG_(do_syscall4)(__NR_rt_sigtimedwait, (UWord)set, (UWord)info,
175 (UWord)timeout, sizeof(*set));
176 return res.isError ? -1 : res.val;
177}
178
179Int VG_(signal)(Int signum, void (*sighandler)(Int))
180{
181 SysRes res;
182 Int n;
183 struct vki_sigaction sa;
184 sa.ksa_handler = sighandler;
185 sa.sa_flags = VKI_SA_ONSTACK | VKI_SA_RESTART;
186 sa.sa_restorer = NULL;
187 n = VG_(sigemptyset)( &sa.sa_mask );
188 vg_assert(n == 0);
189 res = VG_(do_syscall4)(__NR_rt_sigaction, signum, (UWord)&sa, (UWord)NULL,
190 _VKI_NSIG_WORDS * sizeof(UWord));
191 return res.isError ? -1 : 0;
192}
193
194
195Int VG_(kill)( Int pid, Int signo )
196{
197 SysRes res = VG_(do_syscall2)(__NR_kill, pid, signo);
198 return res.isError ? -1 : 0;
199}
200
201
202Int VG_(tkill)( ThreadId tid, Int signo )
203{
204 SysRes res = VG_(mk_SysRes_Error)(VKI_ENOSYS);
205
206#if 0
207 /* This isn't right because the client may create a process
208 structure with multiple thread groups */
njn94c6ace2005-06-11 17:19:18 +0000209 res = VG_(do_syscall3)(__NR_tgkill, VG_(getpid)(), tid, signo);
njnde62cbf2005-06-10 22:08:14 +0000210#endif
211
212 res = VG_(do_syscall2)(__NR_tkill, tid, signo);
213
214 if (res.isError && res.val == VKI_ENOSYS)
215 res = VG_(do_syscall2)(__NR_kill, tid, signo);
216
217 return res.isError ? -1 : 0;
218}
219
220Int VG_(sigpending) ( vki_sigset_t* set )
221{
222// Nb: AMD64/Linux doesn't have __NR_sigpending; it only provides
223// __NR_rt_sigpending. This function will have to be abstracted in some
224// way to account for this. In the meantime, the easy option is to forget
225// about it for AMD64 until it's needed.
226#ifdef __amd64__
227 I_die_here;
228#else
229 SysRes res = VG_(do_syscall1)(__NR_sigpending, (UWord)set);
230 return res.isError ? -1 : 0;
231#endif
232}
233
234/*--------------------------------------------------------------------*/
235/*--- end ---*/
236/*--------------------------------------------------------------------*/