1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <signal.h>
17 #include <sys/time.h>
18 #include "functionalext.h"
19
20 static int count = 0;
expireTimer(int signo)21 void expireTimer(int signo)
22 {
23 struct itimerval val;
24 count++;
25 if (count > 1) {
26 getitimer(signo, &val);
27 val.it_value.tv_sec = 0;
28 val.it_value.tv_usec = 0;
29 setitimer(signo, &val, NULL);
30 count = 0;
31 }
32 }
33
signalHandler(int signo)34 void signalHandler(int signo)
35 {
36 switch (signo) {
37 case SIGALRM:
38 printf("catch a SIGALRM signal\n");
39 expireTimer(signo);
40 break;
41 case SIGVTALRM:
42 printf("catch a SIGVTALRM signal\n");
43 break;
44 case SIGPROF:
45 printf("catch a SIGPROF signal\n");
46 break;
47 default:
48 printf("no signal\n");
49 break;
50 }
51 }
52
53 /**
54 * @tc.name : getitimer_0100
55 * @tc.desc : Verify the value of the interval timer (parameter is ITIMER_REAL)
56 * @tc.level : Level 0
57 */
getitimer_0100(void)58 void getitimer_0100(void)
59 {
60 signal(SIGALRM, signalHandler);
61 struct itimerval old;
62 int result = getitimer(ITIMER_REAL, &old);
63 EXPECT_EQ("getitimer_0100", result, 0);
64 }
65
66 /**
67 * @tc.name : getitimer_0200
68 * @tc.desc : Verify the value of the interval timer (parameter is ITIMER_VIRTUAL)
69 * @tc.level : Level 0
70 */
getitimer_0200(void)71 void getitimer_0200(void)
72 {
73 signal(SIGALRM, signalHandler);
74 struct itimerval old;
75 int result = getitimer(ITIMER_VIRTUAL, &old);
76 EXPECT_EQ("getitimer_0200", result, 0);
77 }
78
79 /**
80 * @tc.name : getitimer_0300
81 * @tc.desc : Verify the value of the interval timer (parameter is ITIMER_PROF)
82 * @tc.level : Level 0
83 */
getitimer_0300(void)84 void getitimer_0300(void)
85 {
86 signal(SIGALRM, signalHandler);
87 struct itimerval old;
88 int result = getitimer(ITIMER_PROF, &old);
89 EXPECT_EQ("getitimer_0300", result, 0);
90 }
91
main(void)92 int main(void)
93 {
94 getitimer_0100();
95 getitimer_0200();
96 getitimer_0300();
97
98 return t_status;
99 }