Skip to content

Commit 16e934e

Browse files
userzlibumem
authored andcommitted
Initial commit for epoll
1 parent d8a9d82 commit 16e934e

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

qiling/os/posix/const.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,3 +1090,21 @@ class qnx_mmap_prot_flags(QlPrettyFlag):
10901090
# https://docs.huihoo.com/doxygen/linux/kernel/3.7/include_2uapi_2linux_2fcntl_8h.html
10911091
AT_SYMLINK_NOFOLLOW = 0x100
10921092
AT_FDCWD = 0xffffff9c # -0n100 in 2's complement
1093+
# epoll syscall
1094+
EPOLL_OPS = {0x001: "EPOLL_CTL_ADD", 0x002: "EPOLL_CTL_DEL", 0x003: "EPOLL_CTL_MOD"}
1095+
1096+
EPOLLIN = 0x001
1097+
EPOLLPRI = 0x002
1098+
EPOLLOUT = 0x004
1099+
EPOLLRDNORM = 0x040
1100+
EPOLLRDBAND = 0x080
1101+
EPOLLWRNORM = 0x100
1102+
EPOLLWRBAND = 0x200
1103+
EPOLLMSG = 0x400
1104+
EPOLLERR = 0x008
1105+
EPOLLHUP = 0x010
1106+
EPOLLRDHUP = 0x2000
1107+
EPOLLEXCLUSIVE = 1 << 28
1108+
EPOLLWAKEUP = 1 << 29
1109+
EPOLLONESHOT = 1 << 30
1110+
EPOLLET = 1 << 31

qiling/os/posix/syscall/epoll.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ def is_present(self, fd):
5959
return 1
6060

6161

