xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/tgkill/tgkill02.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() should fail with EAGAIN when RLIMIT_SIGPENDING is reached with a
6*49cdfc7eSAndroid Build Coastguard Worker  * real-time signal.  Test this by starting a child thread with SIGRTMIN
7*49cdfc7eSAndroid Build Coastguard Worker  * blocked and a limit of 0 pending signals, then attempting to deliver
8*49cdfc7eSAndroid Build Coastguard Worker  * SIGRTMIN from the parent thread.
9*49cdfc7eSAndroid Build Coastguard Worker  */
10*49cdfc7eSAndroid Build Coastguard Worker 
11*49cdfc7eSAndroid Build Coastguard Worker #include <pthread.h>
12*49cdfc7eSAndroid Build Coastguard Worker #include <signal.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 
thread_func(void * arg)18*49cdfc7eSAndroid Build Coastguard Worker static void *thread_func(void *arg)
19*49cdfc7eSAndroid Build Coastguard Worker {
20*49cdfc7eSAndroid Build Coastguard Worker 	const struct rlimit sigpending = {
21*49cdfc7eSAndroid Build Coastguard Worker 		.rlim_cur = 0,
22*49cdfc7eSAndroid Build Coastguard Worker 		.rlim_max = 0,
23*49cdfc7eSAndroid Build Coastguard Worker 	};
24*49cdfc7eSAndroid Build Coastguard Worker 	sigset_t sigrtmin;
25*49cdfc7eSAndroid Build Coastguard Worker 	int err;
26*49cdfc7eSAndroid Build Coastguard Worker 	pid_t *tid = arg;
27*49cdfc7eSAndroid Build Coastguard Worker 
28*49cdfc7eSAndroid Build Coastguard Worker 	sigemptyset(&sigrtmin);
29*49cdfc7eSAndroid Build Coastguard Worker 	sigaddset(&sigrtmin, SIGRTMIN);
30*49cdfc7eSAndroid Build Coastguard Worker 
31*49cdfc7eSAndroid Build Coastguard Worker 	err = pthread_sigmask(SIG_BLOCK, &sigrtmin, NULL);
32*49cdfc7eSAndroid Build Coastguard Worker 	if (err)
33*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK, "pthread_sigmask() failed: %s",
34*49cdfc7eSAndroid Build Coastguard Worker 			tst_strerrno(err));
35*49cdfc7eSAndroid Build Coastguard Worker 
36*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SETRLIMIT(RLIMIT_SIGPENDING, &sigpending);
37*49cdfc7eSAndroid Build Coastguard Worker 	*tid = sys_gettid();
38*49cdfc7eSAndroid Build Coastguard Worker 
39*49cdfc7eSAndroid Build Coastguard Worker 	TST_CHECKPOINT_WAKE_AND_WAIT(0);
40*49cdfc7eSAndroid Build Coastguard Worker 
41*49cdfc7eSAndroid Build Coastguard Worker 	return arg;
42*49cdfc7eSAndroid Build Coastguard Worker }
43*49cdfc7eSAndroid Build Coastguard Worker 
run(void)44*49cdfc7eSAndroid Build Coastguard Worker static void run(void)
45*49cdfc7eSAndroid Build Coastguard Worker {
46*49cdfc7eSAndroid Build Coastguard Worker 	pthread_t thread;
47*49cdfc7eSAndroid Build Coastguard Worker 	pid_t tid = -1;
48*49cdfc7eSAndroid Build Coastguard Worker 
49*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_PTHREAD_CREATE(&thread, NULL, thread_func, &tid);
50*49cdfc7eSAndroid Build Coastguard Worker 
51*49cdfc7eSAndroid Build Coastguard Worker 	TST_CHECKPOINT_WAIT(0);
52*49cdfc7eSAndroid Build Coastguard Worker 
53*49cdfc7eSAndroid Build Coastguard Worker 	TEST(sys_tgkill(getpid(), tid, SIGRTMIN));
54*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET && TST_ERR == EAGAIN)
55*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "tgkill() failed with EAGAIN as expected");
56*49cdfc7eSAndroid Build Coastguard Worker 	else
57*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL | TTERRNO,
58*49cdfc7eSAndroid Build Coastguard Worker 			"tgkill() should have failed with EAGAIN");
59*49cdfc7eSAndroid Build Coastguard Worker 
60*49cdfc7eSAndroid Build Coastguard Worker 	TST_CHECKPOINT_WAKE(0);
61*49cdfc7eSAndroid Build Coastguard Worker 
62*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_PTHREAD_JOIN(thread, NULL);
63*49cdfc7eSAndroid Build Coastguard Worker }
64*49cdfc7eSAndroid Build Coastguard Worker 
65*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
66*49cdfc7eSAndroid Build Coastguard Worker 	.needs_checkpoints = 1,
67*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = run,
68*49cdfc7eSAndroid Build Coastguard Worker };
69