plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright (c) International Business Machines Corp., 2001 |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 13 | * the GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | */ |
| 19 | |
| 20 | /* |
| 21 | * Test Name: sockioctl01 |
| 22 | * |
| 23 | * Test Description: |
| 24 | * Verify that ioctl() on sockets returns the proper errno for various |
| 25 | * failure cases |
| 26 | * |
| 27 | * Usage: <for command-line> |
| 28 | * sockioctl01 [-c n] [-e] [-i n] [-I x] [-p x] [-t] |
| 29 | * where, -c n : Run n copies concurrently. |
| 30 | * -e : Turn on errno logging. |
| 31 | * -i n : Execute test n times. |
| 32 | * -I x : Execute test for x seconds. |
| 33 | * -P x : Pause for x seconds between iterations. |
| 34 | * -t : Turn on syscall timing. |
| 35 | * |
| 36 | * History |
| 37 | * 07/2001 John George |
| 38 | * -Ported |
| 39 | * |
| 40 | * Restrictions: |
| 41 | * None. |
| 42 | * |
| 43 | */ |
| 44 | |
| 45 | #include <stdio.h> |
| 46 | #include <unistd.h> |
| 47 | #include <errno.h> |
plars | c6bc39d | 2002-08-08 21:41:22 +0000 | [diff] [blame] | 48 | #include <fcntl.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 49 | |
| 50 | #include <sys/types.h> |
| 51 | #include <sys/socket.h> |
| 52 | #include <sys/signal.h> |
| 53 | #include <sys/ioctl.h> |
subrata_modak | b29f7fb | 2008-04-26 14:34:29 +0000 | [diff] [blame] | 54 | #include <sys/stat.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 55 | |
| 56 | #include <netinet/in.h> |
| 57 | #include <net/if.h> |
| 58 | |
| 59 | #include "test.h" |
| 60 | #include "usctest.h" |
| 61 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 62 | char *TCID = "sockioctl01"; /* Test program identifier. */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 63 | int testno; |
| 64 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 65 | int s; /* socket descriptor */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 66 | struct sockaddr_in sin0, fsin1; |
| 67 | struct ifconf ifc; |
| 68 | struct ifreq ifr; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 69 | int sinlen; |
| 70 | int optval; |
| 71 | int exp_enos[] = { EBADF, EINVAL, EFAULT, 0 }; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 72 | |
| 73 | char buf[8192]; |
| 74 | |
| 75 | void setup(void), setup0(void), setup1(void), setup2(void), setup3(void), |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 76 | cleanup(void), cleanup0(void), cleanup1(void); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 77 | |
| 78 | struct test_case_t { /* test case structure */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 79 | int domain; /* PF_INET, PF_UNIX, ... */ |
| 80 | int type; /* SOCK_STREAM, SOCK_DGRAM ... */ |
| 81 | int proto; /* protocol number (usually 0 = default) */ |
| 82 | int cmd; /* IPPROTO_* */ |
| 83 | void *arg; |
| 84 | struct sockaddr *sin; |
| 85 | int salen; |
| 86 | int retval; /* syscall return value */ |
| 87 | int experrno; /* expected errno */ |
| 88 | void (*setup) (void); |
| 89 | void (*cleanup) (void); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 90 | char *desc; |
| 91 | } tdat[] = { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 92 | { |
| 93 | PF_INET, SOCK_STREAM, 0, SIOCATMARK, &optval, |
| 94 | (struct sockaddr *)&fsin1, sizeof(fsin1), -1, |
| 95 | EBADF, setup0, cleanup0, "bad file descriptor"} |
| 96 | , { |
| 97 | PF_INET, SOCK_STREAM, 0, SIOCATMARK, &optval, |
| 98 | (struct sockaddr *)&fsin1, sizeof(fsin1), -1, |
| 99 | EINVAL, setup0, cleanup0, "not a socket"} |
| 100 | , |
vapier | b5ed1f6 | 2006-08-24 04:16:32 +0000 | [diff] [blame] | 101 | #if !defined(UCLINUX) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 102 | { |
| 103 | PF_INET, SOCK_STREAM, 0, SIOCATMARK, 0, |
| 104 | (struct sockaddr *)&fsin1, sizeof(fsin1), -1, |
| 105 | EFAULT, setup1, cleanup1, "invalid option buffer"} |
| 106 | , |
vapier | b5ed1f6 | 2006-08-24 04:16:32 +0000 | [diff] [blame] | 107 | #endif |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 108 | { |
| 109 | PF_INET, SOCK_DGRAM, 0, SIOCATMARK, &optval, |
| 110 | (struct sockaddr *)&fsin1, sizeof(fsin1), -1, |
| 111 | EINVAL, setup1, cleanup1, "ATMARK on UDP"} |
| 112 | , { |
| 113 | PF_INET, SOCK_DGRAM, 0, SIOCGIFCONF, &ifc, |
| 114 | (struct sockaddr *)&fsin1, sizeof(fsin1), 0, |
| 115 | 0, setup2, cleanup1, "SIOCGIFCONF"} |
| 116 | , { |
| 117 | PF_INET, SOCK_DGRAM, 0, SIOCGIFFLAGS, &ifr, |
| 118 | (struct sockaddr *)&fsin1, sizeof(fsin1), 0, |
| 119 | 0, setup3, cleanup1, "SIOCGIFFLAGS"} |
| 120 | , { |
| 121 | PF_INET, SOCK_DGRAM, 0, SIOCGIFFLAGS, 0, |
| 122 | (struct sockaddr *)&fsin1, sizeof(fsin1), -1, |
| 123 | EFAULT, setup3, cleanup1, "SIOCGIFFLAGS with invalid ifr"} |
| 124 | , { |
| 125 | PF_INET, SOCK_DGRAM, 0, SIOCSIFFLAGS, 0, |
| 126 | (struct sockaddr *)&fsin1, sizeof(fsin1), -1, |
| 127 | EFAULT, setup3, cleanup1, "SIOCSIFFLAGS with invalid ifr"} |
| 128 | ,}; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 129 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 130 | int TST_TOTAL = sizeof(tdat) / sizeof(tdat[0]); /* Total number of test cases. */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 131 | |
| 132 | extern int Tst_count; |
| 133 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 134 | int main(int argc, char *argv[]) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 135 | { |
| 136 | int lc; /* loop counter */ |
| 137 | char *msg; /* message returned from parse_opts */ |
| 138 | |
| 139 | /* Parse standard options given to run the test. */ |
| 140 | msg = parse_opts(argc, argv, (option_t *) NULL, NULL); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 141 | if (msg != (char *)NULL) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 142 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
| 143 | tst_exit(); |
| 144 | } |
| 145 | |
| 146 | setup(); |
| 147 | /* Check looping state if -i option given */ |
| 148 | for (lc = 0; TEST_LOOPING(lc); ++lc) { |
| 149 | Tst_count = 0; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 150 | for (testno = 0; testno < TST_TOTAL; ++testno) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 151 | tdat[testno].setup(); |
| 152 | |
| 153 | TEST(ioctl(s, tdat[testno].cmd, tdat[testno].arg)); |
| 154 | TEST_ERROR_LOG(TEST_ERRNO); |
| 155 | if (TEST_RETURN != tdat[testno].retval || |
| 156 | (TEST_RETURN < 0 && |
| 157 | TEST_ERRNO != tdat[testno].experrno)) { |
| 158 | tst_resm(TFAIL, "%s ; returned" |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 159 | " %d (expected %d), errno %d (expected" |
| 160 | " %d)", tdat[testno].desc, |
| 161 | TEST_RETURN, tdat[testno].retval, |
| 162 | TEST_ERRNO, tdat[testno].experrno); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 163 | } else { |
| 164 | tst_resm(TPASS, "%s successful", |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 165 | tdat[testno].desc); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 166 | } |
| 167 | tdat[testno].cleanup(); |
| 168 | } |
| 169 | } |
| 170 | cleanup(); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 171 | /*NOTREACHED*/ return 0; |
robbiew | fa451a1 | 2003-03-27 20:52:36 +0000 | [diff] [blame] | 172 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 173 | } /* End main */ |
robbiew | fa451a1 | 2003-03-27 20:52:36 +0000 | [diff] [blame] | 174 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 175 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 176 | { |
| 177 | /* set the expected errnos... */ |
| 178 | TEST_EXP_ENOS(exp_enos); |
| 179 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 180 | TEST_PAUSE; /* if -P option specified */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 181 | |
| 182 | /* initialize local sockaddr */ |
| 183 | sin0.sin_family = AF_INET; |
| 184 | sin0.sin_port = 0; |
| 185 | sin0.sin_addr.s_addr = INADDR_ANY; |
| 186 | } |
| 187 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 188 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 189 | { |
| 190 | TEST_CLEANUP; |
| 191 | tst_exit(); |
| 192 | } |
| 193 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 194 | void setup0(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 195 | { |
| 196 | if (tdat[testno].experrno == EBADF) |
subrata_modak | b29f7fb | 2008-04-26 14:34:29 +0000 | [diff] [blame] | 197 | s = 1025; /* anything not an open file */ |
| 198 | else { |
| 199 | tst_tmpdir(); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 200 | if ((mknod("test", O_RDWR | O_CREAT | S_IFIFO, 0)) == -1) |
| 201 | tst_brkm(TBROK, cleanup, "Could not create test - " |
| 202 | "errno: %s", strerror(errno)); |
| 203 | if ((s = open("test", O_RDWR)) == -1) |
| 204 | tst_brkm(TBROK, cleanup, "Could not open test - " |
| 205 | "errno: %s", strerror(errno)); |
subrata_modak | b29f7fb | 2008-04-26 14:34:29 +0000 | [diff] [blame] | 206 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 207 | } |
| 208 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 209 | void cleanup0(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 210 | { |
subrata_modak | b29f7fb | 2008-04-26 14:34:29 +0000 | [diff] [blame] | 211 | /* delete the test directory created in setup0() */ |
| 212 | if (tdat[testno].experrno != EBADF) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 213 | (void)close(s); |
subrata_modak | b29f7fb | 2008-04-26 14:34:29 +0000 | [diff] [blame] | 214 | s = -1; |
| 215 | tst_rmdir(); |
| 216 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 217 | } |
| 218 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 219 | void setup1(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 220 | { |
| 221 | s = socket(tdat[testno].domain, tdat[testno].type, tdat[testno].proto); |
| 222 | if (s < 0) { |
| 223 | tst_brkm(TBROK, cleanup, "socket setup failed: %s", |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 224 | strerror(errno)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 225 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 226 | if (bind(s, (struct sockaddr *)&sin0, sizeof(sin0)) < 0) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 227 | tst_brkm(TBROK, cleanup, "socket bind failed for: %s", |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 228 | strerror(errno)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 229 | } |
| 230 | sinlen = sizeof(fsin1); |
| 231 | } |
| 232 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 233 | void setup2(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 234 | { |
| 235 | s = socket(tdat[testno].domain, tdat[testno].type, tdat[testno].proto); |
| 236 | if (s < 0) { |
| 237 | tst_brkm(TBROK, cleanup, "socket setup failed: %s", |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 238 | strerror(errno)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 239 | } |
| 240 | ifc.ifc_len = sizeof(buf); |
| 241 | ifc.ifc_buf = buf; |
| 242 | } |
| 243 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 244 | void setup3(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 245 | { |
| 246 | setup2(); |
| 247 | if (ioctl(s, SIOCGIFCONF, &ifc) < 0) { |
| 248 | tst_brkm(TBROK, cleanup, "socket setup failed: %s", |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 249 | strerror(errno)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 250 | } |
| 251 | ifr = *(struct ifreq *)ifc.ifc_buf; |
| 252 | } |
| 253 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 254 | void cleanup1(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 255 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 256 | (void)close(s); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 257 | s = -1; |
| 258 | } |