2 * Copyright (C) 2023 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: LGPL-2.1-only
8 #ifndef LTTNG_FILE_DESCRIPTOR_HPP
9 #define LTTNG_FILE_DESCRIPTOR_HPP
11 #include <common/error.hpp>
12 #include <common/format.hpp>
21 * RAII wrapper around a UNIX file descriptor. A file_descriptor's underlying
24 class file_descriptor {
30 explicit file_descriptor(int raw_fd) noexcept : _raw_fd{ raw_fd }
32 LTTNG_ASSERT(_is_valid_fd(_raw_fd));
35 file_descriptor(const file_descriptor&) = delete;
36 file_descriptor& operator=(const file_descriptor&) = delete;
37 file_descriptor& operator=(file_descriptor&& other)
40 std::swap(_raw_fd, other._raw_fd);
44 file_descriptor(file_descriptor&& other) noexcept
46 std::swap(_raw_fd, other._raw_fd);
54 int fd() const noexcept
56 LTTNG_ASSERT(_is_valid_fd(_raw_fd));
61 static bool _is_valid_fd(int fd)
68 if (!_is_valid_fd(_raw_fd)) {
72 const auto ret = ::close(_raw_fd);
76 PERROR("Failed to close file descriptor: fd=%i", _raw_fd);
83 } /* namespace lttng */
85 #endif /* LTTNG_FILE_DESCRIPTOR_HPP */