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 #include "safe_macros.h"
44
45 char *TCID = "sockioctl01";
46 int testno;
47
48 static int s; /* socket descriptor */
49 static struct sockaddr_in sin0, fsin1;
50 static struct ifconf ifc;
51 static struct ifreq ifr;
52 static int sinlen;
53 static int optval;
54
55 static struct ifreq buf[200];
56
57 static void setup(void);
58 static void setup0(void);
59 static void setup1(void);
60 static void setup2(void);
61 static void setup3(void);
62
63 static void cleanup(void);
64 static void cleanup0(void);
65 static void cleanup1(void);
66
67 struct test_case_t {
68 int domain; /* PF_INET, PF_UNIX, ... */
69 int type; /* SOCK_STREAM, SOCK_DGRAM ... */
70 int proto; /* protocol number (usually 0 = default) */
71 int cmd; /* IPPROTO_* */
72 void *arg;
73 struct sockaddr *sin;
74 int salen;
75 int retval; /* syscall return value */
76 int experrno; /* expected errno */
77 void (*setup) (void);
78 void (*cleanup) (void);
79 char *desc;
80 } tdat[] = {
81 {
82 PF_INET, SOCK_STREAM, 0, SIOCATMARK, &optval,
83 (struct sockaddr *)&fsin1, sizeof(fsin1), -1,
84 EBADF, setup0, cleanup0, "bad file descriptor"}
85 , {
86 PF_INET, SOCK_STREAM, 0, SIOCATMARK, &optval,
87 (struct sockaddr *)&fsin1, sizeof(fsin1), -1,
88 EINVAL, setup0, cleanup0, "not a socket"}
89 ,
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 {
96 PF_INET, SOCK_DGRAM, 0, SIOCATMARK, &optval,
97 (struct sockaddr *)&fsin1, sizeof(fsin1), -1,
98 EINVAL, setup1, cleanup1, "ATMARK on UDP"}
99 , {
100 PF_INET, SOCK_DGRAM, 0, SIOCGIFCONF, &ifc,
101 (struct sockaddr *)&fsin1, sizeof(fsin1), 0,
102 0, setup2, cleanup1, "SIOCGIFCONF"}
103 , {
104 PF_INET, SOCK_DGRAM, 0, SIOCGIFFLAGS, &ifr,
105 (struct sockaddr *)&fsin1, sizeof(fsin1), 0,
106 0, setup3, cleanup1, "SIOCGIFFLAGS"}
107 , {
108 PF_INET, SOCK_DGRAM, 0, SIOCGIFFLAGS, 0,
109 (struct sockaddr *)&fsin1, sizeof(fsin1), -1,
110 EFAULT, setup3, cleanup1, "SIOCGIFFLAGS with invalid ifr"}
111 , {
112 PF_INET, SOCK_DGRAM, 0, SIOCSIFFLAGS, 0,
113 (struct sockaddr *)&fsin1, sizeof(fsin1), -1,
114 EFAULT, setup3, cleanup1, "SIOCSIFFLAGS with invalid ifr"}
115 ,};
116
117 int TST_TOTAL = sizeof(tdat) / sizeof(tdat[0]);
118
main(int argc,char * argv[])119 int main(int argc, char *argv[])
120 {
121 int lc;
122
123 tst_parse_opts(argc, argv, NULL, NULL);
124
125 setup();
126
127 for (lc = 0; TEST_LOOPING(lc); ++lc) {
128 tst_count = 0;
129 for (testno = 0; testno < TST_TOTAL; ++testno) {
130 tdat[testno].setup();
131
132 TEST(ioctl(s, tdat[testno].cmd, tdat[testno].arg));
133 if (TEST_RETURN != tdat[testno].retval ||
134 (TEST_RETURN < 0 &&
135 TEST_ERRNO != tdat[testno].experrno)) {
136 tst_resm(TFAIL, "%s ; returned"
137 " %ld (expected %d), errno %d (expected"
138 " %d)", tdat[testno].desc,
139 TEST_RETURN, tdat[testno].retval,
140 TEST_ERRNO, tdat[testno].experrno);
141 } else {
142 tst_resm(TPASS, "%s successful",
143 tdat[testno].desc);
144 }
145 tdat[testno].cleanup();
146 }
147 }
148
149 cleanup();
150 tst_exit();
151 }
152
setup(void)153 static void setup(void)
154 {
155 TEST_PAUSE;
156
157 sin0.sin_family = AF_INET;
158 sin0.sin_port = 0;
159 sin0.sin_addr.s_addr = INADDR_ANY;
160
161 tst_tmpdir();
162 }
163
cleanup(void)164 static void cleanup(void)
165 {
166 tst_rmdir();
167 }
168
setup0(void)169 static void setup0(void)
170 {
171 if (tdat[testno].experrno == EBADF) {
172 s = 1025; /* anything not an open file */
173 } else {
174 unlink("test");
175
176 if ((mknod("test", S_IRWXU | S_IFIFO, 0)) == -1) {
177 tst_brkm(TBROK, cleanup, "Could not create test - "
178 "errno: %s", strerror(errno));
179 }
180
181 if ((s = open("test", O_RDWR)) == -1) {
182 tst_brkm(TBROK, cleanup, "Could not open test - "
183 "errno: %s", strerror(errno));
184 }
185
186 /*
187 * kernel commit 46ce341b2f176c2611f12ac390adf862e932eb02
188 * changed -EINVAL to -ENOIOCTLCMD, so vfs_ioctl now
189 * returns -ENOTTY.
190 */
191 tdat[testno].experrno = ENOTTY;
192 }
193 }
194
cleanup0(void)195 static void cleanup0(void)
196 {
197 if (tdat[testno].experrno != EBADF) {
198 (void)close(s);
199 s = -1;
200 }
201 }
202
setup1(void)203 static void setup1(void)
204 {
205 s = SAFE_SOCKET(cleanup, tdat[testno].domain, tdat[testno].type,
206 tdat[testno].proto);
207 SAFE_BIND(cleanup, s, (struct sockaddr *)&sin0, sizeof(sin0));
208 sinlen = sizeof(fsin1);
209
210 if (strncmp(tdat[testno].desc, "ATMARK on UDP", 14) == 0)
211 tdat[testno].experrno = ENOTTY;
212 }
213
setup2(void)214 static void setup2(void)
215 {
216 s = SAFE_SOCKET(cleanup, tdat[testno].domain, tdat[testno].type,
217 tdat[testno].proto);
218 ifc.ifc_len = sizeof(buf);
219 ifc.ifc_buf = (char *)buf;
220 }
221
setup3(void)222 static void setup3(void)
223 {
224 setup2();
225 SAFE_IOCTL(cleanup, s, SIOCGIFCONF, &ifc);
226 ifr = *(struct ifreq *)ifc.ifc_buf;
227 }
228
cleanup1(void)229 static void cleanup1(void)
230 {
231 (void)close(s);
232 s = -1;
233 }
234