blob: e6fb2e0441e9720a081c332c03f27bc3bd6e05f9 [file] [log] [blame]
yaberauneya7b3f2222009-11-26 12:04:07 +00001/*
2 * Copyright (c) 2009 Cisco Systems, Inc. All Rights Reserved.
Garrett Coopercdb5d0f2010-02-26 04:55:11 -08003 * Copyright (c) 2009 FUJITSU LIMITED. All Rights Reserved.
yaberauneya7b3f2222009-11-26 12:04:07 +00004 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it would be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 *
13 * Further, this software is distributed without any warranty that it is
14 * free of the rightful claim of any third person regarding infringement
15 * or the like. Any license provided herein, whether implied or
16 * otherwise, applies only to this software file. Patent licenses, if
17 * any, provided herein do not apply to combinations of this program with
18 * other software, or any other product whatsoever.
19 *
20 * You should have received a copy of the GNU General Public License along
Wanlong Gaofed96412012-10-24 10:10:29 +080021 * with this program; if not, write the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
yaberauneya7b3f2222009-11-26 12:04:07 +000023 *
Garrett Coopercdb5d0f2010-02-26 04:55:11 -080024 * Author: Liu Bo <liubo2009@cn.fujitsu.com>
25 * Author: Garrett Cooper <yanegomi@gmail.com>
26 *
yaberauneya7b3f2222009-11-26 12:04:07 +000027 */
28
29#ifndef __LTP_SIGNAL_H
30#define __LTP_SIGNAL_H
31
yaberauneya402ec1f2009-11-30 20:38:48 +000032#include <errno.h>
yaberauneya7b3f2222009-11-26 12:04:07 +000033#include <signal.h>
yaberauneya402ec1f2009-11-30 20:38:48 +000034#include <stdio.h>
35#include "config.h"
yaberauneya7b3f2222009-11-26 12:04:07 +000036
Stanislav Kholmanskikh0eccc582013-08-14 11:31:41 +040037/*
38 * For all but __mips__:
39 *
40 * _COMPAT_NSIG / _COMPAT_NSIG_BPW == 2.
41 *
42 * For __mips__:
43 *
44 * _COMPAT_NSIG / _COMPAT_NSIG_BPW == 4.
45 *
46 * See asm/compat.h under the kernel source for more details.
47 *
48 * Multiply that by a fudge factor of 4 and you have your SIGSETSIZE.
49 */
50#if defined __mips__
51#define SIGSETSIZE 16
52#else
yaberauneya7b3f2222009-11-26 12:04:07 +000053#define SIGSETSIZE (_NSIG / 8)
Stanislav Kholmanskikh0eccc582013-08-14 11:31:41 +040054#endif
yaberauneya7b3f2222009-11-26 12:04:07 +000055
yaberauneya402ec1f2009-11-30 20:38:48 +000056#ifdef LTP_RT_SIG_TEST
57
Garrett Coopercdb5d0f2010-02-26 04:55:11 -080058#ifdef __x86_64__
yaberauneya402ec1f2009-11-30 20:38:48 +000059
60/*
61 * From asm/signal.h -- this value isn't exported anywhere outside of glibc and
62 * asm/signal.h and is only required for the rt_sig* function family because
63 * sigaction(2), et all, appends this if necessary to
64 * (struct sigaction).sa_flags. HEH.
65 *
66 * I do #undef though, just in case...
Garrett Coopercdb5d0f2010-02-26 04:55:11 -080067 *
68 * Also, from .../arch/x86/kernel/signal.c:448 for v2.6.30 (something or
69 * other):
yaberauneya402ec1f2009-11-30 20:38:48 +000070 *
71 * x86-64 should always use SA_RESTORER.
72 *
73 * -- thus SA_RESTORER must always be defined along with
Garrett Coopercdb5d0f2010-02-26 04:55:11 -080074 * (struct sigaction).sa_restorer for this architecture.
yaberauneya402ec1f2009-11-30 20:38:48 +000075 */
Garrett Coopercdb5d0f2010-02-26 04:55:11 -080076#undef SA_RESTORER
77#define HAVE_SA_RESTORER
78#define SA_RESTORER 0x04000000
yaberauneya402ec1f2009-11-30 20:38:48 +000079
Garrett Coopercdb5d0f2010-02-26 04:55:11 -080080struct kernel_sigaction {
Garrett Cooper181f8dc2010-02-26 20:23:40 -080081 __sighandler_t k_sa_handler;
Garrett Coopercdb5d0f2010-02-26 04:55:11 -080082 unsigned long sa_flags;
83 void (*sa_restorer) (void);
84 sigset_t sa_mask;
85};
yaberauneya402ec1f2009-11-30 20:38:48 +000086
Cyril Hrubisf8b7fd82012-10-04 14:29:22 +020087void (*restore_rt)(void);
Garrett Coopercdb5d0f2010-02-26 04:55:11 -080088
Cyril Hrubisf8b7fd82012-10-04 14:29:22 +020089static void handler_h(int signal)
yaberauneya402ec1f2009-11-30 20:38:48 +000090{
Garrett Coopercdb5d0f2010-02-26 04:55:11 -080091 return;
yaberauneya402ec1f2009-11-30 20:38:48 +000092}
93
Garrett Coopercdb5d0f2010-02-26 04:55:11 -080094/* Setup an initial signal handler for signal number = sig for x86_64. */
Cyril Hrubisf8b7fd82012-10-04 14:29:22 +020095static inline int sig_initial(int sig)
Garrett Coopercdb5d0f2010-02-26 04:55:11 -080096{
Garrett Cooper181f8dc2010-02-26 20:23:40 -080097 int ret_code = -1;
Garrett Coopercdb5d0f2010-02-26 04:55:11 -080098 struct sigaction act, oact;
99
100 act.sa_handler = handler_h;
Jan Stancekc5532e42013-05-14 11:59:33 +0200101 act.sa_flags = 0;
Garrett Cooper181f8dc2010-02-26 20:23:40 -0800102 /* Clear out the signal set. */
103 if (sigemptyset(&act.sa_mask) < 0) {
104 /* Add the signal to the mask set. */
105 } else if (sigaddset(&act.sa_mask, sig) < 0) {
106 /* Set act.sa_restorer via syscall(2) */
107 } else if (sigaction(sig, &act, &oact) < 0) {
108 /* Copy oact.sa_restorer via syscall(2) */
109 } else if (sigaction(sig, &act, &oact) < 0) {
110 /* And voila -- we just tricked the kernel into giving us our
111 * restorer function! */
112 } else {
113 restore_rt = oact.sa_restorer;
114 ret_code = 0;
115 }
116
117 return ret_code;
118
Garrett Coopercdb5d0f2010-02-26 04:55:11 -0800119}
120
121#endif /* __x86_64__ */
122
123#endif /* LTP_RT_SIG_TEST */
yaberauneya402ec1f2009-11-30 20:38:48 +0000124
yaberauneya7b3f2222009-11-26 12:04:07 +0000125#endif