1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
3 * Author: Billy Jean Horne
4 *
5 * Test Description:
6 * 1) alarm() return UINT_MAX if seconds is UINT_MAX.
7 * 2) alarm() return UINT_MAX/2 if seconds is UINT_MAX/2.
8 * 3) alarm() return UINT_MAX/4 if seconds is UINT_MAX/4.
9 */
10
11 #include <unistd.h>
12 #include <errno.h>
13 #include <sys/signal.h>
14 #include <limits.h>
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 unsigned int ret;
33
34 alarms_received = 0;
35
36 ret = alarm(tc->sec);
37 if (ret != 0) {
38 tst_res(TFAIL,
39 "alarm(%u) returned %ld, when 0 was ",
40 tc->sec, TST_RET);
41 return;
42 }
43
44 TEST(alarm(0));
45 if (alarms_received == 1) {
46 tst_res(TFAIL,
47 "alarm(%u) signal was received for value %s",
48 tc->sec, tc->str);
49 return;
50 }
51
52 if (tc->sec != TST_RET) {
53 tst_res(TFAIL,
54 "alarm(%u) returned %ld as unexpected",
55 tc->sec, TST_RET);
56 return;
57 }
58
59 tst_res(TPASS,
60 "alarm(%u) returned %ld as expected "
61 "for value %s",
62 tc->sec, TST_RET, tc->str);
63 }
64
sighandler(int sig)65 static void sighandler(int sig)
66 {
67 if (sig == SIGALRM)
68 alarms_received++;
69 }
70
setup(void)71 static void setup(void)
72 {
73 SAFE_SIGNAL(SIGALRM, sighandler);
74 }
75
76 static struct tst_test test = {
77 .tcnt = ARRAY_SIZE(tcases),
78 .test = verify_alarm,
79 .setup = setup,
80 };
81