Skip to content

Commit 223ec07

Browse files
committed
arch/sim: Add host socket and epoll APIs
Add host-side helpers for socket, epoll and additional file operations so sim applications can call selected host Linux APIs without being affected by NuttX symbol renaming. Move the socket ABI definitions shared with usrsock into a common host socket header, and keep usrsock-specific structures in the usrsock header. Signed-off-by: Lingao Meng <menglingao@xiaomi.com>
1 parent ae96aa0 commit 223ec07

10 files changed

Lines changed: 1573 additions & 231 deletions

File tree

arch/sim/src/Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,10 @@ ifeq ($(CONFIG_FS_LARGEFILE),y)
131131
endif
132132

133133
HOSTSRCS = sim_hostirq.c sim_hostmemory.c sim_hostmisc.c sim_hosttime.c sim_hostuart.c
134-
HOSTSRCS += sim_hostfs.c sim_errno.c
134+
HOSTSRCS += sim_hostfs.c sim_hostsocket.c sim_errno.c
135+
ifeq ($(CONFIG_HOST_LINUX),y)
136+
HOSTSRCS += sim_hostepoll.c
137+
endif
135138

136139
hostfs.h: $(TOPDIR)/include/nuttx/fs/hostfs.h
137140
@echo "CP: $<"

arch/sim/src/nuttx-names.in

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ NXSYMBOLS(closedir)
5050
NXSYMBOLS(connect)
5151
NXSYMBOLS(dlsym)
5252
NXSYMBOLS(dup)
53+
NXSYMBOLS(epoll_create1)
54+
NXSYMBOLS(epoll_ctl)
55+
NXSYMBOLS(epoll_wait)
5356
NXSYMBOLS(exit)
5457
NXSYMBOLS(fchmod)
5558
NXSYMBOLS(fchown)
@@ -59,8 +62,10 @@ NXSYMBOLS(fdopen)
5962
NXSYMBOLS(feof)
6063
NXSYMBOLS(fopen)
6164
NXSYMBOLS(fprintf)
65+
NXSYMBOLS(fgets)
6266
NXSYMBOLS(fread)
6367
NXSYMBOLS(free)
68+
NXSYMBOLS(freeaddrinfo)
6469
NXSYMBOLS(fseek)
6570
NXSYMBOLS(fstat)
6671
NXSYMBOLS(fsync)
@@ -71,8 +76,10 @@ NXSYMBOLS(fwrite)
7176
NXSYMBOLS(getpeername)
7277
NXSYMBOLS(getsockname)
7378
NXSYMBOLS(getenv)
79+
NXSYMBOLS(getaddrinfo)
7480
NXSYMBOLS(getpid)
7581
NXSYMBOLS(getsockopt)
82+
NXSYMBOLS(htons)
7683
NXSYMBOLS(if_nametoindex)
7784
NXSYMBOLS(ioctl)
7885
NXSYMBOLS(listen)
@@ -83,11 +90,13 @@ NXSYMBOLS(malloc_size)
8390
NXSYMBOLS(malloc_usable_size)
8491
NXSYMBOLS(memchr)
8592
NXSYMBOLS(memcpy)
93+
NXSYMBOLS(memset)
8694
NXSYMBOLS(memfd_create)
8795
NXSYMBOLS(mkdir)
8896
NXSYMBOLS(mmap)
8997
NXSYMBOLS(mprotect)
9098
NXSYMBOLS(munmap)
99+
NXSYMBOLS(ntohs)
91100
NXSYMBOLS(open)
92101
NXSYMBOLS(opendir)
93102
NXSYMBOLS(pclose)
@@ -126,12 +135,14 @@ NXSYMBOLS(readdir)
126135
NXSYMBOLS(readlink)
127136
NXSYMBOLS(readv)
128137
NXSYMBOLS(realloc)
138+
NXSYMBOLS(recv)
129139
NXSYMBOLS(recvfrom)
130140
NXSYMBOLS(rename)
131141
NXSYMBOLS(rewinddir)
132142
NXSYMBOLS(rmdir)
133143
NXSYMBOLS(sched_yield)
134144
NXSYMBOLS(select)
145+
NXSYMBOLS(send)
135146
NXSYMBOLS(sendmsg)
136147
NXSYMBOLS(sendto)
137148
NXSYMBOLS(setbuf)
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/****************************************************************************
2+
* arch/sim/src/sim/posix/sim_hostepoll.c
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Licensed to the Apache Software Foundation (ASF) under one or more
7+
* contributor license agreements. See the NOTICE file distributed with
8+
* this work for additional information regarding copyright ownership. The
9+
* ASF licenses this file to you under the Apache License, Version 2.0 (the
10+
* "License"); you may not use this file except in compliance with the
11+
* License. You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18+
* License for the specific language governing permissions and limitations
19+
* under the License.
20+
*
21+
****************************************************************************/
22+
23+
/****************************************************************************
24+
* Included Files
25+
****************************************************************************/
26+
27+
#include <sys/epoll.h>
28+
29+
#include <errno.h>
30+
#include <stdint.h>
31+
#include <stdlib.h>
32+
#include <string.h>
33+
34+
#include "sim_internal.h"
35+
#include "sim_hostepoll.h"
36+
37+
/****************************************************************************
38+
* Public Functions
39+
****************************************************************************/
40+
41+
int host_epoll_create1(int flags)
42+
{
43+
int hostflags = 0;
44+
int ret;
45+
46+
if (flags & NUTTX_EPOLL_CLOEXEC)
47+
{
48+
hostflags |= EPOLL_CLOEXEC;
49+
flags &= ~NUTTX_EPOLL_CLOEXEC;
50+
}
51+
52+
if (flags != 0)
53+
{
54+
return -EINVAL;
55+
}
56+
57+
ret = epoll_create1(hostflags);
58+
return ret < 0 ? host_errno_convert(-errno) : ret;
59+
}
60+
61+
int host_epoll_ctl(int epoll_fd, int op, int fd, uint32_t events,
62+
uintptr_t data)
63+
{
64+
struct epoll_event event;
65+
struct epoll_event *eventptr = NULL;
66+
int ret;
67+
68+
memset(&event, 0, sizeof(event));
69+
70+
if (op != EPOLL_CTL_DEL)
71+
{
72+
event.events = events;
73+
event.data.ptr = (void *)data;
74+
eventptr = &event;
75+
}
76+
77+
ret = epoll_ctl(epoll_fd, op, fd, eventptr);
78+
return ret < 0 ? host_errno_convert(-errno) : ret;
79+
}
80+
81+
int host_epoll_wait(int epoll_fd, struct host_epoll_event *events,
82+
int maxevents, int timeout_ms)
83+
{
84+
struct epoll_event *hostevents;
85+
int ret;
86+
int i;
87+
88+
if (events == NULL || maxevents <= 0 ||
89+
(size_t)maxevents > SIZE_MAX / sizeof(*hostevents))
90+
{
91+
return -EINVAL;
92+
}
93+
94+
hostevents = malloc(maxevents * sizeof(*hostevents));
95+
if (hostevents == NULL)
96+
{
97+
return -ENOMEM;
98+
}
99+
100+
ret = epoll_wait(epoll_fd, hostevents, maxevents, timeout_ms);
101+
if (ret < 0)
102+
{
103+
ret = host_errno_convert(-errno);
104+
goto out;
105+
}
106+
107+
for (i = 0; i < ret; i++)
108+
{
109+
events[i].events = hostevents[i].events;
110+
events[i].data = (uintptr_t)hostevents[i].data.ptr;
111+
}
112+
113+
out:
114+
free(hostevents);
115+
return ret;
116+
}

0 commit comments

Comments
 (0)