| 
									
										
										
										
											2020-11-21 21:59:12 +03:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2020, Sergey Bugaev <bugaevc@serenityos.org> | 
					
						
							| 
									
										
										
										
											2024-10-04 13:19:50 +02:00
										 |  |  |  * Copyright (c) 2021, Andreas Kling <andreas@ladybird.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>
 | 
					
						
							| 
									
										
										
										
											2023-02-09 03:02:46 +01:00
										 |  |  | #include <LibCore/File.h>
 | 
					
						
							| 
									
										
										
										
											2024-04-16 13:46:30 -06:00
										 |  |  | #include <LibCore/System.h>
 | 
					
						
							| 
									
										
										
										
											2021-01-14 09:31:21 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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: | 
					
						
							| 
									
										
										
										
											2021-09-16 00:01:30 -07:00
										 |  |  |     File() = default; | 
					
						
							| 
									
										
										
										
											2020-11-21 21:59:12 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-16 13:46:30 -06:00
										 |  |  |     static File adopt_file(NonnullOwnPtr<Core::File> file) | 
					
						
							| 
									
										
										
										
											2020-11-21 21:59:12 +03:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-04-16 13:46:30 -06:00
										 |  |  |         return File(file->leak_fd(Badge<File> {})); | 
					
						
							| 
									
										
										
										
											2020-11-21 21:59:12 +03:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-16 13:46:30 -06:00
										 |  |  |     static File adopt_fd(int fd) | 
					
						
							| 
									
										
										
										
											2021-01-14 09:31:21 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-04-16 13:46:30 -06:00
										 |  |  |         return File(fd); | 
					
						
							| 
									
										
										
										
											2021-01-14 09:31:21 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-16 13:46:30 -06:00
										 |  |  |     static ErrorOr<File> clone_fd(int fd) | 
					
						
							| 
									
										
										
										
											2022-12-17 17:43:36 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-04-16 13:46:30 -06:00
										 |  |  |         int new_fd = TRY(Core::System::dup(fd)); | 
					
						
							|  |  |  |         return File(new_fd); | 
					
						
							| 
									
										
										
										
											2022-12-17 17:43:36 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-14 09:31:21 +01:00
										 |  |  |     File(File&& other) | 
					
						
							|  |  |  |         : m_fd(exchange(other.m_fd, -1)) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     File& operator=(File&& other) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (this != &other) { | 
					
						
							|  |  |  |             m_fd = exchange(other.m_fd, -1); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return *this; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ~File() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-04-16 13:46:30 -06:00
										 |  |  |         if (m_fd != -1) | 
					
						
							|  |  |  |             (void)Core::System::close(m_fd); | 
					
						
							| 
									
										
										
										
											2021-01-14 09:31:21 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-25 14:57:16 -06:00
										 |  |  |     // FIXME: IPC::Files transferred over the wire are always set O_CLOEXEC during decoding.
 | 
					
						
							|  |  |  |     //        Perhaps we should add an option to IPC::File to allow the receiver to decide whether to
 | 
					
						
							|  |  |  |     //        make it O_CLOEXEC or not. Or an attribute in the .ipc file?
 | 
					
						
							|  |  |  |     ErrorOr<void> clear_close_on_exec() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         auto fd_flags = TRY(Core::System::fcntl(m_fd, F_GETFD)); | 
					
						
							|  |  |  |         fd_flags &= ~FD_CLOEXEC; | 
					
						
							|  |  |  |         TRY(Core::System::fcntl(m_fd, F_SETFD, fd_flags)); | 
					
						
							|  |  |  |         return {}; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-21 21:59:12 +03:00
										 |  |  | private: | 
					
						
							| 
									
										
										
										
											2024-04-16 13:46:30 -06:00
										 |  |  |     explicit File(int fd) | 
					
						
							|  |  |  |         : m_fd(fd) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-14 09:31:21 +01:00
										 |  |  |     mutable int m_fd { -1 }; | 
					
						
							| 
									
										
										
										
											2020-11-21 21:59:12 +03:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |