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 <errno.h>
17 #include <signal.h>
18 #include <time.h>
19 #include <unistd.h>
20 #include <dlfcn.h>
21 #include <stdio.h>
22 #include <pthread.h>
23
24 #include "test.h"
25 #define SIGNUM 40
26 #define SLEEPTIME 5
27 static int count = 0;
28
handler(int sig)29 void handler(int sig)
30 {
31 count++;
32
33 return;
34 }
35
36 /**
37 * @tc.name : timer_create_0100
38 * @tc.desc : create a per-process timer
39 * @tc.level : Level 0
40 */
timer_create_0100(void)41 void timer_create_0100(void)
42 {
43 struct sigevent sev;
44 timer_t timerid;
45
46 sev.sigev_notify = SIGEV_SIGNAL;
47 sev.sigev_signo = SIGNUM;
48 sev.sigev_value.sival_ptr = &timerid;
49
50 signal(SIGNUM, handler);
51
52 int result = timer_create(CLOCK_REALTIME, &sev, &timerid);
53 if (result != 0) {
54 t_error("%s failed: result = %d\n", __func__, result);
55 }
56
57 struct itimerspec its;
58 its.it_value.tv_sec = 1;
59 its.it_value.tv_nsec = 0;
60 its.it_interval.tv_sec = its.it_value.tv_sec;
61 its.it_interval.tv_nsec = its.it_value.tv_nsec;
62
63 result = timer_settime(timerid, 0, &its, NULL);
64 if (result != 0) {
65 t_error("%s failed: result = %d\n", __func__, result);
66 }
67
68 while (count <= 0) {
69 sleep(1);
70 }
71
72 result = timer_delete(timerid);
73 if (result != 0) {
74 t_error("%s failed: result = %d\n", __func__, result);
75 }
76 }
77
78 /**
79 * @tc.name : timer_create_0200
80 * @tc.desc : create a per-process timer with invalid parameters
81 * @tc.level : Level 2
82 */
timer_create_0200(void)83 void timer_create_0200(void)
84 {
85 errno = 0;
86 int result = timer_create(-1, NULL, NULL);
87 if (result == 0) {
88 t_error("%s failed: result = %d\n", __func__, result);
89 }
90
91 if (errno != EINVAL) {
92 t_error("%s failed: errno = %d\n", __func__, errno);
93 }
94 }
95
timer_handler(union sigval sv)96 void timer_handler(union sigval sv)
97 {
98 void *data = sv.sival_ptr;
99 printf("timer_handler handler: %p \n", data);
100 dlclose(data);
101 printf("dlclose data \n");
102 }
103
104 /**
105 * @tc.name : timer_create_0300
106 * @tc.desc : create a per-process timer with SIGEV_THREAD mode
107 * @tc.level : Level 2
108 */
timer_create_0300(void)109 void timer_create_0300(void)
110 {
111 timer_t timerid;
112 struct sigevent sev;
113 struct itimerspec its;
114 int ret;
115
116 void* handle = dlopen("/data/local/tmp/libtls_timer.so", RTLD_LAZY);
117 if (handle == NULL) {
118 t_error("%s dlopen error \n", __func__);
119 return;
120 }
121
122 sev.sigev_notify = SIGEV_THREAD;
123 sev.sigev_notify_function = timer_handler;
124 sev.sigev_notify_attributes = NULL;
125 sev.sigev_value.sival_ptr = handle;
126
127 ret = timer_create(CLOCK_REALTIME, &sev, &timerid);
128 if (ret == -1) {
129 t_error("%s timer_create failed \n", __func__);
130 return;
131 }
132 printf("timer_create succeed: %lu \n", (unsigned long)timerid);
133
134 its.it_value.tv_sec = 1;
135 its.it_value.tv_nsec = 0;
136 its.it_interval.tv_sec = 0;
137 its.it_interval.tv_nsec = 0;
138
139 ret = timer_settime(timerid, 0, &its, NULL);
140 if (ret == -1) {
141 t_error("%s timer_settime failed \n", __func__);
142 return;
143 }
144
145 for (int i = 0; i < SLEEPTIME; i++) {
146 printf("main running waiting for callback: %d \n", i);
147 sleep(1);
148 }
149
150 ret = timer_delete(timerid);
151 if (ret == -1) {
152 t_error("%s timer_delete failed \n", __func__);
153 return;
154 }
155 }
156
main(int argc,char * argv[])157 int main(int argc, char *argv[])
158 {
159 timer_create_0100();
160 timer_create_0200();
161 timer_create_0300();
162
163 return t_status;
164 }
165