2018-08-13 19:15:09 +02:00
|
|
|
//
|
2018-12-31 23:02:34 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2019
|
2018-08-13 19:15:09 +02:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#include "td/utils/port/detail/NativeFd.h"
|
|
|
|
|
|
|
|
#include "td/utils/format.h"
|
2018-09-07 02:41:21 +02:00
|
|
|
#include "td/utils/logging.h"
|
|
|
|
#include "td/utils/Status.h"
|
2018-08-13 19:15:09 +02:00
|
|
|
|
|
|
|
#if TD_PORT_POSIX
|
2018-09-10 17:09:08 +02:00
|
|
|
#include <fcntl.h>
|
2018-09-11 15:27:20 +02:00
|
|
|
#include <unistd.h>
|
2018-08-13 19:15:09 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace td {
|
2018-09-07 02:41:21 +02:00
|
|
|
|
2018-09-11 15:27:20 +02:00
|
|
|
NativeFd::NativeFd(Fd fd) : fd_(fd) {
|
2018-08-13 19:15:09 +02:00
|
|
|
VLOG(fd) << *this << " create";
|
|
|
|
}
|
2018-09-07 02:41:21 +02:00
|
|
|
|
2018-09-11 15:27:20 +02:00
|
|
|
NativeFd::NativeFd(Fd fd, bool nolog) : fd_(fd) {
|
2018-08-13 19:15:09 +02:00
|
|
|
}
|
2018-09-07 02:41:21 +02:00
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
#if TD_PORT_WINDOWS
|
2018-09-11 15:27:20 +02:00
|
|
|
NativeFd::NativeFd(Socket socket) : fd_(reinterpret_cast<Fd>(socket)), is_socket_(true) {
|
2018-08-13 19:15:09 +02:00
|
|
|
VLOG(fd) << *this << " create";
|
|
|
|
}
|
|
|
|
#endif
|
2018-09-07 02:41:21 +02:00
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
NativeFd::~NativeFd() {
|
|
|
|
close();
|
|
|
|
}
|
2018-09-07 02:41:21 +02:00
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
NativeFd::operator bool() const {
|
2018-09-11 15:27:20 +02:00
|
|
|
return fd_.get() != empty_fd();
|
2018-08-13 19:15:09 +02:00
|
|
|
}
|
2018-09-07 02:41:21 +02:00
|
|
|
|
2018-09-11 15:27:20 +02:00
|
|
|
NativeFd::Fd NativeFd::empty_fd() {
|
2018-08-13 19:15:09 +02:00
|
|
|
#if TD_PORT_POSIX
|
|
|
|
return -1;
|
|
|
|
#elif TD_PORT_WINDOWS
|
|
|
|
return INVALID_HANDLE_VALUE;
|
|
|
|
#endif
|
|
|
|
}
|
2018-09-07 02:41:21 +02:00
|
|
|
|
2018-09-11 15:27:20 +02:00
|
|
|
NativeFd::Fd NativeFd::fd() const {
|
2018-08-13 19:15:09 +02:00
|
|
|
return fd_.get();
|
|
|
|
}
|
2018-09-07 02:41:21 +02:00
|
|
|
|
2018-09-11 15:27:20 +02:00
|
|
|
NativeFd::Socket NativeFd::socket() const {
|
|
|
|
#if TD_PORT_POSIX
|
|
|
|
return fd();
|
|
|
|
#elif TD_PORT_WINDOWS
|
2018-08-13 19:15:09 +02:00
|
|
|
CHECK(is_socket_);
|
2018-09-11 15:27:20 +02:00
|
|
|
return reinterpret_cast<Socket>(fd_.get());
|
2018-08-13 19:15:09 +02:00
|
|
|
#endif
|
2018-09-11 15:27:20 +02:00
|
|
|
}
|
2018-09-07 02:41:21 +02:00
|
|
|
|
2018-09-10 01:08:12 +02:00
|
|
|
Status NativeFd::set_is_blocking(bool is_blocking) const {
|
2018-09-10 16:05:12 +02:00
|
|
|
#if TD_PORT_POSIX
|
|
|
|
auto old_flags = fcntl(fd(), F_GETFL);
|
|
|
|
if (old_flags == -1) {
|
|
|
|
return OS_SOCKET_ERROR("Failed to get socket flags");
|
|
|
|
}
|
|
|
|
auto new_flags = is_blocking ? old_flags & ~O_NONBLOCK : old_flags | O_NONBLOCK;
|
|
|
|
if (new_flags != old_flags && fcntl(fd(), F_SETFL, new_flags) == -1) {
|
|
|
|
return OS_SOCKET_ERROR("Failed to set socket flags");
|
|
|
|
}
|
|
|
|
|
|
|
|
return Status::OK();
|
|
|
|
#elif TD_PORT_WINDOWS
|
|
|
|
return set_is_blocking_unsafe(is_blocking);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
Status NativeFd::set_is_blocking_unsafe(bool is_blocking) const {
|
2018-09-10 01:08:12 +02:00
|
|
|
#if TD_PORT_POSIX
|
|
|
|
if (fcntl(fd(), F_SETFL, is_blocking ? 0 : O_NONBLOCK) == -1) {
|
|
|
|
#elif TD_PORT_WINDOWS
|
|
|
|
u_long mode = is_blocking;
|
|
|
|
if (ioctlsocket(socket(), FIONBIO, &mode) != 0) {
|
|
|
|
#endif
|
|
|
|
return OS_SOCKET_ERROR("Failed to change socket flags");
|
|
|
|
}
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2018-09-10 16:47:28 +02:00
|
|
|
Status NativeFd::duplicate(const NativeFd &to) const {
|
|
|
|
#if TD_PORT_POSIX
|
|
|
|
CHECK(*this);
|
|
|
|
CHECK(to);
|
|
|
|
if (dup2(fd(), to.fd()) == -1) {
|
|
|
|
return OS_ERROR("Failed to duplicate file descriptor");
|
|
|
|
}
|
|
|
|
return Status::OK();
|
|
|
|
#elif TD_PORT_WINDOWS
|
|
|
|
return Status::Error("Not supported");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
void NativeFd::close() {
|
|
|
|
if (!*this) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
VLOG(fd) << *this << " close";
|
|
|
|
#if TD_PORT_WINDOWS
|
2018-09-11 15:27:20 +02:00
|
|
|
if (is_socket_ ? closesocket(socket()) : !CloseHandle(fd())) {
|
2018-08-13 19:15:09 +02:00
|
|
|
#elif TD_PORT_POSIX
|
|
|
|
if (::close(fd()) < 0) {
|
|
|
|
#endif
|
|
|
|
auto error = OS_ERROR("Close fd");
|
|
|
|
LOG(ERROR) << error;
|
|
|
|
}
|
|
|
|
fd_ = {};
|
|
|
|
}
|
2018-09-07 02:41:21 +02:00
|
|
|
|
2018-09-11 15:27:20 +02:00
|
|
|
NativeFd::Fd NativeFd::release() {
|
2018-08-13 19:15:09 +02:00
|
|
|
VLOG(fd) << *this << " release";
|
|
|
|
auto res = fd_.get();
|
|
|
|
fd_ = {};
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
StringBuilder &operator<<(StringBuilder &sb, const NativeFd &fd) {
|
2018-09-11 15:27:20 +02:00
|
|
|
return sb << tag("fd", fd.fd());
|
2018-08-13 19:15:09 +02:00
|
|
|
}
|
2018-09-07 02:41:21 +02:00
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
} // namespace td
|