subrata_modak | a97f116 | 2009-01-16 10:24:56 +0000 | [diff] [blame] | 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 */ |
Wanlong Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 18 | /* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ |
subrata_modak | a97f116 | 2009-01-16 10:24:56 +0000 | [diff] [blame] | 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 | |
subrata_modak | a97f116 | 2009-01-16 10:24:56 +0000 | [diff] [blame] | 56 | #include "test.h" |
| 57 | #include "usctest.h" |
| 58 | |
subrata_modak | 8d4fee7 | 2009-01-27 14:48:38 +0000 | [diff] [blame] | 59 | #ifndef SOCK_NONBLOCK |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 60 | #define SOCK_NONBLOCK O_NONBLOCK |
subrata_modak | 8d4fee7 | 2009-01-27 14:48:38 +0000 | [diff] [blame] | 61 | #endif |
subrata_modak | a97f116 | 2009-01-16 10:24:56 +0000 | [diff] [blame] | 62 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 63 | char *TCID = "socket03"; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 64 | int testno; |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 65 | int TST_TOTAL = 1; |
subrata_modak | a97f116 | 2009-01-16 10:24:56 +0000 | [diff] [blame] | 66 | |
| 67 | /* Extern Global Functions */ |
| 68 | /******************************************************************************/ |
| 69 | /* */ |
| 70 | /* Function: cleanup */ |
| 71 | /* */ |
| 72 | /* Description: Performs all one time clean up for this test on successful */ |
| 73 | /* completion, premature exit or failure. Closes all temporary */ |
| 74 | /* files, removes all temporary directories exits the test with */ |
| 75 | /* appropriate return code by calling tst_exit() function. */ |
| 76 | /* */ |
| 77 | /* Input: None. */ |
| 78 | /* */ |
| 79 | /* Output: None. */ |
| 80 | /* */ |
| 81 | /* Return: On failure - Exits calling tst_exit(). Non '0' return code. */ |
| 82 | /* On success - Exits calling tst_exit(). With '0' return code. */ |
| 83 | /* */ |
| 84 | /******************************************************************************/ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 85 | void cleanup(void) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 86 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 87 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 88 | TEST_CLEANUP; |
| 89 | tst_rmdir(); |
subrata_modak | a97f116 | 2009-01-16 10:24:56 +0000 | [diff] [blame] | 90 | |
subrata_modak | a97f116 | 2009-01-16 10:24:56 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | /* Local Functions */ |
| 94 | /******************************************************************************/ |
| 95 | /* */ |
| 96 | /* Function: setup */ |
| 97 | /* */ |
| 98 | /* Description: Performs all one time setup for this test. This function is */ |
| 99 | /* typically used to capture signals, create temporary dirs */ |
| 100 | /* and temporary files that may be used in the course of this */ |
| 101 | /* test. */ |
| 102 | /* */ |
| 103 | /* Input: None. */ |
| 104 | /* */ |
| 105 | /* Output: None. */ |
| 106 | /* */ |
| 107 | /* Return: On failure - Exits by calling cleanup(). */ |
| 108 | /* On success - returns 0. */ |
| 109 | /* */ |
| 110 | /******************************************************************************/ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 111 | void setup(void) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 112 | { |
| 113 | /* Capture signals if any */ |
| 114 | /* Create temporary directories */ |
| 115 | TEST_PAUSE; |
| 116 | tst_tmpdir(); |
subrata_modak | a97f116 | 2009-01-16 10:24:56 +0000 | [diff] [blame] | 117 | } |
| 118 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 119 | int main(int argc, char *argv[]) |
| 120 | { |
| 121 | int fd, fl; |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 122 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 123 | const char *msg; |
subrata_modak | a97f116 | 2009-01-16 10:24:56 +0000 | [diff] [blame] | 124 | |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 125 | msg = parse_opts(argc, argv, NULL, NULL); |
| 126 | if (msg != NULL) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 127 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 128 | } |
| 129 | if ((tst_kvercmp(2, 6, 27)) < 0) { |
Cyril Hrubis | 526fdf8 | 2014-12-04 14:35:01 +0100 | [diff] [blame] | 130 | tst_brkm(TCONF, |
| 131 | NULL, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 132 | "This test can only run on kernels that are 2.6.27 and higher"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 133 | } |
| 134 | setup(); |
subrata_modak | a97f116 | 2009-01-16 10:24:56 +0000 | [diff] [blame] | 135 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 136 | for (lc = 0; TEST_LOOPING(lc); ++lc) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 137 | tst_count = 0; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 138 | for (testno = 0; testno < TST_TOTAL; ++testno) { |
| 139 | fd = socket(PF_INET, SOCK_STREAM, 0); |
| 140 | if (fd == -1) { |
Cyril Hrubis | 9fa8ad0 | 2014-12-16 13:20:49 +0100 | [diff] [blame^] | 141 | tst_brkm(TFAIL, cleanup, "socket(0) failed"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 142 | } |
| 143 | fl = fcntl(fd, F_GETFL); |
| 144 | if (fl == -1) { |
| 145 | tst_brkm(TBROK, cleanup, "fcntl failed"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 146 | } |
| 147 | if (fl & O_NONBLOCK) { |
Cyril Hrubis | 9fa8ad0 | 2014-12-16 13:20:49 +0100 | [diff] [blame^] | 148 | tst_brkm(TFAIL, |
| 149 | cleanup, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 150 | "socket(0) set non-blocking mode"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 151 | } |
| 152 | close(fd); |
subrata_modak | a97f116 | 2009-01-16 10:24:56 +0000 | [diff] [blame] | 153 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 154 | fd = socket(PF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0); |
| 155 | if (fd == -1) { |
Cyril Hrubis | 9fa8ad0 | 2014-12-16 13:20:49 +0100 | [diff] [blame^] | 156 | tst_brkm(TFAIL, cleanup, |
| 157 | "socket(SOCK_NONBLOCK) failed"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 158 | } |
| 159 | fl = fcntl(fd, F_GETFL); |
| 160 | if (fl == -1) { |
| 161 | tst_brkm(TBROK, cleanup, "fcntl failed"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 162 | } |
| 163 | if ((fl & O_NONBLOCK) == 0) { |
Cyril Hrubis | 9fa8ad0 | 2014-12-16 13:20:49 +0100 | [diff] [blame^] | 164 | tst_brkm(TFAIL, |
| 165 | cleanup, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 166 | "socket(SOCK_NONBLOCK) does not set non-blocking mode"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 167 | } |
| 168 | close(fd); |
| 169 | tst_resm(TPASS, "socket(SOCK_NONBLOCK) PASSED"); |
| 170 | cleanup(); |
| 171 | } |
| 172 | } |
| 173 | tst_exit(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 174 | } |