Fix: sessiond: instance uuid is not sufficiently unique
[lttng-tools.git] / src / common / file-descriptor.hpp
diff --git a/src/common/file-descriptor.hpp b/src/common/file-descriptor.hpp
new file mode 100644 (file)
index 0000000..6354a12
--- /dev/null
@@ -0,0 +1,66 @@
+/*
+ * Copyright (C) 2023 Jérémie Galarneau <jeremie.galarneau@efficios.com>
+ *
+ * SPDX-License-Identifier: LGPL-2.1-only
+ *
+ */
+
+#include <common/error.hpp>
+#include <common/format.hpp>
+
+#include <algorithm>
+
+#include <unistd.h>
+
+namespace lttng {
+
+/*
+ * RAII wrapper around a UNIX file descriptor. A file_descriptor's underlying
+ * file descriptor
+ */
+class file_descriptor {
+public:
+       explicit file_descriptor(int raw_fd) noexcept : _raw_fd{raw_fd}
+       {
+               LTTNG_ASSERT(_is_valid_fd(_raw_fd));
+       }
+
+       file_descriptor(const file_descriptor&) = delete;
+
+       file_descriptor(file_descriptor&& other) : _raw_fd{-1}
+       {
+               LTTNG_ASSERT(_is_valid_fd(_raw_fd));
+               std::swap(_raw_fd, other._raw_fd);
+       }
+
+       ~file_descriptor()
+       {
+               if (!_is_valid_fd(_raw_fd)) {
+                       return;
+               }
+
+               const auto ret = ::close(_raw_fd);
+               if (ret) {
+                       PERROR("%s",
+                                       fmt::format("Failed to close file descriptor: fd = {}",
+                                                       _raw_fd)
+                                                       .c_str());
+               }
+       }
+
+       int fd() const noexcept
+       {
+               LTTNG_ASSERT(_is_valid_fd(_raw_fd));
+               return _raw_fd;
+       }
+
+private:
+       static bool _is_valid_fd(int fd)
+       {
+               return fd >= 0;
+       }
+
+       int _raw_fd;
+};
+
+} /* namespace lttng */
\ No newline at end of file
This page took 0.024237 seconds and 4 git commands to generate.