1/*
2 * Copyright (c) 2014 Fujitsu Ltd.
3 * Author: Zeng Linggang <zenglg.jy@cn.fujitsu.com>
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 the
13 * GNU Library 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 *
19 */
20/*
21 * Test Description:
22 *  Verify that,
23 *   1. pselect() fails with -1 return value and sets errno to EBADF
24 *      if a file descriptor that was already closed.
25 *   2. pselect() fails with -1 return value and sets errno to EINVAL
26 *      if nfds was negative.
27 *   3. pselect() fails with -1 return value and sets errno to EINVAL
28 *      if the value contained within timeout was invalid.
29 */
30
31#include <errno.h>
32#include "test.h"
33#include "safe_macros.h"
34
35TCID_DEFINE(pselect02);
36
37static fd_set read_fds;
38static struct timespec time_buf;
39
40static struct test_case_t {
41	int nfds;
42	fd_set *readfds;
43	struct timespec *timeout;
44	int exp_errno;
45} test_cases[] = {
46	{128, &read_fds, NULL, EBADF},
47	{-1, NULL, NULL, EINVAL},
48	{128, NULL, &time_buf, EINVAL},
49};
50
51int TST_TOTAL = ARRAY_SIZE(test_cases);
52
53static void setup(void);
54static void cleanup(void);
55static void pselect_verify(const struct test_case_t *);
56
57int main(int argc, char **argv)
58{
59	int lc, i;
60
61	tst_parse_opts(argc, argv, NULL, NULL);
62
63	setup();
64
65	for (lc = 0; TEST_LOOPING(lc); lc++) {
66		tst_count = 0;
67		for (i = 0; i < TST_TOTAL; i++)
68			pselect_verify(&test_cases[i]);
69	}
70
71	cleanup();
72	tst_exit();
73}
74
75static void setup(void)
76{
77	int fd;
78
79	tst_sig(NOFORK, DEF_HANDLER, cleanup);
80
81	TEST_PAUSE;
82
83	tst_tmpdir();
84
85	fd = SAFE_OPEN(cleanup, "test_file", O_RDWR | O_CREAT, 0777);
86
87	FD_ZERO(&read_fds);
88	FD_SET(fd, &read_fds);
89
90	SAFE_CLOSE(cleanup, fd);
91
92	time_buf.tv_sec = -1;
93	time_buf.tv_nsec = 0;
94}
95
96static void pselect_verify(const struct test_case_t *test)
97{
98	TEST(pselect(test->nfds, test->readfds, NULL, NULL, test->timeout,
99	     NULL));
100
101	if (TEST_RETURN != -1) {
102		tst_resm(TFAIL, "pselect() succeeded unexpectedly");
103		return;
104	}
105
106	if (TEST_ERRNO == test->exp_errno) {
107		tst_resm(TPASS | TTERRNO, "pselect() failed as expected");
108	} else {
109		tst_resm(TFAIL | TTERRNO,
110			 "pselect() failed unexpectedly; expected: %d - %s",
111			 test->exp_errno, strerror(test->exp_errno));
112	}
113}
114
115static void cleanup(void)
116{
117	tst_rmdir();
118}
119