2 * Copyright (C) 2023 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: LGPL-2.1-only
8 #include <common/error.hpp>
9 #include <common/format.hpp>
18 * RAII wrapper around a UNIX file descriptor. A file_descriptor's underlying
21 class file_descriptor {
23 explicit file_descriptor(int raw_fd) noexcept : _raw_fd{raw_fd}
25 LTTNG_ASSERT(_is_valid_fd(_raw_fd));
28 file_descriptor(const file_descriptor&) = delete;
30 file_descriptor(file_descriptor&& other) : _raw_fd{-1}
32 LTTNG_ASSERT(_is_valid_fd(_raw_fd));
33 std::swap(_raw_fd, other._raw_fd);
38 if (!_is_valid_fd(_raw_fd)) {
42 const auto ret = ::close(_raw_fd);
44 PERROR("Failed to close file descriptor: fd=%i", _raw_fd);
48 int fd() const noexcept
50 LTTNG_ASSERT(_is_valid_fd(_raw_fd));
55 static bool _is_valid_fd(int fd)
63 } /* namespace lttng */