blob: aad524a8480fdf2ed9ab4f40351ebfbb392a0bf2 [file] [log] [blame]
subrata_modaka97f1162009-01-16 10:24:56 +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_modaka97f1162009-01-16 10:24:56 +000019/* */
20/******************************************************************************/
21/******************************************************************************/
22/* */
23/* File: socket03.c */
24/* */
25/* Description: This Program tests the new system call introduced in 2.6.27. */
26/* Ulrich´s comment as in: */
27/* http://git.kernel.org/?p=linux/kernel/git/torvalds/linux-2.6.git;a=commit;h=77d2720059618b9b6e827a8b73831eb6c6fad63c */
28/* */
29/* Usage: <for command-line> */
30/* socket03 [-c n] [-e][-i n] [-I x] [-p x] [-t] */
31/* where, -c n : Run n copies concurrently. */
32/* -e : Turn on errno logging. */
33/* -i n : Execute test n times. */
34/* -I x : Execute test for x seconds. */
35/* -P x : Pause for x seconds between iterations. */
36/* -t : Turn on syscall timing. */
37/* */
38/* Total Tests: 1 */
39/* */
40/* Test Name: socket03 */
41/* */
42/* Author: Ulrich Drepper <drepper@redhat.com> */
43/* */
44/* History: Created - Jan 13 2009 - Ulrich Drepper <drepper@redhat.com> */
45/* Ported to LTP */
46/* - Jan 13 2009 - Subrata <subrata@linux.vnet.ibm.com> */
47/******************************************************************************/
48#include <fcntl.h>
49#include <pthread.h>
50#include <stdio.h>
51#include <unistd.h>
52#include <netinet/in.h>
53#include <sys/socket.h>
54#include <sys/syscall.h>
55
subrata_modaka97f1162009-01-16 10:24:56 +000056#include "test.h"
subrata_modaka97f1162009-01-16 10:24:56 +000057
subrata_modak8d4fee72009-01-27 14:48:38 +000058#ifndef SOCK_NONBLOCK
Wanlong Gao354ebb42012-12-07 10:10:04 +080059#define SOCK_NONBLOCK O_NONBLOCK
subrata_modak8d4fee72009-01-27 14:48:38 +000060#endif
subrata_modaka97f1162009-01-16 10:24:56 +000061
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020062char *TCID = "socket03";
subrata_modak56207ce2009-03-23 13:35:39 +000063int testno;
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020064int TST_TOTAL = 1;
subrata_modaka97f1162009-01-16 10:24:56 +000065
66/* Extern Global Functions */
67/******************************************************************************/
68/* */
69/* Function: cleanup */
70/* */
71/* Description: Performs all one time clean up for this test on successful */
72/* completion, premature exit or failure. Closes all temporary */
73/* files, removes all temporary directories exits the test with */
74/* appropriate return code by calling tst_exit() function. */
75/* */
76/* Input: None. */
77/* */
78/* Output: None. */
79/* */
80/* Return: On failure - Exits calling tst_exit(). Non '0' return code. */
81/* On success - Exits calling tst_exit(). With '0' return code. */
82/* */
83/******************************************************************************/
Mike Frysingerc57fba52014-04-09 18:56:30 -040084void cleanup(void)
subrata_modak56207ce2009-03-23 13:35:39 +000085{
Garrett Cooper2c282152010-12-16 00:55:50 -080086
subrata_modak56207ce2009-03-23 13:35:39 +000087 tst_rmdir();
subrata_modaka97f1162009-01-16 10:24:56 +000088
subrata_modaka97f1162009-01-16 10:24:56 +000089}
90
91/* Local Functions */
92/******************************************************************************/
93/* */
94/* Function: setup */
95/* */
96/* Description: Performs all one time setup for this test. This function is */
97/* typically used to capture signals, create temporary dirs */
98/* and temporary files that may be used in the course of this */
99/* test. */
100/* */
101/* Input: None. */
102/* */
103/* Output: None. */
104/* */
105/* Return: On failure - Exits by calling cleanup(). */
106/* On success - returns 0. */
107/* */
108/******************************************************************************/
Mike Frysingerc57fba52014-04-09 18:56:30 -0400109void setup(void)
subrata_modak56207ce2009-03-23 13:35:39 +0000110{
111 /* Capture signals if any */
112 /* Create temporary directories */
113 TEST_PAUSE;
114 tst_tmpdir();
subrata_modaka97f1162009-01-16 10:24:56 +0000115}
116
subrata_modak56207ce2009-03-23 13:35:39 +0000117int main(int argc, char *argv[])
118{
119 int fd, fl;
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200120 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200121 const char *msg;
subrata_modaka97f1162009-01-16 10:24:56 +0000122
Garrett Cooper45e285d2010-11-22 12:19:25 -0800123 msg = parse_opts(argc, argv, NULL, NULL);
124 if (msg != NULL) {
subrata_modak56207ce2009-03-23 13:35:39 +0000125 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
subrata_modak56207ce2009-03-23 13:35:39 +0000126 }
127 if ((tst_kvercmp(2, 6, 27)) < 0) {
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100128 tst_brkm(TCONF,
129 NULL,
subrata_modak56207ce2009-03-23 13:35:39 +0000130 "This test can only run on kernels that are 2.6.27 and higher");
subrata_modak56207ce2009-03-23 13:35:39 +0000131 }
132 setup();
subrata_modaka97f1162009-01-16 10:24:56 +0000133
subrata_modak56207ce2009-03-23 13:35:39 +0000134 for (lc = 0; TEST_LOOPING(lc); ++lc) {
Caspar Zhangd59a6592013-03-07 14:59:12 +0800135 tst_count = 0;
subrata_modak56207ce2009-03-23 13:35:39 +0000136 for (testno = 0; testno < TST_TOTAL; ++testno) {
137 fd = socket(PF_INET, SOCK_STREAM, 0);
138 if (fd == -1) {
Cyril Hrubis9fa8ad02014-12-16 13:20:49 +0100139 tst_brkm(TFAIL, cleanup, "socket(0) failed");
subrata_modak56207ce2009-03-23 13:35:39 +0000140 }
141 fl = fcntl(fd, F_GETFL);
142 if (fl == -1) {
143 tst_brkm(TBROK, cleanup, "fcntl failed");
subrata_modak56207ce2009-03-23 13:35:39 +0000144 }
145 if (fl & O_NONBLOCK) {
Cyril Hrubis9fa8ad02014-12-16 13:20:49 +0100146 tst_brkm(TFAIL,
147 cleanup,
subrata_modak56207ce2009-03-23 13:35:39 +0000148 "socket(0) set non-blocking mode");
subrata_modak56207ce2009-03-23 13:35:39 +0000149 }
150 close(fd);
subrata_modaka97f1162009-01-16 10:24:56 +0000151
subrata_modak56207ce2009-03-23 13:35:39 +0000152 fd = socket(PF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
153 if (fd == -1) {
Cyril Hrubis9fa8ad02014-12-16 13:20:49 +0100154 tst_brkm(TFAIL, cleanup,
155 "socket(SOCK_NONBLOCK) failed");
subrata_modak56207ce2009-03-23 13:35:39 +0000156 }
157 fl = fcntl(fd, F_GETFL);
158 if (fl == -1) {
159 tst_brkm(TBROK, cleanup, "fcntl failed");
subrata_modak56207ce2009-03-23 13:35:39 +0000160 }
161 if ((fl & O_NONBLOCK) == 0) {
Cyril Hrubis9fa8ad02014-12-16 13:20:49 +0100162 tst_brkm(TFAIL,
163 cleanup,
subrata_modak56207ce2009-03-23 13:35:39 +0000164 "socket(SOCK_NONBLOCK) does not set non-blocking mode");
subrata_modak56207ce2009-03-23 13:35:39 +0000165 }
166 close(fd);
167 tst_resm(TPASS, "socket(SOCK_NONBLOCK) PASSED");
168 cleanup();
169 }
170 }
171 tst_exit();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700172}