xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/socket/socket02.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:	socket02
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 socket() in kernel 2.6.27.
13*49cdfc7eSAndroid Build Coastguard Worker */
14*49cdfc7eSAndroid Build Coastguard Worker 
15*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
16*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
17*49cdfc7eSAndroid Build Coastguard Worker #include <netinet/in.h>
18*49cdfc7eSAndroid Build Coastguard Worker #include <sys/socket.h>
19*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/fcntl.h"
20*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
21*49cdfc7eSAndroid Build Coastguard Worker 
22*49cdfc7eSAndroid Build Coastguard Worker static int fd;
23*49cdfc7eSAndroid Build Coastguard Worker 
24*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
25*49cdfc7eSAndroid Build Coastguard Worker 	int type;
26*49cdfc7eSAndroid Build Coastguard Worker 	int flag;
27*49cdfc7eSAndroid Build Coastguard Worker 	int fl_flag;
28*49cdfc7eSAndroid Build Coastguard Worker 	char *des;
29*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
30*49cdfc7eSAndroid Build Coastguard Worker 	{SOCK_STREAM, 0, F_GETFD, "no close-on-exec"},
31*49cdfc7eSAndroid Build Coastguard Worker 	{SOCK_STREAM | SOCK_CLOEXEC, FD_CLOEXEC, F_GETFD, "close-on-exec"},
32*49cdfc7eSAndroid Build Coastguard Worker 	{SOCK_STREAM, 0, F_GETFL, "no non-blocking"},
33*49cdfc7eSAndroid Build Coastguard Worker 	{SOCK_STREAM | SOCK_NONBLOCK, O_NONBLOCK, F_GETFL, "non-blocking"}
34*49cdfc7eSAndroid Build Coastguard Worker };
35*49cdfc7eSAndroid Build Coastguard Worker 
verify_socket(unsigned int n)36*49cdfc7eSAndroid Build Coastguard Worker static void verify_socket(unsigned int n)
37*49cdfc7eSAndroid Build Coastguard Worker {
38*49cdfc7eSAndroid Build Coastguard Worker 	int res;
39*49cdfc7eSAndroid Build Coastguard Worker 	struct tcase *tc = &tcases[n];
40*49cdfc7eSAndroid Build Coastguard Worker 
41*49cdfc7eSAndroid Build Coastguard Worker 	fd = socket(PF_INET, tc->type, 0);
42*49cdfc7eSAndroid Build Coastguard Worker 	if (fd == -1)
43*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TFAIL | TERRNO, "socket() failed");
44*49cdfc7eSAndroid Build Coastguard Worker 
45*49cdfc7eSAndroid Build Coastguard Worker 	res = SAFE_FCNTL(fd, tc->fl_flag);
46*49cdfc7eSAndroid Build Coastguard Worker 
47*49cdfc7eSAndroid Build Coastguard Worker 	if (tc->flag != 0 && (res & tc->flag) == 0) {
48*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "socket() failed to set %s flag", tc->des);
49*49cdfc7eSAndroid Build Coastguard Worker 		return;
50*49cdfc7eSAndroid Build Coastguard Worker 	}
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, "socket() failed to set %s flag", tc->des);
54*49cdfc7eSAndroid Build Coastguard Worker 		return;
55*49cdfc7eSAndroid Build Coastguard Worker 	}
56*49cdfc7eSAndroid Build Coastguard Worker 
57*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TPASS, "socket() passed to set %s flag", tc->des);
58*49cdfc7eSAndroid Build Coastguard Worker 
59*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(fd);
60*49cdfc7eSAndroid Build Coastguard Worker }
61*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)62*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
63*49cdfc7eSAndroid Build Coastguard Worker {
64*49cdfc7eSAndroid Build Coastguard Worker 	if (fd > 0)
65*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(fd);
66*49cdfc7eSAndroid Build Coastguard Worker }
67*49cdfc7eSAndroid Build Coastguard Worker 
68*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
69*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(tcases),
70*49cdfc7eSAndroid Build Coastguard Worker 	.test = verify_socket,
71*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup
72*49cdfc7eSAndroid Build Coastguard Worker };
73