1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (C) 2015-2017 Cyril Hrubis <chrubis@suse.cz>
4f08c3bdfSopenharmony_ci */
5f08c3bdfSopenharmony_ci
6f08c3bdfSopenharmony_ci/*
7f08c3bdfSopenharmony_ci * Check that poll() timeouts correctly.
8f08c3bdfSopenharmony_ci */
9f08c3bdfSopenharmony_ci#include <errno.h>
10f08c3bdfSopenharmony_ci#include <fcntl.h>
11f08c3bdfSopenharmony_ci#include <sys/wait.h>
12f08c3bdfSopenharmony_ci#include <sys/poll.h>
13f08c3bdfSopenharmony_ci
14f08c3bdfSopenharmony_ci#include "tst_timer_test.h"
15f08c3bdfSopenharmony_ci
16f08c3bdfSopenharmony_cistatic int fds[2];
17f08c3bdfSopenharmony_ci
18f08c3bdfSopenharmony_ciint sample_fn(int clk_id, long long usec)
19f08c3bdfSopenharmony_ci{
20f08c3bdfSopenharmony_ci	unsigned int sleep_ms = usec / 1000;
21f08c3bdfSopenharmony_ci
22f08c3bdfSopenharmony_ci	struct pollfd pfds[] = {
23f08c3bdfSopenharmony_ci		{.fd = fds[0], .events = POLLIN}
24f08c3bdfSopenharmony_ci	};
25f08c3bdfSopenharmony_ci
26f08c3bdfSopenharmony_ci	tst_timer_start(clk_id);
27f08c3bdfSopenharmony_ci	TEST(poll(pfds, 1, sleep_ms));
28f08c3bdfSopenharmony_ci	tst_timer_stop();
29f08c3bdfSopenharmony_ci	tst_timer_sample();
30f08c3bdfSopenharmony_ci
31f08c3bdfSopenharmony_ci	if (TST_RET != 0) {
32f08c3bdfSopenharmony_ci		tst_res(TFAIL | TTERRNO, "poll() returned %li", TST_RET);
33f08c3bdfSopenharmony_ci		return 1;
34f08c3bdfSopenharmony_ci	}
35f08c3bdfSopenharmony_ci
36f08c3bdfSopenharmony_ci	return 0;
37f08c3bdfSopenharmony_ci}
38f08c3bdfSopenharmony_ci
39f08c3bdfSopenharmony_cistatic void setup(void)
40f08c3bdfSopenharmony_ci{
41f08c3bdfSopenharmony_ci	SAFE_PIPE(fds);
42f08c3bdfSopenharmony_ci}
43f08c3bdfSopenharmony_ci
44f08c3bdfSopenharmony_cistatic void cleanup(void)
45f08c3bdfSopenharmony_ci{
46f08c3bdfSopenharmony_ci	if (fds[0] > 0)
47f08c3bdfSopenharmony_ci		SAFE_CLOSE(fds[0]);
48f08c3bdfSopenharmony_ci
49f08c3bdfSopenharmony_ci	if (fds[1] > 0)
50f08c3bdfSopenharmony_ci		SAFE_CLOSE(fds[1]);
51f08c3bdfSopenharmony_ci}
52f08c3bdfSopenharmony_ci
53f08c3bdfSopenharmony_cistatic struct tst_test test = {
54f08c3bdfSopenharmony_ci	.scall = "poll()",
55f08c3bdfSopenharmony_ci	.sample = sample_fn,
56f08c3bdfSopenharmony_ci	.setup = setup,
57f08c3bdfSopenharmony_ci	.cleanup = cleanup,
58f08c3bdfSopenharmony_ci};
59