xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/prctl/prctl03.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 FUJITSU LIMITED. All rights reserved.
4*49cdfc7eSAndroid Build Coastguard Worker  * Author: Xiao Yang <[email protected]>
5*49cdfc7eSAndroid Build Coastguard Worker  */
6*49cdfc7eSAndroid Build Coastguard Worker 
7*49cdfc7eSAndroid Build Coastguard Worker /*\
8*49cdfc7eSAndroid Build Coastguard Worker  * [Description]
9*49cdfc7eSAndroid Build Coastguard Worker  *
10*49cdfc7eSAndroid Build Coastguard Worker  * Test PR_SET_CHILD_SUBREAPER and PR_GET_CHILD_SUBREAPER of prctl(2).
11*49cdfc7eSAndroid Build Coastguard Worker  *
12*49cdfc7eSAndroid Build Coastguard Worker  * - If PR_SET_CHILD_SUBREAPER marks a process as a child subreaper, it
13*49cdfc7eSAndroid Build Coastguard Worker  *   fulfills the role of init(1) for its descendant orphaned process.
14*49cdfc7eSAndroid Build Coastguard Worker  *   The PPID of its orphaned process will be reparented to the subreaper
15*49cdfc7eSAndroid Build Coastguard Worker  *   process, and the subreaper process can receive a SIGCHLD signal and
16*49cdfc7eSAndroid Build Coastguard Worker  *   wait(2) on the orphaned process to discover corresponding termination
17*49cdfc7eSAndroid Build Coastguard Worker  *   status.
18*49cdfc7eSAndroid Build Coastguard Worker  *
19*49cdfc7eSAndroid Build Coastguard Worker  * - The setting of PR_SET_CHILD_SUBREAPER is not inherited by children
20*49cdfc7eSAndroid Build Coastguard Worker  *   reated by fork(2).
21*49cdfc7eSAndroid Build Coastguard Worker  *
22*49cdfc7eSAndroid Build Coastguard Worker  * - PR_GET_CHILD_SUBREAPER can get the setting of PR_SET_CHILD_SUBREAPER.
23*49cdfc7eSAndroid Build Coastguard Worker  *
24*49cdfc7eSAndroid Build Coastguard Worker  * These flags was added by kernel commit ebec18a6d3aa:
25*49cdfc7eSAndroid Build Coastguard Worker  * "prctl: add PR_{SET,GET}_CHILD_SUBREAPER to allow simple process supervision"
26*49cdfc7eSAndroid Build Coastguard Worker  */
27*49cdfc7eSAndroid Build Coastguard Worker 
28*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
29*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
30*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
31*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
32*49cdfc7eSAndroid Build Coastguard Worker #include <sys/wait.h>
33*49cdfc7eSAndroid Build Coastguard Worker #include <signal.h>
34*49cdfc7eSAndroid Build Coastguard Worker #include <sys/prctl.h>
35*49cdfc7eSAndroid Build Coastguard Worker 
36*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
37*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/prctl.h"
38*49cdfc7eSAndroid Build Coastguard Worker 
39*49cdfc7eSAndroid Build Coastguard Worker static volatile int sigchild_recv;
40*49cdfc7eSAndroid Build Coastguard Worker 
check_get_subreaper(int exp_val)41*49cdfc7eSAndroid Build Coastguard Worker static void check_get_subreaper(int exp_val)
42*49cdfc7eSAndroid Build Coastguard Worker {
43*49cdfc7eSAndroid Build Coastguard Worker 	int get_val;
44*49cdfc7eSAndroid Build Coastguard Worker 
45*49cdfc7eSAndroid Build Coastguard Worker 	TEST(prctl(PR_GET_CHILD_SUBREAPER, &get_val));
46*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET == -1) {
47*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL | TTERRNO, "prctl(PR_GET_CHILD_SUBREAPER) failed");
48*49cdfc7eSAndroid Build Coastguard Worker 		return;
49*49cdfc7eSAndroid Build Coastguard Worker 	}
50*49cdfc7eSAndroid Build Coastguard Worker 
51*49cdfc7eSAndroid Build Coastguard Worker 	if (get_val == exp_val) {
52*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "prctl(PR_GET_CHILD_SUBREAPER) got expected %d",
53*49cdfc7eSAndroid Build Coastguard Worker 			get_val);
54*49cdfc7eSAndroid Build Coastguard Worker 	} else {
55*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "prctl(PR_GET_CHILD_SUBREAPER) got %d, expected %d",
56*49cdfc7eSAndroid Build Coastguard Worker 			get_val, exp_val);
57*49cdfc7eSAndroid Build Coastguard Worker 	}
58*49cdfc7eSAndroid Build Coastguard Worker }
59*49cdfc7eSAndroid Build Coastguard Worker 
verify_prctl(void)60*49cdfc7eSAndroid Build Coastguard Worker static void verify_prctl(void)
61*49cdfc7eSAndroid Build Coastguard Worker {
62*49cdfc7eSAndroid Build Coastguard Worker 	int status, ret;
63*49cdfc7eSAndroid Build Coastguard Worker 	pid_t pid;
64*49cdfc7eSAndroid Build Coastguard Worker 	pid_t ppid = getpid();
65*49cdfc7eSAndroid Build Coastguard Worker 
66*49cdfc7eSAndroid Build Coastguard Worker 	sigchild_recv = 0;
67*49cdfc7eSAndroid Build Coastguard Worker 
68*49cdfc7eSAndroid Build Coastguard Worker 	TEST(prctl(PR_SET_CHILD_SUBREAPER, 1));
69*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET == -1) {
70*49cdfc7eSAndroid Build Coastguard Worker 		if (TST_ERR == EINVAL) {
71*49cdfc7eSAndroid Build Coastguard Worker 			tst_res(TCONF,
72*49cdfc7eSAndroid Build Coastguard Worker 				"prctl() doesn't support PR_SET_CHILD_SUBREAPER");
73*49cdfc7eSAndroid Build Coastguard Worker 		} else {
74*49cdfc7eSAndroid Build Coastguard Worker 			tst_res(TFAIL | TTERRNO,
75*49cdfc7eSAndroid Build Coastguard Worker 				"prctl(PR_SET_CHILD_SUBREAPER) failed");
76*49cdfc7eSAndroid Build Coastguard Worker 		}
77*49cdfc7eSAndroid Build Coastguard Worker 		return;
78*49cdfc7eSAndroid Build Coastguard Worker 	}
79*49cdfc7eSAndroid Build Coastguard Worker 
80*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TPASS, "prctl(PR_SET_CHILD_SUBREAPER) succeeded");
81*49cdfc7eSAndroid Build Coastguard Worker 
82*49cdfc7eSAndroid Build Coastguard Worker 	pid = SAFE_FORK();
83*49cdfc7eSAndroid Build Coastguard Worker 	if (!pid) {
84*49cdfc7eSAndroid Build Coastguard Worker 		pid_t cpid;
85*49cdfc7eSAndroid Build Coastguard Worker 
86*49cdfc7eSAndroid Build Coastguard Worker 		cpid = SAFE_FORK();
87*49cdfc7eSAndroid Build Coastguard Worker 		if (!cpid) {
88*49cdfc7eSAndroid Build Coastguard Worker 			TST_CHECKPOINT_WAIT(0);
89*49cdfc7eSAndroid Build Coastguard Worker 			if (getppid() != ppid) {
90*49cdfc7eSAndroid Build Coastguard Worker 				tst_res(TFAIL,
91*49cdfc7eSAndroid Build Coastguard Worker 					"PPID of orphaned process was not reparented");
92*49cdfc7eSAndroid Build Coastguard Worker 				exit(0);
93*49cdfc7eSAndroid Build Coastguard Worker 			}
94*49cdfc7eSAndroid Build Coastguard Worker 
95*49cdfc7eSAndroid Build Coastguard Worker 			tst_res(TPASS, "PPID of orphaned process was reparented");
96*49cdfc7eSAndroid Build Coastguard Worker 			exit(0);
97*49cdfc7eSAndroid Build Coastguard Worker 		}
98*49cdfc7eSAndroid Build Coastguard Worker 
99*49cdfc7eSAndroid Build Coastguard Worker 		check_get_subreaper(0);
100*49cdfc7eSAndroid Build Coastguard Worker 		exit(0);
101*49cdfc7eSAndroid Build Coastguard Worker 	}
102*49cdfc7eSAndroid Build Coastguard Worker 
103*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_WAITPID(pid, NULL, 0);
104*49cdfc7eSAndroid Build Coastguard Worker 	TST_CHECKPOINT_WAKE(0);
105*49cdfc7eSAndroid Build Coastguard Worker 	ret = wait(&status);
106*49cdfc7eSAndroid Build Coastguard Worker 	if (ret > 0) {
107*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "wait() got orphaned process, pid %d, status %d",
108*49cdfc7eSAndroid Build Coastguard Worker 			ret, status);
109*49cdfc7eSAndroid Build Coastguard Worker 	} else {
110*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL | TERRNO,
111*49cdfc7eSAndroid Build Coastguard Worker 			"wait() failed to get orphaned process");
112*49cdfc7eSAndroid Build Coastguard Worker 	}
113*49cdfc7eSAndroid Build Coastguard Worker 
114*49cdfc7eSAndroid Build Coastguard Worker 	if (sigchild_recv == 2)
115*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "received SIGCHLD from orphaned process");
116*49cdfc7eSAndroid Build Coastguard Worker 	else
117*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "didn't receive SIGCHLD from orphaned process");
118*49cdfc7eSAndroid Build Coastguard Worker 
119*49cdfc7eSAndroid Build Coastguard Worker 	check_get_subreaper(1);
120*49cdfc7eSAndroid Build Coastguard Worker }
121*49cdfc7eSAndroid Build Coastguard Worker 
sighandler(int sig LTP_ATTRIBUTE_UNUSED)122*49cdfc7eSAndroid Build Coastguard Worker static void sighandler(int sig LTP_ATTRIBUTE_UNUSED)
123*49cdfc7eSAndroid Build Coastguard Worker {
124*49cdfc7eSAndroid Build Coastguard Worker 	sigchild_recv++;
125*49cdfc7eSAndroid Build Coastguard Worker }
126*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)127*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
128*49cdfc7eSAndroid Build Coastguard Worker {
129*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SIGNAL(SIGCHLD, sighandler);
130*49cdfc7eSAndroid Build Coastguard Worker }
131*49cdfc7eSAndroid Build Coastguard Worker 
132*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
133*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
134*49cdfc7eSAndroid Build Coastguard Worker 	.forks_child = 1,
135*49cdfc7eSAndroid Build Coastguard Worker 	.needs_checkpoints = 1,
136*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = verify_prctl,
137*49cdfc7eSAndroid Build Coastguard Worker };
138