xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/fcntl/fcntl31.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker /*
2*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2014 Fujitsu Ltd.
3*49cdfc7eSAndroid Build Coastguard Worker  * Author: Xiaoguang Wang <[email protected]>
4*49cdfc7eSAndroid Build Coastguard Worker  *
5*49cdfc7eSAndroid Build Coastguard Worker  * This program is free software; you can redistribute it and/or modify it
6*49cdfc7eSAndroid Build Coastguard Worker  * under the terms of version 2 of the GNU General Public License as
7*49cdfc7eSAndroid Build Coastguard Worker  * published by the Free Software Foundation.
8*49cdfc7eSAndroid Build Coastguard Worker  *
9*49cdfc7eSAndroid Build Coastguard Worker  * This program is distributed in the hope that it would be useful, but
10*49cdfc7eSAndroid Build Coastguard Worker  * WITHOUT ANY WARRANTY; without even the implied warranty of
11*49cdfc7eSAndroid Build Coastguard Worker  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12*49cdfc7eSAndroid Build Coastguard Worker  *
13*49cdfc7eSAndroid Build Coastguard Worker  * You should have received a copy of the GNU General Public License along
14*49cdfc7eSAndroid Build Coastguard Worker  * with this program; if not, write the Free Software Foundation, Inc.,
15*49cdfc7eSAndroid Build Coastguard Worker  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16*49cdfc7eSAndroid Build Coastguard Worker  */
17*49cdfc7eSAndroid Build Coastguard Worker 
18*49cdfc7eSAndroid Build Coastguard Worker /*
19*49cdfc7eSAndroid Build Coastguard Worker  * Description:
20*49cdfc7eSAndroid Build Coastguard Worker  * Verify that:
21*49cdfc7eSAndroid Build Coastguard Worker  *     Basic test for fcntl(2) using F_GETOWN, F_SETOWN, F_GETOWN_EX,
22*49cdfc7eSAndroid Build Coastguard Worker  *     F_SETOWN_EX, F_GETSIG, F_SETSIG argument.
23*49cdfc7eSAndroid Build Coastguard Worker  */
24*49cdfc7eSAndroid Build Coastguard Worker 
25*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
26*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
27*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
28*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
29*49cdfc7eSAndroid Build Coastguard Worker #include <signal.h>
30*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
31*49cdfc7eSAndroid Build Coastguard Worker #include <sys/wait.h>
32*49cdfc7eSAndroid Build Coastguard Worker #include <pwd.h>
33*49cdfc7eSAndroid Build Coastguard Worker #include <sched.h>
34*49cdfc7eSAndroid Build Coastguard Worker 
35*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
36*49cdfc7eSAndroid Build Coastguard Worker #include "config.h"
37*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/syscalls.h"
38*49cdfc7eSAndroid Build Coastguard Worker #include "safe_macros.h"
39*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/fcntl.h"
40*49cdfc7eSAndroid Build Coastguard Worker 
41*49cdfc7eSAndroid Build Coastguard Worker static void setup(void);
42*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void);
43*49cdfc7eSAndroid Build Coastguard Worker 
44*49cdfc7eSAndroid Build Coastguard Worker static void setown_pid_test(void);
45*49cdfc7eSAndroid Build Coastguard Worker static void setown_pgrp_test(void);
46*49cdfc7eSAndroid Build Coastguard Worker 
47*49cdfc7eSAndroid Build Coastguard Worker #if defined(HAVE_STRUCT_F_OWNER_EX)
48*49cdfc7eSAndroid Build Coastguard Worker static void setownex_tid_test(void);
49*49cdfc7eSAndroid Build Coastguard Worker static void setownex_pid_test(void);
50*49cdfc7eSAndroid Build Coastguard Worker static void setownex_pgrp_test(void);
51*49cdfc7eSAndroid Build Coastguard Worker 
52*49cdfc7eSAndroid Build Coastguard Worker static struct f_owner_ex orig_own_ex;
53*49cdfc7eSAndroid Build Coastguard Worker #endif
54*49cdfc7eSAndroid Build Coastguard Worker 
55*49cdfc7eSAndroid Build Coastguard Worker static void signal_parent(void);
56*49cdfc7eSAndroid Build Coastguard Worker static void check_io_signal(char *des);
57*49cdfc7eSAndroid Build Coastguard Worker static void test_set_and_get_sig(int sig, char *des);
58*49cdfc7eSAndroid Build Coastguard Worker 
59*49cdfc7eSAndroid Build Coastguard Worker static pid_t pid;
60*49cdfc7eSAndroid Build Coastguard Worker static pid_t orig_pid;
61*49cdfc7eSAndroid Build Coastguard Worker static pid_t pgrp_pid;
62*49cdfc7eSAndroid Build Coastguard Worker 
63*49cdfc7eSAndroid Build Coastguard Worker static struct timespec timeout;
64*49cdfc7eSAndroid Build Coastguard Worker static sigset_t newset, oldset;
65*49cdfc7eSAndroid Build Coastguard Worker 
66*49cdfc7eSAndroid Build Coastguard Worker static int test_fd;
67*49cdfc7eSAndroid Build Coastguard Worker static int pipe_fds[2];
68*49cdfc7eSAndroid Build Coastguard Worker 
69*49cdfc7eSAndroid Build Coastguard Worker static void (*testfunc[])(void) = {
70*49cdfc7eSAndroid Build Coastguard Worker 	setown_pid_test, setown_pgrp_test,
71*49cdfc7eSAndroid Build Coastguard Worker #if defined(HAVE_STRUCT_F_OWNER_EX)
72*49cdfc7eSAndroid Build Coastguard Worker 	setownex_tid_test, setownex_pid_test, setownex_pgrp_test
73*49cdfc7eSAndroid Build Coastguard Worker #endif
74*49cdfc7eSAndroid Build Coastguard Worker };
75*49cdfc7eSAndroid Build Coastguard Worker 
76*49cdfc7eSAndroid Build Coastguard Worker char *TCID = "fcntl31";
77*49cdfc7eSAndroid Build Coastguard Worker int TST_TOTAL = ARRAY_SIZE(testfunc);
78*49cdfc7eSAndroid Build Coastguard Worker 
79*49cdfc7eSAndroid Build Coastguard Worker 
main(int ac,char ** av)80*49cdfc7eSAndroid Build Coastguard Worker int main(int ac, char **av)
81*49cdfc7eSAndroid Build Coastguard Worker {
82*49cdfc7eSAndroid Build Coastguard Worker 	int lc, i;
83*49cdfc7eSAndroid Build Coastguard Worker 
84*49cdfc7eSAndroid Build Coastguard Worker 	tst_parse_opts(ac, av, NULL, NULL);
85*49cdfc7eSAndroid Build Coastguard Worker 
86*49cdfc7eSAndroid Build Coastguard Worker 	setup();
87*49cdfc7eSAndroid Build Coastguard Worker 
88*49cdfc7eSAndroid Build Coastguard Worker 	for (lc = 0; TEST_LOOPING(lc); lc++) {
89*49cdfc7eSAndroid Build Coastguard Worker 		tst_count = 0;
90*49cdfc7eSAndroid Build Coastguard Worker 
91*49cdfc7eSAndroid Build Coastguard Worker 		for (i = 0; i < TST_TOTAL; i++)
92*49cdfc7eSAndroid Build Coastguard Worker 			(*testfunc[i])();
93*49cdfc7eSAndroid Build Coastguard Worker 	}
94*49cdfc7eSAndroid Build Coastguard Worker 
95*49cdfc7eSAndroid Build Coastguard Worker 	cleanup();
96*49cdfc7eSAndroid Build Coastguard Worker 	tst_exit();
97*49cdfc7eSAndroid Build Coastguard Worker }
98*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)99*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
100*49cdfc7eSAndroid Build Coastguard Worker {
101*49cdfc7eSAndroid Build Coastguard Worker 	int ret;
102*49cdfc7eSAndroid Build Coastguard Worker 
103*49cdfc7eSAndroid Build Coastguard Worker 	tst_sig(FORK, DEF_HANDLER, cleanup);
104*49cdfc7eSAndroid Build Coastguard Worker 
105*49cdfc7eSAndroid Build Coastguard Worker 	TEST_PAUSE;
106*49cdfc7eSAndroid Build Coastguard Worker 
107*49cdfc7eSAndroid Build Coastguard Worker 	/* we have these tests on pipe */
108*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_PIPE(cleanup, pipe_fds);
109*49cdfc7eSAndroid Build Coastguard Worker 	test_fd = pipe_fds[0];
110*49cdfc7eSAndroid Build Coastguard Worker 	if (fcntl(test_fd, F_SETFL, O_ASYNC) < 0)
111*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TBROK | TERRNO, cleanup, "fcntl set O_ASYNC failed");
112*49cdfc7eSAndroid Build Coastguard Worker 
113*49cdfc7eSAndroid Build Coastguard Worker 	pid = getpid();
114*49cdfc7eSAndroid Build Coastguard Worker 
115*49cdfc7eSAndroid Build Coastguard Worker 	/* Changing process group ID is forbidden when PID == SID i.e. we are session leader */
116*49cdfc7eSAndroid Build Coastguard Worker 	if (pid != getsid(0)) {
117*49cdfc7eSAndroid Build Coastguard Worker 		ret = setpgrp();
118*49cdfc7eSAndroid Build Coastguard Worker 		if (ret < 0)
119*49cdfc7eSAndroid Build Coastguard Worker 			tst_brkm(TBROK | TERRNO, cleanup, "setpgrp() failed");
120*49cdfc7eSAndroid Build Coastguard Worker 	}
121*49cdfc7eSAndroid Build Coastguard Worker 	pgrp_pid = getpgid(0);
122*49cdfc7eSAndroid Build Coastguard Worker 	if (pgrp_pid < 0)
123*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TBROK | TERRNO, cleanup, "getpgid() failed");
124*49cdfc7eSAndroid Build Coastguard Worker 
125*49cdfc7eSAndroid Build Coastguard Worker #if defined(HAVE_STRUCT_F_OWNER_EX)
126*49cdfc7eSAndroid Build Coastguard Worker 	/* get original f_owner_ex info */
127*49cdfc7eSAndroid Build Coastguard Worker 	TEST(fcntl(test_fd, F_GETOWN_EX, &orig_own_ex));
128*49cdfc7eSAndroid Build Coastguard Worker 	if (TEST_RETURN < 0) {
129*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TFAIL | TTERRNO, cleanup,
130*49cdfc7eSAndroid Build Coastguard Worker 			 "fcntl get original f_owner_ex info failed");
131*49cdfc7eSAndroid Build Coastguard Worker 	}
132*49cdfc7eSAndroid Build Coastguard Worker #endif
133*49cdfc7eSAndroid Build Coastguard Worker 
134*49cdfc7eSAndroid Build Coastguard Worker 	/* get original pid info */
135*49cdfc7eSAndroid Build Coastguard Worker 	TEST(fcntl(test_fd, F_GETOWN));
136*49cdfc7eSAndroid Build Coastguard Worker 	if (TEST_RETURN < 0) {
137*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TFAIL | TTERRNO, cleanup,
138*49cdfc7eSAndroid Build Coastguard Worker 			 "fcntl get original pid info failed");
139*49cdfc7eSAndroid Build Coastguard Worker 	}
140*49cdfc7eSAndroid Build Coastguard Worker 	orig_pid = TEST_RETURN;
141*49cdfc7eSAndroid Build Coastguard Worker 
142*49cdfc7eSAndroid Build Coastguard Worker 	sigemptyset(&newset);
143*49cdfc7eSAndroid Build Coastguard Worker 	sigaddset(&newset, SIGUSR1);
144*49cdfc7eSAndroid Build Coastguard Worker 	sigaddset(&newset, SIGIO);
145*49cdfc7eSAndroid Build Coastguard Worker 
146*49cdfc7eSAndroid Build Coastguard Worker 	if (sigprocmask(SIG_SETMASK, &newset, &oldset) < 0)
147*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TBROK | TERRNO, cleanup, "sigprocmask failed");
148*49cdfc7eSAndroid Build Coastguard Worker 
149*49cdfc7eSAndroid Build Coastguard Worker 	timeout.tv_sec = 5;
150*49cdfc7eSAndroid Build Coastguard Worker 	timeout.tv_nsec = 0;
151*49cdfc7eSAndroid Build Coastguard Worker }
152*49cdfc7eSAndroid Build Coastguard Worker 
setown_pid_test(void)153*49cdfc7eSAndroid Build Coastguard Worker static void setown_pid_test(void)
154*49cdfc7eSAndroid Build Coastguard Worker {
155*49cdfc7eSAndroid Build Coastguard Worker 	TEST(fcntl(test_fd, F_SETOWN, pid));
156*49cdfc7eSAndroid Build Coastguard Worker 	if (TEST_RETURN < 0) {
157*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TFAIL | TTERRNO, cleanup,
158*49cdfc7eSAndroid Build Coastguard Worker 			 "fcntl(F_SETOWN) set process id failed");
159*49cdfc7eSAndroid Build Coastguard Worker 	}
160*49cdfc7eSAndroid Build Coastguard Worker 	test_set_and_get_sig(SIGUSR1, "F_GETOWN, F_SETOWN for process ID");
161*49cdfc7eSAndroid Build Coastguard Worker 
162*49cdfc7eSAndroid Build Coastguard Worker 	TEST(fcntl(test_fd, F_SETOWN, orig_pid));
163*49cdfc7eSAndroid Build Coastguard Worker 	if (TEST_RETURN < 0) {
164*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TFAIL | TTERRNO, cleanup,
165*49cdfc7eSAndroid Build Coastguard Worker 			 "fcntl(F_SETOWN) restore orig_pid failed");
166*49cdfc7eSAndroid Build Coastguard Worker 	}
167*49cdfc7eSAndroid Build Coastguard Worker }
168*49cdfc7eSAndroid Build Coastguard Worker 
setown_pgrp_test(void)169*49cdfc7eSAndroid Build Coastguard Worker static void setown_pgrp_test(void)
170*49cdfc7eSAndroid Build Coastguard Worker {
171*49cdfc7eSAndroid Build Coastguard Worker 	TEST(fcntl(test_fd, F_SETOWN, -pgrp_pid));
172*49cdfc7eSAndroid Build Coastguard Worker 	if (TEST_RETURN < 0) {
173*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TFAIL | TTERRNO, cleanup,
174*49cdfc7eSAndroid Build Coastguard Worker 			 "fcntl(F_SETOWN) set process group id failed");
175*49cdfc7eSAndroid Build Coastguard Worker 	}
176*49cdfc7eSAndroid Build Coastguard Worker 	test_set_and_get_sig(SIGUSR1,
177*49cdfc7eSAndroid Build Coastguard Worker 			     "F_GETOWN, F_SETOWN for process group ID");
178*49cdfc7eSAndroid Build Coastguard Worker 
179*49cdfc7eSAndroid Build Coastguard Worker 	TEST(fcntl(test_fd, F_SETOWN, orig_pid));
180*49cdfc7eSAndroid Build Coastguard Worker 	if (TEST_RETURN < 0) {
181*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TFAIL | TTERRNO, cleanup,
182*49cdfc7eSAndroid Build Coastguard Worker 			 "fcntl(F_SETOWN) restore orig_pid failed");
183*49cdfc7eSAndroid Build Coastguard Worker 	}
184*49cdfc7eSAndroid Build Coastguard Worker }
185*49cdfc7eSAndroid Build Coastguard Worker 
186*49cdfc7eSAndroid Build Coastguard Worker #if defined(HAVE_STRUCT_F_OWNER_EX)
setownex_cleanup(void)187*49cdfc7eSAndroid Build Coastguard Worker static void setownex_cleanup(void)
188*49cdfc7eSAndroid Build Coastguard Worker {
189*49cdfc7eSAndroid Build Coastguard Worker 	TEST(fcntl(test_fd, F_SETOWN_EX, &orig_own_ex));
190*49cdfc7eSAndroid Build Coastguard Worker 	if (TEST_RETURN < 0) {
191*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TFAIL | TTERRNO, cleanup,
192*49cdfc7eSAndroid Build Coastguard Worker 			 "fcntl F_SETOWN_EX restore orig_own_ex failed");
193*49cdfc7eSAndroid Build Coastguard Worker 	}
194*49cdfc7eSAndroid Build Coastguard Worker }
195*49cdfc7eSAndroid Build Coastguard Worker 
setownex_tid_test(void)196*49cdfc7eSAndroid Build Coastguard Worker static void setownex_tid_test(void)
197*49cdfc7eSAndroid Build Coastguard Worker {
198*49cdfc7eSAndroid Build Coastguard Worker 	static struct f_owner_ex tst_own_ex;
199*49cdfc7eSAndroid Build Coastguard Worker 
200*49cdfc7eSAndroid Build Coastguard Worker 	tst_own_ex.type = F_OWNER_TID;
201*49cdfc7eSAndroid Build Coastguard Worker 	tst_own_ex.pid = tst_syscall(__NR_gettid);
202*49cdfc7eSAndroid Build Coastguard Worker 
203*49cdfc7eSAndroid Build Coastguard Worker 	TEST(fcntl(test_fd, F_SETOWN_EX, &tst_own_ex));
204*49cdfc7eSAndroid Build Coastguard Worker 	if (TEST_RETURN < 0) {
205*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TFAIL | TTERRNO, cleanup,
206*49cdfc7eSAndroid Build Coastguard Worker 			 "fcntl F_SETOWN_EX failed");
207*49cdfc7eSAndroid Build Coastguard Worker 	}
208*49cdfc7eSAndroid Build Coastguard Worker 	test_set_and_get_sig(SIGUSR1, "F_GETOWN_EX, F_SETOWN_EX for thread ID");
209*49cdfc7eSAndroid Build Coastguard Worker 
210*49cdfc7eSAndroid Build Coastguard Worker 	setownex_cleanup();
211*49cdfc7eSAndroid Build Coastguard Worker }
212*49cdfc7eSAndroid Build Coastguard Worker 
setownex_pid_test(void)213*49cdfc7eSAndroid Build Coastguard Worker static void setownex_pid_test(void)
214*49cdfc7eSAndroid Build Coastguard Worker {
215*49cdfc7eSAndroid Build Coastguard Worker 	static struct f_owner_ex tst_own_ex;
216*49cdfc7eSAndroid Build Coastguard Worker 
217*49cdfc7eSAndroid Build Coastguard Worker 	tst_own_ex.type = F_OWNER_PID;
218*49cdfc7eSAndroid Build Coastguard Worker 	tst_own_ex.pid = pid;
219*49cdfc7eSAndroid Build Coastguard Worker 
220*49cdfc7eSAndroid Build Coastguard Worker 	TEST(fcntl(test_fd, F_SETOWN_EX, &tst_own_ex));
221*49cdfc7eSAndroid Build Coastguard Worker 	if (TEST_RETURN < 0) {
222*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TFAIL | TTERRNO, cleanup,
223*49cdfc7eSAndroid Build Coastguard Worker 			 "fcntl F_SETOWN_EX failed");
224*49cdfc7eSAndroid Build Coastguard Worker 	}
225*49cdfc7eSAndroid Build Coastguard Worker 	test_set_and_get_sig(SIGUSR1,
226*49cdfc7eSAndroid Build Coastguard Worker 			     "F_GETOWN_EX, F_SETOWN_EX for process ID");
227*49cdfc7eSAndroid Build Coastguard Worker 
228*49cdfc7eSAndroid Build Coastguard Worker 	setownex_cleanup();
229*49cdfc7eSAndroid Build Coastguard Worker }
230*49cdfc7eSAndroid Build Coastguard Worker 
setownex_pgrp_test(void)231*49cdfc7eSAndroid Build Coastguard Worker static void setownex_pgrp_test(void)
232*49cdfc7eSAndroid Build Coastguard Worker {
233*49cdfc7eSAndroid Build Coastguard Worker 	static struct f_owner_ex tst_own_ex;
234*49cdfc7eSAndroid Build Coastguard Worker 
235*49cdfc7eSAndroid Build Coastguard Worker 	tst_own_ex.type = F_OWNER_PGRP;
236*49cdfc7eSAndroid Build Coastguard Worker 	tst_own_ex.pid = pgrp_pid;
237*49cdfc7eSAndroid Build Coastguard Worker 
238*49cdfc7eSAndroid Build Coastguard Worker 	TEST(fcntl(test_fd, F_SETOWN_EX, &tst_own_ex));
239*49cdfc7eSAndroid Build Coastguard Worker 	if (TEST_RETURN < 0) {
240*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TFAIL | TTERRNO, cleanup,
241*49cdfc7eSAndroid Build Coastguard Worker 			 "fcntl F_SETOWN_EX failed");
242*49cdfc7eSAndroid Build Coastguard Worker 	}
243*49cdfc7eSAndroid Build Coastguard Worker 	test_set_and_get_sig(SIGUSR1,
244*49cdfc7eSAndroid Build Coastguard Worker 			     "F_GETOWN_EX, F_SETOWN_EX for process group ID");
245*49cdfc7eSAndroid Build Coastguard Worker 
246*49cdfc7eSAndroid Build Coastguard Worker 	setownex_cleanup();
247*49cdfc7eSAndroid Build Coastguard Worker }
248*49cdfc7eSAndroid Build Coastguard Worker #endif
249*49cdfc7eSAndroid Build Coastguard Worker 
test_set_and_get_sig(int sig,char * des)250*49cdfc7eSAndroid Build Coastguard Worker static void test_set_and_get_sig(int sig, char *des)
251*49cdfc7eSAndroid Build Coastguard Worker {
252*49cdfc7eSAndroid Build Coastguard Worker 	int orig_sig;
253*49cdfc7eSAndroid Build Coastguard Worker 
254*49cdfc7eSAndroid Build Coastguard Worker 	TEST(fcntl(test_fd, F_GETSIG));
255*49cdfc7eSAndroid Build Coastguard Worker 	if (TEST_RETURN < 0) {
256*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TFAIL | TTERRNO, cleanup,
257*49cdfc7eSAndroid Build Coastguard Worker 			 "fcntl(fd, F_GETSIG) get orig_sig failed");
258*49cdfc7eSAndroid Build Coastguard Worker 	}
259*49cdfc7eSAndroid Build Coastguard Worker 	orig_sig = TEST_RETURN;
260*49cdfc7eSAndroid Build Coastguard Worker 
261*49cdfc7eSAndroid Build Coastguard Worker 	if (orig_sig == 0 || orig_sig == SIGIO)
262*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TINFO, "default io events signal is SIGIO");
263*49cdfc7eSAndroid Build Coastguard Worker 
264*49cdfc7eSAndroid Build Coastguard Worker 	TEST(fcntl(test_fd, F_SETSIG, sig));
265*49cdfc7eSAndroid Build Coastguard Worker 	if (TEST_RETURN < 0) {
266*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TFAIL | TTERRNO, cleanup,
267*49cdfc7eSAndroid Build Coastguard Worker 			 "fcntl(fd, F_SETSIG, SIG: %d) failed", sig);
268*49cdfc7eSAndroid Build Coastguard Worker 	}
269*49cdfc7eSAndroid Build Coastguard Worker 
270*49cdfc7eSAndroid Build Coastguard Worker 	TEST(fcntl(test_fd, F_GETSIG));
271*49cdfc7eSAndroid Build Coastguard Worker 	if (TEST_RETURN < 0) {
272*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TFAIL | TTERRNO, cleanup,
273*49cdfc7eSAndroid Build Coastguard Worker 			 "fcntl(fd, F_GETSIG) get the set signal failed");
274*49cdfc7eSAndroid Build Coastguard Worker 	}
275*49cdfc7eSAndroid Build Coastguard Worker 	if (TEST_RETURN != sig) {
276*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TFAIL | TTERRNO, cleanup,
277*49cdfc7eSAndroid Build Coastguard Worker 			 "fcntl F_SETSIG set SIG: %d failed", sig);
278*49cdfc7eSAndroid Build Coastguard Worker 	}
279*49cdfc7eSAndroid Build Coastguard Worker 
280*49cdfc7eSAndroid Build Coastguard Worker 	check_io_signal(des);
281*49cdfc7eSAndroid Build Coastguard Worker 
282*49cdfc7eSAndroid Build Coastguard Worker 	/* restore the default signal*/
283*49cdfc7eSAndroid Build Coastguard Worker 	TEST(fcntl(test_fd, F_SETSIG, orig_sig));
284*49cdfc7eSAndroid Build Coastguard Worker 	if (TEST_RETURN < 0) {
285*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TFAIL | TTERRNO, cleanup,
286*49cdfc7eSAndroid Build Coastguard Worker 			 "fcntl restore default signal failed");
287*49cdfc7eSAndroid Build Coastguard Worker 	}
288*49cdfc7eSAndroid Build Coastguard Worker }
289*49cdfc7eSAndroid Build Coastguard Worker 
signal_parent(void)290*49cdfc7eSAndroid Build Coastguard Worker static void signal_parent(void)
291*49cdfc7eSAndroid Build Coastguard Worker {
292*49cdfc7eSAndroid Build Coastguard Worker 	int ret, fd;
293*49cdfc7eSAndroid Build Coastguard Worker 
294*49cdfc7eSAndroid Build Coastguard Worker 	fd = pipe_fds[1];
295*49cdfc7eSAndroid Build Coastguard Worker 	close(pipe_fds[0]);
296*49cdfc7eSAndroid Build Coastguard Worker 
297*49cdfc7eSAndroid Build Coastguard Worker 	ret = setpgrp();
298*49cdfc7eSAndroid Build Coastguard Worker 	if (ret < 0) {
299*49cdfc7eSAndroid Build Coastguard Worker 		fprintf(stderr, "child process(%d) setpgrp() failed: %s \n",
300*49cdfc7eSAndroid Build Coastguard Worker 			getpid(), strerror(errno));
301*49cdfc7eSAndroid Build Coastguard Worker 	}
302*49cdfc7eSAndroid Build Coastguard Worker 
303*49cdfc7eSAndroid Build Coastguard Worker 	/* Wait for parent process to enter sigtimedwait(). */
304*49cdfc7eSAndroid Build Coastguard Worker 	tst_process_state_wait2(getppid(), 'S');
305*49cdfc7eSAndroid Build Coastguard Worker 
306*49cdfc7eSAndroid Build Coastguard Worker 	ret = write(fd, "c", 1);
307*49cdfc7eSAndroid Build Coastguard Worker 
308*49cdfc7eSAndroid Build Coastguard Worker 	switch (ret) {
309*49cdfc7eSAndroid Build Coastguard Worker 	case 0:
310*49cdfc7eSAndroid Build Coastguard Worker 		fprintf(stderr, "No data written, something is wrong\n");
311*49cdfc7eSAndroid Build Coastguard Worker 	break;
312*49cdfc7eSAndroid Build Coastguard Worker 	case -1:
313*49cdfc7eSAndroid Build Coastguard Worker 		fprintf(stderr, "Failed to write to pipe: %s\n",
314*49cdfc7eSAndroid Build Coastguard Worker 			strerror(errno));
315*49cdfc7eSAndroid Build Coastguard Worker 	break;
316*49cdfc7eSAndroid Build Coastguard Worker 	}
317*49cdfc7eSAndroid Build Coastguard Worker 
318*49cdfc7eSAndroid Build Coastguard Worker 	close(fd);
319*49cdfc7eSAndroid Build Coastguard Worker 	return;
320*49cdfc7eSAndroid Build Coastguard Worker }
321*49cdfc7eSAndroid Build Coastguard Worker 
check_io_signal(char * des)322*49cdfc7eSAndroid Build Coastguard Worker static void check_io_signal(char *des)
323*49cdfc7eSAndroid Build Coastguard Worker {
324*49cdfc7eSAndroid Build Coastguard Worker 	int ret;
325*49cdfc7eSAndroid Build Coastguard Worker 	char c;
326*49cdfc7eSAndroid Build Coastguard Worker 	pid_t child;
327*49cdfc7eSAndroid Build Coastguard Worker 
328*49cdfc7eSAndroid Build Coastguard Worker 	child = tst_fork();
329*49cdfc7eSAndroid Build Coastguard Worker 	if (child < 0)
330*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TBROK | TERRNO, cleanup, "fork failed");
331*49cdfc7eSAndroid Build Coastguard Worker 
332*49cdfc7eSAndroid Build Coastguard Worker 	if (child == 0) {
333*49cdfc7eSAndroid Build Coastguard Worker 		signal_parent();
334*49cdfc7eSAndroid Build Coastguard Worker 		exit(0);
335*49cdfc7eSAndroid Build Coastguard Worker 	} else {
336*49cdfc7eSAndroid Build Coastguard Worker 		ret = sigtimedwait(&newset, NULL, &timeout);
337*49cdfc7eSAndroid Build Coastguard Worker 		if (ret == -1) {
338*49cdfc7eSAndroid Build Coastguard Worker 			tst_brkm(TBROK | TERRNO, NULL,
339*49cdfc7eSAndroid Build Coastguard Worker 				 "sigtimedwait() failed.");
340*49cdfc7eSAndroid Build Coastguard Worker 		}
341*49cdfc7eSAndroid Build Coastguard Worker 
342*49cdfc7eSAndroid Build Coastguard Worker 		switch (ret) {
343*49cdfc7eSAndroid Build Coastguard Worker 		case SIGUSR1:
344*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TPASS, "fcntl test %s success", des);
345*49cdfc7eSAndroid Build Coastguard Worker 		break;
346*49cdfc7eSAndroid Build Coastguard Worker 		case SIGIO:
347*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TFAIL, "received default SIGIO, fcntl test "
348*49cdfc7eSAndroid Build Coastguard Worker 				 "%s failed", des);
349*49cdfc7eSAndroid Build Coastguard Worker 		break;
350*49cdfc7eSAndroid Build Coastguard Worker 		default:
351*49cdfc7eSAndroid Build Coastguard Worker 			tst_brkm(TBROK, cleanup, "fcntl io events "
352*49cdfc7eSAndroid Build Coastguard Worker 				 "signal mechanism work abnormally");
353*49cdfc7eSAndroid Build Coastguard Worker 		}
354*49cdfc7eSAndroid Build Coastguard Worker 
355*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_READ(cleanup, 1, test_fd, &c, 1);
356*49cdfc7eSAndroid Build Coastguard Worker 		wait(NULL);
357*49cdfc7eSAndroid Build Coastguard Worker 	}
358*49cdfc7eSAndroid Build Coastguard Worker }
359*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)360*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
361*49cdfc7eSAndroid Build Coastguard Worker {
362*49cdfc7eSAndroid Build Coastguard Worker 	if (sigprocmask(SIG_SETMASK, &oldset, NULL) < 0)
363*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TWARN | TERRNO, "sigprocmask restore oldset failed");
364*49cdfc7eSAndroid Build Coastguard Worker 
365*49cdfc7eSAndroid Build Coastguard Worker 	if (pipe_fds[0] > 0 && close(pipe_fds[0]) == -1)
366*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TWARN | TERRNO, "close(%d) failed", pipe_fds[0]);
367*49cdfc7eSAndroid Build Coastguard Worker 	if (pipe_fds[1] > 0 && close(pipe_fds[1]) == -1)
368*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TWARN | TERRNO, "close(%d) failed", pipe_fds[1]);
369*49cdfc7eSAndroid Build Coastguard Worker }
370