1 /* __sig_atomic_t, __sigset_t, and related definitions.  Linux version.
2    Copyright (C) 1991, 1992, 1994, 1996, 1997 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4 
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9 
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14 
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <http://www.gnu.org/licenses/>.  */
18 
19 #ifndef	_SIGSET_H_types
20 # define _SIGSET_H_types	1
21 
22 typedef int __sig_atomic_t;
23 
24 /* A 'sigset_t' has a bit for each signal.
25  * glibc has space for 1024 signals (!), but most arches supported
26  * by Linux have 64 signals, and only MIPS has 128.
27  * There seems to be some historical baggage in sparc[64]
28  * where they might have (or had in the past) 32 signals only,
29  * I hope it's irrelevant now.
30  * Signal 0 does not exist, so we have signals 1..64, not 0..63.
31  * In uclibc, kernel and userspace sigset_t is always the same.
32  * BTW, struct sigaction is also the same on kernel and userspace side.
33  */
34 #if defined(__mips__)
35 # define _SIGSET_NWORDS	(128 / (8 * sizeof (unsigned long)))
36 #else
37 # define _SIGSET_NWORDS	(64 / (8 * sizeof (unsigned long)))
38 #endif
39 typedef struct {
40 	unsigned long __val[_SIGSET_NWORDS];
41 } __sigset_t;
42 
43 #endif
44 
45 
46 /* We only want to define these functions if <signal.h> was actually
47    included; otherwise we were included just to define the types.  Since we
48    are namespace-clean, it wouldn't hurt to define extra macros.  But
49    trouble can be caused by functions being defined (e.g., any global
50    register vars declared later will cause compilation errors).  */
51 
52 #if !defined _SIGSET_H_fns && defined _SIGNAL_H
53 # define _SIGSET_H_fns 1
54 
55 # ifndef _EXTERN_INLINE
56 #  define _EXTERN_INLINE __extern_inline
57 # endif
58 
59 /* Return a mask that includes the bit for SIG only.  */
60 /* Unsigned cast ensures shift/mask insns are used.  */
61 # define __sigmask(sig) \
62   (((unsigned long) 1) << ((unsigned)((sig) - 1) % (8 * sizeof (unsigned long))))
63 
64 /* Return the word index for SIG.  */
65 # define __sigword(sig)	((unsigned)((sig) - 1) / (8 * sizeof (unsigned long)))
66 
67 /* gcc 4.3.1 is not clever enough to optimize for _SIGSET_NWORDS == 1 and 2,
68  * which are about the only values which can be there */
69 
70 # if defined __GNUC__ && __GNUC__ >= 2
71 #  define __sigemptyset(set) \
72 (__extension__ ({ \
73 	sigset_t *__set = (set);					\
74 	if (_SIGSET_NWORDS <= 2) {					\
75 		__set->__val[0] = 0;					\
76 		if (_SIGSET_NWORDS == 2)				\
77 			__set->__val[1] = 0;				\
78 	} else {							\
79 		int __cnt = _SIGSET_NWORDS;				\
80 		while (--__cnt >= 0) __set->__val[__cnt] = 0;		\
81 	}								\
82 	0;								\
83 }))
84 #  define __sigfillset(set) \
85 (__extension__ ({ \
86 	sigset_t *__set = (set);					\
87 	if (_SIGSET_NWORDS <= 2) {					\
88 		__set->__val[0] = ~0UL;					\
89 		if (_SIGSET_NWORDS == 2)				\
90 			__set->__val[1] = ~0UL;				\
91 	} else {							\
92 		int __cnt = _SIGSET_NWORDS;				\
93 		while (--__cnt >= 0) __set->__val[__cnt] = ~0UL;	\
94 	}								\
95 	0;								\
96 }))
97 
98 #  ifdef __USE_GNU
99 /* The POSIX does not specify for handling the whole signal set in one
100    command.  This is often wanted and so we define three more functions
101    here.  */
102 #   define __sigisemptyset(set) \
103 (__extension__ ({ \
104 	long __ret;							\
105 	const sigset_t *__set = (set);					\
106 	if (_SIGSET_NWORDS == 1) {					\
107 		__ret = __set->__val[0];				\
108 	} else if (_SIGSET_NWORDS == 2) {				\
109 		__ret = __set->__val[0] | __set->__val[1];		\
110 	} else {							\
111 		int __cnt = _SIGSET_NWORDS;				\
112 		__ret = __set->__val[--__cnt];				\
113 		while (!__ret && --__cnt >= 0)				\
114 			__ret = __set->__val[__cnt];			\
115 	}								\
116 	__ret == 0;							\
117 }))
118 #   define __sigandset(dest, left, right) \
119 (__extension__ ({ \
120 	sigset_t *__dest = (dest);					\
121 	const sigset_t *__left = (left);				\
122 	const sigset_t *__right = (right);				\
123 	if (_SIGSET_NWORDS <= 2) {					\
124 		__dest->__val[0] = __left->__val[0] & __right->__val[0];\
125 		if (_SIGSET_NWORDS == 2)				\
126 			__dest->__val[1] = __left->__val[1] & __right->__val[1];\
127 	} else {							\
128 		int __cnt = _SIGSET_NWORDS;				\
129 		while (--__cnt >= 0)					\
130 			__dest->__val[__cnt] = (__left->__val[__cnt]	\
131 					& __right->__val[__cnt]);	\
132 	}								\
133 	0;								\
134 }))
135 #   define __sigorset(dest, left, right) \
136 (__extension__ ({ \
137 	sigset_t *__dest = (dest);					\
138 	const sigset_t *__left = (left);				\
139 	const sigset_t *__right = (right);				\
140 	if (_SIGSET_NWORDS <= 2) {					\
141 		__dest->__val[0] = __left->__val[0] | __right->__val[0];\
142 		if (_SIGSET_NWORDS == 2)				\
143 			__dest->__val[1] = __left->__val[1] | __right->__val[1];\
144 	} else {							\
145 		int __cnt = _SIGSET_NWORDS;				\
146 		while (--__cnt >= 0)					\
147 			__dest->__val[__cnt] = (__left->__val[__cnt]	\
148 					| __right->__val[__cnt]);	\
149 	}								\
150 	0;								\
151 }))
152 #  endif
153 # endif
154 
155 /* These functions needn't check for a bogus signal number -- error
156    checking is done in the non __ versions.  */
157 
158 extern int __sigismember (const __sigset_t *, int);
159 libc_hidden_proto(__sigismember)
160 extern void __sigaddset (__sigset_t *, int);
161 libc_hidden_proto(__sigaddset)
162 extern void __sigdelset (__sigset_t *, int);
163 libc_hidden_proto(__sigdelset)
164 
165 # ifdef __USE_EXTERN_INLINES
166 #  define __SIGSETFN(RET_TYPE, NAME, BODY, CONST)			\
167 _EXTERN_INLINE RET_TYPE							\
168 NAME (CONST __sigset_t *__set, int __sig)				\
169 {									\
170 	unsigned long __mask = __sigmask (__sig);			\
171 	unsigned __word = __sigword (__sig);				\
172 	BODY;							\
173 }
174 
175 __SIGSETFN (int, __sigismember, return (__set->__val[__word] & __mask) ? 1 : 0,
176 	const)
177 __SIGSETFN (void, __sigaddset, (__set->__val[__word] |= __mask), )
178 __SIGSETFN (void, __sigdelset, (__set->__val[__word] &= ~__mask), )
179 
180 #  undef __SIGSETFN
181 # endif
182 
183 # ifdef _LIBC
184 /* It's far too much PITA to __USE_EXTERN_INLINES from within libc.
185  * Especially since we want to inline only calls with const sig,
186  * but __USE_EXTERN_INLINES will inline all calls!
187  */
188 static __always_inline unsigned long
const_sigismember(const __sigset_t * set,int sig)189 const_sigismember(const __sigset_t *set, int sig)
190 {
191 	unsigned long mask = __sigmask(sig);
192 	unsigned word = __sigword(sig);
193 	return (set->__val[word] & mask);
194 }
195 #  define __sigismember(set, sig) \
196 	(__builtin_constant_p(sig) ? (const_sigismember(set, sig) != 0) : __sigismember(set, sig))
197 static __always_inline void
const_sigaddset(__sigset_t * set,int sig)198 const_sigaddset(__sigset_t *set, int sig)
199 {
200 	unsigned long mask = __sigmask(sig);
201 	unsigned word = __sigword(sig);
202 	set->__val[word] |= mask;
203 }
204 #  define __sigaddset(set, sig) \
205 	(__builtin_constant_p(sig) ? const_sigaddset(set, sig)  : __sigaddset(set, sig))
206 static __always_inline void
const_sigdelset(__sigset_t * set,int sig)207 const_sigdelset(__sigset_t *set, int sig)
208 {
209 	unsigned long mask = __sigmask(sig);
210 	unsigned word = __sigword(sig);
211 	set->__val[word] &= ~mask;
212 }
213 #  define __sigdelset(set, sig) \
214 	(__builtin_constant_p(sig) ? const_sigdelset(set, sig) : __sigdelset(set, sig))
215 # endif
216 
217 #endif /* ! _SIGSET_H_fns.  */
218