| 
									
										
										
										
											2021-08-06 01:04:11 +02:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2024-10-04 13:19:50 +02:00
										 |  |  |  * Copyright (c) 2021, Andreas Kling <andreas@ladybird.org> | 
					
						
							| 
									
										
										
										
											2022-05-10 00:24:15 +01:00
										 |  |  |  * Copyright (c) 2022, MacDue <macdue@dueutil.tech> | 
					
						
							| 
									
										
										
										
											2023-01-22 19:55:20 +00:00
										 |  |  |  * Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org> | 
					
						
							| 
									
										
										
										
											2024-04-23 16:35:36 -04:00
										 |  |  |  * Copyright (c) 2024, Tim Flynn <trflynn89@serenityos.org> | 
					
						
							| 
									
										
										
										
											2021-08-06 01:04:11 +02:00
										 |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-16 17:49:34 +03:30
										 |  |  | #include <AK/ByteString.h>
 | 
					
						
							| 
									
										
										
										
											2021-08-06 01:04:11 +02:00
										 |  |  | #include <AK/Forward.h>
 | 
					
						
							| 
									
										
										
										
											2022-05-10 00:24:15 +01:00
										 |  |  | #include <AK/Span.h>
 | 
					
						
							| 
									
										
										
										
											2023-11-06 23:26:04 -05:00
										 |  |  | #include <LibCore/File.h>
 | 
					
						
							| 
									
										
										
										
											2021-08-06 01:04:11 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace Core { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-06 23:26:04 -05:00
										 |  |  | namespace FileAction { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | struct OpenFile { | 
					
						
							| 
									
										
										
										
											2023-12-16 17:49:34 +03:30
										 |  |  |     ByteString path; | 
					
						
							| 
									
										
										
										
											2023-11-06 23:26:04 -05:00
										 |  |  |     File::OpenMode mode = File::OpenMode::NotOpen; | 
					
						
							|  |  |  |     int fd = -1; | 
					
						
							|  |  |  |     mode_t permissions = 0600; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-23 15:31:07 -04:00
										 |  |  | struct CloseFile { | 
					
						
							|  |  |  |     int fd { -1 }; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-06 23:26:04 -05:00
										 |  |  | // FIXME: Implement other file actions
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | struct ProcessSpawnOptions { | 
					
						
							| 
									
										
										
										
											2024-04-23 16:35:36 -04:00
										 |  |  |     StringView name {}; | 
					
						
							|  |  |  |     ByteString executable {}; | 
					
						
							| 
									
										
										
										
											2024-01-16 13:42:30 +00:00
										 |  |  |     bool search_for_executable_in_path { false }; | 
					
						
							| 
									
										
										
										
											2024-04-23 15:31:07 -04:00
										 |  |  |     Vector<ByteString> const& arguments {}; | 
					
						
							|  |  |  |     Optional<ByteString> working_directory {}; | 
					
						
							| 
									
										
										
										
											2024-04-23 16:35:36 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     using FileActionType = Variant<FileAction::OpenFile, FileAction::CloseFile>; | 
					
						
							| 
									
										
										
										
											2024-04-27 08:39:41 -04:00
										 |  |  |     Vector<FileActionType> file_actions {}; | 
					
						
							| 
									
										
										
										
											2023-11-06 23:26:04 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-06 01:04:11 +02:00
										 |  |  | class Process { | 
					
						
							| 
									
										
										
										
											2023-11-06 23:26:04 -05:00
										 |  |  |     AK_MAKE_NONCOPYABLE(Process); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-06 01:04:11 +02:00
										 |  |  | public: | 
					
						
							| 
									
										
										
										
											2023-03-15 16:49:13 +00:00
										 |  |  |     enum class KeepAsChild { | 
					
						
							|  |  |  |         Yes, | 
					
						
							|  |  |  |         No | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-06 23:26:04 -05:00
										 |  |  |     Process(Process&& other) | 
					
						
							|  |  |  |         : m_pid(exchange(other.m_pid, 0)) | 
					
						
							|  |  |  |         , m_should_disown(exchange(other.m_should_disown, false)) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-23 19:06:23 -04:00
										 |  |  |     Process& operator=(Process&& other) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         m_pid = exchange(other.m_pid, 0); | 
					
						
							|  |  |  |         m_should_disown = exchange(other.m_should_disown, false); | 
					
						
							|  |  |  |         return *this; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-11-06 23:26:04 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     ~Process() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         (void)disown(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     static ErrorOr<Process> spawn(ProcessSpawnOptions const& options); | 
					
						
							| 
									
										
										
										
											2024-06-29 22:24:01 -06:00
										 |  |  |     static Process current(); | 
					
						
							| 
									
										
										
										
											2023-11-06 23:26:04 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-17 20:11:13 +05:00
										 |  |  |     static ErrorOr<Process> spawn(StringView path, ReadonlySpan<ByteString> arguments, ByteString working_directory = {}, KeepAsChild keep_as_child = KeepAsChild::No); | 
					
						
							|  |  |  |     static ErrorOr<Process> spawn(StringView path, ReadonlySpan<StringView> arguments, ByteString working_directory = {}, KeepAsChild keep_as_child = KeepAsChild::No); | 
					
						
							| 
									
										
										
										
											2023-11-06 23:26:04 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-22 19:55:20 +00:00
										 |  |  |     static ErrorOr<String> get_name(); | 
					
						
							|  |  |  |     enum class SetThreadName { | 
					
						
							|  |  |  |         No, | 
					
						
							|  |  |  |         Yes, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     static ErrorOr<void> set_name(StringView, SetThreadName = SetThreadName::No); | 
					
						
							| 
									
										
										
										
											2023-08-02 10:27:58 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-02 14:09:19 +02:00
										 |  |  |     static void wait_for_debugger_and_break(); | 
					
						
							| 
									
										
										
										
											2023-08-02 10:27:58 +02:00
										 |  |  |     static ErrorOr<bool> is_being_debugged(); | 
					
						
							| 
									
										
										
										
											2023-11-06 23:26:04 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     pid_t pid() const { return m_pid; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ErrorOr<void> disown(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // FIXME: Make it return an exit code.
 | 
					
						
							|  |  |  |     ErrorOr<bool> wait_for_termination(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							| 
									
										
										
										
											2024-11-17 20:11:13 +05:00
										 |  |  |     Process(pid_t pid = -1) | 
					
						
							| 
									
										
										
										
											2023-11-06 23:26:04 -05:00
										 |  |  |         : m_pid(pid) | 
					
						
							|  |  |  |         , m_should_disown(true) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     pid_t m_pid; | 
					
						
							|  |  |  |     bool m_should_disown; | 
					
						
							| 
									
										
										
										
											2021-08-06 01:04:11 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |