plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright (c) International Business Machines Corp., 2001 |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 13 | * the GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
Wanlong Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | /* |
| 21 | * NAME |
nstraz | fa31d55 | 2002-05-14 16:50:06 +0000 | [diff] [blame] | 22 | * signal05.c |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 23 | * |
| 24 | * DESCRIPTION |
nstraz | fa31d55 | 2002-05-14 16:50:06 +0000 | [diff] [blame] | 25 | * signal05 - set the signal handler to our own function |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 26 | * |
| 27 | * ALGORITHM |
| 28 | * loop if that option was specified |
| 29 | * issue the system call |
| 30 | * check the return value |
| 31 | * if return value == -1 |
| 32 | * issue a FAIL message, break remaining tests and cleanup |
| 33 | * if we are doing functional testing |
| 34 | * send the signal with kill() |
| 35 | * if we catch the signal in the signal handler |
| 36 | * issue a PASS message |
| 37 | * else |
| 38 | * issue a FAIL message |
| 39 | * call cleanup |
| 40 | * |
| 41 | * USAGE: <for command-line> |
nstraz | fa31d55 | 2002-05-14 16:50:06 +0000 | [diff] [blame] | 42 | * signal05 [-c n] [-f] [-i n] [-I x] [-p x] [-t] |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 43 | * where, -c n : Run n copies concurrently. |
| 44 | * -f : Turn off functionality Testing. |
| 45 | * -i n : Execute test n times. |
| 46 | * -I x : Execute test for x seconds. |
| 47 | * -P x : Pause for x seconds between iterations. |
| 48 | * -t : Turn on syscall timing. |
| 49 | * |
| 50 | * History |
| 51 | * 07/2001 John George |
| 52 | * -Ported |
| 53 | * |
| 54 | * Restrictions |
| 55 | * none |
| 56 | */ |
| 57 | |
| 58 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 59 | |
| 60 | #include <errno.h> |
| 61 | #include <signal.h> |
| 62 | |
| 63 | void cleanup(void); |
| 64 | void setup(void); |
| 65 | void sighandler(int); |
| 66 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 67 | int siglist[] = { SIGHUP, SIGINT, SIGQUIT, SIGILL, SIGTRAP, SIGABRT, SIGIOT, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 68 | SIGBUS, SIGFPE, SIGUSR1, SIGSEGV, SIGUSR2, SIGPIPE, SIGALRM, |
| 69 | SIGTERM, |
plars | 067f384 | 2002-05-28 14:45:38 +0000 | [diff] [blame] | 70 | #ifdef SIGSTKFLT |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 71 | SIGSTKFLT, |
plars | 067f384 | 2002-05-28 14:45:38 +0000 | [diff] [blame] | 72 | #endif |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 73 | SIGCHLD, SIGCONT, SIGTSTP, SIGTTIN, |
| 74 | SIGTTOU, SIGURG, SIGXCPU, SIGXFSZ, SIGVTALRM, SIGPROF, |
| 75 | SIGWINCH, SIGIO, SIGPWR, SIGSYS, |
plars | 067f384 | 2002-05-28 14:45:38 +0000 | [diff] [blame] | 76 | #ifdef SIGUNUSED |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 77 | SIGUNUSED |
plars | 067f384 | 2002-05-28 14:45:38 +0000 | [diff] [blame] | 78 | #endif |
| 79 | }; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 80 | |
Cyril Hrubis | 63564ce | 2015-02-04 13:28:31 +0100 | [diff] [blame] | 81 | char *TCID = "signal05"; |
| 82 | int TST_TOTAL = ARRAY_SIZE(siglist); |
| 83 | |
| 84 | typedef void (*sighandler_t) (int); |
| 85 | |
| 86 | sighandler_t Tret; |
| 87 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 88 | int pass = 0; |
| 89 | |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 90 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 91 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 92 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 93 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 94 | pid_t pid; |
| 95 | int i, rval; |
| 96 | |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 97 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) { |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 98 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 99 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 100 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 101 | setup(); /* global setup */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 102 | |
| 103 | /* The following loop checks looping state if -i option given */ |
| 104 | |
| 105 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 106 | /* reset tst_count in case we are looping */ |
| 107 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 108 | |
| 109 | /* |
| 110 | * loop through the list of signals and test each one |
| 111 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 112 | for (i = 0; i < TST_TOTAL; i++) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 113 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 114 | errno = 0; |
| 115 | Tret = signal(siglist[i], &sighandler); |
| 116 | TEST_ERRNO = errno; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 117 | |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 118 | if (Tret == SIG_ERR) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 119 | tst_resm(TFAIL, "%s call failed - errno = %d " |
| 120 | ": %s", TCID, TEST_ERRNO, |
| 121 | strerror(TEST_ERRNO)); |
| 122 | continue; |
| 123 | } |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 124 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 125 | /* |
| 126 | * Send the signals and make sure they are |
| 127 | * handled in our handler. |
| 128 | */ |
| 129 | pid = getpid(); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 130 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 131 | if ((rval = kill(pid, siglist[i])) != 0) { |
| 132 | tst_brkm(TBROK, cleanup, |
| 133 | "call to kill failed"); |
| 134 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 135 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 136 | if (siglist[i] == pass) { |
| 137 | tst_resm(TPASS, |
| 138 | "%s call succeeded", TCID); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 139 | } else { |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 140 | tst_resm(TFAIL, |
| 141 | "received unexpected signal"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | cleanup(); |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 147 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | /* |
| 151 | * sighandler() - handle the signals |
| 152 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 153 | void sighandler(int sig) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 154 | { |
| 155 | /* set the global pass variable = sig */ |
| 156 | pass = sig; |
| 157 | } |
| 158 | |
| 159 | /* |
| 160 | * setup() - performs all the ONE TIME setup for this test. |
| 161 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 162 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 163 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 164 | TEST_PAUSE; |
| 165 | } |
| 166 | |
| 167 | /* |
| 168 | * cleanup() - performs all the ONE TIME cleanup for this test at completion |
| 169 | * or premature exit. |
| 170 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 171 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 172 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 173 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 174 | } |