blob: 4f0a0da117a8b36f7b11b4ae79841024eeb651fb [file] [log] [blame]
subrata_modakd97cb8e2009-06-15 18:39:45 +00001/******************************************************************************/
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 Gao4548c6c2012-10-19 18:03:36 +080016/* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */
subrata_modakd97cb8e2009-06-15 18:39:45 +000017/* */
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_modakd97cb8e2009-06-15 18:39:45 +000049#include "test.h"
subrata_modakd97cb8e2009-06-15 18:39:45 +000050#include "linux_syscall_numbers.h"
51
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020052char *TCID = "waitid01";
Wanlong Gao354ebb42012-12-07 10:10:04 +080053int testno;
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020054int TST_TOTAL = 3;
subrata_modakd97cb8e2009-06-15 18:39:45 +000055
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 Frysingerc57fba52014-04-09 18:56:30 -040074void cleanup(void)
Wanlong Gao354ebb42012-12-07 10:10:04 +080075{
Garrett Cooper2c282152010-12-16 00:55:50 -080076
Wanlong Gao354ebb42012-12-07 10:10:04 +080077 tst_rmdir();
subrata_modakd97cb8e2009-06-15 18:39:45 +000078
Wanlong Gao354ebb42012-12-07 10:10:04 +080079 tst_exit();
subrata_modakd97cb8e2009-06-15 18:39:45 +000080}
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 Frysingerc57fba52014-04-09 18:56:30 -0400100void setup(void)
subrata_modakd97cb8e2009-06-15 18:39:45 +0000101{
Wanlong Gao354ebb42012-12-07 10:10:04 +0800102 /* Capture signals if any */
103 /* Create temporary directories */
104 TEST_PAUSE;
105 tst_tmpdir();
subrata_modakd97cb8e2009-06-15 18:39:45 +0000106}
107
Wanlong Gao354ebb42012-12-07 10:10:04 +0800108void 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
118int main(int ac, char **av)
119{
subrata_modakd97cb8e2009-06-15 18:39:45 +0000120 id_t pid;
Wanlong Gao354ebb42012-12-07 10:10:04 +0800121 siginfo_t infop;
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200122 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200123 const char *msg;
Garrett Cooper2c282152010-12-16 00:55:50 -0800124
Wanlong Gao354ebb42012-12-07 10:10:04 +0800125 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) {
126 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800127 }
subrata_modakd97cb8e2009-06-15 18:39:45 +0000128
Wanlong Gao354ebb42012-12-07 10:10:04 +0800129 setup();
subrata_modakd97cb8e2009-06-15 18:39:45 +0000130
Wanlong Gao354ebb42012-12-07 10:10:04 +0800131 for (lc = 0; TEST_LOOPING(lc); ++lc) {
Caspar Zhangd59a6592013-03-07 14:59:12 +0800132 tst_count = 0;
Wanlong Gao354ebb42012-12-07 10:10:04 +0800133 for (testno = 0; testno < TST_TOTAL; ++testno) {
Garrett Cooper2c282152010-12-16 00:55:50 -0800134
Wanlong Gao354ebb42012-12-07 10:10:04 +0800135 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 Hrubis526fdf82014-12-04 14:35:01 +0100141 tst_brkm(TFAIL | TTERRNO,
142 NULL,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800143 "waitid(getpid()) failed");
Wanlong Gao354ebb42012-12-07 10:10:04 +0800144 } else
145 display_status(&infop); //CLD_EXITED = 1
146 }
subrata_modakd97cb8e2009-06-15 18:39:45 +0000147
Wanlong Gao354ebb42012-12-07 10:10:04 +0800148 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 Hrubis526fdf82014-12-04 14:35:01 +0100156 tst_brkm(TFAIL | TTERRNO,
157 NULL, "waitid(0) failed");
Wanlong Gao354ebb42012-12-07 10:10:04 +0800158 } else
159 display_status(&infop); //CLD_DUMPED = 3 ; SIGFPE = 8
160 }
subrata_modakd97cb8e2009-06-15 18:39:45 +0000161
Wanlong Gao354ebb42012-12-07 10:10:04 +0800162 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 Hrubis526fdf82014-12-04 14:35:01 +0100170 tst_brkm(TFAIL | TTERRNO, NULL,
171 "waitid(0) failed");
Wanlong Gao354ebb42012-12-07 10:10:04 +0800172 } else
173 display_status(&infop); //CLD_KILLED = 2 ; SIGHUP = 1
174 }
175 }
176 tst_resm(TPASS, "waitid(): system call passed");
subrata_modakd97cb8e2009-06-15 18:39:45 +0000177 cleanup();
Wanlong Gao354ebb42012-12-07 10:10:04 +0800178 tst_exit();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700179}