1/*
2 *
3 *   Copyright (c) International Business Machines  Corp., 2001
4 *   07/2001 Ported by Wayne Boyer
5 *
6 *   This program is free software;  you can redistribute it and/or modify
7 *   it under the terms of the GNU General Public License as published by
8 *   the Free Software Foundation; either version 2 of the License, or
9 *   (at your option) any later version.
10 *
11 *   This program is distributed in the hope that it will be useful,
12 *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
13 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
14 *   the GNU General Public License for more details.
15 *
16 *   You should have received a copy of the GNU General Public License
17 *   along with this program;  if not, write to the Free Software
18 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21/*
22 * Description:
23 *  Verify that accept() returns the proper errno for various failure cases
24 */
25
26#include <stdio.h>
27#include <unistd.h>
28#include <errno.h>
29#include <fcntl.h>
30
31#include <sys/types.h>
32#include <sys/socket.h>
33#include <sys/signal.h>
34
35#include <netinet/in.h>
36
37#include "test.h"
38#include "safe_macros.h"
39
40char *TCID = "accept01";
41int testno;
42
43int s;				/* socket descriptor */
44struct sockaddr_in sin0, fsin1;
45socklen_t sinlen;
46
47static void setup(void);
48static void cleanup(void);
49static void setup0(void);
50static void cleanup0(void);
51static void setup1(void);
52static void cleanup1(void);
53static void setup2(void);
54static void setup3(void);
55
56struct test_case_t {		/* test case structure */
57	int domain;		/* PF_INET, PF_UNIX, ... */
58	int type;		/* SOCK_STREAM, SOCK_DGRAM ... */
59	int proto;		/* protocol number (usually 0 = default) */
60	struct sockaddr *sockaddr;	/* socket address buffer */
61	socklen_t *salen;	/* accept's 3rd argument */
62	int retval;		/* syscall return value */
63	int experrno;		/* expected errno */
64	void (*setup) (void);
65	void (*cleanup) (void);
66	char *desc;
67} tdat[] = {
68	{
69	PF_INET, SOCK_STREAM, 0, (struct sockaddr *)&fsin1,
70		    &sinlen, -1, EBADF, setup0, cleanup0,
71		    "bad file descriptor"}, {
72	PF_INET, SOCK_STREAM, 0, (struct sockaddr *)&fsin1,
73		    &sinlen, -1, ENOTSOCK, setup0, cleanup0,
74		    "bad file descriptor"}, {
75	PF_INET, SOCK_STREAM, 0, (struct sockaddr *)3,
76		    &sinlen, -1, EINVAL, setup1, cleanup1,
77		    "invalid socket buffer"}, {
78	PF_INET, SOCK_STREAM, 0, (struct sockaddr *)&fsin1,
79		    (socklen_t *) 1, -1, EINVAL, setup1, cleanup1,
80		    "invalid salen"}, {
81	PF_INET, SOCK_STREAM, 0, (struct sockaddr *)&fsin1,
82		    &sinlen, -1, EINVAL, setup2, cleanup1,
83		    "invalid salen"}, {
84	PF_INET, SOCK_STREAM, 0, (struct sockaddr *)&fsin1,
85		    &sinlen, -1, EINVAL, setup3, cleanup1,
86		    "no queued connections"}, {
87	PF_INET, SOCK_DGRAM, 0, (struct sockaddr *)&fsin1,
88		    &sinlen, -1, EOPNOTSUPP, setup1, cleanup1,
89		    "UDP accept"},};
90
91int TST_TOTAL = sizeof(tdat) / sizeof(tdat[0]);
92
93int main(int ac, char *av[])
94{
95	int lc;
96
97	tst_parse_opts(ac, av, NULL, NULL);
98
99	setup();
100
101	for (lc = 0; TEST_LOOPING(lc); ++lc) {
102		tst_count = 0;
103		for (testno = 0; testno < TST_TOTAL; ++testno) {
104			tdat[testno].setup();
105
106			TEST(accept(s, tdat[testno].sockaddr,
107				    tdat[testno].salen));
108			if (TEST_RETURN > 0)
109				TEST_RETURN = 0;
110			if (TEST_RETURN != tdat[testno].retval ||
111			    (TEST_RETURN < 0 &&
112			     TEST_ERRNO != tdat[testno].experrno)) {
113				tst_resm(TFAIL, "%s ; returned"
114					 " %ld (expected %d), errno %d (expected"
115					 " %d)", tdat[testno].desc,
116					 TEST_RETURN, tdat[testno].retval,
117					 TEST_ERRNO, tdat[testno].experrno);
118			} else {
119				tst_resm(TPASS, "%s successful",
120					 tdat[testno].desc);
121			}
122			tdat[testno].cleanup();
123		}
124	}
125
126	cleanup();
127	tst_exit();
128}
129
130static void setup(void)
131{
132	TEST_PAUSE;
133
134	/* initialize local sockaddr */
135	sin0.sin_family = AF_INET;
136	sin0.sin_port = 0;
137	sin0.sin_addr.s_addr = INADDR_ANY;
138}
139
140static void cleanup(void)
141{
142}
143
144static void setup0(void)
145{
146	if (tdat[testno].experrno == EBADF)
147		s = 400;	/* anything not an open file */
148	else if ((s = open("/dev/null", O_WRONLY)) == -1)
149		tst_brkm(TBROK | TERRNO, cleanup, "error opening /dev/null");
150}
151
152static void cleanup0(void)
153{
154	s = -1;
155}
156
157static void setup1(void)
158{
159	s = socket(tdat[testno].domain, tdat[testno].type, tdat[testno].proto);
160	if (s < 0) {
161		tst_brkm(TBROK, cleanup, "socket setup failed for accept "
162			 "test %d: %s", testno, strerror(errno));
163	}
164	if (bind(s, (struct sockaddr *)&sin0, sizeof(sin0)) < 0) {
165		tst_brkm(TBROK, cleanup, "socket bind failed for accept "
166			 "test %d: %s", testno, strerror(errno));
167	}
168	sinlen = sizeof(fsin1);
169}
170
171static void cleanup1(void)
172{
173	(void)close(s);
174	s = -1;
175}
176
177static void setup2(void)
178{
179	setup1();		/* get a socket in s */
180	sinlen = 1;		/* invalid s */
181}
182
183static void setup3(void)
184{
185	int one = 1;
186
187	setup1();
188	SAFE_IOCTL(cleanup, s, FIONBIO, &one);
189}
190