166b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak/*
266b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak *   Copyright (c) 2008 Vijay Kumar B. <vijaykumar@bravegnu.org>
366b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak *
466b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak *   Based on testcases/kernel/syscalls/waitpid/waitpid01.c
566b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak *   Original copyright message:
666b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak *
766b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak *   Copyright (c) International Business Machines  Corp., 2001
866b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak *
966b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak *   This program is free software;  you can redistribute it and/or modify
1066b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak *   it under the terms of the GNU General Public License as published by
1166b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak *   the Free Software Foundation; either version 2 of the License, or
1266b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak *   (at your option) any later version.
1366b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak *
1466b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak *   This program is distributed in the hope that it will be useful,
1566b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
1666b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
1766b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak *   the GNU General Public License for more details.
1866b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak *
1966b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak *   You should have received a copy of the GNU General Public License
2066b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak *   along with this program;  if not, write to the Free Software
214548c6cf9bcdd96d8303caa4130ab638b61f8a30Wanlong Gao *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2266b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak */
2366b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak
2466b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak/*
2566b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak * NAME
2666b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak *	eventfd01.c
2766b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak *
2866b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak * DESCRIPTION
2966b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak *      Test cases for eventfd syscall.
3066b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak *
3166b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak * USAGE:  <for command-line>
3266b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak *      eventfd01 [-c n] [-i n] [-I x] [-P x] [-t]
3366b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak *      where,  -c n : Run n copies concurrently.
3466b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak *              -i n : Execute test n times.
3566b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak *              -I x : Execute test for x seconds.
3666b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak *              -P x : Pause for x seconds between iterations.
3766b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak *
3866b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak * History
3966b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak *	07/2008 Vijay Kumar
4066b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak *		Initial Version.
4166b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak *
4266b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak * Restrictions
4366b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak *	None
4466b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak */
4566b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak
464fc9f54c5f65d10d7f1258ab424967e14e40da74subrata_modak#include "config.h"
474fc9f54c5f65d10d7f1258ab424967e14e40da74subrata_modak
4866b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak#include <sys/types.h>
4902d739be8b3256ddc2a50075d15e7bc1ee769e30Garrett Cooper#include <sys/select.h>
5066b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak#include <sys/wait.h>
5166b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak#include <errno.h>
5202d739be8b3256ddc2a50075d15e7bc1ee769e30Garrett Cooper#include <fcntl.h>
53c8b68a4bde2003d55bb243d1bef9e72c84822af5vapier#include <inttypes.h>
54a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak#include <poll.h>
5502d739be8b3256ddc2a50075d15e7bc1ee769e30Garrett Cooper#include <signal.h>
5602d739be8b3256ddc2a50075d15e7bc1ee769e30Garrett Cooper#include <stdint.h>
5702d739be8b3256ddc2a50075d15e7bc1ee769e30Garrett Cooper#include <string.h>
5802d739be8b3256ddc2a50075d15e7bc1ee769e30Garrett Cooper#include <unistd.h>
5966b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak
6002d739be8b3256ddc2a50075d15e7bc1ee769e30Garrett Cooper#include "test.h"
616aa2737d5de9256fcdb5b68a6aa6b12249005049yaberauneya#define CLEANUP cleanup
626aa2737d5de9256fcdb5b68a6aa6b12249005049yaberauneya#include "linux_syscall_numbers.h"
6366b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak
644fc9f54c5f65d10d7f1258ab424967e14e40da74subrata_modak#ifdef HAVE_LIBAIO_H
65a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak#include <libaio.h>
66a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak#endif
67a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak
6866b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modakstatic void setup(void);
6966b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak
704fc9f54c5f65d10d7f1258ab424967e14e40da74subrata_modakTCID_DEFINE(eventfd01);
71a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modakint TST_TOTAL = 15;
7266b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak
7356207cec7732e09c216c751c0b5f88a242bacae6subrata_modakstatic int myeventfd(unsigned int initval, int flags)
7466b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak{
7556207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	/* eventfd2 uses FLAGS but eventfd doesn't take FLAGS. */
76359980f68b19c77c698b121b57a071dfe6e3ca31Jan Stancek	return ltp_syscall(__NR_eventfd, initval);
7766b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak}
7866b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak
7966b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak/*
8066b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak * clear_counter() - clears the counter by performing a dummy read
8166b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak * @fd: the eventfd
8266b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak *
8366b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak * RETURNS:
8466b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak * 0 on success, and -1 on failure
8566b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak */
8656207cec7732e09c216c751c0b5f88a242bacae6subrata_modakstatic int clear_counter(int fd)
8766b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak{
8866b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	uint64_t dummy;
8966b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	int ret;
9066b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak
9166b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	ret = read(fd, &dummy, sizeof(dummy));
9266b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	if (ret == -1) {
9366b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak		if (errno != EAGAIN) {
94354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			tst_resm(TINFO | TERRNO, "error clearing counter");
9566b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak			return -1;
9666b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak		}
9766b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	}
9866b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak
9966b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	return 0;
10066b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak}
10166b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak
10266b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak/*
10366b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak * set_counter() - sets the count to specified value
10466b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak * @fd: the eventfd
10566b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak * @val: the value to be set
10666b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak *
10766b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak * Clears the counter and sets the counter to @val.
10866b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak *
10966b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak * RETURNS:
11066b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak * 0 on success, -1 on failure
11166b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak */
11256207cec7732e09c216c751c0b5f88a242bacae6subrata_modakstatic int set_counter(int fd, uint64_t val)
11366b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak{
11466b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	int ret;
11566b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak
11666b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	ret = clear_counter(fd);
11780678cff0facff97be57def1f58ab0403d5f7be3Garrett Cooper	if (ret == -1)
11866b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak		return -1;
11966b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak
12066b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	ret = write(fd, &val, sizeof(val));
12166b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	if (ret == -1) {
122354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		tst_resm(TINFO | TERRNO, "error setting counter value");
12366b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak		return -1;
12466b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	}
12566b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak
12666b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	return 0;
12766b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak}
12866b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak
12966b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak/*
13066b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak * Test whether the current value of the counter matches @required.
13166b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak */
13256207cec7732e09c216c751c0b5f88a242bacae6subrata_modakstatic void read_test(int fd, uint64_t required)
13366b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak{
13466b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	int ret;
13566b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	uint64_t val;
13666b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak
13766b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	ret = read(fd, &val, sizeof(val));
13866b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	if (ret == -1) {
139354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		tst_resm(TBROK | TERRNO, "error reading eventfd");
14066b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak		return;
14166b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	}
14266b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak
14366b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	if (val == required)
14466b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak		tst_resm(TPASS, "counter value matches required");
14566b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	else
14666b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak		tst_resm(TFAIL, "counter value mismatch: "
147354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			 "required: %" PRIu64 ", got: %" PRIu64, required, val);
14866b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak}
14966b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak
15066b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak/*
15166b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak * Test whether read returns with error EAGAIN when counter is at 0.
15266b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak */
15356207cec7732e09c216c751c0b5f88a242bacae6subrata_modakstatic void read_eagain_test(int fd)
15466b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak{
15566b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	int ret;
15666b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	uint64_t val;
15766b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak
15866b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	ret = clear_counter(fd);
159a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak	if (ret == -1) {
160a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak		tst_resm(TBROK, "error clearing counter");
16166b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak		return;
162a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak	}
16366b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak
16466b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	ret = read(fd, &val, sizeof(val));
16566b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	if (ret == -1) {
16666b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak		if (errno == EAGAIN)
16766b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak			tst_resm(TPASS, "read failed with EAGAIN as expected");
16866b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak		else
169354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			tst_resm(TFAIL | TERRNO, "read failed (wanted EAGAIN)");
17080678cff0facff97be57def1f58ab0403d5f7be3Garrett Cooper	} else
171c8b68a4bde2003d55bb243d1bef9e72c84822af5vapier		tst_resm(TFAIL, "read returned with %d", ret);
17266b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak}
17366b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak
17466b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak/*
17566b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak * Test whether writing to counter works.
17666b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak */
17756207cec7732e09c216c751c0b5f88a242bacae6subrata_modakstatic void write_test(int fd)
17866b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak{
17966b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	int ret;
18066b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	uint64_t val;
18166b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak
18266b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	val = 12;
18366b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak
18466b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	ret = set_counter(fd, val);
185a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak	if (ret == -1) {
186354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		tst_resm(TBROK, "error setting counter value to %" PRIu64, val);
18766b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak		return;
188a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak	}
18966b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak
19066b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	read_test(fd, val);
19166b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak}
19266b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak
19366b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak/*
19466b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak * Test whether write returns with error EAGAIN when counter is at
19566b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak * (UINT64_MAX - 1).
19666b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak */
19756207cec7732e09c216c751c0b5f88a242bacae6subrata_modakstatic void write_eagain_test(int fd)
19866b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak{
19966b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	int ret;
20066b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	uint64_t val;
20166b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak
20266b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	ret = set_counter(fd, UINT64_MAX - 1);
203a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak	if (ret == -1) {
204a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak		tst_resm(TBROK, "error setting counter value to UINT64_MAX-1");
20566b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak		return;
206a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak	}
20766b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak
20866b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	val = 1;
20966b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	ret = write(fd, &val, sizeof(val));
21066b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	if (ret == -1) {
21166b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak		if (errno == EAGAIN)
212c8b68a4bde2003d55bb243d1bef9e72c84822af5vapier			tst_resm(TPASS, "write failed with EAGAIN as expected");
21366b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak		else
214c8b68a4bde2003d55bb243d1bef9e72c84822af5vapier			tst_resm(TFAIL, "write failed (wanted EAGAIN)");
21580678cff0facff97be57def1f58ab0403d5f7be3Garrett Cooper	} else
21666b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak		tst_resm(TFAIL, "write returned with %d", ret);
21766b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak}
21866b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak
21966b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak/*
22066b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak * Test whether read returns with error EINVAL, if buffer size is less
22166b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak * than 8 bytes.
22266b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak */
22356207cec7732e09c216c751c0b5f88a242bacae6subrata_modakstatic void read_einval_test(int fd)
22466b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak{
22566b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	uint32_t invalid;
22666b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	int ret;
22766b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak
22866b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	ret = read(fd, &invalid, sizeof(invalid));
22966b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	if (ret == -1) {
23080678cff0facff97be57def1f58ab0403d5f7be3Garrett Cooper		if (errno == EINVAL)
23166b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak			tst_resm(TPASS, "read failed with EINVAL as expected");
23280678cff0facff97be57def1f58ab0403d5f7be3Garrett Cooper		else
233354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			tst_resm(TFAIL | TERRNO, "read failed (wanted EINVAL)");
23480678cff0facff97be57def1f58ab0403d5f7be3Garrett Cooper	} else
23566b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak		tst_resm(TFAIL, "read returned with %d", ret);
23666b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak}
23766b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak
23866b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak/*
23966b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak * Test whether write returns with error EINVAL, if buffer size is
24066b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak * less than 8 bytes.
24166b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak */
24256207cec7732e09c216c751c0b5f88a242bacae6subrata_modakstatic void write_einval_test(int fd)
24366b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak{
24466b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	uint32_t invalid;
24566b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	int ret;
24666b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak
24766b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	ret = write(fd, &invalid, sizeof(invalid));
24866b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	if (ret == -1) {
24980678cff0facff97be57def1f58ab0403d5f7be3Garrett Cooper		if (errno == EINVAL)
250c8b68a4bde2003d55bb243d1bef9e72c84822af5vapier			tst_resm(TPASS, "write failed with EINVAL as expected");
25180678cff0facff97be57def1f58ab0403d5f7be3Garrett Cooper		else
252354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			tst_resm(TFAIL | TERRNO,
253354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				 "write failed (wanted EINVAL)");
25480678cff0facff97be57def1f58ab0403d5f7be3Garrett Cooper	} else
25566b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak		tst_resm(TFAIL, "write returned with %d", ret);
25666b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak}
25766b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak
25866b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak/*
25966b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak * Test wheter write returns with error EINVAL, when the written value
26066b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak * is 0xFFFFFFFFFFFFFFFF.
26166b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak */
26256207cec7732e09c216c751c0b5f88a242bacae6subrata_modakstatic void write_einval2_test(int fd)
26366b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak{
26466b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	int ret;
26566b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	uint64_t val;
26666b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak
26766b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	ret = clear_counter(fd);
268a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak	if (ret == -1) {
269a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak		tst_resm(TBROK, "error clearing counter");
27066b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak		return;
271a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak	}
27266b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak
27366b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	val = 0xffffffffffffffffLL;
27466b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	ret = write(fd, &val, sizeof(val));
27566b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	if (ret == -1) {
27666b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak		if (errno == EINVAL)
277c8b68a4bde2003d55bb243d1bef9e72c84822af5vapier			tst_resm(TPASS, "write failed with EINVAL as expected");
27866b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak		else
279354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			tst_resm(TFAIL | TERRNO,
280354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				 "write failed (wanted EINVAL)");
28166b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	} else {
28266b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak		tst_resm(TFAIL, "write returned with %d", ret);
28366b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	}
28466b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak}
28566b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak
28666b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak/*
28766b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak * Test whether readfd is set by select when counter value is
28866b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak * non-zero.
28966b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak */
29056207cec7732e09c216c751c0b5f88a242bacae6subrata_modakstatic void readfd_set_test(int fd)
29166b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak{
29266b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	int ret;
29366b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	fd_set readfds;
29466b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	struct timeval timeout = { 0, 0 };
29566b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	uint64_t non_zero = 10;
29666b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak
29766b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	FD_ZERO(&readfds);
29866b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	FD_SET(fd, &readfds);
29966b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak
30066b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	ret = set_counter(fd, non_zero);
301a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak	if (ret == -1) {
302354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		tst_resm(TBROK, "error setting counter value to %" PRIu64,
303a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak			 non_zero);
30466b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak		return;
305a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak	}
30666b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak
30766b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	ret = select(fd + 1, &readfds, NULL, NULL, &timeout);
30866b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	if (ret == -1) {
30966b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak		/* EINTR cannot occur, since we don't block. */
310354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		tst_resm(TBROK | TERRNO, "select() failed");
31166b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak		return;
31266b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	}
31366b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak
31466b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	if (FD_ISSET(fd, &readfds))
31566b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak		tst_resm(TPASS, "fd is set in readfds");
31666b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	else
31766b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak		tst_resm(TFAIL, "fd is not set in readfds");
31866b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak}
31966b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak
32066b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak/*
32166b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak * Test whether readfd is not set by select when counter value is
32266b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak * zero.
32366b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak */
32456207cec7732e09c216c751c0b5f88a242bacae6subrata_modakstatic void readfd_not_set_test(int fd)
32566b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak{
32666b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	int ret;
32766b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	fd_set readfds;
32866b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	struct timeval timeout = { 0, 0 };
32966b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak
33066b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	FD_ZERO(&readfds);
33166b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	FD_SET(fd, &readfds);
33266b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak
33366b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	ret = clear_counter(fd);
334a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak	if (ret == -1) {
335a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak		tst_resm(TBROK, "error clearing counter");
33666b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak		return;
337a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak	}
33866b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak
33966b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	ret = select(fd + 1, &readfds, NULL, NULL, &timeout);
34066b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	if (ret == -1) {
34166b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak		/* EINTR cannot occur, since we don't block. */
342354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		tst_resm(TBROK | TERRNO, "select() failed");
34366b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak		return;
34466b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	}
34566b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak
34666b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	if (!FD_ISSET(fd, &readfds))
34766b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak		tst_resm(TPASS, "fd is not set in readfds");
34866b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	else
34966b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak		tst_resm(TFAIL, "fd is set in readfds");
35066b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak}
35166b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak
35266b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak/*
35366b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak * Test whether writefd is set by select when counter value is not the
35466b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak * maximum counter value.
35566b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak */
35656207cec7732e09c216c751c0b5f88a242bacae6subrata_modakstatic void writefd_set_test(int fd)
35766b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak{
35866b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	int ret;
35966b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	fd_set writefds;
36066b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	struct timeval timeout = { 0, 0 };
36166b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	uint64_t non_max = 10;
36266b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak
36366b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	FD_ZERO(&writefds);
36466b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	FD_SET(fd, &writefds);
36566b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak
36666b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	ret = set_counter(fd, non_max);
367a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak	if (ret == -1) {
368354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		tst_resm(TBROK, "error setting counter value to %" PRIu64,
369354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			 non_max);
37066b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak		return;
371a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak	}
37266b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak
37366b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	ret = select(fd + 1, NULL, &writefds, NULL, &timeout);
37466b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	if (ret == -1) {
37566b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak		/* EINTR cannot occur, since we don't block. */
376354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		tst_resm(TBROK | TERRNO, "select: error getting fd status");
37766b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak		return;
37866b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	}
37966b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak
38066b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	if (FD_ISSET(fd, &writefds))
38166b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak		tst_resm(TPASS, "fd is set in writefds");
38266b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	else
38366b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak		tst_resm(TFAIL, "fd is not set in writefds");
38466b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak}
38566b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak
38666b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak/*
38766b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak * Test whether writefd is not set by select when counter value is at
38866b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak * (UINT64_MAX - 1).
38966b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak */
39056207cec7732e09c216c751c0b5f88a242bacae6subrata_modakstatic void writefd_not_set_test(int fd)
39166b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak{
39266b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	int ret;
39366b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	fd_set writefds;
39466b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	struct timeval timeout = { 0, 0 };
39566b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak
39666b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	FD_ZERO(&writefds);
39766b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	FD_SET(fd, &writefds);
39866b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak
39966b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	ret = set_counter(fd, UINT64_MAX - 1);
400a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak	if (ret == -1) {
401a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak		tst_resm(TBROK, "error setting counter value to UINT64_MAX-1");
40266b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak		return;
403a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak	}
40466b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak
40566b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	ret = select(fd + 1, NULL, &writefds, NULL, &timeout);
40666b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	if (ret == -1) {
40766b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak		/* EINTR cannot occur, since we don't block. */
408354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		tst_resm(TBROK | TERRNO, "select: error getting fd status");
40966b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak		return;
41066b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	}
41166b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak
41266b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	if (!FD_ISSET(fd, &writefds))
41366b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak		tst_resm(TPASS, "fd is not set in writefds");
41466b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	else
41566b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak		tst_resm(TFAIL, "fd is set in writefds");
41666b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak}
41766b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak
41866b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak/*
41966b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak * Test whether counter update in child is reflected in the parent.
42066b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak */
42156207cec7732e09c216c751c0b5f88a242bacae6subrata_modakstatic void child_inherit_test(int fd)
42266b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak{
42366b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	uint64_t val;
42466b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	pid_t cpid;
42566b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	int ret;
42666b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	int status;
42766b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	uint64_t to_parent = 0xdeadbeef;
42866b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	uint64_t dummy;
42966b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak
43066b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	cpid = fork();
43166b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	if (cpid == -1)
432354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		tst_resm(TBROK | TERRNO, "fork failed");
43380678cff0facff97be57def1f58ab0403d5f7be3Garrett Cooper	else if (cpid != 0) {
43466b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak		ret = wait(&status);
43566b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak		if (ret == -1) {
43666b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak			tst_resm(TBROK, "error getting child exit status");
43766b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak			return;
43866b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak		}
43966b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak
44066b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak		if (WEXITSTATUS(status) == 1) {
44166b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak			tst_resm(TBROK, "counter value write not "
44280678cff0facff97be57def1f58ab0403d5f7be3Garrett Cooper				 "successful in child");
44366b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak			return;
44466b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak		}
44566b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak
44666b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak		ret = read(fd, &val, sizeof(val));
44766b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak		if (ret == -1) {
448354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			tst_resm(TBROK | TERRNO, "error reading eventfd");
44966b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak			return;
45066b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak		}
45166b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak
45266b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak		if (val == to_parent)
45366b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak			tst_resm(TPASS, "counter value write from "
45466b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak				 "child successful");
45566b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak		else
45666b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak			tst_resm(TFAIL, "counter value write in child "
45766b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak				 "failed");
45866b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	} else {
45966b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak		/* Child */
46066b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak		ret = read(fd, &dummy, sizeof(dummy));
46166b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak		if (ret == -1 && errno != EAGAIN) {
462354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			tst_resm(TWARN | TERRNO, "error clearing counter");
46366b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak			exit(1);
46466b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak		}
46566b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak
46666b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak		ret = write(fd, &to_parent, sizeof(to_parent));
46766b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak		if (ret == -1) {
468354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			tst_resm(TWARN | TERRNO, "error writing eventfd");
46966b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak			exit(1);
47066b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak		}
47166b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak
47266b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak		exit(0);
47366b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	}
47466b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak}
47566b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak
4764fc9f54c5f65d10d7f1258ab424967e14e40da74subrata_modak#ifdef HAVE_IO_SET_EVENTFD
477a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak/*
4784bb656a129f7507823e9e6d6b98b1a02fd80ef89subrata_modak * Test whether counter overflow is detected and handled correctly.
479a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak *
480a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak * It is not possible to directly overflow the counter using the
481a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak * write() syscall. Overflows occur when the counter is incremented
482a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak * from kernel space, in an irq context, when it is not possible to
483a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak * block the calling thread of execution.
484a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak *
485a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak * The AIO subsystem internally uses eventfd mechanism for
486a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak * notification of completion of read or write requests. In this test
487a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak * we trigger a counter overflow, by setting the counter value to the
488a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak * max possible value initially. When the AIO subsystem notifies
489a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak * through the eventfd counter, the counter overflows.
490a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak *
491a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak * NOTE: If the the counter starts from an initial value of 0, it will
492a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak * take decades for an overflow to occur. But since we set the initial
493a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak * value to the max possible counter value, we are able to cause it to
494a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak * overflow with a single increment.
495a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak *
496a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak * When the counter overflows, the following are tested
497a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak *   1. Check whether POLLERR event occurs in poll() for the eventfd.
498a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak *   2. Check whether readfd_set/writefd_set is set in select() for the
499a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak        eventfd.
500a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak *   3. The counter value is UINT64_MAX.
501a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak */
50256207cec7732e09c216c751c0b5f88a242bacae6subrata_modakstatic int trigger_eventfd_overflow(int evfd, int *fd, io_context_t * ctx)
503a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak{
504a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak	int ret;
505a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak	struct iocb iocb;
506a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak	struct iocb *iocbap[1];
507b5a697fc0f06439dbba8045622b88822fe7bf81bPatrick Mooney	struct io_event ioev;
508a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak	static char buf[4 * 1024];
509a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak
510a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak	*ctx = 0;
511a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak	ret = io_setup(16, ctx);
512a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak	if (ret < 0) {
513c8b68a4bde2003d55bb243d1bef9e72c84822af5vapier		errno = -ret;
514354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		tst_resm(TINFO | TERRNO, "io_setup error");
515a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak		return -1;
516a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak	}
517a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak
518a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak	*fd = open("testfile", O_RDWR | O_CREAT, 0644);
519a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak	if (*fd == -1) {
520354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		tst_resm(TINFO | TERRNO, "open(testfile) failed");
521a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak		goto err_io_destroy;
522a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak	}
523a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak
524a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak	ret = set_counter(evfd, UINT64_MAX - 1);
525a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak	if (ret == -1) {
526a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak		tst_resm(TINFO, "error setting counter to UINT64_MAX-1");
527a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak		goto err_close_file;
528a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak	}
529a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak
530a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak	io_prep_pwrite(&iocb, *fd, buf, sizeof(buf), 0);
531a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak	io_set_eventfd(&iocb, evfd);
532bdbaec51a423e715c2b03ed9e497e9a1fba6103esubrata_modak
533a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak	iocbap[0] = &iocb;
534a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak	ret = io_submit(*ctx, 1, iocbap);
535a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak	if (ret < 0) {
536c8b68a4bde2003d55bb243d1bef9e72c84822af5vapier		errno = -ret;
537354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		tst_resm(TINFO | TERRNO, "error submitting iocb");
538a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak		goto err_close_file;
539a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak	}
540a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak
541b5a697fc0f06439dbba8045622b88822fe7bf81bPatrick Mooney	ret = io_getevents(*ctx, 1, 1, &ioev, NULL);
542b5a697fc0f06439dbba8045622b88822fe7bf81bPatrick Mooney	if (ret < 0) {
543b5a697fc0f06439dbba8045622b88822fe7bf81bPatrick Mooney		errno = -ret;
544b5a697fc0f06439dbba8045622b88822fe7bf81bPatrick Mooney		tst_resm(TINFO | TERRNO, "error waiting for event");
545b5a697fc0f06439dbba8045622b88822fe7bf81bPatrick Mooney		goto err_close_file;
546b5a697fc0f06439dbba8045622b88822fe7bf81bPatrick Mooney	}
547b5a697fc0f06439dbba8045622b88822fe7bf81bPatrick Mooney
548a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak	return 0;
549a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak
550e70b30b9873818be1593bf495fca3c990e2ef24cyaberauneyaerr_close_file:
551a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak	close(*fd);
552a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak
553e70b30b9873818be1593bf495fca3c990e2ef24cyaberauneyaerr_io_destroy:
554a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak	io_destroy(*ctx);
555a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak
556a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak	return -1;
557a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak}
558a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak
55956207cec7732e09c216c751c0b5f88a242bacae6subrata_modakstatic void cleanup_overflow(int fd, io_context_t ctx)
560a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak{
561a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak	close(fd);
562a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak	io_destroy(ctx);
563a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak}
564a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak
56556207cec7732e09c216c751c0b5f88a242bacae6subrata_modakstatic void overflow_select_test(int evfd)
566a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak{
567a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak	struct timeval timeout = { 10, 0 };
568a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak	fd_set readfds;
569a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak	int fd;
570a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak	io_context_t ctx;
571a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak	int ret;
572a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak
573a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak	ret = trigger_eventfd_overflow(evfd, &fd, &ctx);
574a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak	if (ret == -1) {
575a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak		tst_resm(TBROK, "error triggering eventfd overflow");
576a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak		return;
577a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak	}
578bdbaec51a423e715c2b03ed9e497e9a1fba6103esubrata_modak
579a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak	FD_ZERO(&readfds);
580a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak	FD_SET(evfd, &readfds);
581a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak	ret = select(evfd + 1, &readfds, NULL, NULL, &timeout);
58280678cff0facff97be57def1f58ab0403d5f7be3Garrett Cooper	if (ret == -1)
583354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		tst_resm(TBROK | TERRNO,
584354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			 "error getting evfd status with select");
58580678cff0facff97be57def1f58ab0403d5f7be3Garrett Cooper	else {
586e70b30b9873818be1593bf495fca3c990e2ef24cyaberauneya		if (FD_ISSET(evfd, &readfds))
587e70b30b9873818be1593bf495fca3c990e2ef24cyaberauneya			tst_resm(TPASS, "read fd set as expected");
588e70b30b9873818be1593bf495fca3c990e2ef24cyaberauneya		else
589e70b30b9873818be1593bf495fca3c990e2ef24cyaberauneya			tst_resm(TFAIL, "read fd not set");
590a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak	}
591a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak	cleanup_overflow(fd, ctx);
592a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak}
593a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak
59456207cec7732e09c216c751c0b5f88a242bacae6subrata_modakstatic void overflow_poll_test(int evfd)
595a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak{
596a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak	struct pollfd pollfd;
597a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak	int fd;
598a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak	io_context_t ctx;
599a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak	int ret;
600a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak
601a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak	ret = trigger_eventfd_overflow(evfd, &fd, &ctx);
602f0be8b0c4489defeb6eaf6715576524a48b8998aSubrata Modak	if (ret == -1) {
603a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak		tst_resm(TBROK, "error triggering eventfd overflow");
604a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak		return;
605a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak	}
606a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak
607a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak	pollfd.fd = evfd;
608a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak	pollfd.events = POLLIN;
609a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak	pollfd.revents = 0;
610a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak	ret = poll(&pollfd, 1, 10000);
61180678cff0facff97be57def1f58ab0403d5f7be3Garrett Cooper	if (ret == -1)
612354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		tst_resm(TBROK | TERRNO, "error getting evfd status with poll");
61380678cff0facff97be57def1f58ab0403d5f7be3Garrett Cooper	else {
614e70b30b9873818be1593bf495fca3c990e2ef24cyaberauneya		if (pollfd.revents & POLLERR)
615e70b30b9873818be1593bf495fca3c990e2ef24cyaberauneya			tst_resm(TPASS, "POLLERR occurred as expected");
616e70b30b9873818be1593bf495fca3c990e2ef24cyaberauneya		else
617e70b30b9873818be1593bf495fca3c990e2ef24cyaberauneya			tst_resm(TFAIL, "POLLERR did not occur");
618a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak	}
619a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak	cleanup_overflow(fd, ctx);
620a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak}
621a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak
62256207cec7732e09c216c751c0b5f88a242bacae6subrata_modakstatic void overflow_read_test(int evfd)
623a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak{
624a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak	uint64_t count;
625a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak	io_context_t ctx;
626a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak	int fd;
627a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak	int ret;
628a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak
629a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak	ret = trigger_eventfd_overflow(evfd, &fd, &ctx);
630a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak	if (ret == -1) {
631a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak		tst_resm(TBROK, "error triggering eventfd overflow");
632a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak		return;
633a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak	}
634bdbaec51a423e715c2b03ed9e497e9a1fba6103esubrata_modak
6354bb656a129f7507823e9e6d6b98b1a02fd80ef89subrata_modak	ret = read(evfd, &count, sizeof(count));
63680678cff0facff97be57def1f58ab0403d5f7be3Garrett Cooper	if (ret == -1)
637354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		tst_resm(TBROK | TERRNO, "error reading eventfd");
63880678cff0facff97be57def1f58ab0403d5f7be3Garrett Cooper	else {
639a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak
640e70b30b9873818be1593bf495fca3c990e2ef24cyaberauneya		if (count == UINT64_MAX)
641e70b30b9873818be1593bf495fca3c990e2ef24cyaberauneya			tst_resm(TPASS, "overflow occurred as expected");
642e70b30b9873818be1593bf495fca3c990e2ef24cyaberauneya		else
643e70b30b9873818be1593bf495fca3c990e2ef24cyaberauneya			tst_resm(TFAIL, "overflow did not occur");
644e70b30b9873818be1593bf495fca3c990e2ef24cyaberauneya	}
645a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak	cleanup_overflow(fd, ctx);
646a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak}
647a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak#else
64856207cec7732e09c216c751c0b5f88a242bacae6subrata_modakstatic void overflow_select_test(int evfd)
649a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak{
650a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak	tst_resm(TCONF, "eventfd support is not available in AIO subsystem");
651a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak}
652a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak
65356207cec7732e09c216c751c0b5f88a242bacae6subrata_modakstatic void overflow_poll_test(int evfd)
654a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak{
655a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak	tst_resm(TCONF, "eventfd support is not available in AIO subsystem");
656a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak}
657a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak
65856207cec7732e09c216c751c0b5f88a242bacae6subrata_modakstatic void overflow_read_test(int evfd)
659a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak{
660a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak	tst_resm(TCONF, "eventfd support is not available in AIO subsystem");
661a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak}
662a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak#endif
663a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak
66456207cec7732e09c216c751c0b5f88a242bacae6subrata_modakint main(int argc, char **argv)
66566b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak{
66689af32a63ce8a780ea39337339e14caae244b5a4Cyril Hrubis	int lc;
66766b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	int fd;
66866b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak
669d6d11d08678aac1ed2c370ea8e42e5f45aea07beCyril Hrubis	tst_parse_opts(argc, argv, NULL, NULL);
67066b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak
67166b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	setup();
67266b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak
67366b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	for (lc = 0; TEST_LOOPING(lc); lc++) {
67466b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak		int ret;
67566b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak		uint64_t einit = 10;
67666b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak
677d59a659cd639ca2780b00049d102acd2a783d585Caspar Zhang		tst_count = 0;
67866b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak
67966b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak		fd = myeventfd(einit, 0);
68066b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak		if (fd == -1)
681354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			tst_brkm(TBROK | TERRNO, CLEANUP,
682354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				 "error creating eventfd");
68366b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak
68466b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak		ret = fcntl(fd, F_SETFL, O_NONBLOCK);
68566b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak		if (ret == -1)
686354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			tst_brkm(TBROK | TERRNO, CLEANUP,
687354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				 "error setting non-block mode");
68866b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak
68966b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak		read_test(fd, einit);
69066b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak		read_eagain_test(fd);
69166b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak		write_test(fd);
69266b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak		write_eagain_test(fd);
69366b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak		read_einval_test(fd);
69466b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak		write_einval_test(fd);
69566b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak		write_einval2_test(fd);
69666b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak		readfd_set_test(fd);
69766b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak		readfd_not_set_test(fd);
69866b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak		writefd_set_test(fd);
69966b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak		writefd_not_set_test(fd);
70066b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak		child_inherit_test(fd);
701a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak		overflow_select_test(fd);
702a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak		overflow_poll_test(fd);
703a5b38e4a71a511175eff1c056756dc7faeecff9bsubrata_modak		overflow_read_test(fd);
70466b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak
70566b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak		close(fd);
70666b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	}
70766b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak
70866b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	cleanup();
7092c28215423293e443469a07ae7011135d058b671Garrett Cooper
710e70b30b9873818be1593bf495fca3c990e2ef24cyaberauneya	tst_exit();
71166b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak}
71266b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak
71356207cec7732e09c216c751c0b5f88a242bacae6subrata_modakstatic void setup(void)
71466b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak{
7152c28215423293e443469a07ae7011135d058b671Garrett Cooper
71666b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	tst_sig(FORK, DEF_HANDLER, cleanup);
71766b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak
71866b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	if (tst_kvercmp(2, 6, 22) < 0)
71953740500924f6439623a8ac256b5be2d6c59ed1fGarrett Cooper		tst_brkm(TCONF, NULL, "2.6.22 or greater kernel required");
72066b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak
72194e24dd9d394224cc047130b670245bf52a1725csubrata_modak	tst_tmpdir();
72280678cff0facff97be57def1f58ab0403d5f7be3Garrett Cooper
72366b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak	TEST_PAUSE;
72466b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak}
72566b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak
7266aa2737d5de9256fcdb5b68a6aa6b12249005049yaberauneyastatic void cleanup(void)
72766b8447e2e5011798ce154a3ce3a3d1227056d93subrata_modak{
72894e24dd9d394224cc047130b670245bf52a1725csubrata_modak	tst_rmdir();
72902d739be8b3256ddc2a50075d15e7bc1ee769e30Garrett Cooper}
730