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