1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Copyright (C) 2015-2017 Cyril Hrubis <chrubis@suse.cz>
4 */
5
6/*
7 * Check that select() timeouts correctly.
8 */
9#include <unistd.h>
10#include <errno.h>
11#include <sys/time.h>
12#include <sys/types.h>
13#include <fcntl.h>
14
15#include "tst_timer_test.h"
16
17#include "select_var.h"
18
19static int fds[2];
20
21static int sample_fn(int clk_id, long long usec)
22{
23	struct timeval timeout = tst_us_to_timeval(usec);
24	fd_set sfds;
25
26	FD_ZERO(&sfds);
27
28	FD_SET(fds[0], &sfds);
29
30	tst_timer_start(clk_id);
31	TEST(do_select(1, &sfds, NULL, NULL, &timeout));
32	tst_timer_stop();
33	tst_timer_sample();
34
35	if (TST_RET != 0) {
36		tst_res(TFAIL | TTERRNO, "select() returned %li", TST_RET);
37		return 1;
38	}
39
40	return 0;
41}
42
43static void setup(void)
44{
45	select_info();
46
47	SAFE_PIPE(fds);
48}
49
50static void cleanup(void)
51{
52	if (fds[0] > 0)
53		SAFE_CLOSE(fds[0]);
54
55	if (fds[1] > 0)
56		SAFE_CLOSE(fds[1]);
57}
58
59static struct tst_test test = {
60	.scall = "select()",
61	.sample = sample_fn,
62	.setup = setup,
63	.test_variants = TEST_VARIANTS,
64	.cleanup = cleanup,
65};
66