| 
									
										
										
										
											2020-11-21 21:59:12 +03:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2020, Sergey Bugaev <bugaevc@serenityos.org> | 
					
						
							| 
									
										
										
										
											2021-01-14 09:31:21 +01:00
										 |  |  |  * Copyright (c) 2021, Andreas Kling <kling@serenityos.org> | 
					
						
							| 
									
										
										
										
											2020-11-21 21:59:12 +03:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2021-04-22 01:24:48 -07:00
										 |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							| 
									
										
										
										
											2020-11-21 21:59:12 +03:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-14 09:31:21 +01:00
										 |  |  | #include <AK/Noncopyable.h>
 | 
					
						
							|  |  |  | #include <AK/StdLibExtras.h>
 | 
					
						
							| 
									
										
										
										
											2022-12-17 17:43:36 +01:00
										 |  |  | #include <LibCore/Stream.h>
 | 
					
						
							| 
									
										
										
										
											2021-01-14 09:31:21 +01:00
										 |  |  | #include <unistd.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-21 21:59:12 +03:00
										 |  |  | namespace IPC { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class File { | 
					
						
							| 
									
										
										
										
											2021-01-14 09:31:21 +01:00
										 |  |  |     AK_MAKE_NONCOPYABLE(File); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-21 21:59:12 +03:00
										 |  |  | public: | 
					
						
							|  |  |  |     // Must have a default constructor, because LibIPC
 | 
					
						
							|  |  |  |     // default-constructs arguments prior to decoding them.
 | 
					
						
							| 
									
										
										
										
											2021-09-16 00:01:30 -07:00
										 |  |  |     File() = default; | 
					
						
							| 
									
										
										
										
											2020-11-21 21:59:12 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // Intentionally not `explicit`.
 | 
					
						
							|  |  |  |     File(int fd) | 
					
						
							|  |  |  |         : m_fd(fd) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-14 09:31:21 +01:00
										 |  |  |     // Tagged constructor for fd's that should be closed on destruction unless take_fd() is called.
 | 
					
						
							| 
									
										
										
										
											2021-01-14 09:39:30 +01:00
										 |  |  |     // Note that the tags are the same, this is intentional to allow expressive invocation.
 | 
					
						
							| 
									
										
										
										
											2021-01-14 09:31:21 +01:00
										 |  |  |     enum Tag { | 
					
						
							|  |  |  |         ConstructWithReceivedFileDescriptor = 1, | 
					
						
							| 
									
										
										
										
											2021-01-14 09:39:30 +01:00
										 |  |  |         CloseAfterSending = 1, | 
					
						
							| 
									
										
										
										
											2021-01-14 09:31:21 +01:00
										 |  |  |     }; | 
					
						
							|  |  |  |     File(int fd, Tag) | 
					
						
							|  |  |  |         : m_fd(fd) | 
					
						
							|  |  |  |         , m_close_on_destruction(true) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-17 17:43:36 +01:00
										 |  |  |     template<typename... Args> | 
					
						
							|  |  |  |     File(Core::Stream::File& file, Args... args) | 
					
						
							|  |  |  |         : File(file.leak_fd(Badge<File> {}), args...) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-14 09:31:21 +01:00
										 |  |  |     File(File&& other) | 
					
						
							|  |  |  |         : m_fd(exchange(other.m_fd, -1)) | 
					
						
							|  |  |  |         , m_close_on_destruction(exchange(other.m_close_on_destruction, false)) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     File& operator=(File&& other) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (this != &other) { | 
					
						
							|  |  |  |             m_fd = exchange(other.m_fd, -1); | 
					
						
							|  |  |  |             m_close_on_destruction = exchange(other.m_close_on_destruction, false); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return *this; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ~File() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (m_close_on_destruction && m_fd != -1) | 
					
						
							|  |  |  |             close(m_fd); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-21 21:59:12 +03:00
										 |  |  |     int fd() const { return m_fd; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-14 09:31:21 +01:00
										 |  |  |     // NOTE: This is 'const' since generated IPC messages expose all parameters by const reference.
 | 
					
						
							|  |  |  |     [[nodiscard]] int take_fd() const | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return exchange(m_fd, -1); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-21 21:59:12 +03:00
										 |  |  | private: | 
					
						
							| 
									
										
										
										
											2021-01-14 09:31:21 +01:00
										 |  |  |     mutable int m_fd { -1 }; | 
					
						
							|  |  |  |     bool m_close_on_destruction { false }; | 
					
						
							| 
									
										
										
										
											2020-11-21 21:59:12 +03:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |