blob: 39c81cdb9624372024a65ab5386b09ce538f6b87 [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
sewardje4b0bf02006-06-05 23:21:15 +000010 Copyright (C) 2000-2006 Julian Seward
njnde62cbf2005-06-10 22:08:14 +000011 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"
sewardj4eee4762006-10-14 15:51:32 +000036#include "pub_core_vkiscnums.h"
njnde62cbf2005-06-10 22:08:14 +000037
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
njnde62cbf2005-06-10 22:08:14 +0000166Int VG_(sigtimedwait)( const vki_sigset_t *set, vki_siginfo_t *info,
167 const struct vki_timespec *timeout )
168{
169 SysRes res = VG_(do_syscall4)(__NR_rt_sigtimedwait, (UWord)set, (UWord)info,
170 (UWord)timeout, sizeof(*set));
171 return res.isError ? -1 : res.val;
172}
173
174Int VG_(signal)(Int signum, void (*sighandler)(Int))
175{
176 SysRes res;
177 Int n;
178 struct vki_sigaction sa;
179 sa.ksa_handler = sighandler;
180 sa.sa_flags = VKI_SA_ONSTACK | VKI_SA_RESTART;
181 sa.sa_restorer = NULL;
182 n = VG_(sigemptyset)( &sa.sa_mask );
183 vg_assert(n == 0);
184 res = VG_(do_syscall4)(__NR_rt_sigaction, signum, (UWord)&sa, (UWord)NULL,
185 _VKI_NSIG_WORDS * sizeof(UWord));
186 return res.isError ? -1 : 0;
187}
188
189
190Int VG_(kill)( Int pid, Int signo )
191{
192 SysRes res = VG_(do_syscall2)(__NR_kill, pid, signo);
193 return res.isError ? -1 : 0;
194}
195
196
197Int VG_(tkill)( ThreadId tid, Int signo )
198{
199 SysRes res = VG_(mk_SysRes_Error)(VKI_ENOSYS);
200
201#if 0
202 /* This isn't right because the client may create a process
203 structure with multiple thread groups */
njn94c6ace2005-06-11 17:19:18 +0000204 res = VG_(do_syscall3)(__NR_tgkill, VG_(getpid)(), tid, signo);
njnde62cbf2005-06-10 22:08:14 +0000205#endif
206
207 res = VG_(do_syscall2)(__NR_tkill, tid, signo);
208
209 if (res.isError && res.val == VKI_ENOSYS)
210 res = VG_(do_syscall2)(__NR_kill, tid, signo);
211
212 return res.isError ? -1 : 0;
213}
214
njnde62cbf2005-06-10 22:08:14 +0000215/*--------------------------------------------------------------------*/
216/*--- end ---*/
217/*--------------------------------------------------------------------*/