• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  * - zero when there was no previously scheduled alarm
13  * - number of seconds remaining until any previously scheduled alarm
14  */
15 
16 #include "tst_test.h"
17 
18 static volatile int alarms_received;
19 
20 static struct tcase {
21 	char *str;
22 	unsigned int sec;
23 } tcases[] = {
24 	{"INT_MAX", INT_MAX},
25 	{"UINT_MAX/2", UINT_MAX/2},
26 	{"UINT_MAX/4", UINT_MAX/4},
27 };
28 
verify_alarm(unsigned int n)29 static void verify_alarm(unsigned int n)
30 {
31 	struct tcase *tc = &tcases[n];
32 
33 	alarms_received = 0;
34 
35 	TST_EXP_PASS(alarm(tc->sec), "alarm(%u)", tc->sec);
36 
37 	TST_EXP_VAL(alarm(0), tc->sec);
38 
39 	if (alarms_received == 1) {
40 		tst_res(TFAIL, "alarm(%u) delivered SIGALRM for seconds value %s",
41 				tc->sec, tc->str);
42 	}
43 }
44 
sighandler(int sig)45 static void sighandler(int sig)
46 {
47 	if (sig == SIGALRM)
48 		alarms_received++;
49 }
50 
setup(void)51 static void setup(void)
52 {
53 	SAFE_SIGNAL(SIGALRM, sighandler);
54 }
55 
56 static struct tst_test test = {
57 	.tcnt = ARRAY_SIZE(tcases),
58 	.test = verify_alarm,
59 	.setup = setup,
60 };
61