blob: 87f8185cd0073e511e92e7933a413da7be7d39b5 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
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 Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000018 */
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 Coopere8530df2010-12-21 11:37:57 -080048#include "test.h"
plars865695b2001-08-27 22:15:12 +000049
Wanlong Gaobdb27442012-11-20 10:32:33 +080050static void setup(void);
51static void cleanup(void);
plars865695b2001-08-27 22:15:12 +000052
plars865695b2001-08-27 22:15:12 +000053char *TCID = "waitpid04";
54int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000055
56#define INVAL_FLAG -1
plars865695b2001-08-27 22:15:12 +000057
Wanlong Gaobdb27442012-11-20 10:32:33 +080058static int flag, condition_number;
plars865695b2001-08-27 22:15:12 +000059
plars74948ad2002-11-14 16:16:14 +000060int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000061{
62 int pid, status, ret;
63
Cyril Hrubis89af32a2012-10-24 16:39:11 +020064 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020065 const char *msg;
plars865695b2001-08-27 22:15:12 +000066
Wanlong Gaobdb27442012-11-20 10:32:33 +080067 msg = parse_opts(ac, av, NULL, NULL);
68 if (msg != NULL)
plars865695b2001-08-27 22:15:12 +000069 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Garrett Cooper2c282152010-12-16 00:55:50 -080070
plars865695b2001-08-27 22:15:12 +000071 setup();
72
73 /* check for looping state if -i option is given */
74 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +080075 /* reset tst_count in case we are looping */
76 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000077
78 ret = waitpid(pid, &status, WNOHANG);
79 flag = 0;
subrata_modak3976f662008-02-25 09:40:12 +000080 condition_number = 1;
subrata_modak56207ce2009-03-23 13:35:39 +000081 if (ret != -1) {
plars865695b2001-08-27 22:15:12 +000082 tst_resm(TFAIL, "condition %d test failed",
subrata_modak3976f662008-02-25 09:40:12 +000083 condition_number);
plars865695b2001-08-27 22:15:12 +000084 } else {
plars865695b2001-08-27 22:15:12 +000085 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_modak56207ce2009-03-23 13:35:39 +000091 condition_number);
plars865695b2001-08-27 22:15:12 +000092 }
93 }
subrata_modak3976f662008-02-25 09:40:12 +000094 condition_number++;
plars865695b2001-08-27 22:15:12 +000095
Wanlong Gaobdb27442012-11-20 10:32:33 +080096 if (FORK_OR_VFORK() == 0)
plars865695b2001-08-27 22:15:12 +000097 exit(0);
Wanlong Gaobdb27442012-11-20 10:32:33 +080098
plars1ad84512002-07-23 13:11:18 +000099 pid = 1;
plars865695b2001-08-27 22:15:12 +0000100 ret = waitpid(pid, &status, WUNTRACED);
101 flag = 0;
subrata_modak56207ce2009-03-23 13:35:39 +0000102 if (ret != -1) {
plars865695b2001-08-27 22:15:12 +0000103 tst_resm(TFAIL, "condition %d test failed",
subrata_modak3976f662008-02-25 09:40:12 +0000104 condition_number);
plars865695b2001-08-27 22:15:12 +0000105 } else {
plars865695b2001-08-27 22:15:12 +0000106 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_modak56207ce2009-03-23 13:35:39 +0000112 condition_number);
plars865695b2001-08-27 22:15:12 +0000113 }
114 }
subrata_modak3976f662008-02-25 09:40:12 +0000115 condition_number++;
plars865695b2001-08-27 22:15:12 +0000116
117 /* Option is Inval = INVAL_FLAG */
subrata_modak56207ce2009-03-23 13:35:39 +0000118 ret = waitpid(pid, &status, INVAL_FLAG);
plars865695b2001-08-27 22:15:12 +0000119 flag = 0;
subrata_modak56207ce2009-03-23 13:35:39 +0000120 if (ret != -1) {
plars865695b2001-08-27 22:15:12 +0000121 tst_resm(TFAIL, "condition %d test failed",
subrata_modak3976f662008-02-25 09:40:12 +0000122 condition_number);
plars865695b2001-08-27 22:15:12 +0000123 } else {
plars865695b2001-08-27 22:15:12 +0000124 if (errno != EINVAL) {
125 tst_resm(TFAIL, "waitpid() set invalid "
126 "errno, expected EINVAL, got: %d",
127 errno);
128 } else {
subrata_modak56207ce2009-03-23 13:35:39 +0000129 tst_resm(TPASS, "condition %d test passed",
130 condition_number);
plars865695b2001-08-27 22:15:12 +0000131 }
132 }
subrata_modak3976f662008-02-25 09:40:12 +0000133 condition_number++;
plars865695b2001-08-27 22:15:12 +0000134 }
Wanlong Gaobdb27442012-11-20 10:32:33 +0800135
plars865695b2001-08-27 22:15:12 +0000136 cleanup();
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800137 tst_exit();
plars865695b2001-08-27 22:15:12 +0000138}
139
Wanlong Gaobdb27442012-11-20 10:32:33 +0800140static void setup(void)
plars865695b2001-08-27 22:15:12 +0000141{
Wanlong Gaobdb27442012-11-20 10:32:33 +0800142 TEST_PAUSE;
plars865695b2001-08-27 22:15:12 +0000143}
144
Wanlong Gaobdb27442012-11-20 10:32:33 +0800145static void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000146{
Wanlong Gaobdb27442012-11-20 10:32:33 +0800147}