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 |
Wanlong Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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 |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 26 | */ |
| 27 | |
| 28 | #include <stdio.h> |
| 29 | #include <unistd.h> |
| 30 | #include <errno.h> |
plars | c6bc39d | 2002-08-08 21:41:22 +0000 | [diff] [blame] | 31 | #include <fcntl.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 32 | |
| 33 | #include <sys/types.h> |
| 34 | #include <sys/socket.h> |
| 35 | #include <sys/signal.h> |
| 36 | #include <sys/ioctl.h> |
subrata_modak | b29f7fb | 2008-04-26 14:34:29 +0000 | [diff] [blame] | 37 | #include <sys/stat.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 38 | |
| 39 | #include <netinet/in.h> |
| 40 | #include <net/if.h> |
| 41 | |
| 42 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 43 | |
Cyril Hrubis | 00b8b88 | 2013-02-19 18:08:28 +0100 | [diff] [blame] | 44 | char *TCID = "sockioctl01"; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 45 | int testno; |
| 46 | |
Cyril Hrubis | 00b8b88 | 2013-02-19 18:08:28 +0100 | [diff] [blame] | 47 | static int s; /* socket descriptor */ |
| 48 | static struct sockaddr_in sin0, fsin1; |
| 49 | static struct ifconf ifc; |
| 50 | static struct ifreq ifr; |
| 51 | static int sinlen; |
| 52 | static int optval; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 53 | |
Cyril Hrubis | 00b8b88 | 2013-02-19 18:08:28 +0100 | [diff] [blame] | 54 | static char buf[8192]; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 55 | |
Cyril Hrubis | 00b8b88 | 2013-02-19 18:08:28 +0100 | [diff] [blame] | 56 | static void setup(void); |
| 57 | static void setup0(void); |
| 58 | static void setup1(void); |
| 59 | static void setup2(void); |
| 60 | static void setup3(void); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 61 | |
Cyril Hrubis | 00b8b88 | 2013-02-19 18:08:28 +0100 | [diff] [blame] | 62 | static void cleanup(void); |
| 63 | static void cleanup0(void); |
| 64 | static void cleanup1(void); |
| 65 | |
| 66 | struct test_case_t { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 67 | int domain; /* PF_INET, PF_UNIX, ... */ |
| 68 | int type; /* SOCK_STREAM, SOCK_DGRAM ... */ |
| 69 | int proto; /* protocol number (usually 0 = default) */ |
| 70 | int cmd; /* IPPROTO_* */ |
| 71 | void *arg; |
| 72 | struct sockaddr *sin; |
| 73 | int salen; |
| 74 | int retval; /* syscall return value */ |
| 75 | int experrno; /* expected errno */ |
| 76 | void (*setup) (void); |
| 77 | void (*cleanup) (void); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 78 | char *desc; |
| 79 | } tdat[] = { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 80 | { |
| 81 | PF_INET, SOCK_STREAM, 0, SIOCATMARK, &optval, |
| 82 | (struct sockaddr *)&fsin1, sizeof(fsin1), -1, |
| 83 | EBADF, setup0, cleanup0, "bad file descriptor"} |
| 84 | , { |
| 85 | PF_INET, SOCK_STREAM, 0, SIOCATMARK, &optval, |
| 86 | (struct sockaddr *)&fsin1, sizeof(fsin1), -1, |
| 87 | EINVAL, setup0, cleanup0, "not a socket"} |
| 88 | , |
vapier | b5ed1f6 | 2006-08-24 04:16:32 +0000 | [diff] [blame] | 89 | #if !defined(UCLINUX) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 90 | { |
| 91 | PF_INET, SOCK_STREAM, 0, SIOCATMARK, 0, |
| 92 | (struct sockaddr *)&fsin1, sizeof(fsin1), -1, |
| 93 | EFAULT, setup1, cleanup1, "invalid option buffer"} |
| 94 | , |
vapier | b5ed1f6 | 2006-08-24 04:16:32 +0000 | [diff] [blame] | 95 | #endif |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 96 | { |
| 97 | PF_INET, SOCK_DGRAM, 0, SIOCATMARK, &optval, |
| 98 | (struct sockaddr *)&fsin1, sizeof(fsin1), -1, |
| 99 | EINVAL, setup1, cleanup1, "ATMARK on UDP"} |
| 100 | , { |
| 101 | PF_INET, SOCK_DGRAM, 0, SIOCGIFCONF, &ifc, |
| 102 | (struct sockaddr *)&fsin1, sizeof(fsin1), 0, |
| 103 | 0, setup2, cleanup1, "SIOCGIFCONF"} |
| 104 | , { |
| 105 | PF_INET, SOCK_DGRAM, 0, SIOCGIFFLAGS, &ifr, |
| 106 | (struct sockaddr *)&fsin1, sizeof(fsin1), 0, |
| 107 | 0, setup3, cleanup1, "SIOCGIFFLAGS"} |
| 108 | , { |
| 109 | PF_INET, SOCK_DGRAM, 0, SIOCGIFFLAGS, 0, |
| 110 | (struct sockaddr *)&fsin1, sizeof(fsin1), -1, |
| 111 | EFAULT, setup3, cleanup1, "SIOCGIFFLAGS with invalid ifr"} |
| 112 | , { |
| 113 | PF_INET, SOCK_DGRAM, 0, SIOCSIFFLAGS, 0, |
| 114 | (struct sockaddr *)&fsin1, sizeof(fsin1), -1, |
| 115 | EFAULT, setup3, cleanup1, "SIOCSIFFLAGS with invalid ifr"} |
| 116 | ,}; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 117 | |
Cyril Hrubis | 00b8b88 | 2013-02-19 18:08:28 +0100 | [diff] [blame] | 118 | int TST_TOTAL = sizeof(tdat) / sizeof(tdat[0]); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 119 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 120 | int main(int argc, char *argv[]) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 121 | { |
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; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 124 | |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 125 | msg = parse_opts(argc, argv, NULL, NULL); |
Cyril Hrubis | 00b8b88 | 2013-02-19 18:08:28 +0100 | [diff] [blame] | 126 | if (msg != NULL) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 127 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 128 | |
| 129 | setup(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 130 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 131 | for (lc = 0; TEST_LOOPING(lc); ++lc) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 132 | tst_count = 0; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 133 | for (testno = 0; testno < TST_TOTAL; ++testno) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 134 | tdat[testno].setup(); |
| 135 | |
| 136 | TEST(ioctl(s, tdat[testno].cmd, tdat[testno].arg)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 137 | if (TEST_RETURN != tdat[testno].retval || |
| 138 | (TEST_RETURN < 0 && |
| 139 | TEST_ERRNO != tdat[testno].experrno)) { |
| 140 | tst_resm(TFAIL, "%s ; returned" |
subrata_modak | 923b23f | 2009-11-02 13:57:16 +0000 | [diff] [blame] | 141 | " %ld (expected %d), errno %d (expected" |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 142 | " %d)", tdat[testno].desc, |
| 143 | TEST_RETURN, tdat[testno].retval, |
| 144 | TEST_ERRNO, tdat[testno].experrno); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 145 | } else { |
| 146 | tst_resm(TPASS, "%s successful", |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 147 | tdat[testno].desc); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 148 | } |
| 149 | tdat[testno].cleanup(); |
| 150 | } |
| 151 | } |
Cyril Hrubis | b596770 | 2013-02-19 18:25:45 +0100 | [diff] [blame] | 152 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 153 | cleanup(); |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 154 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 155 | } |
robbiew | fa451a1 | 2003-03-27 20:52:36 +0000 | [diff] [blame] | 156 | |
Cyril Hrubis | 00b8b88 | 2013-02-19 18:08:28 +0100 | [diff] [blame] | 157 | static void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 158 | { |
Cyril Hrubis | 00b8b88 | 2013-02-19 18:08:28 +0100 | [diff] [blame] | 159 | TEST_PAUSE; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 160 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 161 | sin0.sin_family = AF_INET; |
| 162 | sin0.sin_port = 0; |
| 163 | sin0.sin_addr.s_addr = INADDR_ANY; |
Cyril Hrubis | b596770 | 2013-02-19 18:25:45 +0100 | [diff] [blame] | 164 | |
| 165 | tst_tmpdir(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Cyril Hrubis | 00b8b88 | 2013-02-19 18:08:28 +0100 | [diff] [blame] | 168 | static void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 169 | { |
Cyril Hrubis | b596770 | 2013-02-19 18:25:45 +0100 | [diff] [blame] | 170 | tst_rmdir(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Cyril Hrubis | 00b8b88 | 2013-02-19 18:08:28 +0100 | [diff] [blame] | 173 | static void setup0(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 174 | { |
Cyril Hrubis | b596770 | 2013-02-19 18:25:45 +0100 | [diff] [blame] | 175 | if (tdat[testno].experrno == EBADF) { |
subrata_modak | b29f7fb | 2008-04-26 14:34:29 +0000 | [diff] [blame] | 176 | s = 1025; /* anything not an open file */ |
Cyril Hrubis | b596770 | 2013-02-19 18:25:45 +0100 | [diff] [blame] | 177 | } else { |
| 178 | unlink("test"); |
| 179 | |
Cyril Hrubis | ed77dcc | 2013-02-20 15:28:19 +0100 | [diff] [blame] | 180 | if ((mknod("test", S_IRWXU | S_IFIFO, 0)) == -1) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 181 | tst_brkm(TBROK, cleanup, "Could not create test - " |
| 182 | "errno: %s", strerror(errno)); |
Cyril Hrubis | ed77dcc | 2013-02-20 15:28:19 +0100 | [diff] [blame] | 183 | } |
Cyril Hrubis | b596770 | 2013-02-19 18:25:45 +0100 | [diff] [blame] | 184 | |
Cyril Hrubis | ed77dcc | 2013-02-20 15:28:19 +0100 | [diff] [blame] | 185 | if ((s = open("test", O_RDWR)) == -1) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 186 | tst_brkm(TBROK, cleanup, "Could not open test - " |
| 187 | "errno: %s", strerror(errno)); |
Cyril Hrubis | ed77dcc | 2013-02-20 15:28:19 +0100 | [diff] [blame] | 188 | } |
| 189 | |
Jan Stancek | 0cf2671 | 2012-08-03 13:48:37 +0200 | [diff] [blame] | 190 | /* |
| 191 | * kernel commit 46ce341b2f176c2611f12ac390adf862e932eb02 |
| 192 | * changed -EINVAL to -ENOIOCTLCMD, so vfs_ioctl now |
| 193 | * returns -ENOTTY. |
| 194 | */ |
| 195 | if ((tst_kvercmp(3, 5, 0)) >= 0) |
| 196 | tdat[testno].experrno = ENOTTY; |
subrata_modak | b29f7fb | 2008-04-26 14:34:29 +0000 | [diff] [blame] | 197 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Cyril Hrubis | 00b8b88 | 2013-02-19 18:08:28 +0100 | [diff] [blame] | 200 | static void cleanup0(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 201 | { |
subrata_modak | b29f7fb | 2008-04-26 14:34:29 +0000 | [diff] [blame] | 202 | if (tdat[testno].experrno != EBADF) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 203 | (void)close(s); |
subrata_modak | b29f7fb | 2008-04-26 14:34:29 +0000 | [diff] [blame] | 204 | s = -1; |
subrata_modak | b29f7fb | 2008-04-26 14:34:29 +0000 | [diff] [blame] | 205 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Cyril Hrubis | 00b8b88 | 2013-02-19 18:08:28 +0100 | [diff] [blame] | 208 | static void setup1(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 209 | { |
| 210 | s = socket(tdat[testno].domain, tdat[testno].type, tdat[testno].proto); |
| 211 | if (s < 0) { |
| 212 | tst_brkm(TBROK, cleanup, "socket setup failed: %s", |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 213 | strerror(errno)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 214 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 215 | if (bind(s, (struct sockaddr *)&sin0, sizeof(sin0)) < 0) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 216 | tst_brkm(TBROK, cleanup, "socket bind failed for: %s", |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 217 | strerror(errno)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 218 | } |
| 219 | sinlen = sizeof(fsin1); |
Nageswara R Sastry | 53053d8 | 2011-09-12 17:15:40 +0530 | [diff] [blame] | 220 | |
| 221 | if (strncmp(tdat[testno].desc, "ATMARK on UDP", 14) == 0) { |
| 222 | if ((tst_kvercmp(2, 6, 39)) >= 0) |
| 223 | tdat[testno].experrno = ENOTTY; |
| 224 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 225 | } |
| 226 | |
Cyril Hrubis | 00b8b88 | 2013-02-19 18:08:28 +0100 | [diff] [blame] | 227 | static void setup2(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 228 | { |
| 229 | s = socket(tdat[testno].domain, tdat[testno].type, tdat[testno].proto); |
| 230 | if (s < 0) { |
| 231 | tst_brkm(TBROK, cleanup, "socket setup failed: %s", |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 232 | strerror(errno)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 233 | } |
| 234 | ifc.ifc_len = sizeof(buf); |
| 235 | ifc.ifc_buf = buf; |
| 236 | } |
| 237 | |
Cyril Hrubis | 00b8b88 | 2013-02-19 18:08:28 +0100 | [diff] [blame] | 238 | static void setup3(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 239 | { |
| 240 | setup2(); |
| 241 | if (ioctl(s, SIOCGIFCONF, &ifc) < 0) { |
| 242 | tst_brkm(TBROK, cleanup, "socket setup failed: %s", |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 243 | strerror(errno)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 244 | } |
| 245 | ifr = *(struct ifreq *)ifc.ifc_buf; |
| 246 | } |
| 247 | |
Cyril Hrubis | 00b8b88 | 2013-02-19 18:08:28 +0100 | [diff] [blame] | 248 | static void cleanup1(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 249 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 250 | (void)close(s); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 251 | s = -1; |
Nageswara R Sastry | 53053d8 | 2011-09-12 17:15:40 +0530 | [diff] [blame] | 252 | } |