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: listen01
22 *
23 * Test Description:
24 *  Verify that listen() returns the proper errno for various failure cases
25 *
26 * Usage:  <for command-line>
27 *  listen01 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
28 *     where,  -c n : Run n copies concurrently.
29 *             -e   : Turn on errno logging.
30 *	       -i n : Execute test n times.
31 *	       -I x : Execute test for x seconds.
32 *	       -P x : Pause for x seconds between iterations.
33 *	       -t   : Turn on syscall timing.
34 *
35 * HISTORY
36 *	07/2001 Ported by Wayne Boyer
37 *
38 * RESTRICTIONS:
39 *  None.
40 *
41 */
42
43#include <stdio.h>
44#include <unistd.h>
45#include <errno.h>
46#include <fcntl.h>
47
48#include <sys/types.h>
49#include <sys/socket.h>
50#include <sys/signal.h>
51#include <sys/un.h>
52
53#include <netinet/in.h>
54
55#include "test.h"
56
57char *TCID = "listen01";
58int testno;
59
60int s;				/* socket descriptor */
61
62void setup(void), setup0(void), setup1(void),
63cleanup(void), cleanup0(void), cleanup1(void);
64
65struct test_case_t {		/* test case structure */
66	int domain;		/* PF_INET, PF_UNIX, ... */
67	int type;		/* SOCK_STREAM, SOCK_DGRAM ... */
68	int proto;		/* protocol number (usually 0 = default) */
69	int backlog;		/* connect's 3rd argument */
70	int retval;		/* syscall return value */
71	int experrno;		/* expected errno */
72	void (*setup) (void);
73	void (*cleanup) (void);
74	char *desc;
75} tdat[] = {
76	{
77	0, 0, 0, 0, -1, EBADF, setup0, cleanup0, "bad file descriptor"}, {
78	0, 0, 0, 0, -1, ENOTSOCK, setup0, cleanup0, "not a socket"}, {
79PF_INET, SOCK_DGRAM, 0, 0, -1, EOPNOTSUPP, setup1, cleanup1,
80		    "UDP listen"},};
81
82int TST_TOTAL = sizeof(tdat) / sizeof(tdat[0]);
83
84int main(int argc, char *argv[])
85{
86	int lc;
87
88	tst_parse_opts(argc, argv, NULL, NULL);
89
90	setup();
91
92	for (lc = 0; TEST_LOOPING(lc); ++lc) {
93		tst_count = 0;
94		for (testno = 0; testno < TST_TOTAL; ++testno) {
95			tdat[testno].setup();
96
97			TEST(listen(s, tdat[testno].backlog));
98			if (TEST_RETURN != tdat[testno].retval ||
99			    (TEST_RETURN < 0 &&
100			     TEST_ERRNO != tdat[testno].experrno)) {
101				tst_resm(TFAIL, "%s ; returned"
102					 " %ld (expected %d), errno %d (expected"
103					 " %d)", tdat[testno].desc,
104					 TEST_RETURN, tdat[testno].retval,
105					 TEST_ERRNO, tdat[testno].experrno);
106			} else {
107				tst_resm(TPASS, "%s successful",
108					 tdat[testno].desc);
109			}
110			tdat[testno].cleanup();
111		}
112	}
113	cleanup();
114
115	tst_exit();
116}
117
118void setup(void)
119{
120	TEST_PAUSE;
121}
122
123void cleanup(void)
124{
125}
126
127void setup0(void)
128{
129	if (tdat[testno].experrno == EBADF)
130		s = 400;	/* anything not an open file */
131	else if ((s = open("/dev/null", O_WRONLY)) == -1)
132		tst_brkm(TBROK, cleanup, "error opening /dev/null - "
133			 "errno: %s", strerror(errno));
134}
135
136void cleanup0(void)
137{
138	s = -1;
139}
140
141void setup1(void)
142{
143	s = socket(tdat[testno].domain, tdat[testno].type, tdat[testno].proto);
144	if (s < 0) {
145		tst_brkm(TBROK, cleanup, "socket setup failed for listen: "
146			 "%s", strerror(errno));
147	}
148}
149
150void cleanup1(void)
151{
152	(void)close(s);
153	s = -1;
154}
155