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 /* Return a mask that includes the bit for SIG only.  */
56 /* Unsigned cast ensures shift/mask insns are used.  */
57 # define __sigmask(sig) \
58   (((unsigned long) 1) << ((unsigned)((sig) - 1) % (8 * sizeof (unsigned long))))
59 
60 /* Return the word index for SIG.  */
61 # define __sigword(sig)	((unsigned)((sig) - 1) / (8 * sizeof (unsigned long)))
62 
63 /* gcc 4.3.1 is not clever enough to optimize for _SIGSET_NWORDS == 1 and 2,
64  * which are about the only values which can be there */
65 
66 # if defined __GNUC__ && __GNUC__ >= 2
67 #  define __sigemptyset(set) \
68 (__extension__ ({ \
69 	sigset_t *__set = (set);					\
70 	if (_SIGSET_NWORDS <= 2) {					\
71 		__set->__val[0] = 0;					\
72 		if (_SIGSET_NWORDS == 2)				\
73 			__set->__val[1] = 0;				\
74 	} else {							\
75 		int __cnt = _SIGSET_NWORDS;				\
76 		while (--__cnt >= 0) __set->__val[__cnt] = 0;		\
77 	}								\
78 	0;								\
79 }))
80 #  define __sigfillset(set) \
81 (__extension__ ({ \
82 	sigset_t *__set = (set);					\
83 	if (_SIGSET_NWORDS <= 2) {					\
84 		__set->__val[0] = ~0UL;					\
85 		if (_SIGSET_NWORDS == 2)				\
86 			__set->__val[1] = ~0UL;				\
87 	} else {							\
88 		int __cnt = _SIGSET_NWORDS;				\
89 		while (--__cnt >= 0) __set->__val[__cnt] = ~0UL;	\
90 	}								\
91 	0;								\
92 }))
93 
94 #  ifdef __USE_GNU
95 /* The POSIX does not specify for handling the whole signal set in one
96    command.  This is often wanted and so we define three more functions
97    here.  */
98 #   define __sigisemptyset(set) \
99 (__extension__ ({ \
100 	long __ret;							\
101 	const sigset_t *__set = (set);					\
102 	if (_SIGSET_NWORDS == 1) {					\
103 		__ret = __set->__val[0];				\
104 	} else if (_SIGSET_NWORDS == 2) {				\
105 		__ret = __set->__val[0] | __set->__val[1];		\
106 	} else {							\
107 		int __cnt = _SIGSET_NWORDS;				\
108 		__ret = __set->__val[--__cnt];				\
109 		while (!__ret && --__cnt >= 0)				\
110 			__ret = __set->__val[__cnt];			\
111 	}								\
112 	__ret == 0;							\
113 }))
114 #   define __sigandset(dest, left, right) \
115 (__extension__ ({ \
116 	sigset_t *__dest = (dest);					\
117 	const sigset_t *__left = (left);				\
118 	const sigset_t *__right = (right);				\
119 	if (_SIGSET_NWORDS <= 2) {					\
120 		__dest->__val[0] = __left->__val[0] & __right->__val[0];\
121 		if (_SIGSET_NWORDS == 2)				\
122 			__dest->__val[1] = __left->__val[1] & __right->__val[1];\
123 	} else {							\
124 		int __cnt = _SIGSET_NWORDS;				\
125 		while (--__cnt >= 0)					\
126 			__dest->__val[__cnt] = (__left->__val[__cnt]	\
127 					& __right->__val[__cnt]);	\
128 	}								\
129 	0;								\
130 }))
131 #   define __sigorset(dest, left, right) \
132 (__extension__ ({ \
133 	sigset_t *__dest = (dest);					\
134 	const sigset_t *__left = (left);				\
135 	const sigset_t *__right = (right);				\
136 	if (_SIGSET_NWORDS <= 2) {					\
137 		__dest->__val[0] = __left->__val[0] | __right->__val[0];\
138 		if (_SIGSET_NWORDS == 2)				\
139 			__dest->__val[1] = __left->__val[1] | __right->__val[1];\
140 	} else {							\
141 		int __cnt = _SIGSET_NWORDS;				\
142 		while (--__cnt >= 0)					\
143 			__dest->__val[__cnt] = (__left->__val[__cnt]	\
144 					| __right->__val[__cnt]);	\
145 	}								\
146 	0;								\
147 }))
148 #  endif
149 # endif
150 
151 /* These functions needn't check for a bogus signal number -- error
152    checking is done in the non __ versions.  */
153 
154 # if !defined __USE_EXTERN_INLINES || defined __PROVIDE_OUT_OF_LINE_SIGSETFN
155 extern int __sigismember (const __sigset_t *, int);
156 libc_hidden_proto(__sigismember)
157 extern int __sigaddset (__sigset_t *, int);
158 libc_hidden_proto(__sigaddset)
159 extern int __sigdelset (__sigset_t *, int);
160 libc_hidden_proto(__sigdelset)
161 # endif
162 
163 # ifdef __USE_EXTERN_INLINES
164 #  undef _EXTERN_INLINE
165 #  ifdef __PROVIDE_OUT_OF_LINE_SIGSETFN
166 #   define _EXTERN_INLINE
167 #  else /* normal case */
168     /* dropped extern below: otherwise every module with __USE_EXTERN_INLINES
169      * will have its own copy of out-of line function emitted. */
170 #   define _EXTERN_INLINE /*extern*/ __always_inline
171 #  endif
172 #  define __SIGSETFN(NAME, BODY, CONST)					\
173 _EXTERN_INLINE int							\
174 NAME (CONST __sigset_t *__set, int __sig)				\
175 {									\
176 	unsigned long __mask = __sigmask (__sig);			\
177 	unsigned __word = __sigword (__sig);				\
178 	return BODY;							\
179 }
180 
181 __SIGSETFN (__sigismember, (__set->__val[__word] & __mask) ? 1 : 0, const)
182 __SIGSETFN (__sigaddset, ((__set->__val[__word] |= __mask), 0), )
183 __SIGSETFN (__sigdelset, ((__set->__val[__word] &= ~__mask), 0), )
184 
185 #  undef __SIGSETFN
186 # endif
187 
188 # ifdef _LIBC
189 /* It's far too much PITA to __USE_EXTERN_INLINES from within libc.
190  * Especially since we want to inline only calls with const sig,
191  * but __USE_EXTERN_INLINES will inline all calls!
192  */
193 static __always_inline unsigned long
const_sigismember(const __sigset_t * set,int sig)194 const_sigismember(const __sigset_t *set, int sig)
195 {
196 	unsigned long mask = __sigmask(sig);
197 	unsigned word = __sigword(sig);
198 	return (set->__val[word] & mask);
199 }
200 #  define __sigismember(set, sig) \
201 	(__builtin_constant_p(sig) ? (const_sigismember(set, sig) != 0) : __sigismember(set, sig))
202 static __always_inline void
const_sigaddset(__sigset_t * set,int sig)203 const_sigaddset(__sigset_t *set, int sig)
204 {
205 	unsigned long mask = __sigmask(sig);
206 	unsigned word = __sigword(sig);
207 	set->__val[word] |= mask;
208 }
209 #  define __sigaddset(set, sig) \
210 	(__builtin_constant_p(sig) ? (const_sigaddset(set, sig), 0)  : __sigaddset(set, sig))
211 static __always_inline void
const_sigdelset(__sigset_t * set,int sig)212 const_sigdelset(__sigset_t *set, int sig)
213 {
214 	unsigned long mask = __sigmask(sig);
215 	unsigned word = __sigword(sig);
216 	set->__val[word] &= ~mask;
217 }
218 #  define __sigdelset(set, sig) \
219 	(__builtin_constant_p(sig) ? (const_sigdelset(set, sig), 0) : __sigdelset(set, sig))
220 # endif
221 
222 #endif /* ! _SIGSET_H_fns.  */
223