blob: bcb58c265e191676e4762f9cca6d83bcf2365286 [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/*
2 * Copyright (C) 2008 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#ifndef _SIGNAL_H_
29#define _SIGNAL_H_
30
31#include <sys/cdefs.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080032#include <sys/types.h>
David 'Digit' Turnerc124baa2012-07-12 19:06:15 +020033#include <machine/signal.h>
34#include <machine/ucontext.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080035
David 'Digit' Turnerc124baa2012-07-12 19:06:15 +020036#include <limits.h> /* For LONG_BIT */
37#include <string.h> /* For memset() */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080038
39__BEGIN_DECLS
40
David 'Digit' Turnerc124baa2012-07-12 19:06:15 +020041/* Previous versions of <signal.h> didn't define 'struct sigcontext'. This
42 * has led client code to redefine the structure, sometimes in a custom and
43 * incompatible way.
David 'Digit' Turner5c8c00a2010-12-20 15:58:06 +010044 *
David 'Digit' Turnerc124baa2012-07-12 19:06:15 +020045 * The __BIONIC_HAVE_STRUCT_SIGCONTEXT macro is defined in <sys/cdefs.h>
46 * to indicate that <signal.h> does indeed define the structure. For
47 * maximum source compatibility, client code should follow these guidelines:
48 *
49 * - If client code is guaranteed to build against a recent NDK/platform
50 * version, just include <signal.h> as usual, and use the
51 * 'struct sigcontext'.
52 *
53 * - If client code wants to build against older NDK/platform versions,
54 * and only uses 'struct sigcontext' without accessing _any_
55 * of its fields, just always include <asm/sigcontext.h> on Android
56 * to ensure maximum portability (i.e. to old platform/NDK releases).
57 * As in:
58 *
59 * #include <signal.h>
60 * #ifdef __BIONIC__
61 * #include <asm/sigcontext.h>
62 * #endif
63 *
64 * - Otherwise, if the client has a custom sigcontext definition and wants to
65 * keep it to minimize source changes, use a macro trick as below to avoid
66 * naming conflicts:
67 *
68 * #include <signal.h>
69 * #ifdef __BIONIC__
70 * # include <asm/sigcontext.h>
71 * # ifdef __BIONIC_HAVE_STRUCT_SIGCONTEXT
72 * # undef sigcontext
73 * # define sigcontext my_custom_sigcontext
74 * # endif
75 * #endif
David 'Digit' Turner5c8c00a2010-12-20 15:58:06 +010076 */
David 'Digit' Turnerc124baa2012-07-12 19:06:15 +020077
78/* Similarly, previous versions of the C library didn't define ucontext_t
79 * and mcontext_t in <signal.h>, or <sys/ucontext.h>. Client code can check
80 * against the macro __BIONIC_HAVE_UCONTEXT_T to determine if it does.
81 *
82 * Beware, that does _not_ mean that <ucontext.h> and related functions are
83 * available.
84 */
85
86typedef int sig_atomic_t;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080087
88extern const char * const sys_siglist[];
Thorsten Glaser81569aa2009-12-01 15:12:01 +010089extern const char * const sys_signame[];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080090
91static __inline__ int sigismember(sigset_t *set, int signum)
92{
93 unsigned long *local_set = (unsigned long *)set;
94 signum--;
95 return (int)((local_set[signum/LONG_BIT] >> (signum%LONG_BIT)) & 1);
96}
97
98
99static __inline__ int sigaddset(sigset_t *set, int signum)
100{
101 unsigned long *local_set = (unsigned long *)set;
102 signum--;
103 local_set[signum/LONG_BIT] |= 1UL << (signum%LONG_BIT);
104 return 0;
105}
106
107
108static __inline__ int sigdelset(sigset_t *set, int signum)
109{
110 unsigned long *local_set = (unsigned long *)set;
111 signum--;
112 local_set[signum/LONG_BIT] &= ~(1UL << (signum%LONG_BIT));
113 return 0;
114}
115
116
117static __inline__ int sigemptyset(sigset_t *set)
118{
119 memset(set, 0, sizeof *set);
120 return 0;
121}
122
123static __inline__ int sigfillset(sigset_t *set)
124{
125 memset(set, ~0, sizeof *set);
126 return 0;
127}
128
129
130/* compatibility types */
131typedef void (*sig_t)(int);
132typedef sig_t sighandler_t;
133
134/* differentiater between sysv and bsd behaviour 8*/
135extern __sighandler_t sysv_signal(int, __sighandler_t);
136extern __sighandler_t bsd_signal(int, __sighandler_t);
137
138/* the default is bsd */
139static __inline__ __sighandler_t signal(int s, __sighandler_t f)
140{
141 return bsd_signal(s,f);
142}
143
144/* the syscall itself */
145extern __sighandler_t __signal(int, __sighandler_t, int);
146
147extern int sigprocmask(int, const sigset_t *, sigset_t *);
148extern int sigaction(int, const struct sigaction *, struct sigaction *);
149
150extern int sigpending(sigset_t *);
151extern int sigsuspend(const sigset_t *);
152extern int sigwait(const sigset_t *set, int *sig);
153extern int siginterrupt(int sig, int flag);
154
155extern int raise(int);
156extern int kill(pid_t, int);
Colin Cross8c59d962010-01-13 16:39:26 -0800157extern int killpg(int pgrp, int sig);
David 'Digit' Turnerbb5581a2010-10-09 17:56:55 +0200158extern int sigaltstack(const stack_t *ss, stack_t *oss);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800159
Irina Tirdeab5f053b2012-09-08 09:17:54 +0300160extern void psiginfo(const siginfo_t* si, const char* message);
161extern void psignal(int signal_number, const char* message);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800162
163__END_DECLS
164
165#endif /* _SIGNAL_H_ */