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 #pragma once
30
31 #include <sys/cdefs.h>
32
33 #if __ANDROID_API__ < 21
34
35 #include <errno.h>
36 #include <signal.h>
37 #include <string.h>
38
39 __BEGIN_DECLS
40
41 sighandler_t bsd_signal(int __signal, sighandler_t __handler) __REMOVED_IN(21);
42
43 /* These weren't introduced until L. */
44 int __libc_current_sigrtmax() __attribute__((__weak__)) __VERSIONER_NO_GUARD;
45 int __libc_current_sigrtmin() __attribute__((__weak__)) __VERSIONER_NO_GUARD;
46
__ndk_legacy___libc_current_sigrtmax()47 static __inline int __ndk_legacy___libc_current_sigrtmax() {
48 if (__libc_current_sigrtmax) return __libc_current_sigrtmax();
49 return __SIGRTMAX; /* Should match __libc_current_sigrtmax. */
50 }
51
__ndk_legacy___libc_current_sigrtmin()52 static __inline int __ndk_legacy___libc_current_sigrtmin() {
53 if (__libc_current_sigrtmin) return __libc_current_sigrtmin();
54 return __SIGRTMIN + 7; /* Should match __libc_current_sigrtmin. */
55 }
56
57 #undef SIGRTMAX
58 #define SIGRTMAX __ndk_legacy___libc_current_sigrtmax()
59 #undef SIGRTMIN
60 #define SIGRTMIN __ndk_legacy___libc_current_sigrtmin()
61
sigismember(const sigset_t * set,int signum)62 static __inline int sigismember(const sigset_t *set, int signum) {
63 /* Signal numbers start at 1, but bit positions start at 0. */
64 int bit = signum - 1;
65 const unsigned long *local_set = (const unsigned long *)set;
66 if (set == NULL || bit < 0 || bit >= (int)(8 * sizeof(sigset_t))) {
67 errno = EINVAL;
68 return -1;
69 }
70 return (int)((local_set[bit / LONG_BIT] >> (bit % LONG_BIT)) & 1);
71 }
72
sigaddset(sigset_t * set,int signum)73 static __inline int sigaddset(sigset_t *set, int signum) {
74 /* Signal numbers start at 1, but bit positions start at 0. */
75 int bit = signum - 1;
76 unsigned long *local_set = (unsigned long *)set;
77 if (set == NULL || bit < 0 || bit >= (int)(8 * sizeof(sigset_t))) {
78 errno = EINVAL;
79 return -1;
80 }
81 local_set[bit / LONG_BIT] |= 1UL << (bit % LONG_BIT);
82 return 0;
83 }
84
sigdelset(sigset_t * set,int signum)85 static __inline int sigdelset(sigset_t *set, int signum) {
86 /* Signal numbers start at 1, but bit positions start at 0. */
87 int bit = signum - 1;
88 unsigned long *local_set = (unsigned long *)set;
89 if (set == NULL || bit < 0 || bit >= (int)(8 * sizeof(sigset_t))) {
90 errno = EINVAL;
91 return -1;
92 }
93 local_set[bit / LONG_BIT] &= ~(1UL << (bit % LONG_BIT));
94 return 0;
95 }
96
sigemptyset(sigset_t * set)97 static __inline int sigemptyset(sigset_t *set) {
98 if (set == NULL) {
99 errno = EINVAL;
100 return -1;
101 }
102 memset(set, 0, sizeof(sigset_t));
103 return 0;
104 }
105
sigfillset(sigset_t * set)106 static __inline int sigfillset(sigset_t *set) {
107 if (set == NULL) {
108 errno = EINVAL;
109 return -1;
110 }
111 memset(set, ~0, sizeof(sigset_t));
112 return 0;
113 }
114
signal(int s,sighandler_t f)115 static __inline sighandler_t signal(int s, sighandler_t f) {
116 return bsd_signal(s, f);
117 }
118
119 __END_DECLS
120
121 #endif
122