xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/tgkill/tgkill01.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) 2018 Google, Inc.
4*49cdfc7eSAndroid Build Coastguard Worker  *
5*49cdfc7eSAndroid Build Coastguard Worker  * tgkill() delivers a signal to a specific thread.  Test this by installing
6*49cdfc7eSAndroid Build Coastguard Worker  * a SIGUSR1 handler which records the current pthread ID.  Start a number
7*49cdfc7eSAndroid Build Coastguard Worker  * of threads in parallel, then one-by-one call tgkill(..., tid, SIGUSR1)
8*49cdfc7eSAndroid Build Coastguard Worker  * and check that the expected pthread ID was recorded.
9*49cdfc7eSAndroid Build Coastguard Worker  */
10*49cdfc7eSAndroid Build Coastguard Worker 
11*49cdfc7eSAndroid Build Coastguard Worker #include <pthread.h>
12*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
13*49cdfc7eSAndroid Build Coastguard Worker 
14*49cdfc7eSAndroid Build Coastguard Worker #include "tst_safe_pthread.h"
15*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
16*49cdfc7eSAndroid Build Coastguard Worker #include "tgkill.h"
17*49cdfc7eSAndroid Build Coastguard Worker 
18*49cdfc7eSAndroid Build Coastguard Worker struct thread_state {
19*49cdfc7eSAndroid Build Coastguard Worker 	pthread_t thread;
20*49cdfc7eSAndroid Build Coastguard Worker 	pid_t tid;
21*49cdfc7eSAndroid Build Coastguard Worker };
22*49cdfc7eSAndroid Build Coastguard Worker 
23*49cdfc7eSAndroid Build Coastguard Worker static char *str_threads;
24*49cdfc7eSAndroid Build Coastguard Worker static int n_threads = 10;
25*49cdfc7eSAndroid Build Coastguard Worker static struct thread_state *threads;
26*49cdfc7eSAndroid Build Coastguard Worker 
27*49cdfc7eSAndroid Build Coastguard Worker static pthread_t sigusr1_thread;
28*49cdfc7eSAndroid Build Coastguard Worker 
sigusr1_handler(int signum)29*49cdfc7eSAndroid Build Coastguard Worker static void sigusr1_handler(int signum __attribute__((unused)))
30*49cdfc7eSAndroid Build Coastguard Worker {
31*49cdfc7eSAndroid Build Coastguard Worker 	sigusr1_thread = pthread_self();
32*49cdfc7eSAndroid Build Coastguard Worker }
33*49cdfc7eSAndroid Build Coastguard Worker 
thread_func(void * arg)34*49cdfc7eSAndroid Build Coastguard Worker static void *thread_func(void *arg)
35*49cdfc7eSAndroid Build Coastguard Worker {
36*49cdfc7eSAndroid Build Coastguard Worker 	struct thread_state *thread = arg;
37*49cdfc7eSAndroid Build Coastguard Worker 
38*49cdfc7eSAndroid Build Coastguard Worker 	/**
39*49cdfc7eSAndroid Build Coastguard Worker 	 * There is no standard way to map pthread -> tid, so we will have the
40*49cdfc7eSAndroid Build Coastguard Worker 	 * child stash its own tid then notify the parent that the stashed tid
41*49cdfc7eSAndroid Build Coastguard Worker 	 * is available.
42*49cdfc7eSAndroid Build Coastguard Worker 	 */
43*49cdfc7eSAndroid Build Coastguard Worker 	thread->tid = sys_gettid();
44*49cdfc7eSAndroid Build Coastguard Worker 
45*49cdfc7eSAndroid Build Coastguard Worker 	TST_CHECKPOINT_WAKE(0);
46*49cdfc7eSAndroid Build Coastguard Worker 
47*49cdfc7eSAndroid Build Coastguard Worker 	TST_CHECKPOINT_WAIT(1);
48*49cdfc7eSAndroid Build Coastguard Worker 
49*49cdfc7eSAndroid Build Coastguard Worker 	return arg;
50*49cdfc7eSAndroid Build Coastguard Worker }
51*49cdfc7eSAndroid Build Coastguard Worker 
start_thread(struct thread_state * thread)52*49cdfc7eSAndroid Build Coastguard Worker static void start_thread(struct thread_state *thread)
53*49cdfc7eSAndroid Build Coastguard Worker {
54*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_PTHREAD_CREATE(&thread->thread, NULL, thread_func, thread);
55*49cdfc7eSAndroid Build Coastguard Worker 
56*49cdfc7eSAndroid Build Coastguard Worker 	TST_CHECKPOINT_WAIT(0);
57*49cdfc7eSAndroid Build Coastguard Worker }
58*49cdfc7eSAndroid Build Coastguard Worker 
stop_threads(void)59*49cdfc7eSAndroid Build Coastguard Worker static void stop_threads(void)
60*49cdfc7eSAndroid Build Coastguard Worker {
61*49cdfc7eSAndroid Build Coastguard Worker 	int i;
62*49cdfc7eSAndroid Build Coastguard Worker 
63*49cdfc7eSAndroid Build Coastguard Worker 	TST_CHECKPOINT_WAKE2(1, n_threads);
64*49cdfc7eSAndroid Build Coastguard Worker 
65*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < n_threads; i++) {
66*49cdfc7eSAndroid Build Coastguard Worker 		if (threads[i].tid == -1)
67*49cdfc7eSAndroid Build Coastguard Worker 			continue;
68*49cdfc7eSAndroid Build Coastguard Worker 
69*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_PTHREAD_JOIN(threads[i].thread, NULL);
70*49cdfc7eSAndroid Build Coastguard Worker 		threads[i].tid = -1;
71*49cdfc7eSAndroid Build Coastguard Worker 	}
72*49cdfc7eSAndroid Build Coastguard Worker 
73*49cdfc7eSAndroid Build Coastguard Worker 	if (threads)
74*49cdfc7eSAndroid Build Coastguard Worker 		free(threads);
75*49cdfc7eSAndroid Build Coastguard Worker }
76*49cdfc7eSAndroid Build Coastguard Worker 
run(void)77*49cdfc7eSAndroid Build Coastguard Worker static void run(void)
78*49cdfc7eSAndroid Build Coastguard Worker {
79*49cdfc7eSAndroid Build Coastguard Worker 	int i;
80*49cdfc7eSAndroid Build Coastguard Worker 
81*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < n_threads; i++) {
82*49cdfc7eSAndroid Build Coastguard Worker 		sigusr1_thread = pthread_self();
83*49cdfc7eSAndroid Build Coastguard Worker 
84*49cdfc7eSAndroid Build Coastguard Worker 		TEST(sys_tgkill(getpid(), threads[i].tid, SIGUSR1));
85*49cdfc7eSAndroid Build Coastguard Worker 		if (TST_RET) {
86*49cdfc7eSAndroid Build Coastguard Worker 			tst_res(TFAIL | TTERRNO, "tgkill() failed");
87*49cdfc7eSAndroid Build Coastguard Worker 			return;
88*49cdfc7eSAndroid Build Coastguard Worker 		}
89*49cdfc7eSAndroid Build Coastguard Worker 
90*49cdfc7eSAndroid Build Coastguard Worker 		while (pthread_equal(sigusr1_thread, pthread_self()))
91*49cdfc7eSAndroid Build Coastguard Worker 			usleep(1000);
92*49cdfc7eSAndroid Build Coastguard Worker 
93*49cdfc7eSAndroid Build Coastguard Worker 		if (!pthread_equal(sigusr1_thread, threads[i].thread)) {
94*49cdfc7eSAndroid Build Coastguard Worker 			tst_res(TFAIL, "SIGUSR1 delivered to wrong thread");
95*49cdfc7eSAndroid Build Coastguard Worker 			return;
96*49cdfc7eSAndroid Build Coastguard Worker 		}
97*49cdfc7eSAndroid Build Coastguard Worker 	}
98*49cdfc7eSAndroid Build Coastguard Worker 
99*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TPASS, "SIGUSR1 delivered to correct threads");
100*49cdfc7eSAndroid Build Coastguard Worker }
101*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)102*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
103*49cdfc7eSAndroid Build Coastguard Worker {
104*49cdfc7eSAndroid Build Coastguard Worker 	int i;
105*49cdfc7eSAndroid Build Coastguard Worker 
106*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_parse_int(str_threads, &n_threads, 1, INT_MAX))
107*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK, "Invalid number of threads '%s'", str_threads);
108*49cdfc7eSAndroid Build Coastguard Worker 
109*49cdfc7eSAndroid Build Coastguard Worker 	threads = SAFE_MALLOC(sizeof(*threads) * n_threads);
110*49cdfc7eSAndroid Build Coastguard Worker 
111*49cdfc7eSAndroid Build Coastguard Worker 	struct sigaction sigusr1 = {
112*49cdfc7eSAndroid Build Coastguard Worker 		.sa_handler = sigusr1_handler,
113*49cdfc7eSAndroid Build Coastguard Worker 	};
114*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SIGACTION(SIGUSR1, &sigusr1, NULL);
115*49cdfc7eSAndroid Build Coastguard Worker 
116*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < n_threads; i++)
117*49cdfc7eSAndroid Build Coastguard Worker 		threads[i].tid = -1;
118*49cdfc7eSAndroid Build Coastguard Worker 
119*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < n_threads; i++)
120*49cdfc7eSAndroid Build Coastguard Worker 		start_thread(&threads[i]);
121*49cdfc7eSAndroid Build Coastguard Worker }
122*49cdfc7eSAndroid Build Coastguard Worker 
123*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
124*49cdfc7eSAndroid Build Coastguard Worker 	.options = (struct tst_option[]) {
125*49cdfc7eSAndroid Build Coastguard Worker 		{"t:", &str_threads, "Number of threads (default 10)"},
126*49cdfc7eSAndroid Build Coastguard Worker 		{}
127*49cdfc7eSAndroid Build Coastguard Worker 	},
128*49cdfc7eSAndroid Build Coastguard Worker 	.needs_checkpoints = 1,
129*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
130*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = run,
131*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = stop_threads,
132*49cdfc7eSAndroid Build Coastguard Worker };
133