Skip to content

Commit 08a8767

Browse files
authored
[posix] move SocketWithCloseExec() helper to common utils.hpp (openthread#11427)
1 parent f42af4e commit 08a8767

11 files changed

Lines changed: 73 additions & 49 deletions

File tree

src/posix/platform/daemon.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
#include "cli/cli_config.h"
4848
#include "common/code_utils.hpp"
4949
#include "posix/platform/platform-posix.h"
50+
#include "posix/platform/utils.hpp"
5051

5152
#if OPENTHREAD_POSIX_CONFIG_DAEMON_ENABLE
5253

src/posix/platform/infra_if.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,11 @@
5757
#include <openthread/border_router.h>
5858
#include <openthread/platform/infra_if.h>
5959

60+
#include "infra_if.hpp"
61+
#include "utils.hpp"
6062
#include "common/code_utils.hpp"
6163
#include "common/debug.hpp"
6264
#include "lib/platform/exit_code.h"
63-
#include "posix/platform/infra_if.hpp"
6465

6566
bool otPlatInfraIfHasAddress(uint32_t aInfraIfIndex, const otIp6Address *aAddress)
6667
{

src/posix/platform/misc.cpp

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -95,29 +95,3 @@ otPlatMcuPowerState otPlatGetMcuPowerState(otInstance *aInstance)
9595

9696
return gPlatMcuPowerState;
9797
}
98-
99-
int SocketWithCloseExec(int aDomain, int aType, int aProtocol, SocketBlockOption aBlockOption)
100-
{
101-
int rval = 0;
102-
int fd = -1;
103-
104-
#ifdef __APPLE__
105-
VerifyOrExit((fd = socket(aDomain, aType, aProtocol)) != -1, perror("socket(SOCK_CLOEXEC)"));
106-
107-
VerifyOrExit((rval = fcntl(fd, F_GETFD, 0)) != -1, perror("fcntl(F_GETFD)"));
108-
rval |= aBlockOption == kSocketNonBlock ? O_NONBLOCK | FD_CLOEXEC : FD_CLOEXEC;
109-
VerifyOrExit((rval = fcntl(fd, F_SETFD, rval)) != -1, perror("fcntl(F_SETFD)"));
110-
#else
111-
aType |= aBlockOption == kSocketNonBlock ? SOCK_CLOEXEC | SOCK_NONBLOCK : SOCK_CLOEXEC;
112-
VerifyOrExit((fd = socket(aDomain, aType, aProtocol)) != -1, perror("socket(SOCK_CLOEXEC)"));
113-
#endif
114-
115-
exit:
116-
if (rval == -1)
117-
{
118-
VerifyOrDie(close(fd) == 0, OT_EXIT_ERROR_ERRNO);
119-
fd = -1;
120-
}
121-
122-
return fd;
123-
}

src/posix/platform/multicast_routing.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
#include <openthread/backbone_router_ftd.h>
4949
#include <openthread/logging.h>
5050

51+
#include "utils.hpp"
5152
#include "common/arg_macros.hpp"
5253
#include "core/common/debug.hpp"
5354

src/posix/platform/netif.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ extern int
154154
#include "ip6_utils.hpp"
155155
#include "logger.hpp"
156156
#include "resolver.hpp"
157+
#include "utils.hpp"
157158
#include "common/code_utils.hpp"
158159

159160
unsigned int gNetifIndex = 0;
@@ -1874,7 +1875,7 @@ static void mldListenerInit(void)
18741875
{
18751876
struct ipv6_mreq mreq6;
18761877

1877-
sMLDMonitorFd = SocketWithCloseExec(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6, kSocketNonBlock);
1878+
sMLDMonitorFd = ot::Posix::SocketWithCloseExec(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6, ot::Posix::kSocketNonBlock);
18781879
VerifyOrDie(sMLDMonitorFd != -1, OT_EXIT_FAILURE);
18791880

18801881
mreq6.ipv6mr_interface = gNetifIndex;
@@ -2067,7 +2068,7 @@ static void platformConfigureTunDevice(otPlatformConfig *aPlatformConfig)
20672068
struct sockaddr_ctl addr;
20682069
struct ctl_info info;
20692070

2070-
sTunFd = SocketWithCloseExec(PF_SYSTEM, SOCK_DGRAM, SYSPROTO_CONTROL, kSocketNonBlock);
2071+
sTunFd = ot::Posix::SocketWithCloseExec(PF_SYSTEM, SOCK_DGRAM, SYSPROTO_CONTROL, ot::Posix::kSocketNonBlock);
20712072
VerifyOrDie(sTunFd >= 0, OT_EXIT_ERROR_ERRNO);
20722073

20732074
memset(&info, 0, sizeof(info));
@@ -2146,9 +2147,9 @@ static void platformConfigureTunDevice(otPlatformConfig *aPlatformConfig)
21462147
static void platformConfigureNetLink(void)
21472148
{
21482149
#ifdef __linux__
2149-
sNetlinkFd = SocketWithCloseExec(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE, kSocketNonBlock);
2150+
sNetlinkFd = ot::Posix::SocketWithCloseExec(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE, ot::Posix::kSocketNonBlock);
21502151
#elif defined(__APPLE__) || defined(__NetBSD__) || defined(__FreeBSD__)
2151-
sNetlinkFd = SocketWithCloseExec(PF_ROUTE, SOCK_RAW, 0, kSocketNonBlock);
2152+
sNetlinkFd = ot::Posix::SocketWithCloseExec(PF_ROUTE, SOCK_RAW, 0, ot::Posix::kSocketNonBlock);
21522153
#else
21532154
#error "!! Unknown platform !!"
21542155
#endif
@@ -2219,7 +2220,7 @@ void platformNetifInit(otPlatformConfig *aPlatformConfig)
22192220
(void)LogNote;
22202221
(void)LogDebg;
22212222

2222-
sIpFd = SocketWithCloseExec(AF_INET6, SOCK_DGRAM, IPPROTO_IP, kSocketNonBlock);
2223+
sIpFd = ot::Posix::SocketWithCloseExec(AF_INET6, SOCK_DGRAM, IPPROTO_IP, ot::Posix::kSocketNonBlock);
22232224
VerifyOrDie(sIpFd >= 0, OT_EXIT_ERROR_ERRNO);
22242225

22252226
platformConfigureNetLink();

src/posix/platform/platform-posix.h

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -342,20 +342,6 @@ void platformTrelUpdateFdSet(otSysMainloopContext *aContext);
342342
*/
343343
void platformTrelProcess(otInstance *aInstance, const otSysMainloopContext *aContext);
344344

345-
/**
346-
* Creates a socket with SOCK_CLOEXEC flag set.
347-
*
348-
* @param[in] aDomain The communication domain.
349-
* @param[in] aType The semantics of communication.
350-
* @param[in] aProtocol The protocol to use.
351-
* @param[in] aBlockOption Whether to add nonblock flags.
352-
*
353-
* @returns The file descriptor of the created socket.
354-
*
355-
* @retval -1 Failed to create socket.
356-
*/
357-
int SocketWithCloseExec(int aDomain, int aType, int aProtocol, SocketBlockOption aBlockOption);
358-
359345
/**
360346
* The name of Thread network interface.
361347
*/

src/posix/platform/trel.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
#include "logger.hpp"
5050
#include "radio_url.hpp"
5151
#include "system.hpp"
52+
#include "utils.hpp"
5253
#include "common/code_utils.hpp"
5354

5455
#if OPENTHREAD_CONFIG_RADIO_LINK_TREL_ENABLE
@@ -172,7 +173,7 @@ static void PrepareSocket(uint16_t &aUdpPort)
172173

173174
LogDebg("PrepareSocket()");
174175

175-
sSocket = SocketWithCloseExec(AF_INET6, SOCK_DGRAM, 0, kSocketNonBlock);
176+
sSocket = ot::Posix::SocketWithCloseExec(AF_INET6, SOCK_DGRAM, 0, ot::Posix::kSocketNonBlock);
176177
VerifyOrDie(sSocket >= 0, OT_EXIT_ERROR_ERRNO);
177178

178179
// Make the socket non-blocking to allow immediate tx attempt.

src/posix/platform/udp.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
#include "posix/platform/ip6_utils.hpp"
5959
#include "posix/platform/mainloop.hpp"
6060
#include "posix/platform/udp.hpp"
61+
#include "posix/platform/utils.hpp"
6162

6263
using namespace ot::Posix::Ip6Utils;
6364

@@ -222,7 +223,7 @@ otError otPlatUdpSocket(otUdpSocket *aUdpSocket)
222223

223224
assert(aUdpSocket->mHandle == nullptr);
224225

225-
fd = SocketWithCloseExec(AF_INET6, SOCK_DGRAM, IPPROTO_UDP, kSocketNonBlock);
226+
fd = ot::Posix::SocketWithCloseExec(AF_INET6, SOCK_DGRAM, IPPROTO_UDP, ot::Posix::kSocketNonBlock);
226227
VerifyOrExit(fd >= 0, error = OT_ERROR_FAILED);
227228

228229
aUdpSocket->mHandle = FdToHandle(fd);

src/posix/platform/utils.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,50 @@
3434
#include "utils.hpp"
3535

3636
#include <errno.h>
37+
#include <fcntl.h>
38+
#include <stdint.h>
3739
#include <stdio.h>
3840
#include <string.h>
41+
#include <sys/ioctl.h>
42+
#include <sys/select.h>
43+
#include <sys/socket.h>
44+
#include <sys/types.h>
45+
#include <unistd.h>
3946

4047
#include <openthread/logging.h>
4148

4249
#include "common/code_utils.hpp"
50+
#include "lib/platform/exit_code.h"
4351

4452
namespace ot {
4553
namespace Posix {
4654

55+
int SocketWithCloseExec(int aDomain, int aType, int aProtocol, SocketBlockOption aBlockOption)
56+
{
57+
int rval = 0;
58+
int fd = -1;
59+
60+
#ifdef __APPLE__
61+
VerifyOrExit((fd = socket(aDomain, aType, aProtocol)) != -1, perror("socket(SOCK_CLOEXEC)"));
62+
63+
VerifyOrExit((rval = fcntl(fd, F_GETFD, 0)) != -1, perror("fcntl(F_GETFD)"));
64+
rval |= aBlockOption == kSocketNonBlock ? O_NONBLOCK | FD_CLOEXEC : FD_CLOEXEC;
65+
VerifyOrExit((rval = fcntl(fd, F_SETFD, rval)) != -1, perror("fcntl(F_SETFD)"));
66+
#else
67+
aType |= aBlockOption == kSocketNonBlock ? SOCK_CLOEXEC | SOCK_NONBLOCK : SOCK_CLOEXEC;
68+
VerifyOrExit((fd = socket(aDomain, aType, aProtocol)) != -1, perror("socket(SOCK_CLOEXEC)"));
69+
#endif
70+
71+
exit:
72+
if (rval == -1)
73+
{
74+
VerifyOrDie(close(fd) == 0, OT_EXIT_ERROR_ERRNO);
75+
fd = -1;
76+
}
77+
78+
return fd;
79+
}
80+
4781
enum
4882
{
4983
kSystemCommandMaxLength = 1024, ///< Max length of a system call command.

src/posix/platform/utils.hpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,28 @@
3434
namespace ot {
3535
namespace Posix {
3636

37+
/**
38+
* Represents socket block/non-block options.
39+
*/
40+
enum SocketBlockOption : uint8_t
41+
{
42+
kSocketBlock,
43+
kSocketNonBlock,
44+
};
45+
46+
/**
47+
* Creates a socket with SOCK_CLOEXEC flag set.
48+
*
49+
* @param[in] aDomain The communication domain.
50+
* @param[in] aType The semantics of communication.
51+
* @param[in] aProtocol The protocol to use.
52+
* @param[in] aBlockOption Whether to add nonblock flags.
53+
*
54+
* @returns The file descriptor of the created socket, or -1 if fails to create the socket.
55+
* @retval -1 Failed to create socket.
56+
*/
57+
int SocketWithCloseExec(int aDomain, int aType, int aProtocol, SocketBlockOption aBlockOption);
58+
3759
/**
3860
* Formats a system command to execute.
3961
*

0 commit comments

Comments
 (0)