blob: 05841638cc1dcf31db1df81930f45a5a26bbe40f [file] [log] [blame]
Xiao Yangd2a810a2016-06-24 17:04:51 +08001/*
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*/
subrata_modak462f71a2009-01-16 10:01:50 +000026
subrata_modak462f71a2009-01-16 10:01:50 +000027#include <fcntl.h>
28#include <stdio.h>
29#include <unistd.h>
30#include <netinet/in.h>
31#include <sys/socket.h>
Cyril Hrubis98f87472014-01-08 13:32:41 +010032#include "lapi/fcntl.h"
Xiao Yangd2a810a2016-06-24 17:04:51 +080033#include "tst_test.h"
subrata_modak462f71a2009-01-16 10:01:50 +000034
Xiao Yangd2a810a2016-06-24 17:04:51 +080035static int fd;
subrata_modak462f71a2009-01-16 10:01:50 +000036
Xiao Yangd2a810a2016-06-24 17:04:51 +080037static 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};
subrata_modak462f71a2009-01-16 10:01:50 +000048
Xiao Yangd2a810a2016-06-24 17:04:51 +080049static void verify_socket(unsigned int n)
subrata_modak56207ce2009-03-23 13:35:39 +000050{
Xiao Yangd2a810a2016-06-24 17:04:51 +080051 int res;
52 struct tcase *tc = &tcases[n];
Garrett Cooper2c282152010-12-16 00:55:50 -080053
Xiao Yangd2a810a2016-06-24 17:04:51 +080054 fd = socket(PF_INET, tc->type, 0);
55 if (fd == -1)
56 tst_brk(TFAIL | TERRNO, "socket() failed");
subrata_modak462f71a2009-01-16 10:01:50 +000057
Xiao Yangd2a810a2016-06-24 17:04:51 +080058 res = SAFE_FCNTL(fd, tc->fl_flag);
subrata_modak462f71a2009-01-16 10:01:50 +000059
Xiao Yangd2a810a2016-06-24 17:04:51 +080060 if (tc->flag != 0 && (res & tc->flag) == 0) {
61 tst_res(TFAIL, "socket() failed to set %s flag", tc->des);
62 return;
subrata_modak56207ce2009-03-23 13:35:39 +000063 }
subrata_modak462f71a2009-01-16 10:01:50 +000064
Xiao Yangd2a810a2016-06-24 17:04:51 +080065 if (tc->flag == 0 && (res & tc->flag) != 0) {
66 tst_res(TFAIL, "socket() failed to set %s flag", tc->des);
67 return;
subrata_modak56207ce2009-03-23 13:35:39 +000068 }
Xiao Yangd2a810a2016-06-24 17:04:51 +080069
70 tst_res(TPASS, "socket() passed to set %s flag", tc->des);
71
72 SAFE_CLOSE(fd);
Chris Dearmanec6edca2012-10-17 19:54:01 -070073}
Xiao Yangd2a810a2016-06-24 17:04:51 +080074
75static void cleanup(void)
76{
Cyril Hrubise1c49ed2017-02-14 13:23:35 +010077 if (fd > 0)
78 SAFE_CLOSE(fd);
Xiao Yangd2a810a2016-06-24 17:04:51 +080079}
80
81static struct tst_test test = {
Xiao Yangd2a810a2016-06-24 17:04:51 +080082 .tcnt = ARRAY_SIZE(tcases),
83 .test = verify_socket,
84 .min_kver = "2.6.27",
85 .cleanup = cleanup
86};