blob: e968c45f51a1ce1878612e334e97ad7cbf4edef2 [file] [log] [blame]
Dan Albert466dbe42015-01-28 18:16:08 -08001/*
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
Elliott Hughes95c6cd72019-12-20 13:26:14 -080029#pragma once
Dan Albert466dbe42015-01-28 18:16:08 -080030
Dan Albert989d8042018-01-18 19:38:50 +000031#include <sys/cdefs.h>
32
Elliott Hughes95c6cd72019-12-20 13:26:14 -080033#if __ANDROID_API__ < 21
Dan Albert989d8042018-01-18 19:38:50 +000034
Josh Gaob8e1b7052016-04-13 17:18:20 -070035#include <errno.h>
36#include <signal.h>
Dan Albert466dbe42015-01-28 18:16:08 -080037#include <string.h>
Josh Gaob8e1b7052016-04-13 17:18:20 -070038
Dan Albert466dbe42015-01-28 18:16:08 -080039__BEGIN_DECLS
40
Elliott Hughesfaa74342017-08-11 17:34:44 -070041sighandler_t bsd_signal(int __signal, sighandler_t __handler) __REMOVED_IN(21);
Elliott Hughes583ef362017-06-13 14:29:15 -070042
Elliott Hughes7ba2bed2017-04-25 15:45:29 -070043/* These weren't introduced until L. */
44int __libc_current_sigrtmax() __attribute__((__weak__)) __VERSIONER_NO_GUARD;
45int __libc_current_sigrtmin() __attribute__((__weak__)) __VERSIONER_NO_GUARD;
46
47static __inline int __ndk_legacy___libc_current_sigrtmax() {
Dan Alberteae41f82021-01-29 16:24:06 -080048 /*
49 * The NDK doesn't use libclang_rt.builtins yet (https://github.com/android/ndk/issues/1231) so it
50 * can't use __builtin_available, but the platform builds with -Werror=unguarded-availability so
51 * it requires __builtin_available.
52 */
53#if defined(__ANDROID_UNGUARDED_AVAILABILITY__)
Jiyong Parkbb192082020-09-02 15:00:21 +090054 if (__builtin_available(android 21, *)) {
Dan Alberteae41f82021-01-29 16:24:06 -080055#else
56 if (__libc_current_sigrtmax) {
57#endif
Jiyong Parkbb192082020-09-02 15:00:21 +090058 return __libc_current_sigrtmax();
59 }
Elliott Hughes7ba2bed2017-04-25 15:45:29 -070060 return __SIGRTMAX; /* Should match __libc_current_sigrtmax. */
61}
62
63static __inline int __ndk_legacy___libc_current_sigrtmin() {
Dan Alberteae41f82021-01-29 16:24:06 -080064 /*
65 * The NDK doesn't use libclang_rt.builtins yet (https://github.com/android/ndk/issues/1231) so it
66 * can't use __builtin_available, but the platform builds with -Werror=unguarded-availability so
67 * it requires __builtin_available.
68 */
69#if defined(__ANDROID_UNGUARDED_AVAILABILITY__)
Jiyong Parkbb192082020-09-02 15:00:21 +090070 if (__builtin_available(android 21, *)) {
Dan Alberteae41f82021-01-29 16:24:06 -080071#else
72 if (__libc_current_sigrtmin) {
73#endif
Jiyong Parkbb192082020-09-02 15:00:21 +090074 return __libc_current_sigrtmin();
75 }
Florian Mayer6883b082019-07-22 13:54:32 +010076 return __SIGRTMIN + 7; /* Should match __libc_current_sigrtmin. */
Elliott Hughes7ba2bed2017-04-25 15:45:29 -070077}
78
79#undef SIGRTMAX
80#define SIGRTMAX __ndk_legacy___libc_current_sigrtmax()
81#undef SIGRTMIN
82#define SIGRTMIN __ndk_legacy___libc_current_sigrtmin()
83
Josh Gaob8e1b7052016-04-13 17:18:20 -070084static __inline int sigismember(const sigset_t *set, int signum) {
Dan Albert466dbe42015-01-28 18:16:08 -080085 /* Signal numbers start at 1, but bit positions start at 0. */
86 int bit = signum - 1;
87 const unsigned long *local_set = (const unsigned long *)set;
88 if (set == NULL || bit < 0 || bit >= (int)(8 * sizeof(sigset_t))) {
89 errno = EINVAL;
90 return -1;
91 }
92 return (int)((local_set[bit / LONG_BIT] >> (bit % LONG_BIT)) & 1);
93}
94
95static __inline int sigaddset(sigset_t *set, int signum) {
96 /* Signal numbers start at 1, but bit positions start at 0. */
97 int bit = signum - 1;
98 unsigned long *local_set = (unsigned long *)set;
99 if (set == NULL || bit < 0 || bit >= (int)(8 * sizeof(sigset_t))) {
100 errno = EINVAL;
101 return -1;
102 }
103 local_set[bit / LONG_BIT] |= 1UL << (bit % LONG_BIT);
104 return 0;
105}
106
107static __inline int sigdelset(sigset_t *set, int signum) {
108 /* Signal numbers start at 1, but bit positions start at 0. */
109 int bit = signum - 1;
110 unsigned long *local_set = (unsigned long *)set;
111 if (set == NULL || bit < 0 || bit >= (int)(8 * sizeof(sigset_t))) {
112 errno = EINVAL;
113 return -1;
114 }
115 local_set[bit / LONG_BIT] &= ~(1UL << (bit % LONG_BIT));
116 return 0;
117}
118
119static __inline int sigemptyset(sigset_t *set) {
120 if (set == NULL) {
121 errno = EINVAL;
122 return -1;
123 }
124 memset(set, 0, sizeof(sigset_t));
125 return 0;
126}
127
128static __inline int sigfillset(sigset_t *set) {
129 if (set == NULL) {
130 errno = EINVAL;
131 return -1;
132 }
133 memset(set, ~0, sizeof(sigset_t));
134 return 0;
135}
136
137static __inline sighandler_t signal(int s, sighandler_t f) {
138 return bsd_signal(s, f);
139}
140
Joachim Sauer54c71522018-01-18 11:37:34 +0000141__END_DECLS
142
Elliott Hughes95c6cd72019-12-20 13:26:14 -0800143#endif