2018-08-13 19:15:09 +02:00
|
|
|
//
|
2021-01-01 13:57:46 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021
|
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"
|
2021-05-17 14:21:11 +02:00
|
|
|
#include "td/utils/SliceBuilder.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
|
|
|
|
|
2019-07-31 11:18:48 +02:00
|
|
|
#if TD_FD_DEBUG
|
|
|
|
#include <mutex>
|
2019-08-01 01:48:34 +02:00
|
|
|
#include <set>
|
2019-07-31 11:18:48 +02:00
|
|
|
#endif
|
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
namespace td {
|
2018-09-07 02:41:21 +02:00
|
|
|
|
2020-10-05 17:07:23 +02:00
|
|
|
int VERBOSITY_NAME(fd) = VERBOSITY_NAME(DEBUG) + 9;
|
|
|
|
|
2019-07-31 11:18:48 +02:00
|
|
|
#if TD_FD_DEBUG
|
|
|
|
class FdSet {
|
|
|
|
public:
|
|
|
|
void on_create_fd(NativeFd::Fd fd) {
|
2019-08-28 15:59:50 +02:00
|
|
|
if (!is_valid(fd)) {
|
|
|
|
return;
|
|
|
|
}
|
2019-07-31 11:18:48 +02:00
|
|
|
if (is_stdio(fd)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
std::unique_lock<std::mutex> guard(mutex_);
|
2019-08-01 06:06:43 +02:00
|
|
|
if (fds_.count(fd) >= 1) {
|
2019-07-31 11:18:48 +02:00
|
|
|
LOG(FATAL) << "Create duplicated fd: " << fd;
|
|
|
|
}
|
|
|
|
fds_.insert(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
Status validate(NativeFd::Fd fd) {
|
2019-08-01 01:48:34 +02:00
|
|
|
if (!is_valid(fd)) {
|
2019-07-31 11:18:48 +02:00
|
|
|
return Status::Error(PSLICE() << "Invalid fd: " << fd);
|
|
|
|
}
|
|
|
|
if (is_stdio(fd)) {
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
std::unique_lock<std::mutex> guard(mutex_);
|
|
|
|
if (fds_.count(fd) != 1) {
|
|
|
|
return Status::Error(PSLICE() << "Unknown fd: " << fd);
|
|
|
|
}
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_close_fd(NativeFd::Fd fd) {
|
2019-08-28 15:59:50 +02:00
|
|
|
if (!is_valid(fd)) {
|
|
|
|
return;
|
|
|
|
}
|
2019-07-31 11:18:48 +02:00
|
|
|
if (is_stdio(fd)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
std::unique_lock<std::mutex> guard(mutex_);
|
|
|
|
if (fds_.count(fd) != 1) {
|
|
|
|
LOG(FATAL) << "Close unknown fd: " << fd;
|
|
|
|
}
|
|
|
|
fds_.erase(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::mutex mutex_;
|
|
|
|
std::set<NativeFd::Fd> fds_;
|
|
|
|
|
2019-08-01 01:48:34 +02:00
|
|
|
bool is_stdio(NativeFd::Fd fd) const {
|
|
|
|
#if TD_PORT_WINDOWS
|
2019-08-01 03:55:56 +02:00
|
|
|
#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP | WINAPI_PARTITION_SYSTEM)
|
2019-08-01 18:43:36 +02:00
|
|
|
return fd == GetStdHandle(STD_INPUT_HANDLE) || fd == GetStdHandle(STD_OUTPUT_HANDLE) ||
|
|
|
|
fd == GetStdHandle(STD_ERROR_HANDLE);
|
2019-08-01 03:55:56 +02:00
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
2019-08-01 01:48:34 +02:00
|
|
|
#else
|
2019-07-31 11:18:48 +02:00
|
|
|
return fd >= 0 && fd <= 2;
|
2019-08-01 01:48:34 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_valid(NativeFd::Fd fd) const {
|
|
|
|
#if TD_PORT_WINDOWS
|
|
|
|
return fd != INVALID_HANDLE_VALUE;
|
|
|
|
#else
|
|
|
|
return fd >= 0;
|
|
|
|
#endif
|
2019-07-31 11:18:48 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
FdSet &get_fd_set() {
|
|
|
|
static FdSet res;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
#endif
|
2019-08-01 01:48:34 +02:00
|
|
|
|
2019-07-31 11:18:48 +02:00
|
|
|
Status NativeFd::validate() const {
|
|
|
|
#if TD_FD_DEBUG
|
2020-01-30 20:11:04 +01:00
|
|
|
return get_fd_set().validate(fd_);
|
2019-07-31 11:18:48 +02:00
|
|
|
#else
|
|
|
|
return Status::OK();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
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";
|
2019-07-31 11:18:48 +02:00
|
|
|
#if TD_FD_DEBUG
|
2020-01-30 20:11:04 +01:00
|
|
|
get_fd_set().on_create_fd(fd_);
|
2019-07-31 11:18:48 +02:00
|
|
|
#endif
|
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::NativeFd(Fd fd, bool nolog) : fd_(fd) {
|
2019-07-31 11:18:48 +02:00
|
|
|
#if TD_FD_DEBUG
|
2020-01-30 20:11:04 +01:00
|
|
|
get_fd_set().on_create_fd(fd_);
|
2019-07-31 11:18:48 +02:00
|
|
|
#endif
|
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";
|
2019-08-01 04:11:55 +02:00
|
|
|
#if TD_FD_DEBUG
|
2020-01-30 20:11:04 +01:00
|
|
|
get_fd_set().on_create_fd(fd_);
|
2019-08-01 04:11:55 +02:00
|
|
|
#endif
|
2018-08-13 19:15:09 +02:00
|
|
|
}
|
|
|
|
#endif
|
2019-08-01 01:48:34 +02:00
|
|
|
|
2021-10-18 13:36:15 +02:00
|
|
|
NativeFd::NativeFd(NativeFd &&other) noexcept : fd_(other.fd_) {
|
2020-01-30 20:11:04 +01:00
|
|
|
#if TD_PORT_WINDOWS
|
|
|
|
is_socket_ = other.is_socket_;
|
|
|
|
#endif
|
|
|
|
other.fd_ = empty_fd();
|
|
|
|
}
|
|
|
|
|
2021-10-18 13:36:15 +02:00
|
|
|
NativeFd &NativeFd::operator=(NativeFd &&other) noexcept {
|
2020-01-30 20:11:04 +01:00
|
|
|
CHECK(this != &other);
|
2019-07-31 11:18:48 +02:00
|
|
|
close();
|
2020-01-30 20:11:04 +01:00
|
|
|
fd_ = other.fd_;
|
2019-07-31 11:18:48 +02:00
|
|
|
#if TD_PORT_WINDOWS
|
2020-01-30 20:11:04 +01:00
|
|
|
is_socket_ = other.is_socket_;
|
2019-07-31 11:18:48 +02:00
|
|
|
#endif
|
2020-01-30 20:11:04 +01:00
|
|
|
other.fd_ = empty_fd();
|
2019-07-31 11:18:48 +02:00
|
|
|
return *this;
|
|
|
|
}
|
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 {
|
2020-01-30 20:11:04 +01:00
|
|
|
return fd_ != 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 {
|
2020-01-30 20:11:04 +01:00
|
|
|
return 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::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_);
|
2020-01-30 20:11:04 +01:00
|
|
|
return reinterpret_cast<Socket>(fd_);
|
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;
|
|
|
|
}
|
2019-07-31 11:18:48 +02:00
|
|
|
|
|
|
|
#if TD_FD_DEBUG
|
|
|
|
get_fd_set().on_close_fd(fd());
|
|
|
|
#endif
|
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
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;
|
|
|
|
}
|
2020-01-30 20:11:04 +01:00
|
|
|
fd_ = 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::release() {
|
2018-08-13 19:15:09 +02:00
|
|
|
VLOG(fd) << *this << " release";
|
2020-01-30 20:11:04 +01:00
|
|
|
auto res = fd_;
|
|
|
|
fd_ = empty_fd();
|
2019-08-01 06:06:43 +02:00
|
|
|
#if TD_FD_DEBUG
|
2019-08-01 18:43:36 +02:00
|
|
|
get_fd_set().on_close_fd(res);
|
2019-08-01 06:06:43 +02:00
|
|
|
#endif
|
2018-08-13 19:15:09 +02:00
|
|
|
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
|