Dan Albert | 466dbe4 | 2015-01-28 18:16:08 -0800 | [diff] [blame] | 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 | |
Elliott Hughes | 95c6cd7 | 2019-12-20 13:26:14 -0800 | [diff] [blame] | 29 | #pragma once |
Dan Albert | 466dbe4 | 2015-01-28 18:16:08 -0800 | [diff] [blame] | 30 | |
Dan Albert | 989d804 | 2018-01-18 19:38:50 +0000 | [diff] [blame] | 31 | #include <sys/cdefs.h> |
| 32 | |
Elliott Hughes | 95c6cd7 | 2019-12-20 13:26:14 -0800 | [diff] [blame] | 33 | #if __ANDROID_API__ < 21 |
Dan Albert | 989d804 | 2018-01-18 19:38:50 +0000 | [diff] [blame] | 34 | |
Josh Gao | b8e1b705 | 2016-04-13 17:18:20 -0700 | [diff] [blame] | 35 | #include <errno.h> |
| 36 | #include <signal.h> |
Dan Albert | 466dbe4 | 2015-01-28 18:16:08 -0800 | [diff] [blame] | 37 | #include <string.h> |
Josh Gao | b8e1b705 | 2016-04-13 17:18:20 -0700 | [diff] [blame] | 38 | |
Dan Albert | 466dbe4 | 2015-01-28 18:16:08 -0800 | [diff] [blame] | 39 | __BEGIN_DECLS |
| 40 | |
Elliott Hughes | faa7434 | 2017-08-11 17:34:44 -0700 | [diff] [blame] | 41 | sighandler_t bsd_signal(int __signal, sighandler_t __handler) __REMOVED_IN(21); |
Elliott Hughes | 583ef36 | 2017-06-13 14:29:15 -0700 | [diff] [blame] | 42 | |
Elliott Hughes | 7ba2bed | 2017-04-25 15:45:29 -0700 | [diff] [blame] | 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 | |
| 47 | static __inline int __ndk_legacy___libc_current_sigrtmax() { |
Jiyong Park | bb19208 | 2020-09-02 15:00:21 +0900 | [diff] [blame^] | 48 | if (__builtin_available(android 21, *)) { |
| 49 | return __libc_current_sigrtmax(); |
| 50 | } |
Elliott Hughes | 7ba2bed | 2017-04-25 15:45:29 -0700 | [diff] [blame] | 51 | return __SIGRTMAX; /* Should match __libc_current_sigrtmax. */ |
| 52 | } |
| 53 | |
| 54 | static __inline int __ndk_legacy___libc_current_sigrtmin() { |
Jiyong Park | bb19208 | 2020-09-02 15:00:21 +0900 | [diff] [blame^] | 55 | if (__builtin_available(android 21, *)) { |
| 56 | return __libc_current_sigrtmin(); |
| 57 | } |
Florian Mayer | 6883b08 | 2019-07-22 13:54:32 +0100 | [diff] [blame] | 58 | return __SIGRTMIN + 7; /* Should match __libc_current_sigrtmin. */ |
Elliott Hughes | 7ba2bed | 2017-04-25 15:45:29 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | #undef SIGRTMAX |
| 62 | #define SIGRTMAX __ndk_legacy___libc_current_sigrtmax() |
| 63 | #undef SIGRTMIN |
| 64 | #define SIGRTMIN __ndk_legacy___libc_current_sigrtmin() |
| 65 | |
Josh Gao | b8e1b705 | 2016-04-13 17:18:20 -0700 | [diff] [blame] | 66 | static __inline int sigismember(const sigset_t *set, int signum) { |
Dan Albert | 466dbe4 | 2015-01-28 18:16:08 -0800 | [diff] [blame] | 67 | /* Signal numbers start at 1, but bit positions start at 0. */ |
| 68 | int bit = signum - 1; |
| 69 | const unsigned long *local_set = (const unsigned long *)set; |
| 70 | if (set == NULL || bit < 0 || bit >= (int)(8 * sizeof(sigset_t))) { |
| 71 | errno = EINVAL; |
| 72 | return -1; |
| 73 | } |
| 74 | return (int)((local_set[bit / LONG_BIT] >> (bit % LONG_BIT)) & 1); |
| 75 | } |
| 76 | |
| 77 | static __inline int sigaddset(sigset_t *set, int signum) { |
| 78 | /* Signal numbers start at 1, but bit positions start at 0. */ |
| 79 | int bit = signum - 1; |
| 80 | unsigned long *local_set = (unsigned long *)set; |
| 81 | if (set == NULL || bit < 0 || bit >= (int)(8 * sizeof(sigset_t))) { |
| 82 | errno = EINVAL; |
| 83 | return -1; |
| 84 | } |
| 85 | local_set[bit / LONG_BIT] |= 1UL << (bit % LONG_BIT); |
| 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | static __inline int sigdelset(sigset_t *set, int signum) { |
| 90 | /* Signal numbers start at 1, but bit positions start at 0. */ |
| 91 | int bit = signum - 1; |
| 92 | unsigned long *local_set = (unsigned long *)set; |
| 93 | if (set == NULL || bit < 0 || bit >= (int)(8 * sizeof(sigset_t))) { |
| 94 | errno = EINVAL; |
| 95 | return -1; |
| 96 | } |
| 97 | local_set[bit / LONG_BIT] &= ~(1UL << (bit % LONG_BIT)); |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | static __inline int sigemptyset(sigset_t *set) { |
| 102 | if (set == NULL) { |
| 103 | errno = EINVAL; |
| 104 | return -1; |
| 105 | } |
| 106 | memset(set, 0, sizeof(sigset_t)); |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | static __inline int sigfillset(sigset_t *set) { |
| 111 | if (set == NULL) { |
| 112 | errno = EINVAL; |
| 113 | return -1; |
| 114 | } |
| 115 | memset(set, ~0, sizeof(sigset_t)); |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | static __inline sighandler_t signal(int s, sighandler_t f) { |
| 120 | return bsd_signal(s, f); |
| 121 | } |
| 122 | |
Joachim Sauer | 54c7152 | 2018-01-18 11:37:34 +0000 | [diff] [blame] | 123 | __END_DECLS |
| 124 | |
Elliott Hughes | 95c6cd7 | 2019-12-20 13:26:14 -0800 | [diff] [blame] | 125 | #endif |