Skip to content

Commit

Permalink
address: add mode to PipeInstance envoyproxy#5808
Browse files Browse the repository at this point in the history
Signed-off-by: Akhil Thampy <[email protected]>
  • Loading branch information
athampy committed Oct 3, 2019
1 parent e8245db commit 0715211
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
16 changes: 13 additions & 3 deletions source/common/network/address_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/un.h>

#include <array>
Expand Down Expand Up @@ -329,7 +330,7 @@ IoHandlePtr Ipv6Instance::socket(SocketType type) const {
return io_handle;
}

PipeInstance::PipeInstance(const sockaddr_un* address, socklen_t ss_len)
PipeInstance::PipeInstance(const sockaddr_un* address, socklen_t ss_len, mode_t mode)
: InstanceBase(Type::Pipe) {
if (address->sun_path[0] == '\0') {
#if !defined(__linux__)
Expand All @@ -345,9 +346,10 @@ PipeInstance::PipeInstance(const sockaddr_un* address, socklen_t ss_len)
abstract_namespace_
? fmt::format("@{}", absl::string_view(address_.sun_path + 1, address_length_ - 1))
: address_.sun_path;
this->mode = mode;
}

PipeInstance::PipeInstance(const std::string& pipe_path) : InstanceBase(Type::Pipe) {
PipeInstance::PipeInstance(const std::string& pipe_path, mode_t mode) : InstanceBase(Type::Pipe) {
if (pipe_path.size() >= sizeof(address_.sun_path)) {
throw EnvoyException(
fmt::format("Path \"{}\" exceeds maximum UNIX domain socket path size of {}.", pipe_path,
Expand All @@ -365,6 +367,7 @@ PipeInstance::PipeInstance(const std::string& pipe_path) : InstanceBase(Type::Pi
address_length_ = strlen(address_.sun_path);
address_.sun_path[0] = '\0';
}
this->mode = mode;
}

bool PipeInstance::operator==(const Instance& rhs) const { return asString() == rhs.asString(); }
Expand All @@ -377,7 +380,14 @@ Api::SysCallIntResult PipeInstance::bind(int fd) const {
unlink(address_.sun_path);
}
auto& os_syscalls = Api::OsSysCallsSingleton::get();
return os_syscalls.bind(fd, sockAddr(), sockAddrLen());
auto bind_result = os_syscalls.bind(fd, sockAddr(), sockAddrLen());
if (mode != 0 and !abstract_namespace_ and bind_result.rc_ == 0) {
auto set_permissions = os_syscalls.chmod(address_.sun_path, mode);
if (set_permissions.rc_ != 0) {
throw EnvoyException(fmt::format("Failed to create socket with mode {}", mode));
}
}
return bind_result;
}

Api::SysCallIntResult PipeInstance::connect(int fd) const {
Expand Down
6 changes: 4 additions & 2 deletions source/common/network/address_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/un.h>

Expand Down Expand Up @@ -230,12 +231,12 @@ class PipeInstance : public InstanceBase {
/**
* Construct from an existing unix address.
*/
explicit PipeInstance(const sockaddr_un* address, socklen_t ss_len);
explicit PipeInstance(const sockaddr_un* address, socklen_t ss_len, mode_t mode = 0);

/**
* Construct from a string pipe path.
*/
explicit PipeInstance(const std::string& pipe_path);
explicit PipeInstance(const std::string& pipe_path, mode_t mode = 0);

// Network::Address::Instance
bool operator==(const Instance& rhs) const override;
Expand All @@ -258,6 +259,7 @@ class PipeInstance : public InstanceBase {
// For abstract namespaces.
bool abstract_namespace_{false};
uint32_t address_length_{0};
mode_t mode{0};
};

} // namespace Address
Expand Down
3 changes: 2 additions & 1 deletion source/common/network/utility.cc
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,8 @@ Utility::protobufAddressToAddress(const envoy::api::v2::core::Address& proto_add
proto_address.socket_address().port_value(),
!proto_address.socket_address().ipv4_compat());
case envoy::api::v2::core::Address::kPipe:
return std::make_shared<Address::PipeInstance>(proto_address.pipe().path());
return std::make_shared<Address::PipeInstance>(proto_address.pipe().path(),
proto_address.pipe().mode());
default:
NOT_REACHED_GCOVR_EXCL_LINE;
}
Expand Down

0 comments on commit 0715211

Please sign in to comment.