xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/socketpair/socketpair02.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) Ulrich Drepper <[email protected]>
4*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) International Business Machines Corp., 2009
5*49cdfc7eSAndroid Build Coastguard Worker */
6*49cdfc7eSAndroid Build Coastguard Worker 
7*49cdfc7eSAndroid Build Coastguard Worker /*
8*49cdfc7eSAndroid Build Coastguard Worker * Test Name:	socketpair02
9*49cdfc7eSAndroid Build Coastguard Worker *
10*49cdfc7eSAndroid Build Coastguard Worker * Description:
11*49cdfc7eSAndroid Build Coastguard Worker * This Program tests the new flag SOCK_CLOEXEC and SOCK_NONBLOCK introduced
12*49cdfc7eSAndroid Build Coastguard Worker * in socketpair() in kernel 2.6.27.
13*49cdfc7eSAndroid Build Coastguard Worker */
14*49cdfc7eSAndroid Build Coastguard Worker 
15*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
16*49cdfc7eSAndroid Build Coastguard Worker #include <pthread.h>
17*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
18*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
19*49cdfc7eSAndroid Build Coastguard Worker #include <netinet/in.h>
20*49cdfc7eSAndroid Build Coastguard Worker #include <sys/socket.h>
21*49cdfc7eSAndroid Build Coastguard Worker #include <sys/syscall.h>
22*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/fcntl.h"
23*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
24*49cdfc7eSAndroid Build Coastguard Worker 
25*49cdfc7eSAndroid Build Coastguard Worker static int fds[2];
26*49cdfc7eSAndroid Build Coastguard Worker 
27*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
28*49cdfc7eSAndroid Build Coastguard Worker 	int type;
29*49cdfc7eSAndroid Build Coastguard Worker 	int flag;
30*49cdfc7eSAndroid Build Coastguard Worker 	int fl_flag;
31*49cdfc7eSAndroid Build Coastguard Worker 	char *des;
32*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
33*49cdfc7eSAndroid Build Coastguard Worker 	{SOCK_STREAM, 0, F_GETFD, "no close-on-exec"},
34*49cdfc7eSAndroid Build Coastguard Worker 	{SOCK_STREAM | SOCK_CLOEXEC, FD_CLOEXEC, F_GETFD, "close-on-exec"},
35*49cdfc7eSAndroid Build Coastguard Worker 	{SOCK_STREAM, 0, F_GETFL, "no non-blocking"},
36*49cdfc7eSAndroid Build Coastguard Worker 	{SOCK_STREAM | SOCK_NONBLOCK, O_NONBLOCK, F_GETFL, "non-blocking"}
37*49cdfc7eSAndroid Build Coastguard Worker };
38*49cdfc7eSAndroid Build Coastguard Worker 
verify_socketpair(unsigned int n)39*49cdfc7eSAndroid Build Coastguard Worker static void verify_socketpair(unsigned int n)
40*49cdfc7eSAndroid Build Coastguard Worker {
41*49cdfc7eSAndroid Build Coastguard Worker 	int res, i;
42*49cdfc7eSAndroid Build Coastguard Worker 	struct tcase *tc = &tcases[n];
43*49cdfc7eSAndroid Build Coastguard Worker 
44*49cdfc7eSAndroid Build Coastguard Worker 	TEST(socketpair(PF_UNIX, tc->type, 0, fds));
45*49cdfc7eSAndroid Build Coastguard Worker 
46*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET == -1)
47*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TFAIL | TTERRNO, "socketpair() failed");
48*49cdfc7eSAndroid Build Coastguard Worker 
49*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < 2; i++) {
50*49cdfc7eSAndroid Build Coastguard Worker 		res = SAFE_FCNTL(fds[i], tc->fl_flag);
51*49cdfc7eSAndroid Build Coastguard Worker 
52*49cdfc7eSAndroid Build Coastguard Worker 		if (tc->flag != 0 && (res & tc->flag) == 0) {
53*49cdfc7eSAndroid Build Coastguard Worker 			tst_res(TFAIL, "socketpair() failed to set %s flag for fds[%d]",
54*49cdfc7eSAndroid Build Coastguard Worker 				tc->des, i);
55*49cdfc7eSAndroid Build Coastguard Worker 			goto ret;
56*49cdfc7eSAndroid Build Coastguard Worker 		}
57*49cdfc7eSAndroid Build Coastguard Worker 
58*49cdfc7eSAndroid Build Coastguard Worker 		if (tc->flag == 0 && (res & tc->flag) != 0) {
59*49cdfc7eSAndroid Build Coastguard Worker 			tst_res(TFAIL, "socketpair() failed to set %s flag for fds[%d]",
60*49cdfc7eSAndroid Build Coastguard Worker 				tc->des, i);
61*49cdfc7eSAndroid Build Coastguard Worker 			goto ret;
62*49cdfc7eSAndroid Build Coastguard Worker 		}
63*49cdfc7eSAndroid Build Coastguard Worker 	}
64*49cdfc7eSAndroid Build Coastguard Worker 
65*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TPASS, "socketpair() passed to set %s flag", tc->des);
66*49cdfc7eSAndroid Build Coastguard Worker 
67*49cdfc7eSAndroid Build Coastguard Worker ret:
68*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(fds[0]);
69*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(fds[1]);
70*49cdfc7eSAndroid Build Coastguard Worker }
71*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)72*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
73*49cdfc7eSAndroid Build Coastguard Worker {
74*49cdfc7eSAndroid Build Coastguard Worker 	if (fds[0] > 0)
75*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(fds[0]);
76*49cdfc7eSAndroid Build Coastguard Worker 
77*49cdfc7eSAndroid Build Coastguard Worker 	if (fds[1] > 0)
78*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(fds[1]);
79*49cdfc7eSAndroid Build Coastguard Worker }
80*49cdfc7eSAndroid Build Coastguard Worker 
81*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
82*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(tcases),
83*49cdfc7eSAndroid Build Coastguard Worker 	.test = verify_socketpair,
84*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup
85*49cdfc7eSAndroid Build Coastguard Worker };
86