Merge "[CLATJ#29] Remove the throwaway sockets for dup2 fds"

This commit is contained in:
Maciej Żenczykowski
2022-03-05 08:32:47 +00:00
committed by Gerrit Code Review

View File

@@ -318,30 +318,13 @@ static jint com_android_server_connectivity_ClatCoordinator_startClatd(
return -1;
}
// 1. create a throwaway socket to reserve a file descriptor number
int passedTunFd = socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0);
if (passedTunFd == -1) {
throwIOException(env, "socket(ipv6/udp) for tun fd failed", errno);
return -1;
}
int passedSockRead = socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0);
if (passedSockRead == -1) {
throwIOException(env, "socket(ipv6/udp) for read socket failed", errno);
return -1;
}
int passedSockWrite = socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0);
if (passedSockWrite == -1) {
throwIOException(env, "socket(ipv6/udp) for write socket failed", errno);
return -1;
}
// these are the FD we'll pass to clatd on the cli, so need it as a string
char passedTunFdStr[INT32_STRLEN];
char passedSockReadStr[INT32_STRLEN];
char passedSockWriteStr[INT32_STRLEN];
snprintf(passedTunFdStr, sizeof(passedTunFdStr), "%d", passedTunFd);
snprintf(passedSockReadStr, sizeof(passedSockReadStr), "%d", passedSockRead);
snprintf(passedSockWriteStr, sizeof(passedSockWriteStr), "%d", passedSockWrite);
// 1. these are the FD we'll pass to clatd on the cli, so need it as a string
char tunFdStr[INT32_STRLEN];
char sockReadStr[INT32_STRLEN];
char sockWriteStr[INT32_STRLEN];
snprintf(tunFdStr, sizeof(tunFdStr), "%d", tunFd);
snprintf(sockReadStr, sizeof(sockReadStr), "%d", readSock);
snprintf(sockWriteStr, sizeof(sockWriteStr), "%d", writeSock);
// 2. we're going to use this as argv[0] to clatd to make ps output more useful
std::string progname("clatd-");
@@ -353,9 +336,9 @@ static jint com_android_server_connectivity_ClatCoordinator_startClatd(
"-p", pfx96Str.c_str(),
"-4", v4Str.c_str(),
"-6", v6Str.c_str(),
"-t", passedTunFdStr,
"-r", passedSockReadStr,
"-w", passedSockWriteStr,
"-t", tunFdStr,
"-r", sockReadStr,
"-w", sockWriteStr,
nullptr};
// clang-format on
@@ -375,7 +358,9 @@ static jint com_android_server_connectivity_ClatCoordinator_startClatd(
// 4. register dup2() action: this is what 'clears' the CLOEXEC flag
// on the tun fd that we want the child clatd process to inherit
// (this will happen after the vfork, and before the execve)
// (this will happen after the vfork, and before the execve).
// Note that even though dup2(2) is a no-op if fd == new_fd but O_CLOEXEC flag will be removed.
// See implementation of bionic's posix_spawn_file_actions_adddup2().
posix_spawn_file_actions_t fa;
if (int ret = posix_spawn_file_actions_init(&fa)) {
posix_spawnattr_destroy(&attr);
@@ -383,19 +368,19 @@ static jint com_android_server_connectivity_ClatCoordinator_startClatd(
return -1;
}
if (int ret = posix_spawn_file_actions_adddup2(&fa, tunFd, passedTunFd)) {
if (int ret = posix_spawn_file_actions_adddup2(&fa, tunFd, tunFd)) {
posix_spawnattr_destroy(&attr);
posix_spawn_file_actions_destroy(&fa);
throwIOException(env, "posix_spawn_file_actions_adddup2 for tun fd failed", ret);
return -1;
}
if (int ret = posix_spawn_file_actions_adddup2(&fa, readSock, passedSockRead)) {
if (int ret = posix_spawn_file_actions_adddup2(&fa, readSock, readSock)) {
posix_spawnattr_destroy(&attr);
posix_spawn_file_actions_destroy(&fa);
throwIOException(env, "posix_spawn_file_actions_adddup2 for read socket failed", ret);
return -1;
}
if (int ret = posix_spawn_file_actions_adddup2(&fa, writeSock, passedSockWrite)) {
if (int ret = posix_spawn_file_actions_adddup2(&fa, writeSock, writeSock)) {
posix_spawnattr_destroy(&attr);
posix_spawn_file_actions_destroy(&fa);
throwIOException(env, "posix_spawn_file_actions_adddup2 for write socket failed", ret);
@@ -414,7 +399,7 @@ static jint com_android_server_connectivity_ClatCoordinator_startClatd(
posix_spawnattr_destroy(&attr);
posix_spawn_file_actions_destroy(&fa);
// 5. Start BPF if any
// 6. Start BPF if any
if (!net::clat::initMaps()) {
net::clat::ClatdTracker tracker = {};
if (!initTracker(ifaceStr.c_str(), pfx96Str.c_str(), v4Str.c_str(), v6Str.c_str(),