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 |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 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" |
| 71 | #include "usctest.h" |
| 72 | |
| 73 | int cflag; /* flag to indicate child process status */ |
| 74 | pid_t cpid; /* child process id */ |
| 75 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 76 | char *TCID = "pause03"; /* Test program identifier. */ |
| 77 | int TST_TOTAL = 1; /* Total number of test cases. */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 78 | extern int Tst_count; /* Test Case counter for tst_* routines */ |
| 79 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 80 | void do_child(); /* Function to run in child process */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 81 | void setup(); /* Main setup function of test */ |
| 82 | void cleanup(); /* cleanup function for the test */ |
| 83 | void sig_handle(int sig); /* signal handler for SIGCLD */ |
| 84 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 85 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 86 | { |
| 87 | int lc; /* loop counter */ |
| 88 | char *msg; /* message returned from parse_opts */ |
| 89 | int status; /* child process exit status */ |
| 90 | int ret_val; /* return value for wait() */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 91 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 92 | /* Parse standard options given to run the test. */ |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 93 | msg = parse_opts(ac, av, NULL, NULL); |
| 94 | if (msg != NULL) { |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame^] | 95 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 96 | } |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 97 | #ifdef UCLINUX |
| 98 | maybe_run_child(&do_child, ""); |
| 99 | #endif |
| 100 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 101 | /* Perform global setup for test */ |
| 102 | setup(); |
| 103 | |
| 104 | /* Check looping state if -i option given */ |
| 105 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 106 | |
| 107 | /* Reset Tst_count in case we are looping. */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 108 | Tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 109 | |
| 110 | /* Creat a new process using fork() */ |
vapier | fed9da3 | 2006-02-19 09:25:08 +0000 | [diff] [blame] | 111 | if ((cpid = FORK_OR_VFORK()) == -1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 112 | tst_brkm(TBROK, cleanup, "fork() failed"); |
| 113 | } |
| 114 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 115 | if (cpid == 0) { /* Child process */ |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 116 | #ifdef UCLINUX |
| 117 | if (self_exec(av[0], "") < 0) { |
| 118 | tst_brkm(TBROK, cleanup, "self_exec failed"); |
| 119 | } |
| 120 | #else |
| 121 | do_child(); |
| 122 | #endif |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | /* Parent process */ |
| 126 | /* sleep to ensure the child executes */ |
| 127 | sleep(1); |
| 128 | |
| 129 | /* Check for the value of cflag */ |
| 130 | if (cflag == 1) { |
| 131 | /* |
| 132 | * Indicates that child terminated |
| 133 | * before receipt of SIGKILL signal. |
| 134 | */ |
| 135 | tst_brkm(TFAIL, cleanup, |
| 136 | "Child exited before SIGKILL signal"); |
| 137 | } |
| 138 | |
| 139 | /* Send the SIGKILL signal now */ |
| 140 | kill(cpid, SIGKILL); |
| 141 | |
| 142 | /* sleep to ensure the signal sent is effected */ |
| 143 | sleep(1); |
| 144 | |
| 145 | /* Verify again the value of cflag */ |
| 146 | if (cflag == 0) { |
| 147 | /* Child still exists */ |
| 148 | tst_resm(TFAIL, "Child still exists, " |
| 149 | "pause() still active"); |
| 150 | cleanup(); |
| 151 | } |
| 152 | |
| 153 | ret_val = wait(&status); |
| 154 | |
| 155 | /* |
| 156 | * Verify that wait returned after child process termination |
| 157 | * due to receipt of SIGKILL signal. |
| 158 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 159 | if (WTERMSIG(status) == SIGKILL) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 160 | ret_val = wait(&status); |
| 161 | if ((ret_val == -1) && (errno == ECHILD)) { |
| 162 | /* |
| 163 | * Child is no longer accessible and pause() |
| 164 | * functionality is successful. |
| 165 | */ |
| 166 | tst_resm(TPASS, "Functionality of " |
| 167 | "pause() is correct"); |
| 168 | } else { |
| 169 | tst_resm(TFAIL, "wait() failed due to " |
vapier | 356d38a | 2009-08-28 13:36:21 +0000 | [diff] [blame] | 170 | "unkown reason, ret_val=%d, " |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 171 | "errno=%d", ret_val, errno); |
| 172 | } |
| 173 | } else { |
| 174 | tst_resm(TFAIL, "Child terminated not due to " |
| 175 | "SIGKILL, errno = %d", errno); |
| 176 | } |
| 177 | |
| 178 | /* reset cflag in case we are looping */ |
| 179 | cflag = 0; |
| 180 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 181 | } /* End for TEST_LOOPING */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 182 | |
| 183 | /* Call cleanup() to undo setup done for the test. */ |
| 184 | cleanup(); |
| 185 | |
subrata_modak | 43337a3 | 2009-02-26 11:43:51 +0000 | [diff] [blame] | 186 | return 0; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 187 | } /* End main */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 188 | |
| 189 | /* |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 190 | * do_child() |
| 191 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 192 | void do_child() |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 193 | { |
| 194 | /* Suspend the child using pause() */ |
| 195 | TEST(pause()); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 196 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 197 | /* print the message if pause() returned */ |
| 198 | tst_resm(TFAIL, "Unexpected return from pause()"); |
| 199 | /* Loop infinitely */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 200 | while (1) ; |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 204 | * setup() - performs all ONE TIME setup for this test. |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 205 | * Setup signal handler to catch SIGCLD signal. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 206 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 207 | void setup() |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 208 | { |
| 209 | /* capture signals */ |
| 210 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 211 | |
| 212 | /* Pause if that option was specified */ |
| 213 | TEST_PAUSE; |
| 214 | |
| 215 | /* Initialise cflag */ |
| 216 | cflag = 0; |
| 217 | |
| 218 | /* Catch SIGCLD */ |
| 219 | signal(SIGCLD, sig_handle); |
| 220 | } |
| 221 | |
| 222 | /* |
| 223 | * sig_handle(int sig) |
| 224 | * This is the signal handler to handle the SIGCLD signal. |
| 225 | * When the child terminates and the parent gets the SIGCLD signal |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 226 | * the handler gets executed and then the cflag variable is set to |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 227 | * indicate the child has terminated. |
| 228 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 229 | void sig_handle(int sig) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 230 | { |
| 231 | /* Set the cflag variable */ |
| 232 | cflag = 1; |
| 233 | } |
| 234 | |
| 235 | /* |
| 236 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 237 | * completion or premature exit. |
| 238 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 239 | void cleanup() |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 240 | { |
| 241 | /* |
| 242 | * print timing stats if that option was specified. |
| 243 | * print errno log if that option was specified. |
| 244 | */ |
| 245 | TEST_CLEANUP; |
| 246 | |
| 247 | /* Cleanup the child if still active */ |
| 248 | kill(cpid, SIGKILL); |
| 249 | |
| 250 | /* exit with return code appropriate for results */ |
| 251 | tst_exit(); |
| 252 | } |