1*1031c584SApple OSS Distributions /*
2*1031c584SApple OSS Distributions * Copyright (c) 2023 Apple Inc. All rights reserved.
3*1031c584SApple OSS Distributions *
4*1031c584SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*1031c584SApple OSS Distributions *
6*1031c584SApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*1031c584SApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*1031c584SApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*1031c584SApple OSS Distributions * compliance with the License. The rights granted to you under the License
10*1031c584SApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
11*1031c584SApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
12*1031c584SApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
13*1031c584SApple OSS Distributions * terms of an Apple operating system software license agreement.
14*1031c584SApple OSS Distributions *
15*1031c584SApple OSS Distributions * Please obtain a copy of the License at
16*1031c584SApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*1031c584SApple OSS Distributions *
18*1031c584SApple OSS Distributions * The Original Code and all software distributed under the License are
19*1031c584SApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*1031c584SApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*1031c584SApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*1031c584SApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*1031c584SApple OSS Distributions * Please see the License for the specific language governing rights and
24*1031c584SApple OSS Distributions * limitations under the License.
25*1031c584SApple OSS Distributions *
26*1031c584SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*1031c584SApple OSS Distributions */
28*1031c584SApple OSS Distributions
29*1031c584SApple OSS Distributions /*
30*1031c584SApple OSS Distributions * These tests verify that proc_pidinfo returns the expected exit reason and
31*1031c584SApple OSS Distributions * namespace for signal-related process termination.
32*1031c584SApple OSS Distributions */
33*1031c584SApple OSS Distributions
34*1031c584SApple OSS Distributions #include <darwintest.h>
35*1031c584SApple OSS Distributions #include <signal.h>
36*1031c584SApple OSS Distributions #include <libproc.h>
37*1031c584SApple OSS Distributions #include <sys/wait.h>
38*1031c584SApple OSS Distributions #include <sys/reason.h>
39*1031c584SApple OSS Distributions #include <stdlib.h>
40*1031c584SApple OSS Distributions #include <dispatch/dispatch.h>
41*1031c584SApple OSS Distributions #include <unistd.h>
42*1031c584SApple OSS Distributions
43*1031c584SApple OSS Distributions T_GLOBAL_META(
44*1031c584SApple OSS Distributions T_META_NAMESPACE("xnu.misc"),
45*1031c584SApple OSS Distributions T_META_RADAR_COMPONENT_NAME("xnu"),
46*1031c584SApple OSS Distributions T_META_RADAR_COMPONENT_VERSION("misc"));
47*1031c584SApple OSS Distributions
48*1031c584SApple OSS Distributions static dispatch_queue_t exit_queue;
49*1031c584SApple OSS Distributions
50*1031c584SApple OSS Distributions static void
cleanup(void)51*1031c584SApple OSS Distributions cleanup(void)
52*1031c584SApple OSS Distributions {
53*1031c584SApple OSS Distributions dispatch_release(exit_queue);
54*1031c584SApple OSS Distributions }
55*1031c584SApple OSS Distributions
56*1031c584SApple OSS Distributions static void
57*1031c584SApple OSS Distributions dispatch_test(void (^test)(void))
58*1031c584SApple OSS Distributions {
59*1031c584SApple OSS Distributions // Use dispatch to schedule DISPATCH_PROC_EXIT blocks to read out exit reasons
60*1031c584SApple OSS Distributions exit_queue = dispatch_queue_create("exit queue", DISPATCH_QUEUE_SERIAL);
61*1031c584SApple OSS Distributions dispatch_async(dispatch_get_main_queue(), ^{
62*1031c584SApple OSS Distributions test();
63*1031c584SApple OSS Distributions });
64*1031c584SApple OSS Distributions T_ATEND(cleanup);
65*1031c584SApple OSS Distributions dispatch_main();
66*1031c584SApple OSS Distributions }
67*1031c584SApple OSS Distributions
68*1031c584SApple OSS Distributions static void
check_exit_reason(int pid,uint64_t expected_reason_namespace,uint64_t expected_signal)69*1031c584SApple OSS Distributions check_exit_reason(int pid, uint64_t expected_reason_namespace, uint64_t expected_signal)
70*1031c584SApple OSS Distributions {
71*1031c584SApple OSS Distributions T_LOG("check_exit_reason %d", expected_signal);
72*1031c584SApple OSS Distributions int ret, status;
73*1031c584SApple OSS Distributions struct proc_exitreasonbasicinfo exit_reason;
74*1031c584SApple OSS Distributions
75*1031c584SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(
76*1031c584SApple OSS Distributions ret = proc_pidinfo(pid, PROC_PIDEXITREASONBASICINFO, 1, &exit_reason, PROC_PIDEXITREASONBASICINFOSIZE),
77*1031c584SApple OSS Distributions "verify proc_pidinfo success"
78*1031c584SApple OSS Distributions );
79*1031c584SApple OSS Distributions T_QUIET; T_ASSERT_EQ(ret, PROC_PIDEXITREASONBASICINFOSIZE, "retrieve basic exit reason info");
80*1031c584SApple OSS Distributions
81*1031c584SApple OSS Distributions waitpid(pid, &status, 0);
82*1031c584SApple OSS Distributions T_QUIET; T_EXPECT_FALSE(WIFEXITED(status), "process did not exit normally");
83*1031c584SApple OSS Distributions T_QUIET; T_EXPECT_TRUE(WIFSIGNALED(status), "process was terminated because of a signal");
84*1031c584SApple OSS Distributions T_QUIET; T_EXPECT_EQ(WTERMSIG(status), expected_signal, "process should terminate due to signal %llu", expected_signal);
85*1031c584SApple OSS Distributions
86*1031c584SApple OSS Distributions T_EXPECT_EQ(exit_reason.beri_namespace, expected_reason_namespace, "expect OS_REASON_SIGNAL");
87*1031c584SApple OSS Distributions T_EXPECT_EQ(exit_reason.beri_code, expected_signal, "expect reason code: %llu", expected_signal);
88*1031c584SApple OSS Distributions }
89*1031c584SApple OSS Distributions
90*1031c584SApple OSS Distributions static void
wait_collect_exit_reason(int pid,int signal)91*1031c584SApple OSS Distributions wait_collect_exit_reason(int pid, int signal)
92*1031c584SApple OSS Distributions {
93*1031c584SApple OSS Distributions dispatch_source_t ds_proc = dispatch_source_create(DISPATCH_SOURCE_TYPE_PROC, pid, DISPATCH_PROC_EXIT, exit_queue);
94*1031c584SApple OSS Distributions dispatch_semaphore_t sem = dispatch_semaphore_create(0);
95*1031c584SApple OSS Distributions dispatch_source_set_event_handler(ds_proc, ^{
96*1031c584SApple OSS Distributions check_exit_reason(pid, OS_REASON_SIGNAL, signal);
97*1031c584SApple OSS Distributions dispatch_semaphore_signal(sem);
98*1031c584SApple OSS Distributions });
99*1031c584SApple OSS Distributions dispatch_activate(ds_proc);
100*1031c584SApple OSS Distributions
101*1031c584SApple OSS Distributions // Wait till exit reason is processed
102*1031c584SApple OSS Distributions dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
103*1031c584SApple OSS Distributions dispatch_release(ds_proc);
104*1031c584SApple OSS Distributions dispatch_release(sem);
105*1031c584SApple OSS Distributions }
106*1031c584SApple OSS Distributions
107*1031c584SApple OSS Distributions static void
__test_exit_reason_abort()108*1031c584SApple OSS Distributions __test_exit_reason_abort()
109*1031c584SApple OSS Distributions {
110*1031c584SApple OSS Distributions pid_t child = fork();
111*1031c584SApple OSS Distributions if (child > 0) {
112*1031c584SApple OSS Distributions wait_collect_exit_reason(child, SIGABRT);
113*1031c584SApple OSS Distributions } else {
114*1031c584SApple OSS Distributions abort();
115*1031c584SApple OSS Distributions }
116*1031c584SApple OSS Distributions }
117*1031c584SApple OSS Distributions
118*1031c584SApple OSS Distributions T_DECL(test_exit_reason_abort, "tests exit reason for abort()")
119*1031c584SApple OSS Distributions {
120*1031c584SApple OSS Distributions dispatch_test(^{
121*1031c584SApple OSS Distributions __test_exit_reason_abort();
122*1031c584SApple OSS Distributions T_END;
123*1031c584SApple OSS Distributions });
124*1031c584SApple OSS Distributions }
125*1031c584SApple OSS Distributions
126*1031c584SApple OSS Distributions static void
__test_exit_reason_external_signal(int signal)127*1031c584SApple OSS Distributions __test_exit_reason_external_signal(int signal)
128*1031c584SApple OSS Distributions {
129*1031c584SApple OSS Distributions T_LOG("Testing external signal %d", signal);
130*1031c584SApple OSS Distributions pid_t child = fork();
131*1031c584SApple OSS Distributions if (child > 0) {
132*1031c584SApple OSS Distributions // Send external signal
133*1031c584SApple OSS Distributions kill(child, signal);
134*1031c584SApple OSS Distributions wait_collect_exit_reason(child, signal);
135*1031c584SApple OSS Distributions } else {
136*1031c584SApple OSS Distributions pause();
137*1031c584SApple OSS Distributions }
138*1031c584SApple OSS Distributions }
139*1031c584SApple OSS Distributions
140*1031c584SApple OSS Distributions T_DECL(test_exit_reason_external_signal, "tests exit reason for external signals")
141*1031c584SApple OSS Distributions {
142*1031c584SApple OSS Distributions dispatch_test(^{
143*1031c584SApple OSS Distributions __test_exit_reason_external_signal(SIGABRT);
144*1031c584SApple OSS Distributions __test_exit_reason_external_signal(SIGKILL);
145*1031c584SApple OSS Distributions __test_exit_reason_external_signal(SIGSYS);
146*1031c584SApple OSS Distributions __test_exit_reason_external_signal(SIGUSR1);
147*1031c584SApple OSS Distributions T_END;
148*1031c584SApple OSS Distributions });
149*1031c584SApple OSS Distributions }
150*1031c584SApple OSS Distributions
151*1031c584SApple OSS Distributions struct pthread_kill_helper_args {
152*1031c584SApple OSS Distributions pthread_t *pthread;
153*1031c584SApple OSS Distributions int signal;
154*1031c584SApple OSS Distributions };
155*1031c584SApple OSS Distributions
156*1031c584SApple OSS Distributions static void
pthread_kill_helper(void * msg)157*1031c584SApple OSS Distributions pthread_kill_helper(void *msg)
158*1031c584SApple OSS Distributions {
159*1031c584SApple OSS Distributions struct pthread_kill_helper_args *args = (struct pthread_kill_helper_args *)msg;
160*1031c584SApple OSS Distributions pthread_kill(*args->pthread, args->signal);
161*1031c584SApple OSS Distributions }
162*1031c584SApple OSS Distributions
163*1031c584SApple OSS Distributions static void
__test_exit_reason_pthread_kill_self(int signal)164*1031c584SApple OSS Distributions __test_exit_reason_pthread_kill_self(int signal)
165*1031c584SApple OSS Distributions {
166*1031c584SApple OSS Distributions T_LOG("Testing pthread_kill for signal %d", signal);
167*1031c584SApple OSS Distributions pid_t child = fork();
168*1031c584SApple OSS Distributions if (child > 0) {
169*1031c584SApple OSS Distributions wait_collect_exit_reason(child, signal);
170*1031c584SApple OSS Distributions } else {
171*1031c584SApple OSS Distributions pthread_t t;
172*1031c584SApple OSS Distributions struct pthread_kill_helper_args args = {&t, signal};
173*1031c584SApple OSS Distributions pthread_create(&t, NULL, pthread_kill_helper, (void *)&args);
174*1031c584SApple OSS Distributions pthread_join(t, NULL);
175*1031c584SApple OSS Distributions }
176*1031c584SApple OSS Distributions }
177*1031c584SApple OSS Distributions
178*1031c584SApple OSS Distributions T_DECL(test_exit_reason_pthread_kill_self, "tests exit reason for pthread_kill on caller thread")
179*1031c584SApple OSS Distributions {
180*1031c584SApple OSS Distributions dispatch_test(^{
181*1031c584SApple OSS Distributions __test_exit_reason_pthread_kill_self(SIGABRT);
182*1031c584SApple OSS Distributions __test_exit_reason_pthread_kill_self(SIGKILL);
183*1031c584SApple OSS Distributions __test_exit_reason_pthread_kill_self(SIGSYS);
184*1031c584SApple OSS Distributions __test_exit_reason_pthread_kill_self(SIGUSR1);
185*1031c584SApple OSS Distributions T_END;
186*1031c584SApple OSS Distributions });
187*1031c584SApple OSS Distributions }
188