• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright (c) Ulrich Drepper <drepper@redhat.com>
3 * Copyright (c) International Business Machines  Corp., 2009
4 *
5 * This program is free software;  you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY;  without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13 * the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program.
17 */
18 
19 /*
20 * Test Name:	socket02
21 *
22 * Description:
23 * This program tests the new flag SOCK_CLOEXEC and SOCK_NONBLOCK introduced
24 * in socket() in kernel 2.6.27.
25 */
26 
27 #include <fcntl.h>
28 #include <stdio.h>
29 #include <unistd.h>
30 #include <netinet/in.h>
31 #include <sys/socket.h>
32 #include "lapi/fcntl.h"
33 #include "tst_test.h"
34 
35 static int fd;
36 
37 static struct tcase {
38 	int type;
39 	int flag;
40 	int fl_flag;
41 	char *des;
42 } tcases[] = {
43 	{SOCK_STREAM, 0, F_GETFD, "no close-on-exec"},
44 	{SOCK_STREAM | SOCK_CLOEXEC, FD_CLOEXEC, F_GETFD, "close-on-exec"},
45 	{SOCK_STREAM, 0, F_GETFL, "no non-blocking"},
46 	{SOCK_STREAM | SOCK_NONBLOCK, O_NONBLOCK, F_GETFL, "non-blocking"}
47 };
48 
verify_socket(unsigned int n)49 static void verify_socket(unsigned int n)
50 {
51 	int res;
52 	struct tcase *tc = &tcases[n];
53 
54 	fd = socket(PF_INET, tc->type, 0);
55 	if (fd == -1)
56 		tst_brk(TFAIL | TERRNO, "socket() failed");
57 
58 	res = SAFE_FCNTL(fd, tc->fl_flag);
59 
60 	if (tc->flag != 0 && (res & tc->flag) == 0) {
61 		tst_res(TFAIL, "socket() failed to set %s flag", tc->des);
62 		return;
63 	}
64 
65 	if (tc->flag == 0 && (res & tc->flag) != 0) {
66 		tst_res(TFAIL, "socket() failed to set %s flag", tc->des);
67 		return;
68 	}
69 
70 	tst_res(TPASS, "socket() passed to set %s flag", tc->des);
71 
72 	SAFE_CLOSE(fd);
73 }
74 
cleanup(void)75 static void cleanup(void)
76 {
77 	if (fd > 0)
78 		SAFE_CLOSE(fd);
79 }
80 
81 static struct tst_test test = {
82 	.tid = "socket02",
83 	.tcnt = ARRAY_SIZE(tcases),
84 	.test = verify_socket,
85 	.min_kver = "2.6.27",
86 	.cleanup = cleanup
87 };
88