1 /******************************************************************************/
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */
19 /* */
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
56 #include "test.h"
57
58 #ifndef SOCK_NONBLOCK
59 #define SOCK_NONBLOCK O_NONBLOCK
60 #endif
61
62 char *TCID = "socket03";
63 int testno;
64 int TST_TOTAL = 1;
65
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 /******************************************************************************/
cleanup(void)84 void cleanup(void)
85 {
86
87 tst_rmdir();
88
89 }
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 /******************************************************************************/
setup(void)109 void setup(void)
110 {
111 /* Capture signals if any */
112 /* Create temporary directories */
113 TEST_PAUSE;
114 tst_tmpdir();
115 }
116
main(int argc,char * argv[])117 int main(int argc, char *argv[])
118 {
119 int fd, fl;
120 int lc;
121
122 tst_parse_opts(argc, argv, NULL, NULL);
123 if ((tst_kvercmp(2, 6, 27)) < 0) {
124 tst_brkm(TCONF,
125 NULL,
126 "This test can only run on kernels that are 2.6.27 and higher");
127 }
128 setup();
129
130 for (lc = 0; TEST_LOOPING(lc); ++lc) {
131 tst_count = 0;
132 for (testno = 0; testno < TST_TOTAL; ++testno) {
133 fd = socket(PF_INET, SOCK_STREAM, 0);
134 if (fd == -1) {
135 tst_brkm(TFAIL, cleanup, "socket(0) failed");
136 }
137 fl = fcntl(fd, F_GETFL);
138 if (fl == -1) {
139 tst_brkm(TBROK, cleanup, "fcntl failed");
140 }
141 if (fl & O_NONBLOCK) {
142 tst_brkm(TFAIL,
143 cleanup,
144 "socket(0) set non-blocking mode");
145 }
146 close(fd);
147
148 fd = socket(PF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
149 if (fd == -1) {
150 tst_brkm(TFAIL, cleanup,
151 "socket(SOCK_NONBLOCK) failed");
152 }
153 fl = fcntl(fd, F_GETFL);
154 if (fl == -1) {
155 tst_brkm(TBROK, cleanup, "fcntl failed");
156 }
157 if ((fl & O_NONBLOCK) == 0) {
158 tst_brkm(TFAIL,
159 cleanup,
160 "socket(SOCK_NONBLOCK) does not set non-blocking mode");
161 }
162 close(fd);
163 tst_resm(TPASS, "socket(SOCK_NONBLOCK) PASSED");
164 cleanup();
165 }
166 }
167 tst_exit();
168 }
169