1f08c3bdfSopenharmony_ci/* 2f08c3bdfSopenharmony_ci * 3f08c3bdfSopenharmony_ci * Copyright (c) International Business Machines Corp., 2001 4f08c3bdfSopenharmony_ci * 5f08c3bdfSopenharmony_ci * This program is free software; you can redistribute it and/or modify 6f08c3bdfSopenharmony_ci * it under the terms of the GNU General Public License as published by 7f08c3bdfSopenharmony_ci * the Free Software Foundation; either version 2 of the License, or 8f08c3bdfSopenharmony_ci * (at your option) any later version. 9f08c3bdfSopenharmony_ci * 10f08c3bdfSopenharmony_ci * This program is distributed in the hope that it will be useful, 11f08c3bdfSopenharmony_ci * but WITHOUT ANY WARRANTY; without even the implied warranty of 12f08c3bdfSopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See 13f08c3bdfSopenharmony_ci * the GNU General Public License for more details. 14f08c3bdfSopenharmony_ci * 15f08c3bdfSopenharmony_ci * You should have received a copy of the GNU General Public License 16f08c3bdfSopenharmony_ci * along with this program; if not, write to the Free Software 17f08c3bdfSopenharmony_ci * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18f08c3bdfSopenharmony_ci */ 19f08c3bdfSopenharmony_ci 20f08c3bdfSopenharmony_ci/* 21f08c3bdfSopenharmony_ci * Test Name: vfork02 22f08c3bdfSopenharmony_ci * 23f08c3bdfSopenharmony_ci * Test Description: 24f08c3bdfSopenharmony_ci * Fork a process using vfork() and verify that, the pending signals in 25f08c3bdfSopenharmony_ci * the parent are not pending in the child process. 26f08c3bdfSopenharmony_ci * $ 27f08c3bdfSopenharmony_ci * Expected Result: 28f08c3bdfSopenharmony_ci * The signal which is pending in the parent should not be pending in the 29f08c3bdfSopenharmony_ci * child process. 30f08c3bdfSopenharmony_ci * 31f08c3bdfSopenharmony_ci * Algorithm: 32f08c3bdfSopenharmony_ci * Setup: 33f08c3bdfSopenharmony_ci * Setup signal handling. 34f08c3bdfSopenharmony_ci * Pause for SIGUSR1 if option specified. 35f08c3bdfSopenharmony_ci * 36f08c3bdfSopenharmony_ci * Test: 37f08c3bdfSopenharmony_ci * Loop if the proper options are given. 38f08c3bdfSopenharmony_ci * Execute system call 39f08c3bdfSopenharmony_ci * Check return code, if system call failed (return=-1) 40f08c3bdfSopenharmony_ci * Log the errno and Issue a FAIL message. 41f08c3bdfSopenharmony_ci * Otherwise, 42f08c3bdfSopenharmony_ci * Verify the Functionality of system call 43f08c3bdfSopenharmony_ci * if successful, 44f08c3bdfSopenharmony_ci * Issue Functionality-Pass message. 45f08c3bdfSopenharmony_ci * Otherwise, 46f08c3bdfSopenharmony_ci * Issue Functionality-Fail message. 47f08c3bdfSopenharmony_ci * Cleanup: 48f08c3bdfSopenharmony_ci * Print errno log and/or timing stats if options given 49f08c3bdfSopenharmony_ci * 50f08c3bdfSopenharmony_ci * Usage: <for command-line> 51f08c3bdfSopenharmony_ci * vfork02 [-c n] [-e] [-f] [-i n] [-I x] [-p x] [-t] 52f08c3bdfSopenharmony_ci * where, -c n : Run n copies concurrently. 53f08c3bdfSopenharmony_ci * -e : Turn on errno logging. 54f08c3bdfSopenharmony_ci * -f : Turn off functionality Testing. 55f08c3bdfSopenharmony_ci * -i n : Execute test n times. 56f08c3bdfSopenharmony_ci * -I x : Execute test for x seconds. 57f08c3bdfSopenharmony_ci * -P x : Pause for x seconds between iterations. 58f08c3bdfSopenharmony_ci * -t : Turn on syscall timing. 59f08c3bdfSopenharmony_ci * 60f08c3bdfSopenharmony_ci * History 61f08c3bdfSopenharmony_ci * 07/2001 John George 62f08c3bdfSopenharmony_ci * -Ported 63f08c3bdfSopenharmony_ci * 64f08c3bdfSopenharmony_ci * Restrictions: 65f08c3bdfSopenharmony_ci * None. 66f08c3bdfSopenharmony_ci * 67f08c3bdfSopenharmony_ci */ 68f08c3bdfSopenharmony_ci#define _GNU_SOURCE 1 69f08c3bdfSopenharmony_ci 70f08c3bdfSopenharmony_ci#include <stdio.h> 71f08c3bdfSopenharmony_ci#include <sys/types.h> 72f08c3bdfSopenharmony_ci#include <errno.h> 73f08c3bdfSopenharmony_ci#include <unistd.h> 74f08c3bdfSopenharmony_ci#include <fcntl.h> 75f08c3bdfSopenharmony_ci#include <string.h> 76f08c3bdfSopenharmony_ci#include <signal.h> 77f08c3bdfSopenharmony_ci#include <sys/stat.h> 78f08c3bdfSopenharmony_ci#include <sys/wait.h> 79f08c3bdfSopenharmony_ci 80f08c3bdfSopenharmony_ci#include "test.h" 81f08c3bdfSopenharmony_ci#include "safe_macros.h" 82f08c3bdfSopenharmony_ci 83f08c3bdfSopenharmony_cichar *TCID = "vfork02"; 84f08c3bdfSopenharmony_ciint TST_TOTAL = 1; 85f08c3bdfSopenharmony_ci 86f08c3bdfSopenharmony_civoid setup(); /* Main setup function of test */ 87f08c3bdfSopenharmony_civoid cleanup(); /* cleanup function for the test */ 88f08c3bdfSopenharmony_civoid sig_handler(); /* signal catching function */ 89f08c3bdfSopenharmony_ci 90f08c3bdfSopenharmony_ciint main(int ac, char **av) 91f08c3bdfSopenharmony_ci{ 92f08c3bdfSopenharmony_ci int lc; 93f08c3bdfSopenharmony_ci pid_t cpid; /* process id of the child process */ 94f08c3bdfSopenharmony_ci int exit_status; /* exit status of child process */ 95f08c3bdfSopenharmony_ci sigset_t PendSig; /* variable to hold pending signal */ 96f08c3bdfSopenharmony_ci 97f08c3bdfSopenharmony_ci tst_parse_opts(ac, av, NULL, NULL); 98f08c3bdfSopenharmony_ci 99f08c3bdfSopenharmony_ci setup(); 100f08c3bdfSopenharmony_ci 101f08c3bdfSopenharmony_ci for (lc = 0; TEST_LOOPING(lc); lc++) { 102f08c3bdfSopenharmony_ci 103f08c3bdfSopenharmony_ci tst_count = 0; 104f08c3bdfSopenharmony_ci 105f08c3bdfSopenharmony_ci /* 106f08c3bdfSopenharmony_ci * Call vfork(2) to create a child process without 107f08c3bdfSopenharmony_ci * fully copying the address space of parent. 108f08c3bdfSopenharmony_ci */ 109f08c3bdfSopenharmony_ci TEST(vfork()); 110f08c3bdfSopenharmony_ci 111f08c3bdfSopenharmony_ci if ((cpid = TEST_RETURN) == -1) { 112f08c3bdfSopenharmony_ci tst_resm(TFAIL, "vfork() Failed, errno=%d : %s", 113f08c3bdfSopenharmony_ci TEST_ERRNO, strerror(TEST_ERRNO)); 114f08c3bdfSopenharmony_ci } else if (cpid == 0) { /* Child process */ 115f08c3bdfSopenharmony_ci /* 116f08c3bdfSopenharmony_ci * Check whether the pending signal SIGUSR1 117f08c3bdfSopenharmony_ci * in the parent is also pending in the child 118f08c3bdfSopenharmony_ci * process by storing it in a variable. 119f08c3bdfSopenharmony_ci */ 120f08c3bdfSopenharmony_ci if (sigpending(&PendSig) == -1) { 121f08c3bdfSopenharmony_ci tst_resm(TFAIL, "sigpending function " 122f08c3bdfSopenharmony_ci "failed in child"); 123f08c3bdfSopenharmony_ci _exit(1); 124f08c3bdfSopenharmony_ci } 125f08c3bdfSopenharmony_ci 126f08c3bdfSopenharmony_ci /* Check if SIGUSR1 is pending in child */ 127f08c3bdfSopenharmony_ci if (sigismember(&PendSig, SIGUSR1) != 0) { 128f08c3bdfSopenharmony_ci tst_resm(TFAIL, "SIGUSR1 also pending " 129f08c3bdfSopenharmony_ci "in child process"); 130f08c3bdfSopenharmony_ci _exit(1); 131f08c3bdfSopenharmony_ci } 132f08c3bdfSopenharmony_ci 133f08c3bdfSopenharmony_ci /* 134f08c3bdfSopenharmony_ci * Exit with normal exit code if everything 135f08c3bdfSopenharmony_ci * fine 136f08c3bdfSopenharmony_ci */ 137f08c3bdfSopenharmony_ci _exit(0); 138f08c3bdfSopenharmony_ci } else { /* parent process */ 139f08c3bdfSopenharmony_ci /* 140f08c3bdfSopenharmony_ci * Let the parent process wait till child completes 141f08c3bdfSopenharmony_ci * its execution. 142f08c3bdfSopenharmony_ci */ 143f08c3bdfSopenharmony_ci wait(&exit_status); 144f08c3bdfSopenharmony_ci 145f08c3bdfSopenharmony_ci /* Check for the exit status of child process */ 146f08c3bdfSopenharmony_ci if (WEXITSTATUS(exit_status) == 0) { 147f08c3bdfSopenharmony_ci tst_resm(TPASS, "Call to vfork() " 148f08c3bdfSopenharmony_ci "successful"); 149f08c3bdfSopenharmony_ci } else if (WEXITSTATUS(exit_status) == 1) { 150f08c3bdfSopenharmony_ci tst_resm(TFAIL, 151f08c3bdfSopenharmony_ci "Child process exited abnormally"); 152f08c3bdfSopenharmony_ci } 153f08c3bdfSopenharmony_ci } 154f08c3bdfSopenharmony_ci tst_count++; /* incr. TEST_LOOP counter */ 155f08c3bdfSopenharmony_ci } 156f08c3bdfSopenharmony_ci 157f08c3bdfSopenharmony_ci cleanup(); 158f08c3bdfSopenharmony_ci tst_exit(); 159f08c3bdfSopenharmony_ci 160f08c3bdfSopenharmony_ci} 161f08c3bdfSopenharmony_ci 162f08c3bdfSopenharmony_ci/* 163f08c3bdfSopenharmony_ci * void 164f08c3bdfSopenharmony_ci * setup() - performs all ONE TIME setup for this test. 165f08c3bdfSopenharmony_ci * This function installs signal handler for SIGUSR1, puts signal SIGUSR1 166f08c3bdfSopenharmony_ci * on hold and then sends the signal SIGUSR1 to itself so that it is in 167f08c3bdfSopenharmony_ci * pending state. 168f08c3bdfSopenharmony_ci */ 169f08c3bdfSopenharmony_civoid setup(void) 170f08c3bdfSopenharmony_ci{ 171f08c3bdfSopenharmony_ci sigset_t PendSig; /* variable to hold pending signal */ 172f08c3bdfSopenharmony_ci 173f08c3bdfSopenharmony_ci tst_sig(FORK, DEF_HANDLER, cleanup); 174f08c3bdfSopenharmony_ci 175f08c3bdfSopenharmony_ci TEST_PAUSE; 176f08c3bdfSopenharmony_ci 177f08c3bdfSopenharmony_ci /* Install the signal handler */ 178f08c3bdfSopenharmony_ci if (signal(SIGUSR1, sig_handler) == SIG_ERR) { 179f08c3bdfSopenharmony_ci tst_brkm(TBROK, cleanup, "Fails to catch the signal SIGUSR1"); 180f08c3bdfSopenharmony_ci } 181f08c3bdfSopenharmony_ci 182f08c3bdfSopenharmony_ci /* Hold the signal SIGUSR1 */ 183f08c3bdfSopenharmony_ci if (sighold(SIGUSR1) == -1) { 184f08c3bdfSopenharmony_ci tst_brkm(TBROK, cleanup, 185f08c3bdfSopenharmony_ci "sighold failed to hold the signal SIGUSR1"); 186f08c3bdfSopenharmony_ci } 187f08c3bdfSopenharmony_ci 188f08c3bdfSopenharmony_ci /* Send the signal SIGUSR1 to itself so that SIGUSR1 is pending */ 189f08c3bdfSopenharmony_ci SAFE_KILL(cleanup, getpid(), SIGUSR1); 190f08c3bdfSopenharmony_ci 191f08c3bdfSopenharmony_ci /* If SIGUSR1 is not pending in the parent, fail */ 192f08c3bdfSopenharmony_ci if (sigpending(&PendSig) == -1) { 193f08c3bdfSopenharmony_ci tst_brkm(TBROK, cleanup, 194f08c3bdfSopenharmony_ci "sigpending function failed in parent"); 195f08c3bdfSopenharmony_ci } 196f08c3bdfSopenharmony_ci 197f08c3bdfSopenharmony_ci /* Check if SIGUSR1 is pending in parent */ 198f08c3bdfSopenharmony_ci if (sigismember(&PendSig, SIGUSR1) != 1) { 199f08c3bdfSopenharmony_ci tst_brkm(TBROK, cleanup, 200f08c3bdfSopenharmony_ci "SIGUSR1 signal is not pending in parent"); 201f08c3bdfSopenharmony_ci } 202f08c3bdfSopenharmony_ci} 203f08c3bdfSopenharmony_ci 204f08c3bdfSopenharmony_ci/* 205f08c3bdfSopenharmony_ci * void 206f08c3bdfSopenharmony_ci * sig_handler() - signal catching function for 'SIGUSR1' signal. 207f08c3bdfSopenharmony_ci * $ 208f08c3bdfSopenharmony_ci * This is a null function and used only to catch the above signal 209f08c3bdfSopenharmony_ci * generated in parent process. 210f08c3bdfSopenharmony_ci */ 211f08c3bdfSopenharmony_civoid sig_handler(void) 212f08c3bdfSopenharmony_ci{ 213f08c3bdfSopenharmony_ci} 214f08c3bdfSopenharmony_ci 215f08c3bdfSopenharmony_ci/* 216f08c3bdfSopenharmony_ci * void 217f08c3bdfSopenharmony_ci * cleanup() - performs all ONE TIME cleanup for this test at 218f08c3bdfSopenharmony_ci * completion or premature exit. 219f08c3bdfSopenharmony_ci * Release the signal 'SIGUSR1' if still in pending state. 220f08c3bdfSopenharmony_ci */ 221f08c3bdfSopenharmony_civoid cleanup(void) 222f08c3bdfSopenharmony_ci{ 223f08c3bdfSopenharmony_ci 224f08c3bdfSopenharmony_ci /* Release the signal 'SIGUSR1' if in pending state */ 225f08c3bdfSopenharmony_ci if (sigrelse(SIGUSR1) == -1) { 226f08c3bdfSopenharmony_ci tst_brkm(TBROK, NULL, "Failed to release 'SIGUSR1' in cleanup"); 227f08c3bdfSopenharmony_ci } 228f08c3bdfSopenharmony_ci 229f08c3bdfSopenharmony_ci} 230