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 | /* |
nstraz | fa31d55 | 2002-05-14 16:50:06 +0000 | [diff] [blame] | 21 | * Test Name: pause03 |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 22 | * |
| 23 | * Test Description: |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 24 | * Verify that a process is no longer accessible on receipt of SIGKILL |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 25 | * signal after being suspended by pause(). |
| 26 | * |
| 27 | * Expected Result: |
| 28 | * pause() does not return due to receipt of SIGKILL signal and specified |
| 29 | * process should be terminated. |
| 30 | * |
| 31 | * Algorithm: |
| 32 | * Setup: |
| 33 | * Setup signal handling. |
| 34 | * Pause for SIGUSR1 if option specified. |
| 35 | * |
| 36 | * Test: |
| 37 | * Loop if the proper options are given. |
| 38 | * Execute system call |
| 39 | * Check return code, if system call failed (return=-1) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 40 | * Log the errno and Issue a FAIL message. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 41 | * Otherwise, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 42 | * Verify the Functionality of system call |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 43 | * if successful, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 44 | * Issue Functionality-Pass message. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 45 | * Otherwise, |
| 46 | * Issue Functionality-Fail message. |
| 47 | * Cleanup: |
| 48 | * Print errno log and/or timing stats if options given |
| 49 | * |
| 50 | * Usage: <for command-line> |
nstraz | fa31d55 | 2002-05-14 16:50:06 +0000 | [diff] [blame] | 51 | * pause03 [-c n] [-i n] [-I x] [-P x] [-t] |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 52 | * where, -c n : Run n copies concurrently. |
| 53 | * -i n : Execute test n times. |
| 54 | * -I x : Execute test for x seconds. |
| 55 | * -P x : Pause for x seconds between iterations. |
| 56 | * -t : Turn on syscall timing. |
| 57 | * |
| 58 | * HISTORY |
| 59 | * 07/2001 Ported by Wayne Boyer |
| 60 | * |
| 61 | * RESTRICTIONS: |
| 62 | * None. |
| 63 | * |
| 64 | */ |
| 65 | #include <unistd.h> |
| 66 | #include <errno.h> |
| 67 | #include <fcntl.h> |
| 68 | #include <wait.h> |
| 69 | |
| 70 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 71 | |
| 72 | int cflag; /* flag to indicate child process status */ |
| 73 | pid_t cpid; /* child process id */ |
| 74 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 75 | char *TCID = "pause03"; |
| 76 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 77 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 78 | void do_child(); /* Function to run in child process */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 79 | void setup(); /* Main setup function of test */ |
| 80 | void cleanup(); /* cleanup function for the test */ |
| 81 | void sig_handle(int sig); /* signal handler for SIGCLD */ |
| 82 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 83 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 84 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 85 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 86 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 87 | int status; /* child process exit status */ |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 88 | int ret_val; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 89 | |
Garrett Cooper | a9e49f1 | 2010-12-16 10:54:03 -0800 | [diff] [blame] | 90 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 91 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 92 | #ifdef UCLINUX |
| 93 | maybe_run_child(&do_child, ""); |
| 94 | #endif |
| 95 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 96 | setup(); |
| 97 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 98 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 99 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 100 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 101 | |
| 102 | /* Creat a new process using fork() */ |
vapier | fed9da3 | 2006-02-19 09:25:08 +0000 | [diff] [blame] | 103 | if ((cpid = FORK_OR_VFORK()) == -1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 104 | tst_brkm(TBROK, cleanup, "fork() failed"); |
| 105 | } |
| 106 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 107 | if (cpid == 0) { /* Child process */ |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 108 | #ifdef UCLINUX |
| 109 | if (self_exec(av[0], "") < 0) { |
| 110 | tst_brkm(TBROK, cleanup, "self_exec failed"); |
| 111 | } |
| 112 | #else |
| 113 | do_child(); |
| 114 | #endif |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | /* Parent process */ |
| 118 | /* sleep to ensure the child executes */ |
| 119 | sleep(1); |
| 120 | |
| 121 | /* Check for the value of cflag */ |
| 122 | if (cflag == 1) { |
| 123 | /* |
| 124 | * Indicates that child terminated |
| 125 | * before receipt of SIGKILL signal. |
| 126 | */ |
| 127 | tst_brkm(TFAIL, cleanup, |
| 128 | "Child exited before SIGKILL signal"); |
| 129 | } |
| 130 | |
| 131 | /* Send the SIGKILL signal now */ |
| 132 | kill(cpid, SIGKILL); |
| 133 | |
| 134 | /* sleep to ensure the signal sent is effected */ |
| 135 | sleep(1); |
| 136 | |
| 137 | /* Verify again the value of cflag */ |
| 138 | if (cflag == 0) { |
| 139 | /* Child still exists */ |
| 140 | tst_resm(TFAIL, "Child still exists, " |
| 141 | "pause() still active"); |
| 142 | cleanup(); |
| 143 | } |
| 144 | |
| 145 | ret_val = wait(&status); |
| 146 | |
| 147 | /* |
| 148 | * Verify that wait returned after child process termination |
| 149 | * due to receipt of SIGKILL signal. |
| 150 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 151 | if (WTERMSIG(status) == SIGKILL) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 152 | ret_val = wait(&status); |
| 153 | if ((ret_val == -1) && (errno == ECHILD)) { |
| 154 | /* |
| 155 | * Child is no longer accessible and pause() |
| 156 | * functionality is successful. |
| 157 | */ |
| 158 | tst_resm(TPASS, "Functionality of " |
| 159 | "pause() is correct"); |
| 160 | } else { |
| 161 | tst_resm(TFAIL, "wait() failed due to " |
vapier | 356d38a | 2009-08-28 13:36:21 +0000 | [diff] [blame] | 162 | "unkown reason, ret_val=%d, " |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 163 | "errno=%d", ret_val, errno); |
| 164 | } |
| 165 | } else { |
| 166 | tst_resm(TFAIL, "Child terminated not due to " |
| 167 | "SIGKILL, errno = %d", errno); |
| 168 | } |
| 169 | |
| 170 | /* reset cflag in case we are looping */ |
| 171 | cflag = 0; |
| 172 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 173 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 174 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 175 | cleanup(); |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 176 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 177 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 178 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 179 | |
| 180 | /* |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 181 | * do_child() |
| 182 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 183 | void do_child(void) |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 184 | { |
| 185 | /* Suspend the child using pause() */ |
| 186 | TEST(pause()); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 187 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 188 | /* print the message if pause() returned */ |
| 189 | tst_resm(TFAIL, "Unexpected return from pause()"); |
| 190 | /* Loop infinitely */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 191 | while (1) ; |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 195 | * setup() - performs all ONE TIME setup for this test. |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 196 | * Setup signal handler to catch SIGCLD signal. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 197 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 198 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 199 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 200 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 201 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 202 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 203 | TEST_PAUSE; |
| 204 | |
| 205 | /* Initialise cflag */ |
| 206 | cflag = 0; |
| 207 | |
| 208 | /* Catch SIGCLD */ |
| 209 | signal(SIGCLD, sig_handle); |
| 210 | } |
| 211 | |
| 212 | /* |
| 213 | * sig_handle(int sig) |
| 214 | * This is the signal handler to handle the SIGCLD signal. |
| 215 | * When the child terminates and the parent gets the SIGCLD signal |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 216 | * the handler gets executed and then the cflag variable is set to |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 217 | * indicate the child has terminated. |
| 218 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 219 | void sig_handle(int sig) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 220 | { |
| 221 | /* Set the cflag variable */ |
| 222 | cflag = 1; |
| 223 | } |
| 224 | |
| 225 | /* |
| 226 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 227 | * completion or premature exit. |
| 228 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 229 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 230 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 231 | |
| 232 | /* Cleanup the child if still active */ |
| 233 | kill(cpid, SIGKILL); |
| 234 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 235 | } |