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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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
28 #include <stdio.h>
29 #include <unistd.h>
30 #include <errno.h>
31 #include <fcntl.h>
32
33 #include <sys/types.h>
34 #include <sys/socket.h>
35 #include <sys/signal.h>
36 #include <sys/ioctl.h>
37 #include <sys/stat.h>
38
39 #include <netinet/in.h>
40 #include <net/if.h>
41
42 #include "test.h"
43
44 char *TCID = "sockioctl01";
45 int testno;
46
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;
53
54 static char buf[8192];
55
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);
61
62 static void cleanup(void);
63 static void cleanup0(void);
64 static void cleanup1(void);
65
66 struct test_case_t {
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);
78 char *desc;
79 } tdat[] = {
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 ,
89 #if !defined(UCLINUX)
90 {
91 PF_INET, SOCK_STREAM, 0, SIOCATMARK, 0,
92 (struct sockaddr *)&fsin1, sizeof(fsin1), -1,
93 EFAULT, setup1, cleanup1, "invalid option buffer"}
94 ,
95 #endif
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 ,};
117
118 int TST_TOTAL = sizeof(tdat) / sizeof(tdat[0]);
119
main(int argc,char * argv[])120 int main(int argc, char *argv[])
121 {
122 int lc;
123
124 tst_parse_opts(argc, argv, NULL, NULL);
125
126 setup();
127
128 for (lc = 0; TEST_LOOPING(lc); ++lc) {
129 tst_count = 0;
130 for (testno = 0; testno < TST_TOTAL; ++testno) {
131 tdat[testno].setup();
132
133 TEST(ioctl(s, tdat[testno].cmd, tdat[testno].arg));
134 if (TEST_RETURN != tdat[testno].retval ||
135 (TEST_RETURN < 0 &&
136 TEST_ERRNO != tdat[testno].experrno)) {
137 tst_resm(TFAIL, "%s ; returned"
138 " %ld (expected %d), errno %d (expected"
139 " %d)", tdat[testno].desc,
140 TEST_RETURN, tdat[testno].retval,
141 TEST_ERRNO, tdat[testno].experrno);
142 } else {
143 tst_resm(TPASS, "%s successful",
144 tdat[testno].desc);
145 }
146 tdat[testno].cleanup();
147 }
148 }
149
150 cleanup();
151 tst_exit();
152 }
153
setup(void)154 static void setup(void)
155 {
156 TEST_PAUSE;
157
158 sin0.sin_family = AF_INET;
159 sin0.sin_port = 0;
160 sin0.sin_addr.s_addr = INADDR_ANY;
161
162 tst_tmpdir();
163 }
164
cleanup(void)165 static void cleanup(void)
166 {
167 tst_rmdir();
168 }
169
setup0(void)170 static void setup0(void)
171 {
172 if (tdat[testno].experrno == EBADF) {
173 s = 1025; /* anything not an open file */
174 } else {
175 unlink("test");
176
177 if ((mknod("test", S_IRWXU | S_IFIFO, 0)) == -1) {
178 tst_brkm(TBROK, cleanup, "Could not create test - "
179 "errno: %s", strerror(errno));
180 }
181
182 if ((s = open("test", O_RDWR)) == -1) {
183 tst_brkm(TBROK, cleanup, "Could not open test - "
184 "errno: %s", strerror(errno));
185 }
186
187 /*
188 * kernel commit 46ce341b2f176c2611f12ac390adf862e932eb02
189 * changed -EINVAL to -ENOIOCTLCMD, so vfs_ioctl now
190 * returns -ENOTTY.
191 */
192 if ((tst_kvercmp(3, 5, 0)) >= 0)
193 tdat[testno].experrno = ENOTTY;
194 }
195 }
196
cleanup0(void)197 static void cleanup0(void)
198 {
199 if (tdat[testno].experrno != EBADF) {
200 (void)close(s);
201 s = -1;
202 }
203 }
204
setup1(void)205 static void setup1(void)
206 {
207 s = socket(tdat[testno].domain, tdat[testno].type, tdat[testno].proto);
208 if (s < 0) {
209 tst_brkm(TBROK, cleanup, "socket setup failed: %s",
210 strerror(errno));
211 }
212 if (bind(s, (struct sockaddr *)&sin0, sizeof(sin0)) < 0) {
213 tst_brkm(TBROK, cleanup, "socket bind failed for: %s",
214 strerror(errno));
215 }
216 sinlen = sizeof(fsin1);
217
218 if (strncmp(tdat[testno].desc, "ATMARK on UDP", 14) == 0) {
219 if ((tst_kvercmp(2, 6, 39)) >= 0)
220 tdat[testno].experrno = ENOTTY;
221 }
222 }
223
setup2(void)224 static void setup2(void)
225 {
226 s = socket(tdat[testno].domain, tdat[testno].type, tdat[testno].proto);
227 if (s < 0) {
228 tst_brkm(TBROK, cleanup, "socket setup failed: %s",
229 strerror(errno));
230 }
231 ifc.ifc_len = sizeof(buf);
232 ifc.ifc_buf = buf;
233 }
234
setup3(void)235 static void setup3(void)
236 {
237 setup2();
238 if (ioctl(s, SIOCGIFCONF, &ifc) < 0) {
239 tst_brkm(TBROK, cleanup, "socket setup failed: %s",
240 strerror(errno));
241 }
242 ifr = *(struct ifreq *)ifc.ifc_buf;
243 }
244
cleanup1(void)245 static void cleanup1(void)
246 {
247 (void)close(s);
248 s = -1;
249 }
250