blob: afdaca8401525bda05ed937675d0a77898b167fc [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
29#ifndef _ANDROID_LEGACY_SIGNAL_INLINES_H_
30#define _ANDROID_LEGACY_SIGNAL_INLINES_H_
31
Josh Gaob8e1b7052016-04-13 17:18:20 -070032#include <errno.h>
33#include <signal.h>
Dan Albert466dbe42015-01-28 18:16:08 -080034#include <string.h>
35#include <sys/cdefs.h>
36
Josh Gaob8e1b7052016-04-13 17:18:20 -070037
Dan Albert466dbe42015-01-28 18:16:08 -080038__BEGIN_DECLS
39
Elliott Hughes3b2096a2016-07-22 18:57:12 -070040sighandler_t bsd_signal(int signum, sighandler_t handler) __REMOVED_IN(21);
Josh Gao46b44162016-05-27 11:14:16 -070041
Elliott Hughes5bc78c82016-11-16 11:35:43 -080042#if __ANDROID_API__ < __ANDROID_API_L__
Dan Albert466dbe42015-01-28 18:16:08 -080043
Josh Gaob8e1b7052016-04-13 17:18:20 -070044static __inline int sigismember(const sigset_t *set, int signum) {
Dan Albert466dbe42015-01-28 18:16:08 -080045 /* Signal numbers start at 1, but bit positions start at 0. */
46 int bit = signum - 1;
47 const unsigned long *local_set = (const unsigned long *)set;
48 if (set == NULL || bit < 0 || bit >= (int)(8 * sizeof(sigset_t))) {
49 errno = EINVAL;
50 return -1;
51 }
52 return (int)((local_set[bit / LONG_BIT] >> (bit % LONG_BIT)) & 1);
53}
54
55static __inline int sigaddset(sigset_t *set, int signum) {
56 /* Signal numbers start at 1, but bit positions start at 0. */
57 int bit = signum - 1;
58 unsigned long *local_set = (unsigned long *)set;
59 if (set == NULL || bit < 0 || bit >= (int)(8 * sizeof(sigset_t))) {
60 errno = EINVAL;
61 return -1;
62 }
63 local_set[bit / LONG_BIT] |= 1UL << (bit % LONG_BIT);
64 return 0;
65}
66
67static __inline int sigdelset(sigset_t *set, int signum) {
68 /* Signal numbers start at 1, but bit positions start at 0. */
69 int bit = signum - 1;
70 unsigned long *local_set = (unsigned long *)set;
71 if (set == NULL || bit < 0 || bit >= (int)(8 * sizeof(sigset_t))) {
72 errno = EINVAL;
73 return -1;
74 }
75 local_set[bit / LONG_BIT] &= ~(1UL << (bit % LONG_BIT));
76 return 0;
77}
78
79static __inline int sigemptyset(sigset_t *set) {
80 if (set == NULL) {
81 errno = EINVAL;
82 return -1;
83 }
84 memset(set, 0, sizeof(sigset_t));
85 return 0;
86}
87
88static __inline int sigfillset(sigset_t *set) {
89 if (set == NULL) {
90 errno = EINVAL;
91 return -1;
92 }
93 memset(set, ~0, sizeof(sigset_t));
94 return 0;
95}
96
97static __inline sighandler_t signal(int s, sighandler_t f) {
98 return bsd_signal(s, f);
99}
100
Elliott Hughes5bc78c82016-11-16 11:35:43 -0800101#endif /* __ANDROID_API__ < __ANDROID_API_L__ */
Josh Gao46b44162016-05-27 11:14:16 -0700102
Dan Albert466dbe42015-01-28 18:16:08 -0800103__END_DECLS
104
Dan Albert466dbe42015-01-28 18:16:08 -0800105#endif /* _ANDROID_LEGACY_SIGNAL_INLINES_H_ */