blob: 470ae64897bf579ca2ca40697886e2251139ea75 [file] [log] [blame]
subrata_modak462f71a2009-01-16 10:01:50 +00001/******************************************************************************/
2/* */
3/* Copyright (c) Ulrich Drepper <drepper@redhat.com> */
4/* Copyright (c) International Business Machines Corp., 2009 */
5/* */
6/* This program is free software; you can redistribute it and/or modify */
7/* it under the terms of the GNU General Public License as published by */
8/* the Free Software Foundation; either version 2 of the License, or */
9/* (at your option) any later version. */
10/* */
11/* This program is distributed in the hope that it will be useful, */
12/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
13/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See */
14/* the GNU General Public License for more details. */
15/* */
16/* You should have received a copy of the GNU General Public License */
17/* along with this program; if not, write to the Free Software */
Wanlong Gao4548c6c2012-10-19 18:03:36 +080018/* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */
subrata_modak462f71a2009-01-16 10:01:50 +000019/* */
20/******************************************************************************/
21/******************************************************************************/
22/* */
23/* File: socket02.c */
24/* */
25/* Description: This program tests the new flag SOCK_CLOEXEC introduced in */
26/* socket() & socketpair() and in kernel 2.6.27. Ulrich´s comment*/
27/* as in: */
28/* http://git.kernel.org/?p=linux/kernel/git/torvalds/linux-2.6.git;a=commit;h=a677a039be7243357d93502bff2b40850c942e2d */
29/* says: */
30/* */
31/* flag parameters: socket and socketpair */
32/* This patch adds support for flag values which are ORed to the */
33/* type passwd to socket and socketpair. The additional code is */
34/* minimal. The flag values in this implementation can and must */
35/* match the O_* flags. This avoids overhead in the conversion. */
36/* The internal functions sock_alloc_fd and sock_map_fd get a new*/
37/* parameters and all callers are changed. */
38/* */
39/* Usage: <for command-line> */
40/* socket02 [-c n] [-e][-i n] [-I x] [-p x] [-t] */
41/* where, -c n : Run n copies concurrently. */
42/* -e : Turn on errno logging. */
43/* -i n : Execute test n times. */
44/* -I x : Execute test for x seconds. */
45/* -P x : Pause for x seconds between iterations. */
46/* -t : Turn on syscall timing. */
47/* */
48/* Total Tests: 1 */
49/* */
50/* Test Name: socket02 */
51/* */
52/* Author: Ulrich Drepper <drepper@redhat.com> */
53/* */
54/* History: Created - Jan 05 2009 - Ulrich Drepper <drepper@redhat.com> */
55/* Ported to LTP */
56/* - Jan 05 2009 - Subrata <subrata@linux.vnet.ibm.com> */
57/******************************************************************************/
58
subrata_modak462f71a2009-01-16 10:01:50 +000059#include <fcntl.h>
60#include <stdio.h>
61#include <unistd.h>
62#include <netinet/in.h>
63#include <sys/socket.h>
64
subrata_modak462f71a2009-01-16 10:01:50 +000065#include "test.h"
66#include "usctest.h"
Cyril Hrubis98f87472014-01-08 13:32:41 +010067#include "lapi/fcntl.h"
subrata_modak462f71a2009-01-16 10:01:50 +000068
69#define PORT 57392
70
subrata_modak462f71a2009-01-16 10:01:50 +000071/* For Linux these must be the same. */
subrata_modak8d4fee72009-01-27 14:48:38 +000072#ifndef SOCK_CLOEXEC
Wanlong Gao354ebb42012-12-07 10:10:04 +080073#define SOCK_CLOEXEC O_CLOEXEC
subrata_modak8d4fee72009-01-27 14:48:38 +000074#endif
subrata_modak462f71a2009-01-16 10:01:50 +000075
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020076char *TCID = "socket02";
subrata_modak56207ce2009-03-23 13:35:39 +000077int testno;
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020078int TST_TOTAL = 1;
subrata_modak462f71a2009-01-16 10:01:50 +000079
80/* Extern Global Functions */
81/******************************************************************************/
82/* */
83/* Function: cleanup */
84/* */
85/* Description: Performs all one time clean up for this test on successful */
86/* completion, premature exit or failure. Closes all temporary */
87/* files, removes all temporary directories exits the test with */
88/* appropriate return code by calling tst_exit() function. */
89/* */
90/* Input: None. */
91/* */
92/* Output: None. */
93/* */
94/* Return: On failure - Exits calling tst_exit(). Non '0' return code. */
95/* On success - Exits calling tst_exit(). With '0' return code. */
96/* */
97/******************************************************************************/
Mike Frysingerc57fba52014-04-09 18:56:30 -040098void cleanup(void)
subrata_modak56207ce2009-03-23 13:35:39 +000099{
Garrett Cooper2c282152010-12-16 00:55:50 -0800100
subrata_modak56207ce2009-03-23 13:35:39 +0000101 TEST_CLEANUP;
102 tst_rmdir();
subrata_modak462f71a2009-01-16 10:01:50 +0000103
subrata_modak462f71a2009-01-16 10:01:50 +0000104}
105
106/* Local Functions */
107/******************************************************************************/
108/* */
109/* Function: setup */
110/* */
111/* Description: Performs all one time setup for this test. This function is */
112/* typically used to capture signals, create temporary dirs */
113/* and temporary files that may be used in the course of this */
114/* test. */
115/* */
116/* Input: None. */
117/* */
118/* Output: None. */
119/* */
120/* Return: On failure - Exits by calling cleanup(). */
121/* On success - returns 0. */
122/* */
123/******************************************************************************/
Mike Frysingerc57fba52014-04-09 18:56:30 -0400124void setup(void)
subrata_modak56207ce2009-03-23 13:35:39 +0000125{
126 /* Capture signals if any */
127 /* Create temporary directories */
128 TEST_PAUSE;
129 tst_tmpdir();
subrata_modak462f71a2009-01-16 10:01:50 +0000130}
131
subrata_modak56207ce2009-03-23 13:35:39 +0000132int main(int argc, char *argv[])
133{
134 int fd, fds[2], i, coe;
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200135 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200136 const char *msg;
subrata_modak462f71a2009-01-16 10:01:50 +0000137
Garrett Cooper45e285d2010-11-22 12:19:25 -0800138 msg = parse_opts(argc, argv, NULL, NULL);
139 if (msg != NULL) {
subrata_modak56207ce2009-03-23 13:35:39 +0000140 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
subrata_modak56207ce2009-03-23 13:35:39 +0000141 }
142 if ((tst_kvercmp(2, 6, 27)) < 0) {
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100143 tst_brkm(TCONF,
144 NULL,
subrata_modak56207ce2009-03-23 13:35:39 +0000145 "This test can only run on kernels that are 2.6.27 and higher");
subrata_modak56207ce2009-03-23 13:35:39 +0000146 }
147 setup();
subrata_modak462f71a2009-01-16 10:01:50 +0000148
subrata_modak56207ce2009-03-23 13:35:39 +0000149 for (lc = 0; TEST_LOOPING(lc); ++lc) {
Caspar Zhangd59a6592013-03-07 14:59:12 +0800150 tst_count = 0;
subrata_modak56207ce2009-03-23 13:35:39 +0000151 for (testno = 0; testno < TST_TOTAL; ++testno) {
152 fd = socket(PF_INET, SOCK_STREAM, 0);
153 if (fd == -1) {
154 tst_brkm(TBROK, cleanup, "socket(0) failed");
subrata_modak56207ce2009-03-23 13:35:39 +0000155 }
156 coe = fcntl(fd, F_GETFD);
157 if (coe == -1) {
158 tst_brkm(TBROK, cleanup, "fcntl failed");
subrata_modak56207ce2009-03-23 13:35:39 +0000159 }
160 if (coe & FD_CLOEXEC) {
Cyril Hrubis9fa8ad02014-12-16 13:20:49 +0100161 tst_brkm(TFAIL,
162 cleanup,
subrata_modak56207ce2009-03-23 13:35:39 +0000163 "socket(0) set close-on-exec flag");
subrata_modak56207ce2009-03-23 13:35:39 +0000164 }
165 close(fd);
subrata_modak462f71a2009-01-16 10:01:50 +0000166
subrata_modak56207ce2009-03-23 13:35:39 +0000167 fd = socket(PF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0);
168 if (fd == -1) {
Cyril Hrubis9fa8ad02014-12-16 13:20:49 +0100169 tst_brkm(TFAIL, cleanup,
170 "socket(SOCK_CLOEXEC) failed");
subrata_modak56207ce2009-03-23 13:35:39 +0000171 }
172 coe = fcntl(fd, F_GETFD);
173 if (coe == -1) {
174 tst_brkm(TBROK, cleanup, "fcntl failed");
subrata_modak56207ce2009-03-23 13:35:39 +0000175 }
176 if ((coe & FD_CLOEXEC) == 0) {
Cyril Hrubis9fa8ad02014-12-16 13:20:49 +0100177 tst_brkm(TFAIL,
178 cleanup,
subrata_modak56207ce2009-03-23 13:35:39 +0000179 "socket(SOCK_CLOEXEC) does not set close-on-exec flag");
subrata_modak56207ce2009-03-23 13:35:39 +0000180 }
181 close(fd);
subrata_modak462f71a2009-01-16 10:01:50 +0000182
subrata_modak56207ce2009-03-23 13:35:39 +0000183 if (socketpair(PF_UNIX, SOCK_STREAM, 0, fds) == -1) {
184 tst_brkm(TBROK, cleanup,
185 "socketpair(0) failed");
subrata_modak56207ce2009-03-23 13:35:39 +0000186 }
187 for (i = 0; i < 2; ++i) {
188 coe = fcntl(fds[i], F_GETFD);
189 if (coe == -1) {
190 tst_brkm(TBROK, cleanup,
191 "fcntl failed");
subrata_modak56207ce2009-03-23 13:35:39 +0000192 }
193 if (coe & FD_CLOEXEC) {
Cyril Hrubis9fa8ad02014-12-16 13:20:49 +0100194 tst_brkm(TFAIL,
195 cleanup, "socketpair(0) set close-on-exec flag for fds[%d]\n",
subrata_modak56207ce2009-03-23 13:35:39 +0000196 i);
subrata_modak56207ce2009-03-23 13:35:39 +0000197 }
198 close(fds[i]);
199 }
subrata_modak462f71a2009-01-16 10:01:50 +0000200
subrata_modak56207ce2009-03-23 13:35:39 +0000201 if (socketpair
202 (PF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0,
203 fds) == -1) {
204 tst_brkm(TBROK, cleanup,
205 "socketpair(SOCK_CLOEXEC) failed");
subrata_modak56207ce2009-03-23 13:35:39 +0000206 }
207 for (i = 0; i < 2; ++i) {
208 coe = fcntl(fds[i], F_GETFD);
209 if (coe == -1) {
210 tst_brkm(TBROK, cleanup,
211 "fcntl failed");
subrata_modak56207ce2009-03-23 13:35:39 +0000212 }
213 if ((coe & FD_CLOEXEC) == 0) {
Cyril Hrubis9fa8ad02014-12-16 13:20:49 +0100214 tst_brkm(TFAIL,
215 cleanup, "socketpair(SOCK_CLOEXEC) does not set close-on-exec flag for fds[%d]\n",
subrata_modak56207ce2009-03-23 13:35:39 +0000216 i);
subrata_modak56207ce2009-03-23 13:35:39 +0000217 }
218 close(fds[i]);
219 }
220 tst_resm(TPASS, "socket(SOCK_CLOEXEC) PASSED");
221 cleanup();
222 }
223 }
224 tst_exit();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700225}