alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved. |
| 3 | * |
| 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. |
| 7 | * |
| 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. |
| 11 | * |
| 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. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License along |
| 20 | * with this program; if not, write the Free Software Foundation, Inc., 59 |
| 21 | * Temple Place - Suite 330, Boston MA 02111-1307, USA. |
| 22 | * |
| 23 | * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy, |
| 24 | * Mountain View, CA 94043, or: |
| 25 | * |
| 26 | * http://www.sgi.com |
| 27 | * |
| 28 | * For further information regarding this notice, see: |
| 29 | * |
| 30 | * http://oss.sgi.com/projects/GenInfo/NoticeExplan/ |
| 31 | */ |
| 32 | |
robbiew | df45043 | 2003-04-28 21:37:40 +0000 | [diff] [blame] | 33 | /* $Id: tst_sig.c,v 1.6 2003/04/28 21:37:40 robbiew 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 |
| 52 | SIGCLD as an expected signal if fork() is being used. |
| 53 | |
| 54 | handler - a pointer to the unexpected signal handler to |
| 55 | be executed after an unexpected signal has been |
| 56 | detected. If handler is set to DEF_HANDLER, a |
| 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 |
| 64 | is set so that other user-defined handlers have |
| 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> |
| 73 | #include "test.h" |
| 74 | |
robbiew | df45043 | 2003-04-28 21:37:40 +0000 | [diff] [blame] | 75 | #ifdef USE_NPTL |
| 76 | #define SIGCANCEL 32 |
| 77 | #define SIGTIMER 33 |
| 78 | #endif |
| 79 | |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 80 | #define MAXMESG 150 /* size of mesg string sent to tst_res */ |
| 81 | |
| 82 | void (*T_cleanup)(); /* pointer to cleanup function */ |
| 83 | |
nstraz | d852201 | 2001-06-01 19:46:13 +0000 | [diff] [blame] | 84 | /**************************************************************************** |
| 85 | * STD_COPIES is defined in parse_opts.c but is externed here in order to |
| 86 | * test whether SIGCHILD should be ignored or not. |
| 87 | ***************************************************************************/ |
| 88 | extern int STD_COPIES; |
| 89 | |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 90 | static void def_handler(); /* default signal handler */ |
alaffin | 3c8699d | 2000-09-07 14:34:44 +0000 | [diff] [blame] | 91 | static void (*tst_setup_signal( int, void (*)(int)))(int); |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 92 | |
| 93 | /**************************************************************************** |
| 94 | * tst_sig() : set-up to catch unexpected signals. fork_flag is set to NOFORK |
| 95 | * if SIGCLD is to be an "unexpected signal", otherwise it is set to |
| 96 | * FORK. cleanup points to a cleanup routine to be executed before |
| 97 | * tst_exit is called (cleanup is set to NULL if no cleanup is desired). |
| 98 | * handler is a pointer to the signal handling routine (if handler is |
| 99 | * set to NULL, a default handler is used). |
| 100 | ***************************************************************************/ |
| 101 | |
| 102 | void |
nstraz | 9418108 | 2000-08-30 18:43:38 +0000 | [diff] [blame] | 103 | tst_sig(int fork_flag, void (*handler)(), void (*cleanup)()) |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 104 | { |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 105 | char mesg[MAXMESG]; /* message buffer for tst_res */ |
| 106 | int sig; |
| 107 | |
| 108 | /* |
| 109 | * save T_cleanup and handler function pointers |
| 110 | */ |
| 111 | T_cleanup = cleanup; /* used by default handler */ |
| 112 | |
| 113 | if (handler == DEF_HANDLER) { |
| 114 | /* use default handler */ |
| 115 | handler = def_handler; |
| 116 | } |
robbiew | df45043 | 2003-04-28 21:37:40 +0000 | [diff] [blame] | 117 | |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 118 | /* |
| 119 | * now loop through all signals and set the handlers |
| 120 | */ |
| 121 | |
| 122 | for (sig = 1; sig < NSIG; sig++) { |
| 123 | /* |
| 124 | * SIGKILL is never unexpected. |
| 125 | * SIGCLD is only unexpected when |
| 126 | * no forking is being done. |
| 127 | * SIGINFO is used for file quotas and should be expected |
| 128 | */ |
| 129 | |
| 130 | switch (sig) { |
| 131 | case SIGKILL: |
| 132 | case SIGSTOP: |
| 133 | case SIGCONT: |
robbiew | df45043 | 2003-04-28 21:37:40 +0000 | [diff] [blame] | 134 | #ifdef USE_NPTL |
| 135 | case SIGCANCEL: |
| 136 | case SIGTIMER: |
| 137 | #endif |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 138 | #ifdef CRAY |
| 139 | case SIGINFO: |
| 140 | case SIGRECOVERY: /* allow chkpnt/restart */ |
| 141 | #endif /* CRAY */ |
| 142 | |
| 143 | #ifdef SIGSWAP |
| 144 | case SIGSWAP: |
| 145 | #endif /* SIGSWAP */ |
| 146 | |
| 147 | #ifdef SIGCKPT |
| 148 | case SIGCKPT: |
| 149 | #endif |
| 150 | #ifdef SIGRESTART |
| 151 | case SIGRESTART: |
| 152 | #endif |
| 153 | /* |
| 154 | * pthread-private signals SIGPTINTR and SIGPTRESCHED. |
| 155 | * Setting a handler for these signals is disallowed when |
| 156 | * the binary is linked against libpthread. |
| 157 | */ |
| 158 | #ifdef SIGPTINTR |
| 159 | case SIGPTINTR: |
| 160 | #endif /* SIGPTINTR */ |
| 161 | #ifdef SIGPTRESCHED |
| 162 | case SIGPTRESCHED: |
| 163 | #endif /* SIGPTRESCHED */ |
| 164 | |
| 165 | break; |
| 166 | |
| 167 | case SIGCLD: |
nstraz | d852201 | 2001-06-01 19:46:13 +0000 | [diff] [blame] | 168 | if ( fork_flag == FORK || STD_COPIES > 1) |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 169 | continue; |
| 170 | |
| 171 | default: |
alaffin | 3c8699d | 2000-09-07 14:34:44 +0000 | [diff] [blame] | 172 | if (tst_setup_signal(sig, handler) == SIG_ERR) { |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 173 | (void) sprintf(mesg, |
| 174 | "signal() failed for signal %d. error:%d %s.", |
| 175 | sig, errno, strerror(errno)); |
| 176 | tst_resm(TWARN, mesg); |
| 177 | } |
| 178 | break; |
| 179 | } |
| 180 | #ifdef __sgi |
| 181 | /* On irix (07/96), signal() fails when signo is 33 or higher */ |
| 182 | if ( sig+1 >= 33 ) |
| 183 | break; |
| 184 | #endif /* __sgi */ |
| 185 | |
| 186 | } /* endfor */ |
| 187 | } |
| 188 | |
| 189 | |
| 190 | |
| 191 | /**************************************************************************** |
| 192 | * def_handler() : default signal handler that is invoked when |
| 193 | * an unexpected signal is caught. |
| 194 | ***************************************************************************/ |
| 195 | |
| 196 | static void |
nstraz | 9418108 | 2000-08-30 18:43:38 +0000 | [diff] [blame] | 197 | def_handler(int sig) |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 198 | { |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 199 | |
| 200 | /* |
| 201 | * Break remaining test cases, do any cleanup, then exit |
| 202 | */ |
alaffin | 3c8699d | 2000-09-07 14:34:44 +0000 | [diff] [blame] | 203 | tst_brkm(TBROK, 0, "Unexpected signal %d received.", sig); |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 204 | |
| 205 | /* now cleanup and exit */ |
| 206 | if (T_cleanup) { |
| 207 | (*T_cleanup)(); |
| 208 | } |
| 209 | |
| 210 | tst_exit(); |
| 211 | } |
alaffin | 3c8699d | 2000-09-07 14:34:44 +0000 | [diff] [blame] | 212 | |
| 213 | /* |
| 214 | * tst_setup_signal - A function like signal(), but we have |
| 215 | * control over its personality. |
| 216 | */ |
| 217 | static void (*tst_setup_signal( int sig, void (*handler)(int)))(int) |
| 218 | { |
| 219 | struct sigaction my_act,old_act; |
| 220 | int ret; |
| 221 | |
| 222 | my_act.sa_handler = handler; |
| 223 | my_act.sa_flags = SA_RESTART; |
| 224 | sigemptyset(&my_act.sa_mask); |
| 225 | |
| 226 | ret = sigaction(sig, &my_act, &old_act); |
| 227 | |
| 228 | if ( ret == 0 ) |
| 229 | return( old_act.sa_handler ); |
| 230 | else |
| 231 | return( SIG_ERR ); |
| 232 | } |
| 233 | |