62+
<<<<<<< HEAD
6263
"""
6364
Recursively checks each epoll instance's 'watched'
6465
fds for an instance of epoll being watched.
@@ -69,6 +70,10 @@ def is_present(self, fd):
6970

7071
def check_epoll_depth(ql_fd_list, epolls_list, depth):
7172
if depth == 7:
73+
=======
74+
def check_epoll_depth(ql_fd_list, epolls_list, depth):
75+
if depth == 6:
76+
>>>>>>> 0aef533c (Initial commit for epoll)
7277
return 1
7378
new_epolls_list = []
7479
flag = 0
@@ -83,6 +88,7 @@ def check_epoll_depth(ql_fd_list, epolls_list, depth):
8388
return 0
8489

8590

91+
<<<<<<< HEAD
8692
"""
8793
Modify an existing epoll
8894
man 7 epoll for more details
@@ -92,6 +98,9 @@ def check_epoll_depth(ql_fd_list, epolls_list, depth):
9298
def ql_syscall_epoll_ctl(
9399
ql: qiling.Qiling, epfd: int, op: int, fd: int, event: POINTER
94100
):
101+
=======
102+
def ql_epoll_ctl(ql: qiling.Qiling, epfd: int, op: int, fd: int, event: POINTER):
103+
>>>>>>> 0aef533c (Initial commit for epoll)
95104
# Basic sanity checks first
96105
if event != 0:
97106
ql_event = ql.unpack32(ql.mem.read(event, 4)) # events list is uint32_t
@@ -148,6 +157,7 @@ def ql_syscall_epoll_ctl(
148157
if epoll_obj is None or fd_obj is None:
149158
# epfd or fd is not a valid file descriptor.
150159
return EBADF
160+
<<<<<<< HEAD
151161
if epfd == fd: # epoll can't monitor itself
152162
return EINVAL
153163
if epoll_obj.fileno() == fd:
@@ -181,17 +191,54 @@ def ql_syscall_epoll_ctl(
181191
if op & EPOLLEXCLUSIVE and fd in epoll_obj.get_fds:
182192
return EINVAL # EINVAL op was EPOLL_CTL_MOD and the EPOLLEXCLUSIVE flag has previously been applied to this epfd, fd pair.
183193
epoll_parent_obj.set_eventmask(ql_event)
194+
=======
195+
if epfd == fd:
196+
return EINVAL
197+
if epoll_obj.fileno() == fd:
198+
return ELOOP # ELOOP ...or a nesting depth of epoll instances greater than 5.
199+
match ql_op:
200+
case "EPOLL_CTL_ADD":
201+
if epoll_parent_obj.is_present(
202+
fd
203+
): # can't add an fd that's already being waited on
204+
return EEXIST # op was EPOLL_CTL_ADD, and the supplied file descriptor fd is already registered with this epoll instance.
205+
epoll_parent_obj.monitor_fd(
206+
fd, ql_event
207+
) # add to list of fds to be monitored with per-fd eventmask
208+
# register will actual epoll instance
209+
# and add eventmask accordingly
210+
case "EPOLL_CTL_DEL":
211+
if not epoll_parent_obj.is_present(
212+
fd
213+
): # op was EPOLL_CTL_MOD or EPOLL_CTL_DEL, and fd is not registered with this epoll instance.
214+
return ENOENT
215+
epoll_parent_obj.delist_fd(fd) # remove from fds list and do so in the
216+
# underlying epoll instance
217+
case "EPOLL_CTL_MOD":
218+
if not epoll_parent_obj.is_present(
219+
fd
220+
): # ENOENT op was EPOLL_CTL_MOD or EPOLL_CTL_DEL, and fd is not registered with this epoll instance
221+
return ENOENT
222+
# EINVAL op was EPOLL_CTL_MOD and events included EPOLLEXCLUSIVE.
223+
if op & EPOLLEXCLUSIVE and fd in epoll_obj.get_fds:
224+
return EINVAL # EINVAL op was EPOLL_CTL_MOD and the EPOLLEXCLUSIVE flag has previously been applied to this epfd, fd pair.
225+
epoll_parent_obj.set_eventmask(ql_event)
226+
>>>>>>> 0aef533c (Initial commit for epoll)
184227

185228
return 0
186229

187230

231+
<<<<<<< HEAD
188232
"""
189233
Wait on an existing epoll for events specified
190234
earlier. man 7 epoll_wait for more info
191235
"""
192236

193237

194238
def ql_syscall_epoll_wait(
239+
=======
240+
def ql_epoll_wait(
241+
>>>>>>> 0aef533c (Initial commit for epoll)
195242
ql: qiling.Qiling, epfd: int, epoll_events: POINTER, maxevents: int, timeout: int
196243
):
197244
if maxevents <= 0:
@@ -244,7 +291,11 @@ def ql_syscall_epoll_wait(
244291
"""
245292

246293

294+
<<<<<<< HEAD
247295
def ql_syscall_epoll_create1(ql: qiling.Qiling, flags: int):
296+
=======
297+
def ql_epoll_create1(ql: qiling.Qiling, flags: int):
298+
>>>>>>> 0aef533c (Initial commit for epoll)
248299
if flags != select.EPOLL_CLOEXEC and flags != 0:
249300
return EINVAL
250301
ret = select.epoll(sizehint=-1, flags=flags)
@@ -256,15 +307,28 @@ def ql_syscall_epoll_create1(ql: qiling.Qiling, flags: int):
256307

257308
"""
258309
Almost identical to above, but can't simply wrap
310+
<<<<<<< HEAD
259311
because of the slightly different prototype
260312
"""
261313

262314

263315
def ql_syscall_epoll_create(ql: qiling.Qiling, size: int):
316+
=======
317+
because of the slightly different args and the different
318+
syscall number
319+
"""
320+
321+
322+
def ql_epoll_create(ql: qiling.Qiling, size: int):
323+
>>>>>>> 0aef533c (Initial commit for epoll)
264324
if size < 0:
265325
return EINVAL
266326
ret = select.epoll(sizehint=size, flags=0)
267327
fd = ret.fileno()
268328
ql_obj = QlEpollObj(ret)
269329
ql.os.fd[fd] = ql_obj
330+
<<<<<<< HEAD
331+
return fd
332+
=======
270333
return fd
334+
>>>>>>> 0aef533c (Initial commit for epoll)

0 commit comments

Comments
 (0)