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