1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) 2000 Silicon Graphics, Inc.  All Rights Reserved.
4f08c3bdfSopenharmony_ci * Author: Billy Jean Horne
5f08c3bdfSopenharmony_ci * Copyright (c) Linux Test Project, 2009-2022
6f08c3bdfSopenharmony_ci */
7f08c3bdfSopenharmony_ci
8f08c3bdfSopenharmony_ci/*\
9f08c3bdfSopenharmony_ci * [Description]
10f08c3bdfSopenharmony_ci *
11f08c3bdfSopenharmony_ci * Verify that alarm() returns:
12f08c3bdfSopenharmony_ci *
13f08c3bdfSopenharmony_ci * - zero when there was no previously scheduled alarm
14f08c3bdfSopenharmony_ci * - number of seconds remaining until any previously scheduled alarm
15f08c3bdfSopenharmony_ci */
16f08c3bdfSopenharmony_ci
17f08c3bdfSopenharmony_ci#include "tst_test.h"
18f08c3bdfSopenharmony_ci
19f08c3bdfSopenharmony_cistatic volatile int alarms_received;
20f08c3bdfSopenharmony_ci
21f08c3bdfSopenharmony_cistatic struct tcase {
22f08c3bdfSopenharmony_ci	char *str;
23f08c3bdfSopenharmony_ci	unsigned int sec;
24f08c3bdfSopenharmony_ci} tcases[] = {
25f08c3bdfSopenharmony_ci	{"INT_MAX", INT_MAX},
26f08c3bdfSopenharmony_ci	{"UINT_MAX/2", UINT_MAX/2},
27f08c3bdfSopenharmony_ci	{"UINT_MAX/4", UINT_MAX/4},
28f08c3bdfSopenharmony_ci};
29f08c3bdfSopenharmony_ci
30f08c3bdfSopenharmony_cistatic void verify_alarm(unsigned int n)
31f08c3bdfSopenharmony_ci{
32f08c3bdfSopenharmony_ci	struct tcase *tc = &tcases[n];
33f08c3bdfSopenharmony_ci
34f08c3bdfSopenharmony_ci	alarms_received = 0;
35f08c3bdfSopenharmony_ci
36f08c3bdfSopenharmony_ci	TST_EXP_PASS(alarm(tc->sec), "alarm(%u)", tc->sec);
37f08c3bdfSopenharmony_ci
38f08c3bdfSopenharmony_ci	TST_EXP_VAL(alarm(0), tc->sec);
39f08c3bdfSopenharmony_ci
40f08c3bdfSopenharmony_ci	if (alarms_received == 1) {
41f08c3bdfSopenharmony_ci		tst_res(TFAIL, "alarm(%u) delivered SIGALRM for seconds value %s",
42f08c3bdfSopenharmony_ci				tc->sec, tc->str);
43f08c3bdfSopenharmony_ci	}
44f08c3bdfSopenharmony_ci}
45f08c3bdfSopenharmony_ci
46f08c3bdfSopenharmony_cistatic void sighandler(int sig)
47f08c3bdfSopenharmony_ci{
48f08c3bdfSopenharmony_ci	if (sig == SIGALRM)
49f08c3bdfSopenharmony_ci		alarms_received++;
50f08c3bdfSopenharmony_ci}
51f08c3bdfSopenharmony_ci
52f08c3bdfSopenharmony_cistatic void setup(void)
53f08c3bdfSopenharmony_ci{
54f08c3bdfSopenharmony_ci	SAFE_SIGNAL(SIGALRM, sighandler);
55f08c3bdfSopenharmony_ci}
56f08c3bdfSopenharmony_ci
57f08c3bdfSopenharmony_cistatic struct tst_test test = {
58f08c3bdfSopenharmony_ci	.tcnt = ARRAY_SIZE(tcases),
59f08c3bdfSopenharmony_ci	.test = verify_alarm,
60f08c3bdfSopenharmony_ci	.setup = setup,
61f08c3bdfSopenharmony_ci};
62