Home | History | Annotate | Download | only in recv
      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: recv01
     22  *
     23  * Test Description:
     24  *  Verify that recv() returns the proper errno for various failure cases
     25  *
     26  * Usage:  <for command-line>
     27  *  recv01 [-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 
     57 char *TCID = "recv01";
     58 int testno;
     59 
     60 char buf[1024];
     61 int s;				/* socket descriptor */
     62 struct sockaddr_in sin1, sin2, sin3, sin4;
     63 static int sfd;			/* shared between do_child and start_server */
     64 
     65 void do_child(void), setup(void), setup0(void), setup1(void),
     66 cleanup(void), cleanup0(void), cleanup1(void);
     67 pid_t start_server(struct sockaddr_in *);
     68 
     69 struct test_case_t {		/* test case structure */
     70 	int domain;		/* PF_INET, PF_UNIX, ... */
     71 	int type;		/* SOCK_STREAM, SOCK_DGRAM ... */
     72 	int proto;		/* protocol number (usually 0 = default) */
     73 	void *buf;		/* recv data buffer */
     74 	int buflen;		/* recv's 3rd argument */
     75 	unsigned flags;		/* recv's 4th argument */
     76 	int retval;		/* syscall return value */
     77 	int experrno;		/* expected errno */
     78 	void (*setup) (void);
     79 	void (*cleanup) (void);
     80 	char *desc;
     81 } tdat[] = {
     82 	{
     83 	PF_INET, SOCK_STREAM, 0, buf, sizeof(buf), 0,
     84 		    -1, EBADF, setup0, cleanup0, "bad file descriptor"}
     85 	, {
     86 	0, 0, 0, buf, sizeof(buf), 0,
     87 		    -1, ENOTSOCK, setup0, cleanup0, "invalid socket"}
     88 	,
     89 #ifndef UCLINUX
     90 	    /* Skip since uClinux does not implement memory protection */
     91 	{
     92 	PF_INET, SOCK_STREAM, 0, (void *)-1, sizeof(buf), 0,
     93 		    -1, EFAULT, setup1, cleanup1, "invalid recv buffer"}
     94 	,
     95 #endif
     96 	{
     97 	PF_INET, SOCK_STREAM, 0, buf, sizeof(buf), MSG_OOB,
     98 		    -1, EINVAL, setup1, cleanup1, "invalid MSG_OOB flag set"}
     99 	,
    100 	{
    101 	PF_INET, SOCK_STREAM, 0, buf, sizeof(buf), MSG_ERRQUEUE,
    102 		    -1, EAGAIN, setup1, cleanup1, "invalid MSG_ERRQUEUE flag set"}
    103 ,};
    104 
    105 int TST_TOTAL = sizeof(tdat) / sizeof(tdat[0]);
    106 
    107 #ifdef UCLINUX
    108 static char *argv0;
    109 #endif
    110 
    111 int main(int argc, char *argv[])
    112 {
    113 	int lc;
    114 
    115 	tst_parse_opts(argc, argv, NULL, NULL);
    116 #ifdef UCLINUX
    117 	argv0 = argv[0];
    118 	maybe_run_child(&do_child, "d", &sfd);
    119 #endif
    120 
    121 	setup();
    122 
    123 	for (lc = 0; TEST_LOOPING(lc); ++lc) {
    124 		tst_count = 0;
    125 		for (testno = 0; testno < TST_TOTAL; ++testno) {
    126 			if ((tst_kvercmp(3, 17, 0) < 0)
    127 			    && (tdat[testno].flags & MSG_ERRQUEUE)
    128 			    && (tdat[testno].type & SOCK_STREAM)) {
    129 				tst_resm(TCONF, "skip MSG_ERRQUEUE test, "
    130 						"it's supported from 3.17");
    131 				continue;
    132 			}
    133 
    134 			tdat[testno].setup();
    135 			TEST(recv(s, tdat[testno].buf, tdat[testno].buflen,
    136 				  tdat[testno].flags));
    137 			if (TEST_RETURN > 0)
    138 				TEST_RETURN = 0;	/* all nonzero equal here */
    139 			if (TEST_RETURN != tdat[testno].retval ||
    140 			    (TEST_RETURN < 0 &&
    141 			     TEST_ERRNO != tdat[testno].experrno)) {
    142 				tst_resm(TFAIL, "%s ; returned"
    143 					 " %ld (expected %d), errno %d (expected"
    144 					 " %d)", tdat[testno].desc,
    145 					 TEST_RETURN, tdat[testno].retval,
    146 					 TEST_ERRNO, tdat[testno].experrno);
    147 			} else {
    148 				tst_resm(TPASS, "%s successful",
    149 					 tdat[testno].desc);
    150 			}
    151 			tdat[testno].cleanup();
    152 		}
    153 	}
    154 	cleanup();
    155 
    156 	tst_exit();
    157 }
    158 
    159 pid_t pid;
    160 
    161 void setup(void)
    162 {
    163 	TEST_PAUSE;
    164 
    165 	pid = start_server(&sin1);
    166 
    167 	(void)signal(SIGPIPE, SIG_IGN);
    168 }
    169 
    170 void cleanup(void)
    171 {
    172 	(void)kill(pid, SIGKILL);
    173 
    174 }
    175 
    176 void setup0(void)
    177 {
    178 	if (tdat[testno].experrno == EBADF)
    179 		s = 400;	/* anything not an open file */
    180 	else if ((s = open("/dev/null", O_WRONLY)) == -1)
    181 		tst_brkm(TBROK | TERRNO, cleanup, "open(/dev/null)");
    182 }
    183 
    184 void cleanup0(void)
    185 {
    186 	s = -1;
    187 }
    188 
    189 void setup1(void)
    190 {
    191 	fd_set rdfds;
    192 	struct timeval timeout;
    193 	int n;
    194 
    195 	s = socket(tdat[testno].domain, tdat[testno].type, tdat[testno].proto);
    196 	if (s < 0)
    197 		tst_brkm(TBROK | TERRNO, cleanup, "socket setup failed");
    198 	if (tdat[testno].type == SOCK_STREAM &&
    199 	    connect(s, (struct sockaddr *)&sin1, sizeof(sin1)) < 0) {
    200 		tst_brkm(TBROK | TERRNO, cleanup, "connect failed");
    201 	}
    202 	/* Wait for something to be readable, else we won't detect EFAULT on recv */
    203 	FD_ZERO(&rdfds);
    204 	FD_SET(s, &rdfds);
    205 	timeout.tv_sec = 2;
    206 	timeout.tv_usec = 0;
    207 	n = select(s + 1, &rdfds, 0, 0, &timeout);
    208 	if (n != 1 || !FD_ISSET(s, &rdfds))
    209 		tst_brkm(TBROK, cleanup,
    210 			 "client setup1 failed - no message ready in 2 sec");
    211 }
    212 
    213 void cleanup1(void)
    214 {
    215 	(void)close(s);
    216 	s = -1;
    217 }
    218 
    219 pid_t start_server(struct sockaddr_in *sin0)
    220 {
    221 	pid_t pid;
    222 	socklen_t slen = sizeof(*sin0);
    223 
    224 	sin0->sin_family = AF_INET;
    225 	sin0->sin_port = 0; /* pick random free port */
    226 	sin0->sin_addr.s_addr = INADDR_ANY;
    227 
    228 	sfd = socket(PF_INET, SOCK_STREAM, 0);
    229 	if (sfd < 0) {
    230 		tst_brkm(TBROK | TERRNO, cleanup, "server socket failed");
    231 		return -1;
    232 	}
    233 	if (bind(sfd, (struct sockaddr *)sin0, sizeof(*sin0)) < 0) {
    234 		tst_brkm(TBROK | TERRNO, cleanup, "server bind failed");
    235 		return -1;
    236 	}
    237 	if (listen(sfd, 10) < 0) {
    238 		tst_brkm(TBROK | TERRNO, cleanup, "server listen failed");
    239 		return -1;
    240 	}
    241 	if (getsockname(sfd, (struct sockaddr *)sin0, &slen) == -1)
    242 		tst_brkm(TBROK | TERRNO, cleanup, "getsockname failed");
    243 
    244 	switch ((pid = FORK_OR_VFORK())) {
    245 	case 0:		/* child */
    246 #ifdef UCLINUX
    247 		if (self_exec(argv0, "d", sfd) < 0)
    248 			tst_brkm(TBROK | TERRNO, cleanup,
    249 				 "server self_exec failed");
    250 #else
    251 		do_child();
    252 #endif
    253 		break;
    254 	case -1:
    255 		tst_brkm(TBROK | TERRNO, cleanup, "server fork failed");
    256 		/* fall through */
    257 	default:		/* parent */
    258 		(void)close(sfd);
    259 		return pid;
    260 	}
    261 
    262 	exit(1);
    263 }
    264 
    265 void do_child(void)
    266 {
    267 	struct sockaddr_in fsin;
    268 	fd_set afds, rfds;
    269 	int nfds, cc, fd;
    270 
    271 	FD_ZERO(&afds);
    272 	FD_SET(sfd, &afds);
    273 
    274 	nfds = sfd + 1;
    275 
    276 	/* accept connections until killed */
    277 	while (1) {
    278 		socklen_t fromlen;
    279 
    280 		memcpy(&rfds, &afds, sizeof(rfds));
    281 
    282 		if (select(nfds, &rfds, NULL, NULL,
    283 			   NULL) < 0)
    284 			if (errno != EINTR)
    285 				exit(1);
    286 		if (FD_ISSET(sfd, &rfds)) {
    287 			int newfd;
    288 
    289 			fromlen = sizeof(fsin);
    290 			newfd = accept(sfd, (struct sockaddr *)&fsin, &fromlen);
    291 			if (newfd >= 0) {
    292 				FD_SET(newfd, &afds);
    293 				nfds = MAX(nfds, newfd + 1);
    294 				/* send something back */
    295 				(void)write(newfd, "hoser\n", 6);
    296 			}
    297 		}
    298 		for (fd = 0; fd < nfds; ++fd)
    299 			if (fd != sfd && FD_ISSET(fd, &rfds)) {
    300 				cc = read(fd, buf, sizeof(buf));
    301 				if (cc == 0 || (cc < 0 && errno != EINTR)) {
    302 					(void)close(fd);
    303 					FD_CLR(fd, &afds);
    304 				}
    305 			}
    306 	}
    307 }
    308