Elliott Hughes | c5d028f | 2013-01-10 14:42:14 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef LIBC_PRIVATE_KERNEL_SIGSET_T_H_ |
| 18 | #define LIBC_PRIVATE_KERNEL_SIGSET_T_H_ |
| 19 | |
Elliott Hughes | 4b1c6e7 | 2018-01-24 18:54:38 -0800 | [diff] [blame] | 20 | #include <errno.h> |
Dan Albert | 75ef63d | 2014-11-21 00:18:07 -0800 | [diff] [blame] | 21 | #include <signal.h> |
| 22 | |
Elliott Hughes | 4b1c6e7 | 2018-01-24 18:54:38 -0800 | [diff] [blame] | 23 | #include <async_safe/log.h> |
| 24 | |
Elliott Hughes | c5d028f | 2013-01-10 14:42:14 -0800 | [diff] [blame] | 25 | // Our sigset_t is wrong for ARM and x86. It's 32-bit but the kernel expects 64 bits. |
Elliott Hughes | 4b1c6e7 | 2018-01-24 18:54:38 -0800 | [diff] [blame] | 26 | // This means we can't support real-time signals correctly without breaking the ABI. |
Elliott Hughes | c5d028f | 2013-01-10 14:42:14 -0800 | [diff] [blame] | 27 | // In the meantime, we can use this union to pass an appropriately-sized block of memory |
Elliott Hughes | 4b1c6e7 | 2018-01-24 18:54:38 -0800 | [diff] [blame] | 28 | // to the kernel, at the cost of not being able to refer to real-time signals when |
| 29 | // initializing from a sigset_t on LP32. |
Elliott Hughes | c5d028f | 2013-01-10 14:42:14 -0800 | [diff] [blame] | 30 | union kernel_sigset_t { |
Elliott Hughes | 4b1c6e7 | 2018-01-24 18:54:38 -0800 | [diff] [blame] | 31 | public: |
Elliott Hughes | c5d028f | 2013-01-10 14:42:14 -0800 | [diff] [blame] | 32 | kernel_sigset_t() { |
Elliott Hughes | c5d028f | 2013-01-10 14:42:14 -0800 | [diff] [blame] | 33 | } |
| 34 | |
Elliott Hughes | 4b1c6e7 | 2018-01-24 18:54:38 -0800 | [diff] [blame] | 35 | explicit kernel_sigset_t(int signal_number) { |
| 36 | clear(); |
| 37 | if (!set(signal_number)) async_safe_fatal("kernel_sigset_t(%d)", signal_number); |
| 38 | } |
| 39 | |
| 40 | explicit kernel_sigset_t(const sigset_t* value) { |
Elliott Hughes | c5d028f | 2013-01-10 14:42:14 -0800 | [diff] [blame] | 41 | clear(); |
| 42 | set(value); |
| 43 | } |
| 44 | |
| 45 | void clear() { |
Elliott Hughes | 1195207 | 2013-10-24 15:15:14 -0700 | [diff] [blame] | 46 | __builtin_memset(this, 0, sizeof(*this)); |
Elliott Hughes | c5d028f | 2013-01-10 14:42:14 -0800 | [diff] [blame] | 47 | } |
| 48 | |
Elliott Hughes | 4b1c6e7 | 2018-01-24 18:54:38 -0800 | [diff] [blame] | 49 | bool clear(int signal_number) { |
| 50 | int bit = bit_of(signal_number); |
| 51 | if (bit == -1) return false; |
| 52 | bits[bit / LONG_BIT] &= ~(1UL << (bit % LONG_BIT)); |
| 53 | return true; |
| 54 | } |
| 55 | |
| 56 | void fill() { |
| 57 | __builtin_memset(this, 0xff, sizeof(*this)); |
| 58 | } |
| 59 | |
| 60 | bool is_set(int signal_number) { |
| 61 | int bit = bit_of(signal_number); |
| 62 | if (bit == -1) return false; |
| 63 | return ((bits[bit / LONG_BIT] >> (bit % LONG_BIT)) & 1) == 1; |
| 64 | } |
| 65 | |
| 66 | bool set(int signal_number) { |
| 67 | int bit = bit_of(signal_number); |
| 68 | if (bit == -1) return false; |
| 69 | bits[bit / LONG_BIT] |= 1UL << (bit % LONG_BIT); |
| 70 | return true; |
| 71 | } |
| 72 | |
Elliott Hughes | c5d028f | 2013-01-10 14:42:14 -0800 | [diff] [blame] | 73 | void set(const sigset_t* value) { |
Elliott Hughes | 4b1c6e7 | 2018-01-24 18:54:38 -0800 | [diff] [blame] | 74 | clear(); |
Elliott Hughes | c5d028f | 2013-01-10 14:42:14 -0800 | [diff] [blame] | 75 | bionic = *value; |
| 76 | } |
| 77 | |
| 78 | sigset_t* get() { |
| 79 | return &bionic; |
| 80 | } |
| 81 | |
| 82 | sigset_t bionic; |
Elliott Hughes | 4b1c6e7 | 2018-01-24 18:54:38 -0800 | [diff] [blame] | 83 | unsigned long bits[_KERNEL__NSIG/LONG_BIT]; |
| 84 | |
| 85 | private: |
| 86 | int bit_of(int signal_number) { |
| 87 | int bit = signal_number - 1; // Signal numbers start at 1, but bit positions start at 0. |
| 88 | if (bit < 0 || bit >= static_cast<int>(8*sizeof(*this))) { |
| 89 | errno = EINVAL; |
| 90 | return -1; |
| 91 | } |
| 92 | return bit; |
| 93 | } |
Elliott Hughes | c5d028f | 2013-01-10 14:42:14 -0800 | [diff] [blame] | 94 | }; |
| 95 | |
Elliott Hughes | 4b1c6e7 | 2018-01-24 18:54:38 -0800 | [diff] [blame] | 96 | extern "C" int __rt_sigpending(const kernel_sigset_t*, size_t); |
| 97 | extern "C" int __rt_sigprocmask(int, const kernel_sigset_t*, kernel_sigset_t*, size_t); |
| 98 | extern "C" int __rt_sigsuspend(const kernel_sigset_t*, size_t); |
| 99 | |
Elliott Hughes | c5d028f | 2013-01-10 14:42:14 -0800 | [diff] [blame] | 100 | #endif |