| 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 |  | 
|  | 29 | #ifndef _ANDROID_LEGACY_SIGNAL_INLINES_H_ | 
|  | 30 | #define _ANDROID_LEGACY_SIGNAL_INLINES_H_ | 
|  | 31 |  | 
| Josh Gao | b8e1b705 | 2016-04-13 17:18:20 -0700 | [diff] [blame] | 32 | #include <errno.h> | 
|  | 33 | #include <signal.h> | 
| Dan Albert | 466dbe4 | 2015-01-28 18:16:08 -0800 | [diff] [blame] | 34 | #include <string.h> | 
|  | 35 | #include <sys/cdefs.h> | 
|  | 36 |  | 
| Josh Gao | b8e1b705 | 2016-04-13 17:18:20 -0700 | [diff] [blame] | 37 |  | 
| Dan Albert | 466dbe4 | 2015-01-28 18:16:08 -0800 | [diff] [blame] | 38 | __BEGIN_DECLS | 
|  | 39 |  | 
| Elliott Hughes | 3b2096a | 2016-07-22 18:57:12 -0700 | [diff] [blame] | 40 | sighandler_t bsd_signal(int signum, sighandler_t handler) __REMOVED_IN(21); | 
| Josh Gao | 46b4416 | 2016-05-27 11:14:16 -0700 | [diff] [blame] | 41 |  | 
| Elliott Hughes | 5bc78c8 | 2016-11-16 11:35:43 -0800 | [diff] [blame] | 42 | #if __ANDROID_API__ < __ANDROID_API_L__ | 
| Dan Albert | 466dbe4 | 2015-01-28 18:16:08 -0800 | [diff] [blame] | 43 |  | 
| Josh Gao | b8e1b705 | 2016-04-13 17:18:20 -0700 | [diff] [blame] | 44 | static __inline int sigismember(const sigset_t *set, int signum) { | 
| Dan Albert | 466dbe4 | 2015-01-28 18:16:08 -0800 | [diff] [blame] | 45 | /* 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 |  | 
|  | 55 | static __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 |  | 
|  | 67 | static __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 |  | 
|  | 79 | static __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 |  | 
|  | 88 | static __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 |  | 
|  | 97 | static __inline sighandler_t signal(int s, sighandler_t f) { | 
|  | 98 | return bsd_signal(s, f); | 
|  | 99 | } | 
|  | 100 |  | 
| Elliott Hughes | 5bc78c8 | 2016-11-16 11:35:43 -0800 | [diff] [blame] | 101 | #endif /* __ANDROID_API__ < __ANDROID_API_L__ */ | 
| Josh Gao | 46b4416 | 2016-05-27 11:14:16 -0700 | [diff] [blame] | 102 |  | 
| Dan Albert | 466dbe4 | 2015-01-28 18:16:08 -0800 | [diff] [blame] | 103 | __END_DECLS | 
|  | 104 |  | 
| Dan Albert | 466dbe4 | 2015-01-28 18:16:08 -0800 | [diff] [blame] | 105 | #endif /* _ANDROID_LEGACY_SIGNAL_INLINES_H_ */ |