• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <stdio.h>
18 #include <string.h>
19 #include <sys/time.h>
20 #include "test.h"
21 
22 extern int __setitimer_time64(int, const struct itimerval *__restrict, struct itimerval *__restrict);
23 
24 static int count = 0;
25 
expireTimer(int signo)26 void expireTimer(int signo)
27 {
28     struct itimerval val;
29     count++;
30     if (count > 1) {
31         getitimer(signo, &val);
32         val.it_value.tv_sec = 0;
33         val.it_value.tv_usec = 0;
34         __setitimer_time64(signo, &val, NULL);
35         count = 0;
36     }
37 }
38 
signalHandler(int signo)39 void signalHandler(int signo)
40 {
41     switch (signo) {
42         case SIGALRM:
43             printf("catch a SIGALRM signal\n");
44             expireTimer(signo);
45             break;
46         case SIGVTALRM:
47             printf("catch a SIGVTALRM signal\n");
48             break;
49         case SIGPROF:
50             printf("catch a SIGPROF signal\n");
51             break;
52         default:
53             printf("no signal\n");
54             break;
55     }
56 }
57 
58 /**
59  * @tc.name      : setitimer_time64_0100
60  * @tc.desc      : Set value of an interval timer
61  * @tc.level     : Level 0
62  */
setitimer_time64_0100(void)63 void setitimer_time64_0100(void)
64 {
65     signal(SIGALRM, signalHandler);
66     struct itimerval it;
67     memset(&it, 0, sizeof(it));
68 
69     it.it_value.tv_sec = 0;
70     it.it_value.tv_usec = 100000;
71     it.it_interval.tv_sec = 0;
72     it.it_interval.tv_usec = 100000;
73 
74     if (__setitimer_time64(ITIMER_REAL, &it, NULL)) {
75         t_error("%s set timer failed", __func__);
76     }
77 }
78 
main(int argc,char * argv[])79 int main(int argc, char *argv[])
80 {
81     setitimer_time64_0100();
82     return t_status;
83 }