| 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 | #if __ANDROID_API__ < 21 | 
 | 38 |  | 
| Dan Albert | 466dbe4 | 2015-01-28 18:16:08 -0800 | [diff] [blame] | 39 | __BEGIN_DECLS | 
 | 40 |  | 
 | 41 | extern sighandler_t bsd_signal(int signum, sighandler_t handler); | 
 | 42 |  | 
| Josh Gao | b8e1b705 | 2016-04-13 17:18:20 -0700 | [diff] [blame] | 43 | static __inline int sigismember(const sigset_t *set, int signum) { | 
| Dan Albert | 466dbe4 | 2015-01-28 18:16:08 -0800 | [diff] [blame] | 44 |   /* Signal numbers start at 1, but bit positions start at 0. */ | 
 | 45 |   int bit = signum - 1; | 
 | 46 |   const unsigned long *local_set = (const unsigned long *)set; | 
 | 47 |   if (set == NULL || bit < 0 || bit >= (int)(8 * sizeof(sigset_t))) { | 
 | 48 |     errno = EINVAL; | 
 | 49 |     return -1; | 
 | 50 |   } | 
 | 51 |   return (int)((local_set[bit / LONG_BIT] >> (bit % LONG_BIT)) & 1); | 
 | 52 | } | 
 | 53 |  | 
 | 54 | static __inline int sigaddset(sigset_t *set, int signum) { | 
 | 55 |   /* Signal numbers start at 1, but bit positions start at 0. */ | 
 | 56 |   int bit = signum - 1; | 
 | 57 |   unsigned long *local_set = (unsigned long *)set; | 
 | 58 |   if (set == NULL || bit < 0 || bit >= (int)(8 * sizeof(sigset_t))) { | 
 | 59 |     errno = EINVAL; | 
 | 60 |     return -1; | 
 | 61 |   } | 
 | 62 |   local_set[bit / LONG_BIT] |= 1UL << (bit % LONG_BIT); | 
 | 63 |   return 0; | 
 | 64 | } | 
 | 65 |  | 
 | 66 | static __inline int sigdelset(sigset_t *set, int signum) { | 
 | 67 |   /* Signal numbers start at 1, but bit positions start at 0. */ | 
 | 68 |   int bit = signum - 1; | 
 | 69 |   unsigned long *local_set = (unsigned long *)set; | 
 | 70 |   if (set == NULL || bit < 0 || bit >= (int)(8 * sizeof(sigset_t))) { | 
 | 71 |     errno = EINVAL; | 
 | 72 |     return -1; | 
 | 73 |   } | 
 | 74 |   local_set[bit / LONG_BIT] &= ~(1UL << (bit % LONG_BIT)); | 
 | 75 |   return 0; | 
 | 76 | } | 
 | 77 |  | 
 | 78 | static __inline int sigemptyset(sigset_t *set) { | 
 | 79 |   if (set == NULL) { | 
 | 80 |     errno = EINVAL; | 
 | 81 |     return -1; | 
 | 82 |   } | 
 | 83 |   memset(set, 0, sizeof(sigset_t)); | 
 | 84 |   return 0; | 
 | 85 | } | 
 | 86 |  | 
 | 87 | static __inline int sigfillset(sigset_t *set) { | 
 | 88 |   if (set == NULL) { | 
 | 89 |     errno = EINVAL; | 
 | 90 |     return -1; | 
 | 91 |   } | 
 | 92 |   memset(set, ~0, sizeof(sigset_t)); | 
 | 93 |   return 0; | 
 | 94 | } | 
 | 95 |  | 
 | 96 | static __inline sighandler_t signal(int s, sighandler_t f) { | 
 | 97 |   return bsd_signal(s, f); | 
 | 98 | } | 
 | 99 |  | 
 | 100 | __END_DECLS | 
 | 101 |  | 
| Josh Gao | b8e1b705 | 2016-04-13 17:18:20 -0700 | [diff] [blame] | 102 | #endif | 
| Dan Albert | 466dbe4 | 2015-01-28 18:16:08 -0800 | [diff] [blame] | 103 | #endif /* _ANDROID_LEGACY_SIGNAL_INLINES_H_ */ |