blob: 3324d5a2851b6965753e26b8544553122bbe567d [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 */
18/* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
19/* */
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
65/* Harness Specific Include Files. */
66#include "test.h"
67#include "usctest.h"
68
69#define PORT 57392
70
71#ifndef O_CLOEXEC
72# define O_CLOEXEC 02000000
73#endif
74
75/* For Linux these must be the same. */
subrata_modak8d4fee72009-01-27 14:48:38 +000076#ifndef SOCK_CLOEXEC
77# define SOCK_CLOEXEC O_CLOEXEC
78#endif
subrata_modak462f71a2009-01-16 10:01:50 +000079
80/* Extern Global Variables */
Garrett Cooper2c282152010-12-16 00:55:50 -080081extern int Tst_count;
subrata_modak56207ce2009-03-23 13:35:39 +000082extern char *TESTDIR; /* temporary dir created by tst_tmpdir() */
subrata_modak462f71a2009-01-16 10:01:50 +000083
84/* Global Variables */
subrata_modak56207ce2009-03-23 13:35:39 +000085char *TCID = "socket02"; /* test program identifier. */
86int testno;
87int TST_TOTAL = 1; /* total number of tests in this file. */
subrata_modak462f71a2009-01-16 10:01:50 +000088
89/* Extern Global Functions */
90/******************************************************************************/
91/* */
92/* Function: cleanup */
93/* */
94/* Description: Performs all one time clean up for this test on successful */
95/* completion, premature exit or failure. Closes all temporary */
96/* files, removes all temporary directories exits the test with */
97/* appropriate return code by calling tst_exit() function. */
98/* */
99/* Input: None. */
100/* */
101/* Output: None. */
102/* */
103/* Return: On failure - Exits calling tst_exit(). Non '0' return code. */
104/* On success - Exits calling tst_exit(). With '0' return code. */
105/* */
106/******************************************************************************/
subrata_modak56207ce2009-03-23 13:35:39 +0000107extern void cleanup()
108{
Garrett Cooper2c282152010-12-16 00:55:50 -0800109
subrata_modak56207ce2009-03-23 13:35:39 +0000110 TEST_CLEANUP;
111 tst_rmdir();
subrata_modak462f71a2009-01-16 10:01:50 +0000112
subrata_modak462f71a2009-01-16 10:01:50 +0000113}
114
115/* Local Functions */
116/******************************************************************************/
117/* */
118/* Function: setup */
119/* */
120/* Description: Performs all one time setup for this test. This function is */
121/* typically used to capture signals, create temporary dirs */
122/* and temporary files that may be used in the course of this */
123/* test. */
124/* */
125/* Input: None. */
126/* */
127/* Output: None. */
128/* */
129/* Return: On failure - Exits by calling cleanup(). */
130/* On success - returns 0. */
131/* */
132/******************************************************************************/
subrata_modak56207ce2009-03-23 13:35:39 +0000133void setup()
134{
135 /* Capture signals if any */
136 /* Create temporary directories */
137 TEST_PAUSE;
138 tst_tmpdir();
subrata_modak462f71a2009-01-16 10:01:50 +0000139}
140
subrata_modak56207ce2009-03-23 13:35:39 +0000141int main(int argc, char *argv[])
142{
143 int fd, fds[2], i, coe;
144 int lc; /* loop counter */
145 char *msg; /* message returned from parse_opts */
subrata_modak462f71a2009-01-16 10:01:50 +0000146
subrata_modak56207ce2009-03-23 13:35:39 +0000147 /* Parse standard options given to run the test. */
Garrett Cooper53740502010-12-16 00:04:01 -0800148<<<<<<< HEAD
Garrett Cooper45e285d2010-11-22 12:19:25 -0800149 msg = parse_opts(argc, argv, NULL, NULL);
Garrett Cooper53740502010-12-16 00:04:01 -0800150=======
subrata_modak56207ce2009-03-23 13:35:39 +0000151 msg = parse_opts(argc, argv, (option_t *) NULL, NULL);
Garrett Cooper53740502010-12-16 00:04:01 -0800152>>>>>>> master
Garrett Cooper45e285d2010-11-22 12:19:25 -0800153 if (msg != NULL) {
subrata_modak56207ce2009-03-23 13:35:39 +0000154 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
155 tst_exit();
156 }
157 if ((tst_kvercmp(2, 6, 27)) < 0) {
158 tst_resm(TCONF,
159 "This test can only run on kernels that are 2.6.27 and higher");
160 tst_exit();
161 }
162 setup();
subrata_modak462f71a2009-01-16 10:01:50 +0000163
subrata_modak56207ce2009-03-23 13:35:39 +0000164 for (lc = 0; TEST_LOOPING(lc); ++lc) {
165 Tst_count = 0;
166 for (testno = 0; testno < TST_TOTAL; ++testno) {
167 fd = socket(PF_INET, SOCK_STREAM, 0);
168 if (fd == -1) {
169 tst_brkm(TBROK, cleanup, "socket(0) failed");
170 tst_exit();
171 }
172 coe = fcntl(fd, F_GETFD);
173 if (coe == -1) {
174 tst_brkm(TBROK, cleanup, "fcntl failed");
175 tst_exit();
176 }
177 if (coe & FD_CLOEXEC) {
178 tst_resm(TFAIL,
179 "socket(0) set close-on-exec flag");
180 cleanup();
181 tst_exit();
182 }
183 close(fd);
subrata_modak462f71a2009-01-16 10:01:50 +0000184
subrata_modak56207ce2009-03-23 13:35:39 +0000185 fd = socket(PF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0);
186 if (fd == -1) {
187 tst_resm(TFAIL, "socket(SOCK_CLOEXEC) failed");
188 cleanup();
189 tst_exit();
190 }
191 coe = fcntl(fd, F_GETFD);
192 if (coe == -1) {
193 tst_brkm(TBROK, cleanup, "fcntl failed");
194 tst_exit();
195 }
196 if ((coe & FD_CLOEXEC) == 0) {
197 tst_resm(TFAIL,
198 "socket(SOCK_CLOEXEC) does not set close-on-exec flag");
199 cleanup();
200 tst_exit();
201 }
202 close(fd);
subrata_modak462f71a2009-01-16 10:01:50 +0000203
subrata_modak56207ce2009-03-23 13:35:39 +0000204 if (socketpair(PF_UNIX, SOCK_STREAM, 0, fds) == -1) {
205 tst_brkm(TBROK, cleanup,
206 "socketpair(0) failed");
207 tst_exit();
208 }
209 for (i = 0; i < 2; ++i) {
210 coe = fcntl(fds[i], F_GETFD);
211 if (coe == -1) {
212 tst_brkm(TBROK, cleanup,
213 "fcntl failed");
214 tst_exit();
215 }
216 if (coe & FD_CLOEXEC) {
217 tst_resm(TFAIL,
218 "socketpair(0) set close-on-exec flag for fds[%d]\n",
219 i);
220 cleanup();
221 tst_exit();
222 }
223 close(fds[i]);
224 }
subrata_modak462f71a2009-01-16 10:01:50 +0000225
subrata_modak56207ce2009-03-23 13:35:39 +0000226 if (socketpair
227 (PF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0,
228 fds) == -1) {
229 tst_brkm(TBROK, cleanup,
230 "socketpair(SOCK_CLOEXEC) failed");
231 tst_exit();
232 }
233 for (i = 0; i < 2; ++i) {
234 coe = fcntl(fds[i], F_GETFD);
235 if (coe == -1) {
236 tst_brkm(TBROK, cleanup,
237 "fcntl failed");
238 tst_exit();
239 }
240 if ((coe & FD_CLOEXEC) == 0) {
241 tst_resm(TFAIL,
242 "socketpair(SOCK_CLOEXEC) does not set close-on-exec flag for fds[%d]\n",
243 i);
244 cleanup();
245 tst_exit();
246 }
247 close(fds[i]);
248 }
249 tst_resm(TPASS, "socket(SOCK_CLOEXEC) PASSED");
250 cleanup();
251 }
252 }
253 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800254}