xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/alarm/alarm05.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) International Business Machines Corp., 2001,2005
4*49cdfc7eSAndroid Build Coastguard Worker  * Ported to LTP: Wayne Boyer
5*49cdfc7eSAndroid Build Coastguard Worker  *	06/2005 Test for alarm cleanup by Amos Waterland
6*49cdfc7eSAndroid Build Coastguard Worker  *
7*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2018 Cyril Hrubis <[email protected]>
8*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) Linux Test Project, 2006-2022
9*49cdfc7eSAndroid Build Coastguard Worker  */
10*49cdfc7eSAndroid Build Coastguard Worker 
11*49cdfc7eSAndroid Build Coastguard Worker /*\
12*49cdfc7eSAndroid Build Coastguard Worker  * [Description]
13*49cdfc7eSAndroid Build Coastguard Worker  *
14*49cdfc7eSAndroid Build Coastguard Worker  *  The return value of the alarm system call should be equal to the
15*49cdfc7eSAndroid Build Coastguard Worker  *  amount previously remaining in the alarm clock.
16*49cdfc7eSAndroid Build Coastguard Worker  *  A SIGALRM signal should be received after the specified amount of
17*49cdfc7eSAndroid Build Coastguard Worker  *  time has elapsed.
18*49cdfc7eSAndroid Build Coastguard Worker  */
19*49cdfc7eSAndroid Build Coastguard Worker 
20*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
21*49cdfc7eSAndroid Build Coastguard Worker 
22*49cdfc7eSAndroid Build Coastguard Worker static volatile int alarms_fired;
23*49cdfc7eSAndroid Build Coastguard Worker 
run(void)24*49cdfc7eSAndroid Build Coastguard Worker static void run(void)
25*49cdfc7eSAndroid Build Coastguard Worker {
26*49cdfc7eSAndroid Build Coastguard Worker 	alarms_fired = 0;
27*49cdfc7eSAndroid Build Coastguard Worker 
28*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_PASS(alarm(10));
29*49cdfc7eSAndroid Build Coastguard Worker 	sleep(1);
30*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_VAL(alarm(1), 9);
31*49cdfc7eSAndroid Build Coastguard Worker 	sleep(2);
32*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_EQ_LU(alarms_fired, 1);
33*49cdfc7eSAndroid Build Coastguard Worker }
34*49cdfc7eSAndroid Build Coastguard Worker 
sighandler(int sig)35*49cdfc7eSAndroid Build Coastguard Worker static void sighandler(int sig)
36*49cdfc7eSAndroid Build Coastguard Worker {
37*49cdfc7eSAndroid Build Coastguard Worker 	if (sig == SIGALRM)
38*49cdfc7eSAndroid Build Coastguard Worker 		alarms_fired++;
39*49cdfc7eSAndroid Build Coastguard Worker }
40*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)41*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
42*49cdfc7eSAndroid Build Coastguard Worker {
43*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SIGNAL(SIGALRM, sighandler);
44*49cdfc7eSAndroid Build Coastguard Worker }
45*49cdfc7eSAndroid Build Coastguard Worker 
46*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
47*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = run,
48*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
49*49cdfc7eSAndroid Build Coastguard Worker };
50