1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) International Business Machines  Corp., 2001
4f08c3bdfSopenharmony_ci *  07/2001 Ported by Wayne Boyer
5f08c3bdfSopenharmony_ci * Copyright (C) Cyril Hrubis <chrubis@suse.cz>
6f08c3bdfSopenharmony_ci */
7f08c3bdfSopenharmony_ci
8f08c3bdfSopenharmony_ci/*
9f08c3bdfSopenharmony_ci * Test Description:
10f08c3bdfSopenharmony_ci *  Verify that nanosleep() will be successful to suspend the execution
11f08c3bdfSopenharmony_ci *  of a process, returns after the receipt of a signal and writes the
12f08c3bdfSopenharmony_ci *  remaining sleep time into the structure.
13f08c3bdfSopenharmony_ci *
14f08c3bdfSopenharmony_ci *  This test also verifies that if the call is interrupted by a signal
15f08c3bdfSopenharmony_ci *  handler, nanosleep() returns -1, sets errno to EINTR.
16f08c3bdfSopenharmony_ci */
17f08c3bdfSopenharmony_ci
18f08c3bdfSopenharmony_ci#include <errno.h>
19f08c3bdfSopenharmony_ci
20f08c3bdfSopenharmony_ci#include "tst_test.h"
21f08c3bdfSopenharmony_ci#include "tst_timer.h"
22f08c3bdfSopenharmony_ci#include "tst_safe_macros.h"
23f08c3bdfSopenharmony_ci
24f08c3bdfSopenharmony_ci/*
25f08c3bdfSopenharmony_ci * Define here the "rem" precision in microseconds,
26f08c3bdfSopenharmony_ci * Various implementations will provide different
27f08c3bdfSopenharmony_ci * precisions. The -aa tree provides up to usec precision.
28f08c3bdfSopenharmony_ci * NOTE: all the trees that don't provide a precision of
29f08c3bdfSopenharmony_ci * the order of the microseconds are subject to an userspace
30f08c3bdfSopenharmony_ci * live lock condition with glibc under a flood of signals,
31f08c3bdfSopenharmony_ci * the "rem" field would never change without the increased
32f08c3bdfSopenharmony_ci * usec precision in the -aa tree.
33f08c3bdfSopenharmony_ci */
34f08c3bdfSopenharmony_ci#define USEC_PRECISION 250000	/* Error margin allowed in usec */
35f08c3bdfSopenharmony_ci
36f08c3bdfSopenharmony_cistatic void do_child(void)
37f08c3bdfSopenharmony_ci{
38f08c3bdfSopenharmony_ci	struct timespec timereq = {.tv_sec = 5, .tv_nsec = 9999};
39f08c3bdfSopenharmony_ci	struct timespec timerem, exp_rem;
40f08c3bdfSopenharmony_ci
41f08c3bdfSopenharmony_ci	tst_timer_start(CLOCK_MONOTONIC);
42f08c3bdfSopenharmony_ci	TEST(nanosleep(&timereq, &timerem));
43f08c3bdfSopenharmony_ci	tst_timer_stop();
44f08c3bdfSopenharmony_ci
45f08c3bdfSopenharmony_ci	if (TST_RET != -1) {
46f08c3bdfSopenharmony_ci		tst_res(TFAIL,
47f08c3bdfSopenharmony_ci			"nanosleep was not interrupted, returned %ld, expected -1",
48f08c3bdfSopenharmony_ci			TST_RET);
49f08c3bdfSopenharmony_ci		return;
50f08c3bdfSopenharmony_ci	}
51f08c3bdfSopenharmony_ci	if (TST_ERR != EINTR) {
52f08c3bdfSopenharmony_ci		tst_res(TFAIL | TTERRNO,
53f08c3bdfSopenharmony_ci			"nanosleep() failed, expected EINTR, got");
54f08c3bdfSopenharmony_ci		return;
55f08c3bdfSopenharmony_ci	}
56f08c3bdfSopenharmony_ci
57f08c3bdfSopenharmony_ci	tst_res(TPASS, "nanosleep() returned -1, set errno to EINTR");
58f08c3bdfSopenharmony_ci
59f08c3bdfSopenharmony_ci	if (tst_timespec_lt(timereq, tst_timer_elapsed()))
60f08c3bdfSopenharmony_ci		tst_brk(TFAIL, "nanosleep() slept more than timereq");
61f08c3bdfSopenharmony_ci
62f08c3bdfSopenharmony_ci	exp_rem = tst_timespec_diff(timereq, tst_timer_elapsed());
63f08c3bdfSopenharmony_ci
64f08c3bdfSopenharmony_ci	if (tst_timespec_abs_diff_us(timerem, exp_rem) > USEC_PRECISION) {
65f08c3bdfSopenharmony_ci		tst_res(TFAIL,
66f08c3bdfSopenharmony_ci			"nanosleep() remaining time %llius, expected %llius, diff %llius",
67f08c3bdfSopenharmony_ci			tst_timespec_to_us(timerem), tst_timespec_to_us(exp_rem),
68f08c3bdfSopenharmony_ci			tst_timespec_abs_diff_us(timerem, exp_rem));
69f08c3bdfSopenharmony_ci	} else {
70f08c3bdfSopenharmony_ci		tst_res(TPASS,
71f08c3bdfSopenharmony_ci			"nanosleep() slept for %llius, remaining time difference %llius",
72f08c3bdfSopenharmony_ci			tst_timer_elapsed_us(),
73f08c3bdfSopenharmony_ci			tst_timespec_abs_diff_us(timerem, exp_rem));
74f08c3bdfSopenharmony_ci	}
75f08c3bdfSopenharmony_ci}
76f08c3bdfSopenharmony_ci
77f08c3bdfSopenharmony_civoid run(void)
78f08c3bdfSopenharmony_ci{
79f08c3bdfSopenharmony_ci	pid_t cpid;
80f08c3bdfSopenharmony_ci
81f08c3bdfSopenharmony_ci	cpid = SAFE_FORK();
82f08c3bdfSopenharmony_ci	if (cpid == 0) {
83f08c3bdfSopenharmony_ci		do_child();
84f08c3bdfSopenharmony_ci	} else {
85f08c3bdfSopenharmony_ci		sleep(1);
86f08c3bdfSopenharmony_ci
87f08c3bdfSopenharmony_ci		SAFE_KILL(cpid, SIGINT);
88f08c3bdfSopenharmony_ci
89f08c3bdfSopenharmony_ci		tst_reap_children();
90f08c3bdfSopenharmony_ci	}
91f08c3bdfSopenharmony_ci}
92f08c3bdfSopenharmony_ci
93f08c3bdfSopenharmony_cistatic void sig_handler(int si LTP_ATTRIBUTE_UNUSED)
94f08c3bdfSopenharmony_ci{
95f08c3bdfSopenharmony_ci}
96f08c3bdfSopenharmony_ci
97f08c3bdfSopenharmony_cistatic void setup(void)
98f08c3bdfSopenharmony_ci{
99f08c3bdfSopenharmony_ci	tst_timer_check(CLOCK_MONOTONIC);
100f08c3bdfSopenharmony_ci	SAFE_SIGNAL(SIGINT, sig_handler);
101f08c3bdfSopenharmony_ci}
102f08c3bdfSopenharmony_ci
103f08c3bdfSopenharmony_cistatic struct tst_test test = {
104f08c3bdfSopenharmony_ci	.forks_child = 1,
105f08c3bdfSopenharmony_ci	.setup = setup,
106f08c3bdfSopenharmony_ci	.test_all = run,
107f08c3bdfSopenharmony_ci};
108