xref: /xnu-10063.121.3/tests/workq/workq_sigmask.c (revision 2c2f96dc2b9a4408a43d3150ae9c105355ca3daa)
1*2c2f96dcSApple OSS Distributions #include <pthread.h>
2*2c2f96dcSApple OSS Distributions #include <signal.h>
3*2c2f96dcSApple OSS Distributions #include <darwintest.h>
4*2c2f96dcSApple OSS Distributions #include <dispatch/dispatch.h>
5*2c2f96dcSApple OSS Distributions 
6*2c2f96dcSApple OSS Distributions // For BSDTHREAD_CTL_WORKQ_PRESERVE_SIGMASK
7*2c2f96dcSApple OSS Distributions #define __PTHREAD_EXPOSE_INTERNALS__
8*2c2f96dcSApple OSS Distributions #include <pthread/bsdthread_private.h>
9*2c2f96dcSApple OSS Distributions #undef __PTHREAD_EXPOSE_INTERNALS__
10*2c2f96dcSApple OSS Distributions 
11*2c2f96dcSApple OSS Distributions extern int __bsdthread_ctl(uintptr_t cmd, uintptr_t arg1, uintptr_t arg2, uintptr_t arg3);
12*2c2f96dcSApple OSS Distributions 
13*2c2f96dcSApple OSS Distributions T_GLOBAL_META(
14*2c2f96dcSApple OSS Distributions 	T_META_NAMESPACE("xnu.workq"),
15*2c2f96dcSApple OSS Distributions 	T_META_RADAR_COMPONENT_NAME("xnu"),
16*2c2f96dcSApple OSS Distributions 	T_META_RADAR_COMPONENT_VERSION("workq"),
17*2c2f96dcSApple OSS Distributions 	T_META_RUN_CONCURRENTLY(true));
18*2c2f96dcSApple OSS Distributions 
19*2c2f96dcSApple OSS Distributions T_DECL(invalid_allow_sigmask, "test that BSDTHREAD_CTL_WORKQ_ALLOW_SIGMASK returns EINVAL for things like SIGKILL")
20*2c2f96dcSApple OSS Distributions {
21*2c2f96dcSApple OSS Distributions 	int ret = __bsdthread_ctl(BSDTHREAD_CTL_WORKQ_ALLOW_SIGMASK, sigmask(SIGKILL), 0, 0);
22*2c2f96dcSApple OSS Distributions 	T_ASSERT_EQ(ret, -1, "BSDTHREAD_CTL_WORKQ_ALLOW_SIGMASK should not work on prohibited signals");
23*2c2f96dcSApple OSS Distributions }
24*2c2f96dcSApple OSS Distributions 
25*2c2f96dcSApple OSS Distributions static void
print_sigmask(void)26*2c2f96dcSApple OSS Distributions print_sigmask(void)
27*2c2f96dcSApple OSS Distributions {
28*2c2f96dcSApple OSS Distributions 	sigset_t omask;
29*2c2f96dcSApple OSS Distributions 	sigprocmask(0, NULL, &omask);
30*2c2f96dcSApple OSS Distributions 	T_LOG("sigmask is %x\n", omask);
31*2c2f96dcSApple OSS Distributions }
32*2c2f96dcSApple OSS Distributions 
33*2c2f96dcSApple OSS Distributions 
34*2c2f96dcSApple OSS Distributions T_DECL(preserve_sigmask_unblock, "test that BSDTHREAD_CTL_WORKQ_ALLOW_SIGMASK preserves unblocking SIGUSR1 on a workq thread")
35*2c2f96dcSApple OSS Distributions {
36*2c2f96dcSApple OSS Distributions 	__block int ret;
37*2c2f96dcSApple OSS Distributions 	const int sig = SIGUSR1;
38*2c2f96dcSApple OSS Distributions 	const sigset_t mask = sigmask(sig);
39*2c2f96dcSApple OSS Distributions 	const sigset_t expected = 0;
40*2c2f96dcSApple OSS Distributions 
41*2c2f96dcSApple OSS Distributions 	dispatch_queue_t q = dispatch_get_global_queue(0, 0);
42*2c2f96dcSApple OSS Distributions 	dispatch_async(q, ^{
43*2c2f96dcSApple OSS Distributions 		T_WITH_ERRNO;
44*2c2f96dcSApple OSS Distributions 		ret = __bsdthread_ctl(BSDTHREAD_CTL_WORKQ_ALLOW_SIGMASK, mask, 0, 0);
45*2c2f96dcSApple OSS Distributions 		T_ASSERT_POSIX_SUCCESS(ret, "BSDTHREAD_CTL_WORKQ_ALLOW_SIGMASK");
46*2c2f96dcSApple OSS Distributions 
47*2c2f96dcSApple OSS Distributions 		print_sigmask();
48*2c2f96dcSApple OSS Distributions 
49*2c2f96dcSApple OSS Distributions 		T_WITH_ERRNO;
50*2c2f96dcSApple OSS Distributions 		ret = sigprocmask(SIG_UNBLOCK, &mask, NULL);
51*2c2f96dcSApple OSS Distributions 		T_ASSERT_POSIX_SUCCESS(ret, "sigprocmask");
52*2c2f96dcSApple OSS Distributions 
53*2c2f96dcSApple OSS Distributions 		print_sigmask();
54*2c2f96dcSApple OSS Distributions 
55*2c2f96dcSApple OSS Distributions 		sigset_t omask;
56*2c2f96dcSApple OSS Distributions 		T_WITH_ERRNO;
57*2c2f96dcSApple OSS Distributions 		ret = sigprocmask(0, NULL, &omask);
58*2c2f96dcSApple OSS Distributions 		T_ASSERT_POSIX_SUCCESS(ret, "sigprocmask");
59*2c2f96dcSApple OSS Distributions 		T_ASSERT_EQ(omask & mask, expected, "Is the mask right?");
60*2c2f96dcSApple OSS Distributions 	});
61*2c2f96dcSApple OSS Distributions 
62*2c2f96dcSApple OSS Distributions 	// This should park the workq, and hopefully use it again
63*2c2f96dcSApple OSS Distributions 	dispatch_time_t sec = dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC);
64*2c2f96dcSApple OSS Distributions 	dispatch_after(sec, q, ^{
65*2c2f96dcSApple OSS Distributions 		print_sigmask();
66*2c2f96dcSApple OSS Distributions 
67*2c2f96dcSApple OSS Distributions 		sigset_t omask;
68*2c2f96dcSApple OSS Distributions 		T_WITH_ERRNO;
69*2c2f96dcSApple OSS Distributions 		ret = sigprocmask(0, NULL, &omask);
70*2c2f96dcSApple OSS Distributions 		T_ASSERT_POSIX_SUCCESS(ret, "sigprocmask");
71*2c2f96dcSApple OSS Distributions 		T_ASSERT_EQ(omask & mask, expected, "Was mask preserved across park?");
72*2c2f96dcSApple OSS Distributions 		T_END;
73*2c2f96dcSApple OSS Distributions 	});
74*2c2f96dcSApple OSS Distributions 	dispatch_main();
75*2c2f96dcSApple OSS Distributions }
76*2c2f96dcSApple OSS Distributions 
77*2c2f96dcSApple OSS Distributions static volatile bool handled;
78*2c2f96dcSApple OSS Distributions 
79*2c2f96dcSApple OSS Distributions static void
handler(int signum)80*2c2f96dcSApple OSS Distributions handler(int signum)
81*2c2f96dcSApple OSS Distributions {
82*2c2f96dcSApple OSS Distributions 	T_ASSERT_EQ(signum, SIGUSR2, "did we get the signal we expected?");
83*2c2f96dcSApple OSS Distributions 	handled = true;
84*2c2f96dcSApple OSS Distributions }
85*2c2f96dcSApple OSS Distributions 
86*2c2f96dcSApple OSS Distributions T_DECL(sigmask_signallable, "test that a workq thread can be signalled after BSDTHREAD_CTL_WORKQ_ALLOW_SIGMASK")
87*2c2f96dcSApple OSS Distributions {
88*2c2f96dcSApple OSS Distributions 	__block int ret;
89*2c2f96dcSApple OSS Distributions 	const int sig = SIGUSR2;
90*2c2f96dcSApple OSS Distributions 	const sigset_t mask = sigmask(sig);
91*2c2f96dcSApple OSS Distributions 
92*2c2f96dcSApple OSS Distributions 	T_ASSERT_FALSE(handled, "make sure our static bool is sane");
93*2c2f96dcSApple OSS Distributions 
94*2c2f96dcSApple OSS Distributions 	// Set up handler
95*2c2f96dcSApple OSS Distributions 	struct sigaction action = {
96*2c2f96dcSApple OSS Distributions 		.sa_handler = handler,
97*2c2f96dcSApple OSS Distributions 	};
98*2c2f96dcSApple OSS Distributions 	ret = sigaction(sig, &action, NULL);
99*2c2f96dcSApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ret, "sigaction");
100*2c2f96dcSApple OSS Distributions 
101*2c2f96dcSApple OSS Distributions 	// Enable signal for workq threads
102*2c2f96dcSApple OSS Distributions 	T_WITH_ERRNO;
103*2c2f96dcSApple OSS Distributions 	ret = __bsdthread_ctl(BSDTHREAD_CTL_WORKQ_ALLOW_SIGMASK, mask, 0, 0);
104*2c2f96dcSApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ret, "BSDTHREAD_CTL_WORKQ_ALLOW_SIGMASK");
105*2c2f96dcSApple OSS Distributions 
106*2c2f96dcSApple OSS Distributions 	// Set sigmask
107*2c2f96dcSApple OSS Distributions 	dispatch_queue_t q = dispatch_get_global_queue(0, 0);
108*2c2f96dcSApple OSS Distributions 	dispatch_async(q, ^{
109*2c2f96dcSApple OSS Distributions 		print_sigmask();
110*2c2f96dcSApple OSS Distributions 
111*2c2f96dcSApple OSS Distributions 		T_WITH_ERRNO;
112*2c2f96dcSApple OSS Distributions 		ret = sigprocmask(SIG_UNBLOCK, &mask, NULL);
113*2c2f96dcSApple OSS Distributions 		T_ASSERT_POSIX_SUCCESS(ret, "sigprocmask");
114*2c2f96dcSApple OSS Distributions 
115*2c2f96dcSApple OSS Distributions 		print_sigmask();
116*2c2f96dcSApple OSS Distributions 	});
117*2c2f96dcSApple OSS Distributions 
118*2c2f96dcSApple OSS Distributions 	sleep(1);
119*2c2f96dcSApple OSS Distributions 
120*2c2f96dcSApple OSS Distributions 	// Get workq and signal it
121*2c2f96dcSApple OSS Distributions 	dispatch_async(q, ^{
122*2c2f96dcSApple OSS Distributions 		T_WITH_ERRNO;
123*2c2f96dcSApple OSS Distributions 		ret = pthread_kill(pthread_self(), sig);
124*2c2f96dcSApple OSS Distributions 		T_ASSERT_POSIX_SUCCESS(ret, "pthread_kill");
125*2c2f96dcSApple OSS Distributions 	});
126*2c2f96dcSApple OSS Distributions 
127*2c2f96dcSApple OSS Distributions 	sleep(2);
128*2c2f96dcSApple OSS Distributions 
129*2c2f96dcSApple OSS Distributions 	// Check delivery
130*2c2f96dcSApple OSS Distributions 	T_ASSERT_TRUE(handled, "signal should have been handled");
131*2c2f96dcSApple OSS Distributions }
132