blob: bdfb729c61260b63ace8d8ea6568acdda7ca5651 [file] [log] [blame]
Elliott Hughesc5d028f2013-01-10 14:42:14 -08001/*
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 Hughes4b1c6e72018-01-24 18:54:38 -080020#include <errno.h>
Dan Albert75ef63d2014-11-21 00:18:07 -080021#include <signal.h>
22
Elliott Hughes4b1c6e72018-01-24 18:54:38 -080023#include <async_safe/log.h>
24
Elliott Hughesc5d028f2013-01-10 14:42:14 -080025// Our sigset_t is wrong for ARM and x86. It's 32-bit but the kernel expects 64 bits.
Elliott Hughes4b1c6e72018-01-24 18:54:38 -080026// This means we can't support real-time signals correctly without breaking the ABI.
Elliott Hughesc5d028f2013-01-10 14:42:14 -080027// In the meantime, we can use this union to pass an appropriately-sized block of memory
Elliott Hughes4b1c6e72018-01-24 18:54:38 -080028// 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 Hughesc5d028f2013-01-10 14:42:14 -080030union kernel_sigset_t {
Elliott Hughes4b1c6e72018-01-24 18:54:38 -080031 public:
Elliott Hughesc5d028f2013-01-10 14:42:14 -080032 kernel_sigset_t() {
Elliott Hughesc5d028f2013-01-10 14:42:14 -080033 }
34
Elliott Hughes4b1c6e72018-01-24 18:54:38 -080035 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 Hughesc5d028f2013-01-10 14:42:14 -080041 clear();
42 set(value);
43 }
44
45 void clear() {
Elliott Hughes11952072013-10-24 15:15:14 -070046 __builtin_memset(this, 0, sizeof(*this));
Elliott Hughesc5d028f2013-01-10 14:42:14 -080047 }
48
Elliott Hughes4b1c6e72018-01-24 18:54:38 -080049 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 Hughesc5d028f2013-01-10 14:42:14 -080073 void set(const sigset_t* value) {
Elliott Hughes4b1c6e72018-01-24 18:54:38 -080074 clear();
Elliott Hughesc5d028f2013-01-10 14:42:14 -080075 bionic = *value;
76 }
77
78 sigset_t* get() {
79 return &bionic;
80 }
81
82 sigset_t bionic;
Elliott Hughes4b1c6e72018-01-24 18:54:38 -080083 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 Hughesc5d028f2013-01-10 14:42:14 -080094};
95
Elliott Hughes4b1c6e72018-01-24 18:54:38 -080096extern "C" int __rt_sigpending(const kernel_sigset_t*, size_t);
97extern "C" int __rt_sigprocmask(int, const kernel_sigset_t*, kernel_sigset_t*, size_t);
98extern "C" int __rt_sigsuspend(const kernel_sigset_t*, size_t);
99
Elliott Hughesc5d028f2013-01-10 14:42:14 -0800100#endif