| 1 | /* |
| 2 | * Copyright (C) 2023 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 3 | * |
| 4 | * SPDX-License-Identifier: LGPL-2.1-only |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #ifndef LTTNG_FILE_DESCRIPTOR_HPP |
| 9 | #define LTTNG_FILE_DESCRIPTOR_HPP |
| 10 | |
| 11 | #include <cstddef> |
| 12 | |
| 13 | namespace lttng { |
| 14 | |
| 15 | /* RAII wrapper around a UNIX file descriptor. */ |
| 16 | class file_descriptor { |
| 17 | public: |
| 18 | file_descriptor() noexcept; |
| 19 | |
| 20 | explicit file_descriptor(int raw_fd) noexcept; |
| 21 | file_descriptor(const file_descriptor&) = delete; |
| 22 | file_descriptor& operator=(const file_descriptor&) = delete; |
| 23 | |
| 24 | file_descriptor(file_descriptor&& other) noexcept; |
| 25 | |
| 26 | file_descriptor& operator=(file_descriptor&& other) noexcept; |
| 27 | |
| 28 | ~file_descriptor() noexcept; |
| 29 | |
| 30 | /* |
| 31 | * Read `size` bytes from the underlying file descriptor, assuming |
| 32 | * raw_fd behaves as a blocking device. |
| 33 | * |
| 34 | * Throws an exception if the requested amount of bytes couldn't be read. |
| 35 | */ |
| 36 | void read(void *buffer, std::size_t size); |
| 37 | /* |
| 38 | * Write `size` bytes to the underlying file descriptor, assuming |
| 39 | * raw_fd behaves as a blocking device. |
| 40 | * |
| 41 | * Throws an exception if the requested amount of bytes couldn't be written. |
| 42 | */ |
| 43 | void write(const void *buffer, std::size_t size); |
| 44 | |
| 45 | int fd() const noexcept; |
| 46 | |
| 47 | protected: |
| 48 | void _cleanup() noexcept; |
| 49 | |
| 50 | private: |
| 51 | int _raw_fd = -1; |
| 52 | }; |
| 53 | |
| 54 | } /* namespace lttng */ |
| 55 | |
| 56 | #endif /* LTTNG_FILE_DESCRIPTOR_HPP */ |