• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) Ulrich Drepper <drepper@redhat.com>
4 * Copyright (c) International Business Machines Corp., 2009
5 */
6 
7 /*
8 * Test Name:	socket02
9 *
10 * Description:
11 * This program tests the new flag SOCK_CLOEXEC and SOCK_NONBLOCK introduced
12 * in socket() in kernel 2.6.27.
13 */
14 
15 #include <fcntl.h>
16 #include <stdio.h>
17 #include <unistd.h>
18 #include <netinet/in.h>
19 #include <sys/socket.h>
20 #include "lapi/fcntl.h"
21 #include "tst_test.h"
22 
23 static int fd;
24 
25 static struct tcase {
26 	int type;
27 	int flag;
28 	int fl_flag;
29 	char *des;
30 } tcases[] = {
31 	{SOCK_STREAM, 0, F_GETFD, "no close-on-exec"},
32 	{SOCK_STREAM | SOCK_CLOEXEC, FD_CLOEXEC, F_GETFD, "close-on-exec"},
33 	{SOCK_STREAM, 0, F_GETFL, "no non-blocking"},
34 	{SOCK_STREAM | SOCK_NONBLOCK, O_NONBLOCK, F_GETFL, "non-blocking"}
35 };
36 
verify_socket(unsigned int n)37 static void verify_socket(unsigned int n)
38 {
39 	int res;
40 	struct tcase *tc = &tcases[n];
41 
42 	fd = socket(PF_INET, tc->type, 0);
43 	if (fd == -1)
44 		tst_brk(TFAIL | TERRNO, "socket() failed");
45 
46 	res = SAFE_FCNTL(fd, tc->fl_flag);
47 
48 	if (tc->flag != 0 && (res & tc->flag) == 0) {
49 		tst_res(TFAIL, "socket() failed to set %s flag", tc->des);
50 		return;
51 	}
52 
53 	if (tc->flag == 0 && (res & tc->flag) != 0) {
54 		tst_res(TFAIL, "socket() failed to set %s flag", tc->des);
55 		return;
56 	}
57 
58 	tst_res(TPASS, "socket() passed to set %s flag", tc->des);
59 
60 	SAFE_CLOSE(fd);
61 }
62 
cleanup(void)63 static void cleanup(void)
64 {
65 	if (fd > 0)
66 		SAFE_CLOSE(fd);
67 }
68 
69 static struct tst_test test = {
70 	.tcnt = ARRAY_SIZE(tcases),
71 	.test = verify_socket,
72 	.min_kver = "2.6.27",
73 	.cleanup = cleanup
74 };
75