xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/kill/kill03.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) International Business Machines  Corp., 2001
4*49cdfc7eSAndroid Build Coastguard Worker  *
5*49cdfc7eSAndroid Build Coastguard Worker  * 1) kill() fails with errno set to EINVAL if given an invalid signal.
6*49cdfc7eSAndroid Build Coastguard Worker  * 2) kill() fails with errno set to ESRCH if given a non-existent pid.
7*49cdfc7eSAndroid Build Coastguard Worker  * 3) kill() fails with errno set to ESRCH if the given pid is INT_MIN.
8*49cdfc7eSAndroid Build Coastguard Worker  *
9*49cdfc7eSAndroid Build Coastguard Worker  * HISTORY
10*49cdfc7eSAndroid Build Coastguard Worker  *	07/2001 Ported by Wayne Boyer
11*49cdfc7eSAndroid Build Coastguard Worker  */
12*49cdfc7eSAndroid Build Coastguard Worker 
13*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
14*49cdfc7eSAndroid Build Coastguard Worker #include <signal.h>
15*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
16*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
17*49cdfc7eSAndroid Build Coastguard Worker 
18*49cdfc7eSAndroid Build Coastguard Worker static pid_t real_pid, fake_pid, int_min_pid;
19*49cdfc7eSAndroid Build Coastguard Worker 
20*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
21*49cdfc7eSAndroid Build Coastguard Worker 	int test_sig;
22*49cdfc7eSAndroid Build Coastguard Worker 	int exp_errno;
23*49cdfc7eSAndroid Build Coastguard Worker 	pid_t *pid;
24*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
25*49cdfc7eSAndroid Build Coastguard Worker 	{2000, EINVAL, &real_pid},
26*49cdfc7eSAndroid Build Coastguard Worker 	{SIGKILL, ESRCH, &fake_pid},
27*49cdfc7eSAndroid Build Coastguard Worker 	{SIGKILL, ESRCH, &int_min_pid}
28*49cdfc7eSAndroid Build Coastguard Worker };
29*49cdfc7eSAndroid Build Coastguard Worker 
verify_kill(unsigned int n)30*49cdfc7eSAndroid Build Coastguard Worker static void verify_kill(unsigned int n)
31*49cdfc7eSAndroid Build Coastguard Worker {
32*49cdfc7eSAndroid Build Coastguard Worker 	struct tcase *tc = &tcases[n];
33*49cdfc7eSAndroid Build Coastguard Worker 
34*49cdfc7eSAndroid Build Coastguard Worker 	TEST(kill(*tc->pid, tc->test_sig));
35*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET != -1) {
36*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "kill should fail but not, return %ld", TST_RET);
37*49cdfc7eSAndroid Build Coastguard Worker 		return;
38*49cdfc7eSAndroid Build Coastguard Worker 	}
39*49cdfc7eSAndroid Build Coastguard Worker 
40*49cdfc7eSAndroid Build Coastguard Worker 	if (tc->exp_errno == TST_ERR)
41*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS | TTERRNO, "kill failed as expected");
42*49cdfc7eSAndroid Build Coastguard Worker 	else
43*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL | TTERRNO, "kill expected %s but got",
44*49cdfc7eSAndroid Build Coastguard Worker 			tst_strerrno(tc->exp_errno));
45*49cdfc7eSAndroid Build Coastguard Worker }
46*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)47*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
48*49cdfc7eSAndroid Build Coastguard Worker {
49*49cdfc7eSAndroid Build Coastguard Worker 	real_pid = getpid();
50*49cdfc7eSAndroid Build Coastguard Worker 	fake_pid = tst_get_unused_pid();
51*49cdfc7eSAndroid Build Coastguard Worker 	int_min_pid = INT_MIN;
52*49cdfc7eSAndroid Build Coastguard Worker }
53*49cdfc7eSAndroid Build Coastguard Worker 
54*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
55*49cdfc7eSAndroid Build Coastguard Worker 	.test = verify_kill,
56*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(tcases),
57*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
58*49cdfc7eSAndroid Build Coastguard Worker };
59