1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) International Business Machines Corp., 2001,2005
4  * Ported to LTP: Wayne Boyer
5  *	06/2005 Test for alarm cleanup by Amos Waterland
6  *
7  * Copyright (c) 2018 Cyril Hrubis <chrubis@suse.cz>
8  */
9 
10 /*
11  * Test Description:
12  *  The return value of the alarm system call should be equal to the
13  *  amount previously remaining in the alarm clock.
14  *  A SIGALRM signal should be received after the specified amount of
15  *  time has elapsed.
16  */
17 
18 #include "tst_test.h"
19 
20 static volatile int alarms_fired = 0;
21 
run(void)22 static void run(void)
23 {
24 	unsigned int ret;
25 
26 	alarms_fired = 0;
27 
28 	ret = alarm(10);
29 	if (ret)
30 		tst_res(TFAIL, "alarm() returned non-zero");
31 	else
32 		tst_res(TPASS, "alarm() returned zero");
33 
34 	sleep(1);
35 
36 	ret = alarm(1);
37 	if (ret == 9)
38 		tst_res(TPASS, "alarm() returned remainder correctly");
39 	else
40 		tst_res(TFAIL, "alarm() returned wrong remained %u", ret);
41 
42 	sleep(2);
43 
44 	if (alarms_fired == 1)
45 		tst_res(TPASS, "alarm handler fired once");
46 	else
47 		tst_res(TFAIL, "alarm handler filred %u times", alarms_fired);
48 }
49 
sighandler(int sig)50 static void sighandler(int sig)
51 {
52 	if (sig == SIGALRM)
53 		alarms_fired++;
54 }
55 
setup(void)56 static void setup(void)
57 {
58 	SAFE_SIGNAL(SIGALRM, sighandler);
59 }
60 
61 static struct tst_test test = {
62 	.test_all = run,
63 	.setup = setup,
64 };
65