blob: 25633db083261927cb72ae449ec3e3a52c2ea739 [file] [log] [blame]
robbiewe237cd32002-12-31 21:21:45 +00001/* IBM Corporation */
2/* 01/02/2003 Port to LTP avenkat@us.ibm.com */
3/* 06/30/2001 Port to Linux nsharoff@us.ibm.com */
4
5/*
6 *
7 * Copyright (c) International Business Machines Corp., 2002
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
17 * the GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23
subrata_modak56207ce2009-03-23 13:35:39 +000024 /*kill2.c */
robbiewe237cd32002-12-31 21:21:45 +000025/*======================================================================
26>KEYS: < kill(), wait(), signal()
subrata_modak4bb656a2009-02-26 12:02:09 +000027>WHAT: < Check that when a child is killed by its parent, it returns the
28 < correct values to the waiting parent--the child sets signal to
subrata_modakbdbaec52009-02-26 12:14:51 +000029 < ignore the kill
robbiewe237cd32002-12-31 21:21:45 +000030>HOW: < For each signal: Send that signal to a child that has elected
31 < to catch the signal, check that the correct status was returned
32 < to the waiting parent.
33 < NOTE: Signal 9 (kill) is not catchable, and must be dealt with
34 < separately.
subrata_modak4bb656a2009-02-26 12:02:09 +000035>BUGS: < None known
robbiewe237cd32002-12-31 21:21:45 +000036======================================================================*/
37#ifndef _GNU_SOURCE
38#define _GNU_SOURCE 1
39#endif
40
robbiewe237cd32002-12-31 21:21:45 +000041#include <stdio.h>
42#include <sys/types.h>
43#include <signal.h>
44#include <stdlib.h>
45#include <unistd.h>
46#include <sys/wait.h>
47#include <errno.h>
48
49//char progname[] = "kill2()";
50/***** LTP Port *****/
51#include "test.h"
52#include "usctest.h"
53#define ITER 3
54#define FAILED 0
55#define PASSED 1
56
robbiewca03b9d2003-05-07 19:04:28 +000057char *TCID = "kill12";
robbiewe237cd32002-12-31 21:21:45 +000058
robbiewe237cd32002-12-31 21:21:45 +000059int local_flag = PASSED;
60int block_number;
61FILE *temp;
62int TST_TOTAL = 1;
63extern int Tst_count;
robbiewd34d5812005-07-11 22:28:09 +000064static int sig;
robbiewe237cd32002-12-31 21:21:45 +000065
66int anyfail();
67int blenter();
68int instress();
69void setup();
70void terror();
71void fail_exit();
72void ok_exit();
73int forkfail();
robbiewd34d5812005-07-11 22:28:09 +000074void do_child();
robbiewe237cd32002-12-31 21:21:45 +000075
76/***** ** ** *****/
77
robbiewe237cd32002-12-31 21:21:45 +000078int chflag;
79
80/*--------------------------------------------------------------------*/
subrata_modak56207ce2009-03-23 13:35:39 +000081int main(int argc, char **argv)
robbiewe237cd32002-12-31 21:21:45 +000082{
subrata_modak56207ce2009-03-23 13:35:39 +000083/***** BEGINNING OF MAIN. *****/
robbiewe237cd32002-12-31 21:21:45 +000084 int pid, npid;
subrata_modak56207ce2009-03-23 13:35:39 +000085 int nsig, exno, nexno, status;
robbiewe237cd32002-12-31 21:21:45 +000086 int ret_val = 0;
87 int core;
88 void chsig();
89
robbiewd34d5812005-07-11 22:28:09 +000090#ifdef UCLINUX
91 char *msg;
92
93 /* parse standard options */
subrata_modak56207ce2009-03-23 13:35:39 +000094 if ((msg =
Garrett Cooper45e285d2010-11-22 12:19:25 -080095 parse_opts(argc, argv, NULL, NULL)) != NULL) {
robbiewd34d5812005-07-11 22:28:09 +000096 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
97 }
98
99 maybe_run_child(&do_child, "dd", &temp, &sig);
100#endif
101
robbiewe237cd32002-12-31 21:21:45 +0000102 setup();
subrata_modak56207ce2009-03-23 13:35:39 +0000103 //tempdir(); /* move to new directory */ 12/20/2003
robbiewe237cd32002-12-31 21:21:45 +0000104/*--------------------------------------------------------------------*/
subrata_modak56207ce2009-03-23 13:35:39 +0000105 blenter(); /*<<<<<ENTER DATA HERE<<<<<<<< */
robbiewe237cd32002-12-31 21:21:45 +0000106
107 exno = 1;
108
109 if (sigset(SIGCLD, chsig) == SIG_ERR) {
110 fprintf(temp, "\tsigset failed, errno = %d\n", errno);
111 fail_exit();
112 }
113
subrata_modak56207ce2009-03-23 13:35:39 +0000114 for (sig = 1; sig < 14; sig++) {
robbiewe237cd32002-12-31 21:21:45 +0000115 fflush(temp);
116 chflag = 0;
117
robbiewd34d5812005-07-11 22:28:09 +0000118 pid = FORK_OR_VFORK();
robbiewe237cd32002-12-31 21:21:45 +0000119 if (pid < 0) {
120 forkfail();
121 }
122
123 if (pid == 0) {
robbiewd34d5812005-07-11 22:28:09 +0000124#ifdef UCLINUX
125 if (self_exec(argv[0], "dd", temp, sig) < 0) {
126 tst_resm(TBROK, "self_exec FAILED - "
127 "terminating test.");
128 tst_exit();
subrata_modak43337a32009-02-26 11:43:51 +0000129 return 0;
robbiewd34d5812005-07-11 22:28:09 +0000130 }
131#else
132 do_child();
133#endif
robbiewe237cd32002-12-31 21:21:45 +0000134 } else {
135 //fprintf(temp, "Testing signal %d\n", sig);
136
subrata_modak56207ce2009-03-23 13:35:39 +0000137 while (!chflag) /* wait for child */
robbiewe237cd32002-12-31 21:21:45 +0000138 sleep(1);
139
subrata_modak56207ce2009-03-23 13:35:39 +0000140 kill(pid, sig); /* child should ignroe this sig */
robbiewe237cd32002-12-31 21:21:45 +0000141 kill(pid, SIGCLD); /* child should exit */
142
143#ifdef BCS
subrata_modak56207ce2009-03-23 13:35:39 +0000144 while ((npid = wait(&status)) != pid
145 || (npid == -1 && errno == EINTR)) ;
146 if (npid != pid) {
147 fprintf(temp,
148 "wait error: wait returned wrong pid\n");
robbiewe237cd32002-12-31 21:21:45 +0000149 ret_val = 1;
150 }
151#else
subrata_modak56207ce2009-03-23 13:35:39 +0000152 while ((npid = waitpid(pid, &status, 0)) != -1
153 || errno == EINTR) ;
robbiewe237cd32002-12-31 21:21:45 +0000154#endif
155
156 /*
subrata_modak56207ce2009-03-23 13:35:39 +0000157 nsig = status & 0177;
158 core = status & 0200;
159 nexno = (status & 0xff00) >> 8;
160 */
161 /***** LTP Port *****/
162 nsig = WTERMSIG(status);
163#ifdef WCOREDUMP
164 core = WCOREDUMP(status);
165#endif
166 nexno = WIFEXITED(status);
167 /***** ** ** *****/
robbiewe237cd32002-12-31 21:21:45 +0000168
subrata_modak56207ce2009-03-23 13:35:39 +0000169 /* nsig is the signal number returned by wait
170 it should be 0, except when sig = 9 */
robbiewe237cd32002-12-31 21:21:45 +0000171
subrata_modak4bb656a2009-02-26 12:02:09 +0000172 if ((sig == 9) && (nsig != sig)) {
robbiewe237cd32002-12-31 21:21:45 +0000173 fprintf(temp, "wait error: unexpected signal"
174 " returned when the signal sent was 9"
subrata_modak56207ce2009-03-23 13:35:39 +0000175 " The status of the process is %d \n",
176 status);
robbiewe237cd32002-12-31 21:21:45 +0000177 ret_val = 1;
178 }
179 if ((sig != 9) && (nsig != 0)) {
subrata_modak4bb656a2009-02-26 12:02:09 +0000180 fprintf(temp, "wait error: unexpected signal "
subrata_modak56207ce2009-03-23 13:35:39 +0000181 "returned, the status of the process is "
182 "%d \n", status);
robbiewe237cd32002-12-31 21:21:45 +0000183 ret_val = 1;
184 }
185
subrata_modak56207ce2009-03-23 13:35:39 +0000186 /* nexno is the exit number returned by wait
187 it should be 1, except when sig = 9 */
robbiewe237cd32002-12-31 21:21:45 +0000188
subrata_modak4bb656a2009-02-26 12:02:09 +0000189 if (sig == 9)
robbiewe237cd32002-12-31 21:21:45 +0000190 if (nexno != 0) {
191 fprintf(temp, "signal error: unexpected"
subrata_modak4bb656a2009-02-26 12:02:09 +0000192 " exit number returned when"
robbiewe237cd32002-12-31 21:21:45 +0000193 " signal sent was 9, the status"
194 " of the process is %d \n",
195 status);
subrata_modak56207ce2009-03-23 13:35:39 +0000196 ret_val = 1;
197 } else;
robbiewe237cd32002-12-31 21:21:45 +0000198 else if (nexno != 1) {
199 fprintf(temp, "signal error: unexpected exit "
200 "number returned,the status of the"
subrata_modak56207ce2009-03-23 13:35:39 +0000201 " process is %d\n", status);
robbiewe237cd32002-12-31 21:21:45 +0000202 ret_val = 1;
203 }
204 }
205 }
subrata_modak56207ce2009-03-23 13:35:39 +0000206 if (ret_val)
robbiewe237cd32002-12-31 21:21:45 +0000207 local_flag = FAILED;
208
209/*--------------------------------------------------------------------*/
subrata_modakbdbaec52009-02-26 12:14:51 +0000210 anyfail();
subrata_modak43337a32009-02-26 11:43:51 +0000211 return 0;
robbiewe237cd32002-12-31 21:21:45 +0000212} /******** END OF MAIN. ********/
subrata_modak56207ce2009-03-23 13:35:39 +0000213
robbiewe237cd32002-12-31 21:21:45 +0000214/*--------------------------------------------------------------------*/
215
216void chsig()
217{
218 chflag++;
219}
220
221/****** LTP Port *****/
222int anyfail()
223{
subrata_modak56207ce2009-03-23 13:35:39 +0000224 (local_flag == FAILED) ? tst_resm(TFAIL,
225 "Test failed") : tst_resm(TPASS,
226 "Test passed");
227 tst_exit();
228 return 0;
robbiewe237cd32002-12-31 21:21:45 +0000229}
230
subrata_modak56207ce2009-03-23 13:35:39 +0000231void do_child()
robbiewd34d5812005-07-11 22:28:09 +0000232{
233 int exno = 1;
234
235#ifdef UCLINUX
236 if (sigset(SIGCLD, chsig) == SIG_ERR) {
237 fprintf(temp, "\tsigset failed, errno = %d\n", errno);
238 fail_exit();
239 }
240#endif
241
subrata_modak56207ce2009-03-23 13:35:39 +0000242 sigset(sig, SIG_IGN); /* set to ignore signal */
robbiewd34d5812005-07-11 22:28:09 +0000243 kill(getppid(), SIGCLD); /* tell parent we are ready */
244 while (!chflag)
subrata_modak56207ce2009-03-23 13:35:39 +0000245 sleep(1); /* wait for parent */
subrata_modakbdbaec52009-02-26 12:14:51 +0000246
robbiewd34d5812005-07-11 22:28:09 +0000247 exit(exno);
248}
robbiewe237cd32002-12-31 21:21:45 +0000249
250void setup()
251{
subrata_modak56207ce2009-03-23 13:35:39 +0000252 temp = stderr;
robbiewe237cd32002-12-31 21:21:45 +0000253}
254
255int blenter()
256{
subrata_modak56207ce2009-03-23 13:35:39 +0000257 //tst_resm(TINFO, "Enter block %d", block_number);
258 local_flag = PASSED;
259 return 0;
robbiewe237cd32002-12-31 21:21:45 +0000260}
261
subrata_modak56207ce2009-03-23 13:35:39 +0000262void terror(char *message)
robbiewe237cd32002-12-31 21:21:45 +0000263{
subrata_modak56207ce2009-03-23 13:35:39 +0000264 tst_resm(TBROK, "Reason: %s:%s", message, strerror(errno));
robbiewe237cd32002-12-31 21:21:45 +0000265}
266
267void fail_exit()
268{
subrata_modak56207ce2009-03-23 13:35:39 +0000269 local_flag = FAILED;
270 anyfail();
robbiewe237cd32002-12-31 21:21:45 +0000271
272}
273
274int forkfail()
275{
subrata_modak56207ce2009-03-23 13:35:39 +0000276 tst_resm(TBROK, "FORK FAILED - terminating test.");
277 tst_exit();
278 return 0;
robbiewe237cd32002-12-31 21:21:45 +0000279}
robbiewe237cd32002-12-31 21:21:45 +0000280
subrata_modak56207ce2009-03-23 13:35:39 +0000281/****** ** ** *******/