• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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
17  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
20 /*
21  * NAME
22  *	kill01.c
23  *
24  * DESCRIPTION
25  *	Test case to check the basic functionality of kill().
26  *
27  * ALGORITHM
28  *	call setup
29  *	loop if the -i option was given
30  *	fork a child
31  *	execute the kill system call
32  *	check the return value
33  *	if return value is -1
34  *		issue a FAIL message, break remaining tests and cleanup
35  *	if we are doing functional testing
36  *		if the process was terminated with the expected signal.
37  *			issue a PASS message
38  *		otherwise
39  *			issue a FAIL message
40  *	call cleanup
41  *
42  * USAGE
43  *  kill01 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
44  *     where,  -c n : Run n copies concurrently.
45  *             -f   : Turn off functionality Testing.
46  *             -i n : Execute test n times.
47  *             -I x : Execute test for x seconds.
48  *             -P x : Pause for x seconds between iterations.
49  *             -t   : Turn on syscall timing.
50  *
51  * HISTORY
52  *	07/2001 Ported by Wayne Boyer
53  *
54  * RESTRICTIONS
55  *	This test should be ran as a non-root user.
56  */
57 
58 #include "test.h"
59 
60 #include <signal.h>
61 #include <errno.h>
62 #include <sys/wait.h>
63 
64 void cleanup(void);
65 void setup(void);
66 void do_child(void);
67 
68 char *TCID = "kill01";
69 int TST_TOTAL = 1;
70 
71 #define TEST_SIG SIGKILL
72 
main(int ac,char ** av)73 int main(int ac, char **av)
74 {
75 	int lc;
76 	pid_t pid;
77 	int exno, status, nsig;
78 
79 	tst_parse_opts(ac, av, NULL, NULL);
80 #ifdef UCLINUX
81 	maybe_run_child(&do_child, "");
82 #endif
83 
84 	setup();		/* global setup */
85 
86 	/* The following loop checks looping state if -i option given */
87 	for (lc = 0; TEST_LOOPING(lc); lc++) {
88 
89 		/* reset tst_count in case we are looping */
90 		tst_count = 0;
91 		status = 1;
92 		exno = 1;
93 		pid = FORK_OR_VFORK();
94 		if (pid < 0) {
95 			tst_brkm(TBROK, cleanup, "Fork of child failed");
96 		} else if (pid == 0) {
97 #ifdef UCLINUX
98 			if (self_exec(av[0], "") < 0) {
99 				tst_brkm(TBROK, cleanup,
100 					 "self_exec of child failed");
101 			}
102 #else
103 			do_child();
104 #endif
105 		} else {
106 			TEST(kill(pid, TEST_SIG));
107 			waitpid(pid, &status, 0);
108 		}
109 
110 		if (TEST_RETURN == -1) {
111 			tst_brkm(TFAIL, cleanup, "%s failed - errno = %d : %s",
112 				 TCID, TEST_ERRNO, strerror(TEST_ERRNO));
113 		}
114 
115 		/*
116 		 * Check to see if the process was terminated with the
117 		 * expected signal.
118 		 */
119 		nsig = WTERMSIG(status);
120 		if (nsig == TEST_SIG) {
121 			tst_resm(TPASS, "received expected signal %d",
122 				 nsig);
123 		} else {
124 			tst_resm(TFAIL,
125 				 "expected signal %d received %d",
126 				 TEST_SIG, nsig);
127 		}
128 	}
129 
130 	cleanup();
131 	tst_exit();
132 }
133 
134 /*
135  * do_child()
136  */
do_child(void)137 void do_child(void)
138 {
139 	int exno = 1;
140 
141 	pause();
142 	exit(exno);
143 }
144 
145 /*
146  * setup() - performs all ONE TIME setup for this test
147  */
setup(void)148 void setup(void)
149 {
150 
151 	TEST_PAUSE;
152 }
153 
154 /*
155  * cleanup() - performs all the ONE TIME cleanup for this test at completion
156  * or premature exit.
157  */
cleanup(void)158 void cleanup(void)
159 {
160 
161 }
162