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 // Solve the build error of this test code when importing the header file <sys/epoll.h>
17 #ifndef _EPOLL_CREATE1_TEST
18 #define _EPOLL_CREATE1_TEST
19 #if defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
20 #undef _GNU_SOURCE
21 #undef _BSD_SOURCE
22 #endif
23 #endif
24
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <stdbool.h>
28 #include <sys/epoll.h>
29 #include "test.h"
30
close_on_exec(int fd,bool close_on_exec)31 void close_on_exec(int fd, bool close_on_exec)
32 {
33 int flags = fcntl(fd, F_GETFD);
34 if (flags == -1) {
35 t_error("%s fcntl failed\n", __func__);
36 }
37 if ((close_on_exec ? FD_CLOEXEC : 0) != (flags & FD_CLOEXEC)) {
38 t_error("%s failed, expect equal\n", __func__);
39 }
40 }
41
42 /**
43 * @tc.name : epoll_create1_0100
44 * @tc.desc : Returns a file descriptor referring to the new epoll instance
45 * @tc.level : Level 0
46 */
epoll_create1_0100(void)47 void epoll_create1_0100(void)
48 {
49 errno = 0;
50
51 int fd;
52 fd = epoll_create1(0);
53 close_on_exec(fd, false);
54 close(fd);
55
56 fd = epoll_create1(EPOLL_CLOEXEC);
57 close_on_exec(fd, true);
58 close(fd);
59 }
60
main(int argc,char * argv[])61 int main(int argc, char *argv[])
62 {
63 epoll_create1_0100();
64 return t_status;
65 }
66
67 #ifdef _EPOLL_CREATE1_TEST
68 #undef _EPOLL_CREATE1_TEST
69 #endif