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