subrata_modak | d97cb8e | 2009-06-15 18:39:45 +0000 | [diff] [blame] | 1 | /******************************************************************************/ |
| 2 | /* Copyright (c) Crackerjack Project., 2007 */ |
| 3 | /* */ |
| 4 | /* This program is free software; you can redistribute it and/or modify */ |
| 5 | /* it under the terms of the GNU General Public License as published by */ |
| 6 | /* the Free Software Foundation; either version 2 of the License, or */ |
| 7 | /* (at your option) any later version. */ |
| 8 | /* */ |
| 9 | /* This program is distributed in the hope that it will be useful, */ |
| 10 | /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ |
| 11 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See */ |
| 12 | /* the GNU General Public License for more details. */ |
| 13 | /* */ |
| 14 | /* You should have received a copy of the GNU General Public License */ |
| 15 | /* along with this program; if not, write to the Free Software */ |
Wanlong Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 16 | /* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ |
subrata_modak | d97cb8e | 2009-06-15 18:39:45 +0000 | [diff] [blame] | 17 | /* */ |
| 18 | /******************************************************************************/ |
| 19 | /******************************************************************************/ |
| 20 | /* */ |
| 21 | /* File: waitid01.c */ |
| 22 | /* */ |
| 23 | /* Description: This tests the waitid() syscall */ |
| 24 | /* */ |
| 25 | /* Usage: <for command-line> */ |
| 26 | /* waitid01 [-c n] [-e][-i n] [-I x] [-p x] [-t] */ |
| 27 | /* where, -c n : Run n copies concurrently. */ |
| 28 | /* -e : Turn on errno logging. */ |
| 29 | /* -i n : Execute test n times. */ |
| 30 | /* -I x : Execute test for x seconds. */ |
| 31 | /* -P x : Pause for x seconds between iterations. */ |
| 32 | /* -t : Turn on syscall timing. */ |
| 33 | /* */ |
| 34 | /* Total Tests: 1 */ |
| 35 | /* */ |
| 36 | /* Test Name: waitid01 */ |
| 37 | /* History: Porting from Crackerjack to LTP is done by */ |
| 38 | /* Manas Kumar Nayak maknayak@in.ibm.com> */ |
| 39 | /******************************************************************************/ |
| 40 | |
| 41 | #include <stdio.h> |
| 42 | #include <errno.h> |
| 43 | #include <stdlib.h> |
| 44 | #include <sys/wait.h> |
| 45 | #include <sys/types.h> |
| 46 | #include <unistd.h> |
| 47 | #include <sys/stat.h> |
| 48 | |
subrata_modak | d97cb8e | 2009-06-15 18:39:45 +0000 | [diff] [blame] | 49 | #include "test.h" |
subrata_modak | d97cb8e | 2009-06-15 18:39:45 +0000 | [diff] [blame] | 50 | #include "linux_syscall_numbers.h" |
| 51 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 52 | char *TCID = "waitid01"; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 53 | int testno; |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 54 | int TST_TOTAL = 3; |
subrata_modak | d97cb8e | 2009-06-15 18:39:45 +0000 | [diff] [blame] | 55 | |
| 56 | /* Extern Global Functions */ |
| 57 | /******************************************************************************/ |
| 58 | /* */ |
| 59 | /* Function: cleanup */ |
| 60 | /* */ |
| 61 | /* Description: Performs all one time clean up for this test on successful */ |
| 62 | /* completion, premature exit or failure. Closes all temporary */ |
| 63 | /* files, removes all temporary directories exits the test with */ |
| 64 | /* appropriate return code by calling tst_exit() function. */ |
| 65 | /* */ |
| 66 | /* Input: None. */ |
| 67 | /* */ |
| 68 | /* Output: None. */ |
| 69 | /* */ |
| 70 | /* Return: On failure - Exits calling tst_exit(). Non '0' return code. */ |
| 71 | /* On success - Exits calling tst_exit(). With '0' return code. */ |
| 72 | /* */ |
| 73 | /******************************************************************************/ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 74 | void cleanup(void) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 75 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 76 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 77 | tst_rmdir(); |
subrata_modak | d97cb8e | 2009-06-15 18:39:45 +0000 | [diff] [blame] | 78 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 79 | tst_exit(); |
subrata_modak | d97cb8e | 2009-06-15 18:39:45 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | /* Local Functions */ |
| 83 | /******************************************************************************/ |
| 84 | /* */ |
| 85 | /* Function: setup */ |
| 86 | /* */ |
| 87 | /* Description: Performs all one time setup for this test. This function is */ |
| 88 | /* typically used to capture signals, create temporary dirs */ |
| 89 | /* and temporary files that may be used in the course of this */ |
| 90 | /* test. */ |
| 91 | /* */ |
| 92 | /* Input: None. */ |
| 93 | /* */ |
| 94 | /* Output: None. */ |
| 95 | /* */ |
| 96 | /* Return: On failure - Exits by calling cleanup(). */ |
| 97 | /* On success - returns 0. */ |
| 98 | /* */ |
| 99 | /******************************************************************************/ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 100 | void setup(void) |
subrata_modak | d97cb8e | 2009-06-15 18:39:45 +0000 | [diff] [blame] | 101 | { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 102 | /* Capture signals if any */ |
| 103 | /* Create temporary directories */ |
| 104 | TEST_PAUSE; |
| 105 | tst_tmpdir(); |
subrata_modak | d97cb8e | 2009-06-15 18:39:45 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 108 | void display_status(siginfo_t * infop) |
| 109 | { |
| 110 | tst_resm(TINFO, "Process %d terminated:", infop->si_pid); |
| 111 | tst_resm(TINFO, "code = %d", infop->si_code); |
| 112 | if (infop->si_code == CLD_EXITED) |
| 113 | tst_resm(TINFO, "exit value = %d", infop->si_status); |
| 114 | else |
| 115 | tst_resm(TINFO, "signal = %d", infop->si_status); |
| 116 | } |
| 117 | |
| 118 | int main(int ac, char **av) |
| 119 | { |
subrata_modak | d97cb8e | 2009-06-15 18:39:45 +0000 | [diff] [blame] | 120 | id_t pid; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 121 | siginfo_t infop; |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 122 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 123 | const char *msg; |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 124 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 125 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) { |
| 126 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 127 | } |
subrata_modak | d97cb8e | 2009-06-15 18:39:45 +0000 | [diff] [blame] | 128 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 129 | setup(); |
subrata_modak | d97cb8e | 2009-06-15 18:39:45 +0000 | [diff] [blame] | 130 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 131 | for (lc = 0; TEST_LOOPING(lc); ++lc) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 132 | tst_count = 0; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 133 | for (testno = 0; testno < TST_TOTAL; ++testno) { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 134 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 135 | TEST(fork()); |
| 136 | if (TEST_RETURN == 0) { |
| 137 | exit(123); |
| 138 | } else { |
| 139 | TEST(waitid(P_ALL, getpid(), &infop, WEXITED)); |
| 140 | if (TEST_RETURN == -1) { |
Cyril Hrubis | 526fdf8 | 2014-12-04 14:35:01 +0100 | [diff] [blame] | 141 | tst_brkm(TFAIL | TTERRNO, |
| 142 | NULL, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 143 | "waitid(getpid()) failed"); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 144 | } else |
| 145 | display_status(&infop); //CLD_EXITED = 1 |
| 146 | } |
subrata_modak | d97cb8e | 2009-06-15 18:39:45 +0000 | [diff] [blame] | 147 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 148 | TEST(fork()); |
| 149 | if (TEST_RETURN == 0) { |
| 150 | int a, b = 0; |
| 151 | a = 1 / b; |
| 152 | tst_exit(); |
| 153 | } else { |
| 154 | TEST(waitid(P_ALL, 0, &infop, WEXITED)); |
| 155 | if (TEST_RETURN == -1) { |
Cyril Hrubis | 526fdf8 | 2014-12-04 14:35:01 +0100 | [diff] [blame] | 156 | tst_brkm(TFAIL | TTERRNO, |
| 157 | NULL, "waitid(0) failed"); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 158 | } else |
| 159 | display_status(&infop); //CLD_DUMPED = 3 ; SIGFPE = 8 |
| 160 | } |
subrata_modak | d97cb8e | 2009-06-15 18:39:45 +0000 | [diff] [blame] | 161 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 162 | TEST(pid = fork()); |
| 163 | if (TEST_RETURN == 0) { |
| 164 | TEST(sleep(10)); |
| 165 | tst_exit(); |
| 166 | } |
| 167 | TEST(kill(pid, SIGHUP)); |
| 168 | TEST(waitid(P_ALL, 0, &infop, WEXITED)); |
| 169 | if (TEST_RETURN == -1) { |
Cyril Hrubis | 526fdf8 | 2014-12-04 14:35:01 +0100 | [diff] [blame] | 170 | tst_brkm(TFAIL | TTERRNO, NULL, |
| 171 | "waitid(0) failed"); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 172 | } else |
| 173 | display_status(&infop); //CLD_KILLED = 2 ; SIGHUP = 1 |
| 174 | } |
| 175 | } |
| 176 | tst_resm(TPASS, "waitid(): system call passed"); |
subrata_modak | d97cb8e | 2009-06-15 18:39:45 +0000 | [diff] [blame] | 177 | cleanup(); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 178 | tst_exit(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 179 | } |