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