1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) International Business Machines Corp., 2001
4  * Copyright (C) 2017 Cyril Hrubis <chrubis@suse.cz>
5  * Ported to LTP: Wayne Boyer
6  */
7 
8 /*
9  * Check the functionality of the Alarm system call when the time input
10  * parameter is zero.
11  *
12  * Expected Result:
13  * The previously specified alarm request should be cancelled and the
14  * SIGALRM should not be received.
15  */
16 
17 #include <stdio.h>
18 #include <unistd.h>
19 #include <sys/types.h>
20 #include <errno.h>
21 #include <string.h>
22 #include <signal.h>
23 
24 #include "tst_test.h"
25 
26 static volatile int alarms_received = 0;
27 
sigproc(int sig)28 static void sigproc(int sig)
29 {
30 	if (sig == SIGALRM)
31 		alarms_received++;
32 }
33 
setup(void)34 static void setup(void)
35 {
36 	SAFE_SIGNAL(SIGALRM, sigproc);
37 }
38 
verify_alarm(void)39 static void verify_alarm(void)
40 {
41 	int ret;
42 
43 	alarm(2);
44 	sleep(1);
45 
46 	ret = alarm(0);
47 
48 	/* Wait for signal SIGALRM */
49 	sleep(2);
50 
51 	if (alarms_received)
52 		tst_res(TFAIL, "Received %i alarms", alarms_received);
53 	else
54 		tst_res(TPASS, "Received 0 alarms");
55 
56 	if (ret == 1)
57 		tst_res(TPASS, "alarm(0) returned 1");
58 	else
59 		tst_res(TFAIL, "alarm(0) returned %i, expected 1", ret);
60 }
61 
62 static struct tst_test test = {
63 	.setup = setup,
64 	.test_all = verify_alarm,
65 };
66