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 |
| 22 | * waitpid04.c |
| 23 | * |
| 24 | * DESCRIPTION |
| 25 | * test to check the error conditions in waitpid sys call |
| 26 | * |
| 27 | * USAGE: <for command-line> |
| 28 | * waitpid04 [-c n] [-e] [-i n] [-I x] [-P x] [-t] |
| 29 | * where, -c n : Run n copies concurrently. |
| 30 | * -e : Turn on errno logging. |
| 31 | * -i n : Execute test n times. |
| 32 | * -I x : Execute test for x seconds. |
| 33 | * -P x : Pause for x seconds between iterations. |
| 34 | * -t : Turn on syscall timing. |
| 35 | * |
| 36 | * History |
| 37 | * 07/2001 John George |
| 38 | * -Ported |
| 39 | * |
| 40 | * Restrictions |
| 41 | * NONE |
| 42 | */ |
| 43 | |
| 44 | #include <sys/signal.h> |
| 45 | #include <sys/types.h> |
| 46 | #include <sys/wait.h> |
| 47 | #include <errno.h> |
Garrett Cooper | e8530df | 2010-12-21 11:37:57 -0800 | [diff] [blame] | 48 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 49 | |
Wanlong Gao | bdb2744 | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 50 | static void setup(void); |
| 51 | static void cleanup(void); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 52 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 53 | char *TCID = "waitpid04"; |
| 54 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 55 | |
| 56 | #define INVAL_FLAG -1 |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 57 | |
Wanlong Gao | bdb2744 | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 58 | static int flag, condition_number; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 59 | |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 60 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 61 | { |
| 62 | int pid, status, ret; |
| 63 | |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 64 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 65 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 66 | |
Wanlong Gao | bdb2744 | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 67 | msg = parse_opts(ac, av, NULL, NULL); |
| 68 | if (msg != NULL) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 69 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 70 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 71 | setup(); |
| 72 | |
| 73 | /* check for looping state if -i option is given */ |
| 74 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 75 | /* reset tst_count in case we are looping */ |
| 76 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 77 | |
| 78 | ret = waitpid(pid, &status, WNOHANG); |
| 79 | flag = 0; |
subrata_modak | 3976f66 | 2008-02-25 09:40:12 +0000 | [diff] [blame] | 80 | condition_number = 1; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 81 | if (ret != -1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 82 | tst_resm(TFAIL, "condition %d test failed", |
subrata_modak | 3976f66 | 2008-02-25 09:40:12 +0000 | [diff] [blame] | 83 | condition_number); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 84 | } else { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 85 | if (errno != ECHILD) { |
| 86 | tst_resm(TFAIL, "waitpid() set invalid " |
| 87 | "errno, expected ECHILD, got: %d", |
| 88 | errno); |
| 89 | } else { |
| 90 | tst_resm(TPASS, "condition %d test passed", |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 91 | condition_number); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 92 | } |
| 93 | } |
subrata_modak | 3976f66 | 2008-02-25 09:40:12 +0000 | [diff] [blame] | 94 | condition_number++; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 95 | |
Wanlong Gao | bdb2744 | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 96 | if (FORK_OR_VFORK() == 0) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 97 | exit(0); |
Wanlong Gao | bdb2744 | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 98 | |
plars | 1ad8451 | 2002-07-23 13:11:18 +0000 | [diff] [blame] | 99 | pid = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 100 | ret = waitpid(pid, &status, WUNTRACED); |
| 101 | flag = 0; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 102 | if (ret != -1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 103 | tst_resm(TFAIL, "condition %d test failed", |
subrata_modak | 3976f66 | 2008-02-25 09:40:12 +0000 | [diff] [blame] | 104 | condition_number); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 105 | } else { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 106 | if (errno != ECHILD) { |
| 107 | tst_resm(TFAIL, "waitpid() set invalid " |
| 108 | "errno, expected ECHILD, got: %d", |
| 109 | errno); |
| 110 | } else { |
| 111 | tst_resm(TPASS, "condition %d test passed", |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 112 | condition_number); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 113 | } |
| 114 | } |
subrata_modak | 3976f66 | 2008-02-25 09:40:12 +0000 | [diff] [blame] | 115 | condition_number++; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 116 | |
| 117 | /* Option is Inval = INVAL_FLAG */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 118 | ret = waitpid(pid, &status, INVAL_FLAG); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 119 | flag = 0; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 120 | if (ret != -1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 121 | tst_resm(TFAIL, "condition %d test failed", |
subrata_modak | 3976f66 | 2008-02-25 09:40:12 +0000 | [diff] [blame] | 122 | condition_number); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 123 | } else { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 124 | if (errno != EINVAL) { |
| 125 | tst_resm(TFAIL, "waitpid() set invalid " |
| 126 | "errno, expected EINVAL, got: %d", |
| 127 | errno); |
| 128 | } else { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 129 | tst_resm(TPASS, "condition %d test passed", |
| 130 | condition_number); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 131 | } |
| 132 | } |
subrata_modak | 3976f66 | 2008-02-25 09:40:12 +0000 | [diff] [blame] | 133 | condition_number++; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 134 | } |
Wanlong Gao | bdb2744 | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 135 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 136 | cleanup(); |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 137 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Wanlong Gao | bdb2744 | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 140 | static void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 141 | { |
Wanlong Gao | bdb2744 | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 142 | TEST_PAUSE; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Wanlong Gao | bdb2744 | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 145 | static void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 146 | { |
Wanlong Gao | bdb2744 | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 147 | } |