Skip to content

Commit eab2cba

Browse files
gneworldxiaoxiang781216
authored andcommitted
enable SOCK_CLOEXEC explicit
leaking here means fork/vfork will duplicate fd without O_CLOEXEC flag to the child process. Signed-off-by: wanggang26 <[email protected]>
1 parent 3f0c930 commit eab2cba

File tree

20 files changed

+56
-30
lines changed

20 files changed

+56
-30
lines changed

examples/netloop/lo_listener.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ static inline bool net_connection(struct net_listener_s *nls)
182182
printf("lo_listener: Accepting new connection on sd=%d\n",
183183
nls->listensd);
184184

185-
sd = accept(nls->listensd, NULL, NULL);
185+
sd = accept4(nls->listensd, NULL, NULL, SOCK_CLOEXEC);
186186
if (sd < 0)
187187
{
188188
printf("lo_listener: accept failed: %d\n", errno);

examples/nettest/nettest_server.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,12 @@ void nettest_server(void)
145145

146146
printf("server: Accepting connections on port %d\n",
147147
CONFIG_EXAMPLES_NETTEST_SERVER_PORTNO);
148+
#ifdef __APPLE__
148149
acceptsd = accept(listensd, (struct sockaddr *)&myaddr, &addrlen);
150+
#else
151+
acceptsd = accept4(listensd, (struct sockaddr *)&myaddr, &addrlen,
152+
SOCK_CLOEXEC);
153+
#endif
149154
if (acceptsd < 0)
150155
{
151156
printf("server: accept failure: %d\n", errno);

examples/poll/net_listener.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ static inline bool net_connection(struct net_listener_s *nls)
182182
printf("net_listener: Accepting new connection on sd=%d\n",
183183
nls->listensd);
184184

185-
sd = accept(nls->listensd, NULL, NULL);
185+
sd = accept4(nls->listensd, NULL, NULL, SOCK_CLOEXEC);
186186
if (sd < 0)
187187
{
188188
printf("net_listener: accept failed: %d\n", errno);

examples/poll/net_reader.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,8 @@ void *net_reader(pthread_addr_t pvarg)
278278
printf("net_reader: Accepting new connections on port %d\n",
279279
LISTENER_PORT);
280280
addrlen = sizeof(struct sockaddr_in);
281-
acceptsd = accept(listensd, (struct sockaddr *)&addr, &addrlen);
281+
acceptsd = accept4(listensd, (struct sockaddr *)&addr, &addrlen,
282+
SOCK_CLOEXEC);
282283
if (acceptsd < 0)
283284
{
284285
printf("net_reader: accept failure: %d\n", errno);

examples/rpmsgsocket/rpsock_server.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,8 @@ static int rpsock_stream_server(int argc, char *argv[])
238238
}
239239

240240
printf("server: try accept ...\n");
241-
new = accept(listensd, (struct sockaddr *)&myaddr, &addrlen);
241+
new = accept4(listensd, (struct sockaddr *)&myaddr, &addrlen,
242+
SOCK_CLOEXEC);
242243
if (new < 0)
243244
break;
244245

examples/tcp_ipc_server/tcp_ipc_server_main.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,9 +243,9 @@ int main(int argc, char *argv[])
243243
/* Wait for a new client socket connection */
244244

245245
addr_size = sizeof server_storage;
246-
new_socket_client_fd = accept(socket_server_fd,
247-
(struct sockaddr *)&server_storage,
248-
&addr_size);
246+
new_socket_client_fd = accept4(socket_server_fd,
247+
(struct sockaddr *)&server_storage,
248+
&addr_size, SOCK_CLOEXEC);
249249

250250
if (new_socket_client_fd < 0)
251251
{

examples/tcpblaster/tcpblaster_server.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,12 @@ void tcpblaster_server(void)
153153

154154
printf("server: Accepting connections on port %d\n",
155155
CONFIG_EXAMPLES_TCPBLASTER_SERVER_PORTNO);
156+
#ifdef __APPLE__
156157
acceptsd = accept(listensd, (FAR struct sockaddr *)&myaddr, &addrlen);
158+
#else
159+
acceptsd = accept4(listensd, (FAR struct sockaddr *)&myaddr, &addrlen,
160+
SOCK_CLOEXEC);
161+
#endif
157162
if (acceptsd < 0)
158163
{
159164
printf("server: accept failure: %d\n", errno);

examples/tcpecho/tcpecho_main.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,8 @@ static int tcpecho_server(void)
244244
/* new client connection */
245245

246246
clilen = sizeof(cliaddr);
247-
connfd = accept(listenfd, (struct sockaddr *)&cliaddr, &clilen);
247+
connfd = accept4(listenfd, (struct sockaddr *)&cliaddr, &clilen,
248+
SOCK_CLOEXEC);
248249

249250
ninfo("new client: %s\n",
250251
inet_ntoa_r(cliaddr.sin_addr, inetaddr, sizeof(inetaddr)));

examples/ustream/ustream_server.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ int main(int argc, char *argv[])
132132
}
133133
#endif
134134

135-
acceptsd = accept(listensd, (struct sockaddr *)&myaddr, &addrlen);
135+
acceptsd = accept4(listensd, (struct sockaddr *)&myaddr, &addrlen,
136+
SOCK_CLOEXEC);
136137
if (acceptsd < 0)
137138
{
138139
printf("server: accept failure: %d\n", errno);

examples/xmlrpc/xmlrpc_main.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,8 @@ int main(int argc, FAR char *argv[])
410410
for (; ; )
411411
{
412412
clilen = sizeof(cliaddr);
413-
connfd = accept(listenfd, (struct sockaddr *)&cliaddr, &clilen);
413+
connfd = accept4(listenfd, (struct sockaddr *)&cliaddr, &clilen,
414+
SOCK_CLOEXEC);
414415
if (connfd <= 0)
415416
{
416417
break;

0 commit comments

Comments
 (0)