blob: aa66bc326fe5fde7d93ffe8ec91d4268b2848d7b [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
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>
plarsc6bc39d2002-08-08 21:41:22 +000048#include <fcntl.h>
plars865695b2001-08-27 22:15:12 +000049
50#include <sys/types.h>
51#include <sys/socket.h>
52#include <sys/signal.h>
53#include <sys/ioctl.h>
subrata_modakb29f7fb2008-04-26 14:34:29 +000054#include <sys/stat.h>
plars865695b2001-08-27 22:15:12 +000055
56#include <netinet/in.h>
57#include <net/if.h>
58
59#include "test.h"
60#include "usctest.h"
61
subrata_modak56207ce2009-03-23 13:35:39 +000062char *TCID = "sockioctl01"; /* Test program identifier. */
plars865695b2001-08-27 22:15:12 +000063int testno;
64
subrata_modak56207ce2009-03-23 13:35:39 +000065int s; /* socket descriptor */
plars865695b2001-08-27 22:15:12 +000066struct sockaddr_in sin0, fsin1;
67struct ifconf ifc;
68struct ifreq ifr;
subrata_modak56207ce2009-03-23 13:35:39 +000069int sinlen;
70int optval;
71int exp_enos[] = { EBADF, EINVAL, EFAULT, 0 };
plars865695b2001-08-27 22:15:12 +000072
73char buf[8192];
74
75void setup(void), setup0(void), setup1(void), setup2(void), setup3(void),
subrata_modak56207ce2009-03-23 13:35:39 +000076cleanup(void), cleanup0(void), cleanup1(void);
plars865695b2001-08-27 22:15:12 +000077
78struct test_case_t { /* test case structure */
subrata_modak56207ce2009-03-23 13:35:39 +000079 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);
plars865695b2001-08-27 22:15:12 +000090 char *desc;
91} tdat[] = {
subrata_modak56207ce2009-03-23 13:35:39 +000092 {
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 ,
vapierb5ed1f62006-08-24 04:16:32 +0000101#if !defined(UCLINUX)
subrata_modak56207ce2009-03-23 13:35:39 +0000102 {
103 PF_INET, SOCK_STREAM, 0, SIOCATMARK, 0,
104 (struct sockaddr *)&fsin1, sizeof(fsin1), -1,
105 EFAULT, setup1, cleanup1, "invalid option buffer"}
106 ,
vapierb5ed1f62006-08-24 04:16:32 +0000107#endif
subrata_modak56207ce2009-03-23 13:35:39 +0000108 {
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,};
plars865695b2001-08-27 22:15:12 +0000129
subrata_modak56207ce2009-03-23 13:35:39 +0000130int TST_TOTAL = sizeof(tdat) / sizeof(tdat[0]); /* Total number of test cases. */
plars865695b2001-08-27 22:15:12 +0000131
132extern int Tst_count;
133
subrata_modak56207ce2009-03-23 13:35:39 +0000134int main(int argc, char *argv[])
plars865695b2001-08-27 22:15:12 +0000135{
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_modak56207ce2009-03-23 13:35:39 +0000141 if (msg != (char *)NULL) {
plars865695b2001-08-27 22:15:12 +0000142 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_modak56207ce2009-03-23 13:35:39 +0000150 for (testno = 0; testno < TST_TOTAL; ++testno) {
plars865695b2001-08-27 22:15:12 +0000151 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_modak923b23f2009-11-02 13:57:16 +0000159 " %ld (expected %d), errno %d (expected"
subrata_modak56207ce2009-03-23 13:35:39 +0000160 " %d)", tdat[testno].desc,
161 TEST_RETURN, tdat[testno].retval,
162 TEST_ERRNO, tdat[testno].experrno);
plars865695b2001-08-27 22:15:12 +0000163 } else {
164 tst_resm(TPASS, "%s successful",
subrata_modak56207ce2009-03-23 13:35:39 +0000165 tdat[testno].desc);
plars865695b2001-08-27 22:15:12 +0000166 }
167 tdat[testno].cleanup();
168 }
169 }
170 cleanup();
subrata_modak56207ce2009-03-23 13:35:39 +0000171 /*NOTREACHED*/ return 0;
robbiewfa451a12003-03-27 20:52:36 +0000172
subrata_modak56207ce2009-03-23 13:35:39 +0000173} /* End main */
robbiewfa451a12003-03-27 20:52:36 +0000174
subrata_modak56207ce2009-03-23 13:35:39 +0000175void setup(void)
plars865695b2001-08-27 22:15:12 +0000176{
177 /* set the expected errnos... */
178 TEST_EXP_ENOS(exp_enos);
179
subrata_modak56207ce2009-03-23 13:35:39 +0000180 TEST_PAUSE; /* if -P option specified */
plars865695b2001-08-27 22:15:12 +0000181
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_modak56207ce2009-03-23 13:35:39 +0000188void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000189{
190 TEST_CLEANUP;
191 tst_exit();
192}
193
subrata_modak56207ce2009-03-23 13:35:39 +0000194void setup0(void)
plars865695b2001-08-27 22:15:12 +0000195{
196 if (tdat[testno].experrno == EBADF)
subrata_modakb29f7fb2008-04-26 14:34:29 +0000197 s = 1025; /* anything not an open file */
198 else {
199 tst_tmpdir();
subrata_modak56207ce2009-03-23 13:35:39 +0000200 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_modakb29f7fb2008-04-26 14:34:29 +0000206 }
plars865695b2001-08-27 22:15:12 +0000207}
208
subrata_modak56207ce2009-03-23 13:35:39 +0000209void cleanup0(void)
plars865695b2001-08-27 22:15:12 +0000210{
subrata_modakb29f7fb2008-04-26 14:34:29 +0000211 /* delete the test directory created in setup0() */
212 if (tdat[testno].experrno != EBADF) {
subrata_modak56207ce2009-03-23 13:35:39 +0000213 (void)close(s);
subrata_modakb29f7fb2008-04-26 14:34:29 +0000214 s = -1;
215 tst_rmdir();
216 }
plars865695b2001-08-27 22:15:12 +0000217}
218
subrata_modak56207ce2009-03-23 13:35:39 +0000219void setup1(void)
plars865695b2001-08-27 22:15:12 +0000220{
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_modak56207ce2009-03-23 13:35:39 +0000224 strerror(errno));
plars865695b2001-08-27 22:15:12 +0000225 }
subrata_modak56207ce2009-03-23 13:35:39 +0000226 if (bind(s, (struct sockaddr *)&sin0, sizeof(sin0)) < 0) {
plars865695b2001-08-27 22:15:12 +0000227 tst_brkm(TBROK, cleanup, "socket bind failed for: %s",
subrata_modak56207ce2009-03-23 13:35:39 +0000228 strerror(errno));
plars865695b2001-08-27 22:15:12 +0000229 }
230 sinlen = sizeof(fsin1);
231}
232
subrata_modak56207ce2009-03-23 13:35:39 +0000233void setup2(void)
plars865695b2001-08-27 22:15:12 +0000234{
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_modak56207ce2009-03-23 13:35:39 +0000238 strerror(errno));
plars865695b2001-08-27 22:15:12 +0000239 }
240 ifc.ifc_len = sizeof(buf);
241 ifc.ifc_buf = buf;
242}
243
subrata_modak56207ce2009-03-23 13:35:39 +0000244void setup3(void)
plars865695b2001-08-27 22:15:12 +0000245{
246 setup2();
247 if (ioctl(s, SIOCGIFCONF, &ifc) < 0) {
248 tst_brkm(TBROK, cleanup, "socket setup failed: %s",
subrata_modak56207ce2009-03-23 13:35:39 +0000249 strerror(errno));
plars865695b2001-08-27 22:15:12 +0000250 }
251 ifr = *(struct ifreq *)ifc.ifc_buf;
252}
253
subrata_modak56207ce2009-03-23 13:35:39 +0000254void cleanup1(void)
plars865695b2001-08-27 22:15:12 +0000255{
subrata_modak56207ce2009-03-23 13:35:39 +0000256 (void)close(s);
plars865695b2001-08-27 22:15:12 +0000257 s = -1;
258}