blob: 39e62a8327c15f32b879bfadfdef783264aa849a [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
2 *
3 * Copyright (c) International Business Machines Corp., 2001
Cyril Hrubis4fee5032012-11-28 13:07:27 +01004 * Copyright (c) 2012 Cyril Hrubis <chrubis@suse.cz>
plars865695b2001-08-27 22:15:12 +00005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
14 * the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
Wanlong Gao4548c6c2012-10-19 18:03:36 +080018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000019 */
20
21/*
plars865695b2001-08-27 22:15:12 +000022 * wait401 - check that a call to wait4() correctly waits for a child
23 * process to exit
plars865695b2001-08-27 22:15:12 +000024 */
25
26#include "test.h"
plars865695b2001-08-27 22:15:12 +000027
28#include <errno.h>
29#define _USE_BSD
30#include <sys/types.h>
31#include <sys/resource.h>
32#include <sys/wait.h>
33
subrata_modak56207ce2009-03-23 13:35:39 +000034char *TCID = "wait401";
plars865695b2001-08-27 22:15:12 +000035int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000036
Cyril Hrubis4fee5032012-11-28 13:07:27 +010037static void cleanup(void);
38static void setup(void);
39
plars74948ad2002-11-14 16:16:14 +000040int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000041{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020042 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020043 const char *msg;
plars865695b2001-08-27 22:15:12 +000044 pid_t pid;
45 int status = 1;
Cyril Hrubis4fee5032012-11-28 13:07:27 +010046 struct rusage rusage;
plars865695b2001-08-27 22:15:12 +000047
Cyril Hrubis4fee5032012-11-28 13:07:27 +010048 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080049 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000050
Cyril Hrubis4fee5032012-11-28 13:07:27 +010051 setup();
plars865695b2001-08-27 22:15:12 +000052
53 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +080054 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000055
robbiewd34d5812005-07-11 22:28:09 +000056 pid = FORK_OR_VFORK();
subrata_modakbdbaec52009-02-26 12:14:51 +000057
Cyril Hrubis4fee5032012-11-28 13:07:27 +010058 switch (pid) {
59 case -1:
plars865695b2001-08-27 22:15:12 +000060 tst_brkm(TBROK, cleanup, "fork() failed");
Wanlong Gao354ebb42012-12-07 10:10:04 +080061 break;
Cyril Hrubis4fee5032012-11-28 13:07:27 +010062 case 0:
plars865695b2001-08-27 22:15:12 +000063 sleep(1);
64 exit(0);
Wanlong Gao354ebb42012-12-07 10:10:04 +080065 break;
Cyril Hrubis4fee5032012-11-28 13:07:27 +010066 default:
67 TEST(wait4(pid, &status, 0, &rusage));
Wanlong Gao354ebb42012-12-07 10:10:04 +080068 break;
plars865695b2001-08-27 22:15:12 +000069 }
subrata_modakbdbaec52009-02-26 12:14:51 +000070
plars865695b2001-08-27 22:15:12 +000071 if (TEST_RETURN == -1) {
72 tst_brkm(TFAIL, cleanup, "%s call failed - errno = %d "
subrata_modak56207ce2009-03-23 13:35:39 +000073 ": %s", TCID, TEST_ERRNO,
74 strerror(TEST_ERRNO));
plars865695b2001-08-27 22:15:12 +000075 }
subrata_modakbdbaec52009-02-26 12:14:51 +000076
Cyril Hrubise38b9612014-06-02 17:20:57 +020077 if (WIFEXITED(status) == 0) {
78 tst_brkm(TFAIL, cleanup,
79 "%s call succeeded but "
80 "WIFEXITED() did not return expected value "
81 "- %d", TCID, WIFEXITED(status));
82 } else if (TEST_RETURN != pid) {
83 tst_resm(TFAIL, "%s did not return the "
84 "expected value (%d), actual: %ld",
85 TCID, pid, TEST_RETURN);
86 } else {
subrata_modakbdbaec52009-02-26 12:14:51 +000087
Cyril Hrubise38b9612014-06-02 17:20:57 +020088 tst_resm(TPASS,
89 "Received child pid as expected.");
plars865695b2001-08-27 22:15:12 +000090 }
Cyril Hrubise38b9612014-06-02 17:20:57 +020091
plars865695b2001-08-27 22:15:12 +000092 tst_resm(TPASS, "%s call succeeded", TCID);
plars865695b2001-08-27 22:15:12 +000093 }
94
95 cleanup();
Garrett Cooper7d0a4a52010-12-16 10:05:08 -080096 tst_exit();
plars865695b2001-08-27 22:15:12 +000097}
98
Cyril Hrubis4fee5032012-11-28 13:07:27 +010099static void setup(void)
plars865695b2001-08-27 22:15:12 +0000100{
plars865695b2001-08-27 22:15:12 +0000101 tst_sig(FORK, DEF_HANDLER, cleanup);
102
plars865695b2001-08-27 22:15:12 +0000103 TEST_PAUSE;
104}
105
Cyril Hrubis4fee5032012-11-28 13:07:27 +0100106static void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000107{
Chris Dearmanec6edca2012-10-17 19:54:01 -0700108}