xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/tkill/tkill01.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) Linux Test Project, 2009-2021
4*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) Crackerjack Project, 2007
5*49cdfc7eSAndroid Build Coastguard Worker  * Ported from Crackerjack to LTP by Manas Kumar Nayak [email protected]>
6*49cdfc7eSAndroid Build Coastguard Worker  */
7*49cdfc7eSAndroid Build Coastguard Worker 
8*49cdfc7eSAndroid Build Coastguard Worker /*\
9*49cdfc7eSAndroid Build Coastguard Worker  * [Description]
10*49cdfc7eSAndroid Build Coastguard Worker  *
11*49cdfc7eSAndroid Build Coastguard Worker  * Basic tests for the tkill syscall.
12*49cdfc7eSAndroid Build Coastguard Worker  *
13*49cdfc7eSAndroid Build Coastguard Worker  * [Algorithm]
14*49cdfc7eSAndroid Build Coastguard Worker  *
15*49cdfc7eSAndroid Build Coastguard Worker  * Calls tkill and capture signal to verify success.
16*49cdfc7eSAndroid Build Coastguard Worker  */
17*49cdfc7eSAndroid Build Coastguard Worker 
18*49cdfc7eSAndroid Build Coastguard Worker #include <signal.h>
19*49cdfc7eSAndroid Build Coastguard Worker 
20*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
21*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/syscalls.h"
22*49cdfc7eSAndroid Build Coastguard Worker 
23*49cdfc7eSAndroid Build Coastguard Worker static volatile sig_atomic_t sig_flag;
24*49cdfc7eSAndroid Build Coastguard Worker 
sighandler(int sig)25*49cdfc7eSAndroid Build Coastguard Worker static void sighandler(int sig)
26*49cdfc7eSAndroid Build Coastguard Worker {
27*49cdfc7eSAndroid Build Coastguard Worker 	if (sig == SIGUSR1)
28*49cdfc7eSAndroid Build Coastguard Worker 		sig_flag = 1;
29*49cdfc7eSAndroid Build Coastguard Worker }
30*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)31*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
32*49cdfc7eSAndroid Build Coastguard Worker {
33*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SIGNAL(SIGUSR1, sighandler);
34*49cdfc7eSAndroid Build Coastguard Worker }
35*49cdfc7eSAndroid Build Coastguard Worker 
run(void)36*49cdfc7eSAndroid Build Coastguard Worker static void run(void)
37*49cdfc7eSAndroid Build Coastguard Worker {
38*49cdfc7eSAndroid Build Coastguard Worker 	int tid;
39*49cdfc7eSAndroid Build Coastguard Worker 	int timeout_ms = 1000;
40*49cdfc7eSAndroid Build Coastguard Worker 	sig_flag = 0;
41*49cdfc7eSAndroid Build Coastguard Worker 
42*49cdfc7eSAndroid Build Coastguard Worker 	tid = tst_syscall(__NR_gettid);
43*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_PASS(tst_syscall(__NR_tkill, tid, SIGUSR1));
44*49cdfc7eSAndroid Build Coastguard Worker 
45*49cdfc7eSAndroid Build Coastguard Worker 	while (timeout_ms--) {
46*49cdfc7eSAndroid Build Coastguard Worker 		if (sig_flag)
47*49cdfc7eSAndroid Build Coastguard Worker 			break;
48*49cdfc7eSAndroid Build Coastguard Worker 
49*49cdfc7eSAndroid Build Coastguard Worker 		usleep(1000);
50*49cdfc7eSAndroid Build Coastguard Worker 	}
51*49cdfc7eSAndroid Build Coastguard Worker 
52*49cdfc7eSAndroid Build Coastguard Worker 	if (sig_flag)
53*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "signal captured");
54*49cdfc7eSAndroid Build Coastguard Worker 	else
55*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "signal not captured");
56*49cdfc7eSAndroid Build Coastguard Worker }
57*49cdfc7eSAndroid Build Coastguard Worker 
58*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
59*49cdfc7eSAndroid Build Coastguard Worker 	.needs_tmpdir = 1,
60*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
61*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = run,
62*49cdfc7eSAndroid Build Coastguard Worker };
63