blob: 7fb384d56b0adc2c63791e77c41e80d8f9431294 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
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 Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000018 */
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
plars865695b2001-08-27 22:15:12 +000026 */
27
28#include <stdio.h>
29#include <unistd.h>
30#include <errno.h>
plarsc6bc39d2002-08-08 21:41:22 +000031#include <fcntl.h>
plars865695b2001-08-27 22:15:12 +000032
33#include <sys/types.h>
34#include <sys/socket.h>
35#include <sys/signal.h>
36#include <sys/ioctl.h>
subrata_modakb29f7fb2008-04-26 14:34:29 +000037#include <sys/stat.h>
plars865695b2001-08-27 22:15:12 +000038
39#include <netinet/in.h>
40#include <net/if.h>
41
42#include "test.h"
plars865695b2001-08-27 22:15:12 +000043
Cyril Hrubis00b8b882013-02-19 18:08:28 +010044char *TCID = "sockioctl01";
plars865695b2001-08-27 22:15:12 +000045int testno;
46
Cyril Hrubis00b8b882013-02-19 18:08:28 +010047static int s; /* socket descriptor */
48static struct sockaddr_in sin0, fsin1;
49static struct ifconf ifc;
50static struct ifreq ifr;
51static int sinlen;
52static int optval;
plars865695b2001-08-27 22:15:12 +000053
Cyril Hrubis00b8b882013-02-19 18:08:28 +010054static char buf[8192];
plars865695b2001-08-27 22:15:12 +000055
Cyril Hrubis00b8b882013-02-19 18:08:28 +010056static void setup(void);
57static void setup0(void);
58static void setup1(void);
59static void setup2(void);
60static void setup3(void);
plars865695b2001-08-27 22:15:12 +000061
Cyril Hrubis00b8b882013-02-19 18:08:28 +010062static void cleanup(void);
63static void cleanup0(void);
64static void cleanup1(void);
65
66struct test_case_t {
subrata_modak56207ce2009-03-23 13:35:39 +000067 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);
plars865695b2001-08-27 22:15:12 +000078 char *desc;
79} tdat[] = {
subrata_modak56207ce2009-03-23 13:35:39 +000080 {
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 ,
vapierb5ed1f62006-08-24 04:16:32 +000089#if !defined(UCLINUX)
subrata_modak56207ce2009-03-23 13:35:39 +000090 {
91 PF_INET, SOCK_STREAM, 0, SIOCATMARK, 0,
92 (struct sockaddr *)&fsin1, sizeof(fsin1), -1,
93 EFAULT, setup1, cleanup1, "invalid option buffer"}
94 ,
vapierb5ed1f62006-08-24 04:16:32 +000095#endif
subrata_modak56207ce2009-03-23 13:35:39 +000096 {
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,};
plars865695b2001-08-27 22:15:12 +0000117
Cyril Hrubis00b8b882013-02-19 18:08:28 +0100118int TST_TOTAL = sizeof(tdat) / sizeof(tdat[0]);
plars865695b2001-08-27 22:15:12 +0000119
subrata_modak56207ce2009-03-23 13:35:39 +0000120int main(int argc, char *argv[])
plars865695b2001-08-27 22:15:12 +0000121{
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200122 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200123 const char *msg;
plars865695b2001-08-27 22:15:12 +0000124
Garrett Cooper45e285d2010-11-22 12:19:25 -0800125 msg = parse_opts(argc, argv, NULL, NULL);
Cyril Hrubis00b8b882013-02-19 18:08:28 +0100126 if (msg != NULL)
plars865695b2001-08-27 22:15:12 +0000127 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +0000128
129 setup();
Garrett Cooper2c282152010-12-16 00:55:50 -0800130
plars865695b2001-08-27 22:15:12 +0000131 for (lc = 0; TEST_LOOPING(lc); ++lc) {
Caspar Zhangd59a6592013-03-07 14:59:12 +0800132 tst_count = 0;
subrata_modak56207ce2009-03-23 13:35:39 +0000133 for (testno = 0; testno < TST_TOTAL; ++testno) {
plars865695b2001-08-27 22:15:12 +0000134 tdat[testno].setup();
135
136 TEST(ioctl(s, tdat[testno].cmd, tdat[testno].arg));
plars865695b2001-08-27 22:15:12 +0000137 if (TEST_RETURN != tdat[testno].retval ||
138 (TEST_RETURN < 0 &&
139 TEST_ERRNO != tdat[testno].experrno)) {
140 tst_resm(TFAIL, "%s ; returned"
subrata_modak923b23f2009-11-02 13:57:16 +0000141 " %ld (expected %d), errno %d (expected"
subrata_modak56207ce2009-03-23 13:35:39 +0000142 " %d)", tdat[testno].desc,
143 TEST_RETURN, tdat[testno].retval,
144 TEST_ERRNO, tdat[testno].experrno);
plars865695b2001-08-27 22:15:12 +0000145 } else {
146 tst_resm(TPASS, "%s successful",
subrata_modak56207ce2009-03-23 13:35:39 +0000147 tdat[testno].desc);
plars865695b2001-08-27 22:15:12 +0000148 }
149 tdat[testno].cleanup();
150 }
151 }
Cyril Hrubisb5967702013-02-19 18:25:45 +0100152
plars865695b2001-08-27 22:15:12 +0000153 cleanup();
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800154 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800155}
robbiewfa451a12003-03-27 20:52:36 +0000156
Cyril Hrubis00b8b882013-02-19 18:08:28 +0100157static void setup(void)
plars865695b2001-08-27 22:15:12 +0000158{
Cyril Hrubis00b8b882013-02-19 18:08:28 +0100159 TEST_PAUSE;
plars865695b2001-08-27 22:15:12 +0000160
plars865695b2001-08-27 22:15:12 +0000161 sin0.sin_family = AF_INET;
162 sin0.sin_port = 0;
163 sin0.sin_addr.s_addr = INADDR_ANY;
Cyril Hrubisb5967702013-02-19 18:25:45 +0100164
165 tst_tmpdir();
plars865695b2001-08-27 22:15:12 +0000166}
167
Cyril Hrubis00b8b882013-02-19 18:08:28 +0100168static void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000169{
Cyril Hrubisb5967702013-02-19 18:25:45 +0100170 tst_rmdir();
plars865695b2001-08-27 22:15:12 +0000171}
172
Cyril Hrubis00b8b882013-02-19 18:08:28 +0100173static void setup0(void)
plars865695b2001-08-27 22:15:12 +0000174{
Cyril Hrubisb5967702013-02-19 18:25:45 +0100175 if (tdat[testno].experrno == EBADF) {
subrata_modakb29f7fb2008-04-26 14:34:29 +0000176 s = 1025; /* anything not an open file */
Cyril Hrubisb5967702013-02-19 18:25:45 +0100177 } else {
178 unlink("test");
179
Cyril Hrubised77dcc2013-02-20 15:28:19 +0100180 if ((mknod("test", S_IRWXU | S_IFIFO, 0)) == -1) {
subrata_modak56207ce2009-03-23 13:35:39 +0000181 tst_brkm(TBROK, cleanup, "Could not create test - "
182 "errno: %s", strerror(errno));
Cyril Hrubised77dcc2013-02-20 15:28:19 +0100183 }
Cyril Hrubisb5967702013-02-19 18:25:45 +0100184
Cyril Hrubised77dcc2013-02-20 15:28:19 +0100185 if ((s = open("test", O_RDWR)) == -1) {
subrata_modak56207ce2009-03-23 13:35:39 +0000186 tst_brkm(TBROK, cleanup, "Could not open test - "
187 "errno: %s", strerror(errno));
Cyril Hrubised77dcc2013-02-20 15:28:19 +0100188 }
189
Jan Stancek0cf26712012-08-03 13:48:37 +0200190 /*
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_modakb29f7fb2008-04-26 14:34:29 +0000197 }
plars865695b2001-08-27 22:15:12 +0000198}
199
Cyril Hrubis00b8b882013-02-19 18:08:28 +0100200static void cleanup0(void)
plars865695b2001-08-27 22:15:12 +0000201{
subrata_modakb29f7fb2008-04-26 14:34:29 +0000202 if (tdat[testno].experrno != EBADF) {
subrata_modak56207ce2009-03-23 13:35:39 +0000203 (void)close(s);
subrata_modakb29f7fb2008-04-26 14:34:29 +0000204 s = -1;
subrata_modakb29f7fb2008-04-26 14:34:29 +0000205 }
plars865695b2001-08-27 22:15:12 +0000206}
207
Cyril Hrubis00b8b882013-02-19 18:08:28 +0100208static void setup1(void)
plars865695b2001-08-27 22:15:12 +0000209{
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_modak56207ce2009-03-23 13:35:39 +0000213 strerror(errno));
plars865695b2001-08-27 22:15:12 +0000214 }
subrata_modak56207ce2009-03-23 13:35:39 +0000215 if (bind(s, (struct sockaddr *)&sin0, sizeof(sin0)) < 0) {
plars865695b2001-08-27 22:15:12 +0000216 tst_brkm(TBROK, cleanup, "socket bind failed for: %s",
subrata_modak56207ce2009-03-23 13:35:39 +0000217 strerror(errno));
plars865695b2001-08-27 22:15:12 +0000218 }
219 sinlen = sizeof(fsin1);
Nageswara R Sastry53053d82011-09-12 17:15:40 +0530220
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 }
plars865695b2001-08-27 22:15:12 +0000225}
226
Cyril Hrubis00b8b882013-02-19 18:08:28 +0100227static void setup2(void)
plars865695b2001-08-27 22:15:12 +0000228{
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_modak56207ce2009-03-23 13:35:39 +0000232 strerror(errno));
plars865695b2001-08-27 22:15:12 +0000233 }
234 ifc.ifc_len = sizeof(buf);
235 ifc.ifc_buf = buf;
236}
237
Cyril Hrubis00b8b882013-02-19 18:08:28 +0100238static void setup3(void)
plars865695b2001-08-27 22:15:12 +0000239{
240 setup2();
241 if (ioctl(s, SIOCGIFCONF, &ifc) < 0) {
242 tst_brkm(TBROK, cleanup, "socket setup failed: %s",
subrata_modak56207ce2009-03-23 13:35:39 +0000243 strerror(errno));
plars865695b2001-08-27 22:15:12 +0000244 }
245 ifr = *(struct ifreq *)ifc.ifc_buf;
246}
247
Cyril Hrubis00b8b882013-02-19 18:08:28 +0100248static void cleanup1(void)
plars865695b2001-08-27 22:15:12 +0000249{
subrata_modak56207ce2009-03-23 13:35:39 +0000250 (void)close(s);
plars865695b2001-08-27 22:15:12 +0000251 s = -1;
Nageswara R Sastry53053d82011-09-12 17:15:40 +0530252}