alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved. |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 3 | * |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 4 | * This program is free software; you can redistribute it and/or modify it |
| 5 | * under the terms of version 2 of the GNU General Public License as |
| 6 | * published by the Free Software Foundation. |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 7 | * |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 8 | * This program is distributed in the hope that it would be useful, but |
| 9 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 11 | * |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 12 | * Further, this software is distributed without any warranty that it is |
| 13 | * free of the rightful claim of any third person regarding infringement |
| 14 | * or the like. Any license provided herein, whether implied or |
| 15 | * otherwise, applies only to this software file. Patent licenses, if |
| 16 | * any, provided herein do not apply to combinations of this program with |
| 17 | * other software, or any other product whatsoever. |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 18 | * |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 19 | * You should have received a copy of the GNU General Public License along |
Wanlong Gao | fed9641 | 2012-10-24 10:10:29 +0800 | [diff] [blame] | 20 | * with this program; if not, write the Free Software Foundation, Inc., |
| 21 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 22 | * |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 23 | * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy, |
| 24 | * Mountain View, CA 94043, or: |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 25 | * |
| 26 | * http://www.sgi.com |
| 27 | * |
| 28 | * For further information regarding this notice, see: |
| 29 | * |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 30 | * http://oss.sgi.com/projects/GenInfo/NoticeExplan/ |
| 31 | */ |
| 32 | |
vapier | 6daba92 | 2009-08-28 09:29:01 +0000 | [diff] [blame] | 33 | /* $Id: tst_sig.c,v 1.13 2009/08/28 09:29:01 vapier Exp $ */ |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 34 | |
| 35 | /***************************************************************************** |
| 36 | OS Testing - Silicon Graphics, Inc. |
| 37 | |
| 38 | FUNCTION IDENTIFIER : tst_sig Set up for unexpected signals. |
| 39 | |
| 40 | AUTHOR : David D. Fenner |
| 41 | |
| 42 | CO-PILOT : Bill Roske |
| 43 | |
| 44 | DATE STARTED : 06/06/90 |
| 45 | |
| 46 | This module may be linked with c-modules requiring unexpected |
| 47 | signal handling. The parameters to tst_sig are as follows: |
| 48 | |
| 49 | fork_flag - set to FORK or NOFORK depending upon whether the |
| 50 | calling program executes a fork() system call. It |
| 51 | is normally the case that the calling program treats |
Khem Raj | ad33a3d | 2016-07-21 21:26:51 -0700 | [diff] [blame] | 52 | SIGCHLD as an expected signal if fork() is being used. |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 53 | |
| 54 | handler - a pointer to the unexpected signal handler to |
| 55 | be executed after an unexpected signal has been |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 56 | detected. If handler is set to DEF_HANDLER, a |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 57 | default handler is used. This routine should be |
| 58 | declared as function returning an int. |
| 59 | |
| 60 | cleanup - a pointer to a cleanup routine to be executed |
| 61 | by the unexpected signal handler before tst_exit is |
| 62 | called. This parameter is set to NULL if no cleanup |
| 63 | routine is required. An external variable, T_cleanup |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 64 | is set so that other user-defined handlers have |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 65 | access to the cleanup routine. This routine should be |
| 66 | declared as returning type void. |
| 67 | |
| 68 | ***************************************************************************/ |
| 69 | |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 70 | #include <errno.h> |
| 71 | #include <string.h> |
| 72 | #include <signal.h> |
mridge | e6508f8 | 2005-01-04 21:00:17 +0000 | [diff] [blame] | 73 | #include <unistd.h> |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 74 | #include "test.h" |
| 75 | |
| 76 | #define MAXMESG 150 /* size of mesg string sent to tst_res */ |
| 77 | |
Cyril Hrubis | ab1d3b6 | 2014-06-02 18:33:17 +0200 | [diff] [blame] | 78 | static void (*T_cleanup) (); |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 79 | |
vapier | 6daba92 | 2009-08-28 09:29:01 +0000 | [diff] [blame] | 80 | static void def_handler(); /* default signal handler */ |
| 81 | static void (*tst_setup_signal(int, void (*)(int))) (int); |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 82 | |
| 83 | /**************************************************************************** |
| 84 | * tst_sig() : set-up to catch unexpected signals. fork_flag is set to NOFORK |
Khem Raj | ad33a3d | 2016-07-21 21:26:51 -0700 | [diff] [blame] | 85 | * if SIGCHLD is to be an "unexpected signal", otherwise it is set to |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 86 | * FORK. cleanup points to a cleanup routine to be executed before |
| 87 | * tst_exit is called (cleanup is set to NULL if no cleanup is desired). |
| 88 | * handler is a pointer to the signal handling routine (if handler is |
| 89 | * set to NULL, a default handler is used). |
| 90 | ***************************************************************************/ |
| 91 | |
vapier | 6daba92 | 2009-08-28 09:29:01 +0000 | [diff] [blame] | 92 | void tst_sig(int fork_flag, void (*handler) (), void (*cleanup) ()) |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 93 | { |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 94 | int sig; |
mridge | e6508f8 | 2005-01-04 21:00:17 +0000 | [diff] [blame] | 95 | #ifdef _SC_SIGRT_MIN |
vapier | 6daba92 | 2009-08-28 09:29:01 +0000 | [diff] [blame] | 96 | long sigrtmin, sigrtmax; |
mridge | e6508f8 | 2005-01-04 21:00:17 +0000 | [diff] [blame] | 97 | #endif |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 98 | |
| 99 | /* |
| 100 | * save T_cleanup and handler function pointers |
| 101 | */ |
vapier | 6daba92 | 2009-08-28 09:29:01 +0000 | [diff] [blame] | 102 | T_cleanup = cleanup; /* used by default handler */ |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 103 | |
| 104 | if (handler == DEF_HANDLER) { |
| 105 | /* use default handler */ |
| 106 | handler = def_handler; |
| 107 | } |
mridge | e6508f8 | 2005-01-04 21:00:17 +0000 | [diff] [blame] | 108 | #ifdef _SC_SIGRT_MIN |
vapier | 6daba92 | 2009-08-28 09:29:01 +0000 | [diff] [blame] | 109 | sigrtmin = sysconf(_SC_SIGRT_MIN); |
| 110 | sigrtmax = sysconf(_SC_SIGRT_MAX); |
mridge | e6508f8 | 2005-01-04 21:00:17 +0000 | [diff] [blame] | 111 | #endif |
| 112 | |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 113 | /* |
| 114 | * now loop through all signals and set the handlers |
| 115 | */ |
| 116 | |
| 117 | for (sig = 1; sig < NSIG; sig++) { |
vapier | 6daba92 | 2009-08-28 09:29:01 +0000 | [diff] [blame] | 118 | /* |
| 119 | * SIGKILL is never unexpected. |
Khem Raj | ad33a3d | 2016-07-21 21:26:51 -0700 | [diff] [blame] | 120 | * SIGCHLD is only unexpected when |
vapier | 6daba92 | 2009-08-28 09:29:01 +0000 | [diff] [blame] | 121 | * no forking is being done. |
| 122 | * SIGINFO is used for file quotas and should be expected |
| 123 | */ |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 124 | |
mridge | e6508f8 | 2005-01-04 21:00:17 +0000 | [diff] [blame] | 125 | #ifdef _SC_SIGRT_MIN |
vapier | 6daba92 | 2009-08-28 09:29:01 +0000 | [diff] [blame] | 126 | if (sig >= sigrtmin && sig <= sigrtmax) |
| 127 | continue; |
mridge | e6508f8 | 2005-01-04 21:00:17 +0000 | [diff] [blame] | 128 | #endif |
| 129 | |
vapier | 6daba92 | 2009-08-28 09:29:01 +0000 | [diff] [blame] | 130 | switch (sig) { |
| 131 | case SIGKILL: |
| 132 | case SIGSTOP: |
| 133 | case SIGCONT: |
mridge | e6508f8 | 2005-01-04 21:00:17 +0000 | [diff] [blame] | 134 | #if !defined(_SC_SIGRT_MIN) && defined(__SIGRTMIN) && defined(__SIGRTMAX) |
vapier | 6daba92 | 2009-08-28 09:29:01 +0000 | [diff] [blame] | 135 | /* Ignore all real-time signals */ |
robbiew | 3e36a84 | 2003-11-25 16:04:28 +0000 | [diff] [blame] | 136 | case __SIGRTMIN: |
vapier | 6daba92 | 2009-08-28 09:29:01 +0000 | [diff] [blame] | 137 | case __SIGRTMIN + 1: |
| 138 | case __SIGRTMIN + 2: |
| 139 | case __SIGRTMIN + 3: |
| 140 | case __SIGRTMIN + 4: |
| 141 | case __SIGRTMIN + 5: |
| 142 | case __SIGRTMIN + 6: |
| 143 | case __SIGRTMIN + 7: |
| 144 | case __SIGRTMIN + 8: |
| 145 | case __SIGRTMIN + 9: |
| 146 | case __SIGRTMIN + 10: |
| 147 | case __SIGRTMIN + 11: |
| 148 | case __SIGRTMIN + 12: |
| 149 | case __SIGRTMIN + 13: |
| 150 | case __SIGRTMIN + 14: |
| 151 | case __SIGRTMIN + 15: |
robbiew | 79c135c | 2004-05-19 20:44:50 +0000 | [diff] [blame] | 152 | /* __SIGRTMIN is 37 on HPPA rather than 32 * |
| 153 | * as on i386, etc. */ |
| 154 | #if !defined(__hppa__) |
vapier | 6daba92 | 2009-08-28 09:29:01 +0000 | [diff] [blame] | 155 | case __SIGRTMAX - 15: |
| 156 | case __SIGRTMAX - 14: |
| 157 | case __SIGRTMAX - 13: |
| 158 | case __SIGRTMAX - 12: |
| 159 | case __SIGRTMAX - 11: |
robbiew | 79c135c | 2004-05-19 20:44:50 +0000 | [diff] [blame] | 160 | #endif |
vapier | 6daba92 | 2009-08-28 09:29:01 +0000 | [diff] [blame] | 161 | case __SIGRTMAX - 10: |
| 162 | case __SIGRTMAX - 9: |
| 163 | case __SIGRTMAX - 8: |
| 164 | case __SIGRTMAX - 7: |
| 165 | case __SIGRTMAX - 6: |
| 166 | case __SIGRTMAX - 5: |
| 167 | case __SIGRTMAX - 4: |
| 168 | case __SIGRTMAX - 3: |
| 169 | case __SIGRTMAX - 2: |
| 170 | case __SIGRTMAX - 1: |
robbiew | 3e36a84 | 2003-11-25 16:04:28 +0000 | [diff] [blame] | 171 | case __SIGRTMAX: |
mridge | e6508f8 | 2005-01-04 21:00:17 +0000 | [diff] [blame] | 172 | #endif |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 173 | #ifdef SIGSWAP |
robbiew | 3e36a84 | 2003-11-25 16:04:28 +0000 | [diff] [blame] | 174 | case SIGSWAP: |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 175 | #endif /* SIGSWAP */ |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 176 | |
| 177 | #ifdef SIGCKPT |
vapier | 6daba92 | 2009-08-28 09:29:01 +0000 | [diff] [blame] | 178 | case SIGCKPT: |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 179 | #endif |
| 180 | #ifdef SIGRESTART |
vapier | 6daba92 | 2009-08-28 09:29:01 +0000 | [diff] [blame] | 181 | case SIGRESTART: |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 182 | #endif |
vapier | 6daba92 | 2009-08-28 09:29:01 +0000 | [diff] [blame] | 183 | /* |
| 184 | * pthread-private signals SIGPTINTR and SIGPTRESCHED. |
| 185 | * Setting a handler for these signals is disallowed when |
| 186 | * the binary is linked against libpthread. |
| 187 | */ |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 188 | #ifdef SIGPTINTR |
vapier | 6daba92 | 2009-08-28 09:29:01 +0000 | [diff] [blame] | 189 | case SIGPTINTR: |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 190 | #endif /* SIGPTINTR */ |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 191 | #ifdef SIGPTRESCHED |
vapier | 6daba92 | 2009-08-28 09:29:01 +0000 | [diff] [blame] | 192 | case SIGPTRESCHED: |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 193 | #endif /* SIGPTRESCHED */ |
mridge | e6508f8 | 2005-01-04 21:00:17 +0000 | [diff] [blame] | 194 | #ifdef _SIGRESERVE |
vapier | 6daba92 | 2009-08-28 09:29:01 +0000 | [diff] [blame] | 195 | case _SIGRESERVE: |
mridge | e6508f8 | 2005-01-04 21:00:17 +0000 | [diff] [blame] | 196 | #endif |
| 197 | #ifdef _SIGDIL |
vapier | 6daba92 | 2009-08-28 09:29:01 +0000 | [diff] [blame] | 198 | case _SIGDIL: |
mridge | e6508f8 | 2005-01-04 21:00:17 +0000 | [diff] [blame] | 199 | #endif |
| 200 | #ifdef _SIGCANCEL |
vapier | 6daba92 | 2009-08-28 09:29:01 +0000 | [diff] [blame] | 201 | case _SIGCANCEL: |
mridge | e6508f8 | 2005-01-04 21:00:17 +0000 | [diff] [blame] | 202 | #endif |
| 203 | #ifdef _SIGGFAULT |
vapier | 6daba92 | 2009-08-28 09:29:01 +0000 | [diff] [blame] | 204 | case _SIGGFAULT: |
mridge | e6508f8 | 2005-01-04 21:00:17 +0000 | [diff] [blame] | 205 | #endif |
vapier | 6daba92 | 2009-08-28 09:29:01 +0000 | [diff] [blame] | 206 | break; |
Cyril Hrubis | 0c1ba5c | 2014-06-11 10:53:13 +0200 | [diff] [blame] | 207 | |
Khem Raj | ad33a3d | 2016-07-21 21:26:51 -0700 | [diff] [blame] | 208 | case SIGCHLD: |
Cyril Hrubis | 0c1ba5c | 2014-06-11 10:53:13 +0200 | [diff] [blame] | 209 | if (fork_flag == FORK) |
| 210 | continue; |
| 211 | |
vapier | 6daba92 | 2009-08-28 09:29:01 +0000 | [diff] [blame] | 212 | default: |
| 213 | if (tst_setup_signal(sig, handler) == SIG_ERR) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 214 | tst_resm(TWARN | TERRNO, |
| 215 | "signal failed for signal %d", sig); |
vapier | 6daba92 | 2009-08-28 09:29:01 +0000 | [diff] [blame] | 216 | break; |
| 217 | } |
Cyril Hrubis | ab1d3b6 | 2014-06-02 18:33:17 +0200 | [diff] [blame] | 218 | } |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 219 | } |
| 220 | |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 221 | /**************************************************************************** |
| 222 | * def_handler() : default signal handler that is invoked when |
| 223 | * an unexpected signal is caught. |
| 224 | ***************************************************************************/ |
| 225 | |
vapier | 6daba92 | 2009-08-28 09:29:01 +0000 | [diff] [blame] | 226 | static void def_handler(int sig) |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 227 | { |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 228 | /* |
vapier | 6daba92 | 2009-08-28 09:29:01 +0000 | [diff] [blame] | 229 | * Break remaining test cases, do any cleanup, then exit |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 230 | */ |
Garrett Cooper | 6a464fe | 2010-12-18 06:32:42 -0800 | [diff] [blame] | 231 | tst_brkm(TBROK, T_cleanup, |
Xiaoguang Wang | 1a6f68a | 2014-05-13 19:02:59 +0800 | [diff] [blame] | 232 | "unexpected signal %s(%d) received (pid = %d).", |
| 233 | tst_strsig(sig), sig, getpid()); |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 234 | } |
alaffin | 3c8699d | 2000-09-07 14:34:44 +0000 | [diff] [blame] | 235 | |
| 236 | /* |
| 237 | * tst_setup_signal - A function like signal(), but we have |
| 238 | * control over its personality. |
| 239 | */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 240 | static void (*tst_setup_signal(int sig, void (*handler) (int))) (int) { |
vapier | 6daba92 | 2009-08-28 09:29:01 +0000 | [diff] [blame] | 241 | struct sigaction my_act, old_act; |
| 242 | int ret; |
alaffin | 3c8699d | 2000-09-07 14:34:44 +0000 | [diff] [blame] | 243 | |
vapier | 6daba92 | 2009-08-28 09:29:01 +0000 | [diff] [blame] | 244 | my_act.sa_handler = handler; |
| 245 | my_act.sa_flags = SA_RESTART; |
| 246 | sigemptyset(&my_act.sa_mask); |
alaffin | 3c8699d | 2000-09-07 14:34:44 +0000 | [diff] [blame] | 247 | |
vapier | 6daba92 | 2009-08-28 09:29:01 +0000 | [diff] [blame] | 248 | ret = sigaction(sig, &my_act, &old_act); |
alaffin | 3c8699d | 2000-09-07 14:34:44 +0000 | [diff] [blame] | 249 | |
vapier | 6daba92 | 2009-08-28 09:29:01 +0000 | [diff] [blame] | 250 | if (ret == 0) |
| 251 | return old_act.sa_handler; |
| 252 | else |
| 253 | return SIG_ERR; |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 254 | } |