1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *  * Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  *  * Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in
12  *    the documentation and/or other materials provided with the
13  *    distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #ifndef _ANDROID_LEGACY_SIGNAL_INLINES_H_
30 #define _ANDROID_LEGACY_SIGNAL_INLINES_H_
31 
32 #include <string.h>
33 #include <sys/cdefs.h>
34 
35 __BEGIN_DECLS
36 
37 extern sighandler_t bsd_signal(int signum, sighandler_t handler);
38 
sigismember(sigset_t * set,int signum)39 static __inline int sigismember(sigset_t *set, int signum) {
40   /* Signal numbers start at 1, but bit positions start at 0. */
41   int bit = signum - 1;
42   const unsigned long *local_set = (const unsigned long *)set;
43   if (set == NULL || bit < 0 || bit >= (int)(8 * sizeof(sigset_t))) {
44     errno = EINVAL;
45     return -1;
46   }
47   return (int)((local_set[bit / LONG_BIT] >> (bit % LONG_BIT)) & 1);
48 }
49 
sigaddset(sigset_t * set,int signum)50 static __inline int sigaddset(sigset_t *set, int signum) {
51   /* Signal numbers start at 1, but bit positions start at 0. */
52   int bit = signum - 1;
53   unsigned long *local_set = (unsigned long *)set;
54   if (set == NULL || bit < 0 || bit >= (int)(8 * sizeof(sigset_t))) {
55     errno = EINVAL;
56     return -1;
57   }
58   local_set[bit / LONG_BIT] |= 1UL << (bit % LONG_BIT);
59   return 0;
60 }
61 
sigdelset(sigset_t * set,int signum)62 static __inline int sigdelset(sigset_t *set, int signum) {
63   /* Signal numbers start at 1, but bit positions start at 0. */
64   int bit = signum - 1;
65   unsigned long *local_set = (unsigned long *)set;
66   if (set == NULL || bit < 0 || bit >= (int)(8 * sizeof(sigset_t))) {
67     errno = EINVAL;
68     return -1;
69   }
70   local_set[bit / LONG_BIT] &= ~(1UL << (bit % LONG_BIT));
71   return 0;
72 }
73 
sigemptyset(sigset_t * set)74 static __inline int sigemptyset(sigset_t *set) {
75   if (set == NULL) {
76     errno = EINVAL;
77     return -1;
78   }
79   memset(set, 0, sizeof(sigset_t));
80   return 0;
81 }
82 
sigfillset(sigset_t * set)83 static __inline int sigfillset(sigset_t *set) {
84   if (set == NULL) {
85     errno = EINVAL;
86     return -1;
87   }
88   memset(set, ~0, sizeof(sigset_t));
89   return 0;
90 }
91 
signal(int s,sighandler_t f)92 static __inline sighandler_t signal(int s, sighandler_t f) {
93   return bsd_signal(s, f);
94 }
95 
96 __END_DECLS
97 
98 #endif /* _ANDROID_LEGACY_SIGNAL_INLINES_H_ */
99