1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2016 Fujitsu Ltd.
4 * Author: Guangwen Feng <fenggw-fnst@cn.fujitsu.com>
5 * Copyright (c) 2021 Xie Ziyao <xieziyao@huawei.com>
6 */
7
8 /*\
9 * [Description]
10 *
11 * Basic test for epoll_wait:
12 *
13 * - epoll_wait fails with EBADF if epfd is not a valid file descriptor.
14 * - epoll_wait fails with EINVAL if epfd is not an epoll file descriptor.
15 * - epoll_wait fails with EINVAL if maxevents is less than zero.
16 * - epoll_wait fails with EINVAL if maxevents is equal to zero.
17 * - epoll_wait fails with EFAULT if the memory area pointed to by events is
18 * not accessible with write permissions.
19 */
20
21 #include <sys/mman.h>
22 #include <sys/epoll.h>
23
24 #include "tst_test.h"
25
26 static struct epoll_event epevs[1] = {
27 {.events = EPOLLOUT}
28 };
29
30 static struct epoll_event *ev_rdonly, *ev_rdwr = epevs;
31 static int fds[2], epfd, inv_epfd, bad_epfd = -1;
32
33 static struct test_case_t {
34 int *epfd;
35 struct epoll_event **ev;
36 int maxevents;
37 int exp_errno;
38 const char *desc;
39 } tc[] = {
40 {&bad_epfd, &ev_rdwr, 1, EBADF, "epfd is not a valid fd"},
41 {&inv_epfd, &ev_rdwr, 1, EINVAL, "epfd is not an epoll fd"},
42 {&epfd, &ev_rdwr, -1, EINVAL, "maxevents is less than zero"},
43 {&epfd, &ev_rdwr, 0, EINVAL, "maxevents is equal to zero"},
44 {&epfd, &ev_rdonly, 1, EFAULT, "events has no write permissions"}
45 };
46
setup(void)47 static void setup(void)
48 {
49 ev_rdonly = SAFE_MMAP(NULL, getpagesize(), PROT_READ,
50 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
51 SAFE_PIPE(fds);
52
53 epfd = epoll_create(1);
54 if (epfd == -1)
55 tst_brk(TBROK | TERRNO, "epoll_create()");
56
57 epevs[0].data.fd = fds[1];
58
59 if (epoll_ctl(epfd, EPOLL_CTL_ADD, fds[1], &epevs[0]))
60 tst_brk(TBROK | TERRNO, "epoll_ctl(..., EPOLL_CTL_ADD, ...)");
61 }
62
verify_epoll_wait(unsigned int n)63 static void verify_epoll_wait(unsigned int n)
64 {
65 TST_EXP_FAIL(epoll_wait(*tc[n].epfd, *tc[n].ev, tc[n].maxevents, -1),
66 tc[n].exp_errno, "epoll_wait() %s", tc[n].desc);
67 }
68
cleanup(void)69 static void cleanup(void)
70 {
71 if (epfd > 0)
72 SAFE_CLOSE(epfd);
73
74 if (fds[0] > 0)
75 SAFE_CLOSE(fds[0]);
76
77 if (fds[1] > 0)
78 SAFE_CLOSE(fds[1]);
79 }
80
81 static struct tst_test test = {
82 .setup = setup,
83 .cleanup = cleanup,
84 .test = verify_epoll_wait,
85 .tcnt = ARRAY_SIZE(tc),
86 };
87