1*49cdfc7eSAndroid Build Coastguard Worker /* IBM Corporation */
2*49cdfc7eSAndroid Build Coastguard Worker /* 01/02/2003 Port to LTP [email protected] */
3*49cdfc7eSAndroid Build Coastguard Worker /* 06/30/2001 Port to Linux [email protected] */
4*49cdfc7eSAndroid Build Coastguard Worker
5*49cdfc7eSAndroid Build Coastguard Worker /*
6*49cdfc7eSAndroid Build Coastguard Worker *
7*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) International Business Machines Corp., 2002
8*49cdfc7eSAndroid Build Coastguard Worker *
9*49cdfc7eSAndroid Build Coastguard Worker * This program is free software; you can redistribute it and/or modify
10*49cdfc7eSAndroid Build Coastguard Worker * it under the terms of the GNU General Public License as published by
11*49cdfc7eSAndroid Build Coastguard Worker * the Free Software Foundation; either version 2 of the License, or
12*49cdfc7eSAndroid Build Coastguard Worker * (at your option) any later version.
13*49cdfc7eSAndroid Build Coastguard Worker *
14*49cdfc7eSAndroid Build Coastguard Worker * This program is distributed in the hope that it will be useful,
15*49cdfc7eSAndroid Build Coastguard Worker * but WITHOUT ANY WARRANTY; without even the implied warranty of
16*49cdfc7eSAndroid Build Coastguard Worker * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
17*49cdfc7eSAndroid Build Coastguard Worker * the GNU General Public License for more details.
18*49cdfc7eSAndroid Build Coastguard Worker *
19*49cdfc7eSAndroid Build Coastguard Worker * You should have received a copy of the GNU General Public License
20*49cdfc7eSAndroid Build Coastguard Worker * along with this program; if not, write to the Free Software
21*49cdfc7eSAndroid Build Coastguard Worker * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22*49cdfc7eSAndroid Build Coastguard Worker */
23*49cdfc7eSAndroid Build Coastguard Worker
24*49cdfc7eSAndroid Build Coastguard Worker /*kill2.c */
25*49cdfc7eSAndroid Build Coastguard Worker /*======================================================================
26*49cdfc7eSAndroid Build Coastguard Worker >KEYS: < kill(), wait(), signal()
27*49cdfc7eSAndroid Build Coastguard Worker >WHAT: < Check that when a child is killed by its parent, it returns the
28*49cdfc7eSAndroid Build Coastguard Worker < correct values to the waiting parent--the child sets signal to
29*49cdfc7eSAndroid Build Coastguard Worker < ignore the kill
30*49cdfc7eSAndroid Build Coastguard Worker >HOW: < For each signal: Send that signal to a child that has elected
31*49cdfc7eSAndroid Build Coastguard Worker < to catch the signal, check that the correct status was returned
32*49cdfc7eSAndroid Build Coastguard Worker < to the waiting parent.
33*49cdfc7eSAndroid Build Coastguard Worker < NOTE: Signal 9 (kill) is not catchable, and must be dealt with
34*49cdfc7eSAndroid Build Coastguard Worker < separately.
35*49cdfc7eSAndroid Build Coastguard Worker >BUGS: < None known
36*49cdfc7eSAndroid Build Coastguard Worker ======================================================================*/
37*49cdfc7eSAndroid Build Coastguard Worker #ifndef _GNU_SOURCE
38*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE 1
39*49cdfc7eSAndroid Build Coastguard Worker #endif
40*49cdfc7eSAndroid Build Coastguard Worker
41*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
42*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
43*49cdfc7eSAndroid Build Coastguard Worker #include <signal.h>
44*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
45*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
46*49cdfc7eSAndroid Build Coastguard Worker #include <sys/wait.h>
47*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
48*49cdfc7eSAndroid Build Coastguard Worker
49*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
50*49cdfc7eSAndroid Build Coastguard Worker #define ITER 3
51*49cdfc7eSAndroid Build Coastguard Worker #define FAILED 0
52*49cdfc7eSAndroid Build Coastguard Worker #define PASSED 1
53*49cdfc7eSAndroid Build Coastguard Worker
54*49cdfc7eSAndroid Build Coastguard Worker char *TCID = "kill12";
55*49cdfc7eSAndroid Build Coastguard Worker
56*49cdfc7eSAndroid Build Coastguard Worker int local_flag = PASSED;
57*49cdfc7eSAndroid Build Coastguard Worker int block_number;
58*49cdfc7eSAndroid Build Coastguard Worker FILE *temp;
59*49cdfc7eSAndroid Build Coastguard Worker int TST_TOTAL = 1;
60*49cdfc7eSAndroid Build Coastguard Worker static int sig;
61*49cdfc7eSAndroid Build Coastguard Worker
62*49cdfc7eSAndroid Build Coastguard Worker int anyfail();
63*49cdfc7eSAndroid Build Coastguard Worker int blenter();
64*49cdfc7eSAndroid Build Coastguard Worker int instress();
65*49cdfc7eSAndroid Build Coastguard Worker void setup();
66*49cdfc7eSAndroid Build Coastguard Worker void terror();
67*49cdfc7eSAndroid Build Coastguard Worker void fail_exit();
68*49cdfc7eSAndroid Build Coastguard Worker void ok_exit();
69*49cdfc7eSAndroid Build Coastguard Worker int forkfail();
70*49cdfc7eSAndroid Build Coastguard Worker void do_child();
71*49cdfc7eSAndroid Build Coastguard Worker
72*49cdfc7eSAndroid Build Coastguard Worker int chflag;
73*49cdfc7eSAndroid Build Coastguard Worker
main(int argc,char ** argv)74*49cdfc7eSAndroid Build Coastguard Worker int main(int argc, char **argv)
75*49cdfc7eSAndroid Build Coastguard Worker {
76*49cdfc7eSAndroid Build Coastguard Worker int pid, npid;
77*49cdfc7eSAndroid Build Coastguard Worker int nsig, exno, nexno, status;
78*49cdfc7eSAndroid Build Coastguard Worker int ret_val = 0;
79*49cdfc7eSAndroid Build Coastguard Worker int core;
80*49cdfc7eSAndroid Build Coastguard Worker void chsig();
81*49cdfc7eSAndroid Build Coastguard Worker
82*49cdfc7eSAndroid Build Coastguard Worker tst_parse_opts(argc, argv, NULL, NULL);
83*49cdfc7eSAndroid Build Coastguard Worker
84*49cdfc7eSAndroid Build Coastguard Worker setup();
85*49cdfc7eSAndroid Build Coastguard Worker blenter();
86*49cdfc7eSAndroid Build Coastguard Worker
87*49cdfc7eSAndroid Build Coastguard Worker exno = 1;
88*49cdfc7eSAndroid Build Coastguard Worker
89*49cdfc7eSAndroid Build Coastguard Worker if (sigset(SIGCHLD, chsig) == SIG_ERR) {
90*49cdfc7eSAndroid Build Coastguard Worker fprintf(temp, "\tsigset failed, errno = %d\n", errno);
91*49cdfc7eSAndroid Build Coastguard Worker fail_exit();
92*49cdfc7eSAndroid Build Coastguard Worker }
93*49cdfc7eSAndroid Build Coastguard Worker
94*49cdfc7eSAndroid Build Coastguard Worker for (sig = 1; sig < 14; sig++) {
95*49cdfc7eSAndroid Build Coastguard Worker fflush(temp);
96*49cdfc7eSAndroid Build Coastguard Worker chflag = 0;
97*49cdfc7eSAndroid Build Coastguard Worker
98*49cdfc7eSAndroid Build Coastguard Worker pid = tst_fork();
99*49cdfc7eSAndroid Build Coastguard Worker if (pid < 0) {
100*49cdfc7eSAndroid Build Coastguard Worker forkfail();
101*49cdfc7eSAndroid Build Coastguard Worker }
102*49cdfc7eSAndroid Build Coastguard Worker
103*49cdfc7eSAndroid Build Coastguard Worker if (pid == 0) {
104*49cdfc7eSAndroid Build Coastguard Worker do_child();
105*49cdfc7eSAndroid Build Coastguard Worker } else {
106*49cdfc7eSAndroid Build Coastguard Worker //fprintf(temp, "Testing signal %d\n", sig);
107*49cdfc7eSAndroid Build Coastguard Worker
108*49cdfc7eSAndroid Build Coastguard Worker while (!chflag) /* wait for child */
109*49cdfc7eSAndroid Build Coastguard Worker sleep(1);
110*49cdfc7eSAndroid Build Coastguard Worker
111*49cdfc7eSAndroid Build Coastguard Worker kill(pid, sig); /* child should ignroe this sig */
112*49cdfc7eSAndroid Build Coastguard Worker kill(pid, SIGCHLD); /* child should exit */
113*49cdfc7eSAndroid Build Coastguard Worker
114*49cdfc7eSAndroid Build Coastguard Worker #ifdef BCS
115*49cdfc7eSAndroid Build Coastguard Worker while ((npid = wait(&status)) != pid
116*49cdfc7eSAndroid Build Coastguard Worker || (npid == -1 && errno == EINTR)) ;
117*49cdfc7eSAndroid Build Coastguard Worker if (npid != pid) {
118*49cdfc7eSAndroid Build Coastguard Worker fprintf(temp,
119*49cdfc7eSAndroid Build Coastguard Worker "wait error: wait returned wrong pid\n");
120*49cdfc7eSAndroid Build Coastguard Worker ret_val = 1;
121*49cdfc7eSAndroid Build Coastguard Worker }
122*49cdfc7eSAndroid Build Coastguard Worker #else
123*49cdfc7eSAndroid Build Coastguard Worker while ((npid = waitpid(pid, &status, 0)) != -1
124*49cdfc7eSAndroid Build Coastguard Worker || errno == EINTR) ;
125*49cdfc7eSAndroid Build Coastguard Worker #endif
126*49cdfc7eSAndroid Build Coastguard Worker
127*49cdfc7eSAndroid Build Coastguard Worker /*
128*49cdfc7eSAndroid Build Coastguard Worker nsig = status & 0177;
129*49cdfc7eSAndroid Build Coastguard Worker core = status & 0200;
130*49cdfc7eSAndroid Build Coastguard Worker nexno = (status & 0xff00) >> 8;
131*49cdfc7eSAndroid Build Coastguard Worker */
132*49cdfc7eSAndroid Build Coastguard Worker /***** LTP Port *****/
133*49cdfc7eSAndroid Build Coastguard Worker nsig = WTERMSIG(status);
134*49cdfc7eSAndroid Build Coastguard Worker #ifdef WCOREDUMP
135*49cdfc7eSAndroid Build Coastguard Worker core = WCOREDUMP(status);
136*49cdfc7eSAndroid Build Coastguard Worker #endif
137*49cdfc7eSAndroid Build Coastguard Worker nexno = WIFEXITED(status);
138*49cdfc7eSAndroid Build Coastguard Worker /***** ** ** *****/
139*49cdfc7eSAndroid Build Coastguard Worker
140*49cdfc7eSAndroid Build Coastguard Worker /* nsig is the signal number returned by wait
141*49cdfc7eSAndroid Build Coastguard Worker it should be 0, except when sig = 9 */
142*49cdfc7eSAndroid Build Coastguard Worker
143*49cdfc7eSAndroid Build Coastguard Worker if ((sig == 9) && (nsig != sig)) {
144*49cdfc7eSAndroid Build Coastguard Worker fprintf(temp, "wait error: unexpected signal"
145*49cdfc7eSAndroid Build Coastguard Worker " returned when the signal sent was 9"
146*49cdfc7eSAndroid Build Coastguard Worker " The status of the process is %d \n",
147*49cdfc7eSAndroid Build Coastguard Worker status);
148*49cdfc7eSAndroid Build Coastguard Worker ret_val = 1;
149*49cdfc7eSAndroid Build Coastguard Worker }
150*49cdfc7eSAndroid Build Coastguard Worker if ((sig != 9) && (nsig != 0)) {
151*49cdfc7eSAndroid Build Coastguard Worker fprintf(temp, "wait error: unexpected signal "
152*49cdfc7eSAndroid Build Coastguard Worker "returned, the status of the process is "
153*49cdfc7eSAndroid Build Coastguard Worker "%d \n", status);
154*49cdfc7eSAndroid Build Coastguard Worker ret_val = 1;
155*49cdfc7eSAndroid Build Coastguard Worker }
156*49cdfc7eSAndroid Build Coastguard Worker
157*49cdfc7eSAndroid Build Coastguard Worker /* nexno is the exit number returned by wait
158*49cdfc7eSAndroid Build Coastguard Worker it should be 1, except when sig = 9 */
159*49cdfc7eSAndroid Build Coastguard Worker
160*49cdfc7eSAndroid Build Coastguard Worker if (sig == 9)
161*49cdfc7eSAndroid Build Coastguard Worker if (nexno != 0) {
162*49cdfc7eSAndroid Build Coastguard Worker fprintf(temp, "signal error: unexpected"
163*49cdfc7eSAndroid Build Coastguard Worker " exit number returned when"
164*49cdfc7eSAndroid Build Coastguard Worker " signal sent was 9, the status"
165*49cdfc7eSAndroid Build Coastguard Worker " of the process is %d \n",
166*49cdfc7eSAndroid Build Coastguard Worker status);
167*49cdfc7eSAndroid Build Coastguard Worker ret_val = 1;
168*49cdfc7eSAndroid Build Coastguard Worker } else;
169*49cdfc7eSAndroid Build Coastguard Worker else if (nexno != 1) {
170*49cdfc7eSAndroid Build Coastguard Worker fprintf(temp, "signal error: unexpected exit "
171*49cdfc7eSAndroid Build Coastguard Worker "number returned,the status of the"
172*49cdfc7eSAndroid Build Coastguard Worker " process is %d\n", status);
173*49cdfc7eSAndroid Build Coastguard Worker ret_val = 1;
174*49cdfc7eSAndroid Build Coastguard Worker }
175*49cdfc7eSAndroid Build Coastguard Worker }
176*49cdfc7eSAndroid Build Coastguard Worker }
177*49cdfc7eSAndroid Build Coastguard Worker if (ret_val)
178*49cdfc7eSAndroid Build Coastguard Worker local_flag = FAILED;
179*49cdfc7eSAndroid Build Coastguard Worker
180*49cdfc7eSAndroid Build Coastguard Worker anyfail();
181*49cdfc7eSAndroid Build Coastguard Worker tst_exit();
182*49cdfc7eSAndroid Build Coastguard Worker }
183*49cdfc7eSAndroid Build Coastguard Worker
chsig(void)184*49cdfc7eSAndroid Build Coastguard Worker void chsig(void)
185*49cdfc7eSAndroid Build Coastguard Worker {
186*49cdfc7eSAndroid Build Coastguard Worker chflag++;
187*49cdfc7eSAndroid Build Coastguard Worker }
188*49cdfc7eSAndroid Build Coastguard Worker
anyfail(void)189*49cdfc7eSAndroid Build Coastguard Worker int anyfail(void)
190*49cdfc7eSAndroid Build Coastguard Worker {
191*49cdfc7eSAndroid Build Coastguard Worker (local_flag == FAILED) ? tst_resm(TFAIL,
192*49cdfc7eSAndroid Build Coastguard Worker "Test failed") : tst_resm(TPASS,
193*49cdfc7eSAndroid Build Coastguard Worker "Test passed");
194*49cdfc7eSAndroid Build Coastguard Worker tst_exit();
195*49cdfc7eSAndroid Build Coastguard Worker }
196*49cdfc7eSAndroid Build Coastguard Worker
do_child(void)197*49cdfc7eSAndroid Build Coastguard Worker void do_child(void)
198*49cdfc7eSAndroid Build Coastguard Worker {
199*49cdfc7eSAndroid Build Coastguard Worker int exno = 1;
200*49cdfc7eSAndroid Build Coastguard Worker
201*49cdfc7eSAndroid Build Coastguard Worker sigset(sig, SIG_IGN); /* set to ignore signal */
202*49cdfc7eSAndroid Build Coastguard Worker kill(getppid(), SIGCHLD); /* tell parent we are ready */
203*49cdfc7eSAndroid Build Coastguard Worker while (!chflag)
204*49cdfc7eSAndroid Build Coastguard Worker sleep(1); /* wait for parent */
205*49cdfc7eSAndroid Build Coastguard Worker
206*49cdfc7eSAndroid Build Coastguard Worker exit(exno);
207*49cdfc7eSAndroid Build Coastguard Worker }
208*49cdfc7eSAndroid Build Coastguard Worker
setup(void)209*49cdfc7eSAndroid Build Coastguard Worker void setup(void)
210*49cdfc7eSAndroid Build Coastguard Worker {
211*49cdfc7eSAndroid Build Coastguard Worker temp = stderr;
212*49cdfc7eSAndroid Build Coastguard Worker }
213*49cdfc7eSAndroid Build Coastguard Worker
blenter(void)214*49cdfc7eSAndroid Build Coastguard Worker int blenter(void)
215*49cdfc7eSAndroid Build Coastguard Worker {
216*49cdfc7eSAndroid Build Coastguard Worker //tst_resm(TINFO, "Enter block %d", block_number);
217*49cdfc7eSAndroid Build Coastguard Worker local_flag = PASSED;
218*49cdfc7eSAndroid Build Coastguard Worker return 0;
219*49cdfc7eSAndroid Build Coastguard Worker }
220*49cdfc7eSAndroid Build Coastguard Worker
terror(char * message)221*49cdfc7eSAndroid Build Coastguard Worker void terror(char *message)
222*49cdfc7eSAndroid Build Coastguard Worker {
223*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TBROK, "Reason: %s:%s", message, strerror(errno));
224*49cdfc7eSAndroid Build Coastguard Worker }
225*49cdfc7eSAndroid Build Coastguard Worker
fail_exit(void)226*49cdfc7eSAndroid Build Coastguard Worker void fail_exit(void)
227*49cdfc7eSAndroid Build Coastguard Worker {
228*49cdfc7eSAndroid Build Coastguard Worker local_flag = FAILED;
229*49cdfc7eSAndroid Build Coastguard Worker anyfail();
230*49cdfc7eSAndroid Build Coastguard Worker
231*49cdfc7eSAndroid Build Coastguard Worker }
232*49cdfc7eSAndroid Build Coastguard Worker
forkfail(void)233*49cdfc7eSAndroid Build Coastguard Worker int forkfail(void)
234*49cdfc7eSAndroid Build Coastguard Worker {
235*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TBROK, NULL, "FORK FAILED - terminating test.");
236*49cdfc7eSAndroid Build Coastguard Worker }
237