xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/listen/listen01.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
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 #include "safe_macros.h"
57 
58 char *TCID = "listen01";
59 int testno;
60 
61 int s;				/* socket descriptor */
62 
63 void setup(void), setup0(void), setup1(void),
64 cleanup(void), cleanup0(void), cleanup1(void);
65 
66 struct test_case_t {		/* test case structure */
67 	int domain;		/* PF_INET, PF_UNIX, ... */
68 	int type;		/* SOCK_STREAM, SOCK_DGRAM ... */
69 	int proto;		/* protocol number (usually 0 = default) */
70 	int backlog;		/* connect's 3rd argument */
71 	int retval;		/* syscall return value */
72 	int experrno;		/* expected errno */
73 	void (*setup) (void);
74 	void (*cleanup) (void);
75 	char *desc;
76 } tdat[] = {
77 	{
78 	0, 0, 0, 0, -1, EBADF, setup0, cleanup0, "bad file descriptor"}, {
79 	0, 0, 0, 0, -1, ENOTSOCK, setup0, cleanup0, "not a socket"}, {
80 PF_INET, SOCK_DGRAM, 0, 0, -1, EOPNOTSUPP, setup1, cleanup1,
81 		    "UDP listen"},};
82 
83 int TST_TOTAL = sizeof(tdat) / sizeof(tdat[0]);
84 
main(int argc,char * argv[])85 int main(int argc, char *argv[])
86 {
87 	int lc;
88 
89 	tst_parse_opts(argc, argv, NULL, NULL);
90 
91 	setup();
92 
93 	for (lc = 0; TEST_LOOPING(lc); ++lc) {
94 		tst_count = 0;
95 		for (testno = 0; testno < TST_TOTAL; ++testno) {
96 			tdat[testno].setup();
97 
98 			TEST(listen(s, tdat[testno].backlog));
99 			if (TEST_RETURN != tdat[testno].retval ||
100 			    (TEST_RETURN < 0 &&
101 			     TEST_ERRNO != tdat[testno].experrno)) {
102 				tst_resm(TFAIL, "%s ; returned"
103 					 " %ld (expected %d), errno %d (expected"
104 					 " %d)", tdat[testno].desc,
105 					 TEST_RETURN, tdat[testno].retval,
106 					 TEST_ERRNO, tdat[testno].experrno);
107 			} else {
108 				tst_resm(TPASS, "%s successful",
109 					 tdat[testno].desc);
110 			}
111 			tdat[testno].cleanup();
112 		}
113 	}
114 	cleanup();
115 
116 	tst_exit();
117 }
118 
setup(void)119 void setup(void)
120 {
121 	TEST_PAUSE;
122 }
123 
cleanup(void)124 void cleanup(void)
125 {
126 }
127 
setup0(void)128 void setup0(void)
129 {
130 	if (tdat[testno].experrno == EBADF)
131 		s = 400;	/* anything not an open file */
132 	else if ((s = open("/dev/null", O_WRONLY)) == -1)
133 		tst_brkm(TBROK, cleanup, "error opening /dev/null - "
134 			 "errno: %s", strerror(errno));
135 }
136 
cleanup0(void)137 void cleanup0(void)
138 {
139 	s = -1;
140 }
141 
setup1(void)142 void setup1(void)
143 {
144 	s = SAFE_SOCKET(cleanup, tdat[testno].domain, tdat[testno].type,
145 		        tdat[testno].proto);
146 }
147 
cleanup1(void)148 void cleanup1(void)
149 {
150 	(void)close(s);
151 	s = -1;
152 }
153