mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 17:52:45 +00:00 
			
		
		
		
	LibCore: Return ErrorOr<pid_t> and support arguments in Process::spawn
This makes the wrapper more like the rest in LibCore, and also removes the annoying limitation of not supporting arguments. There are three overloads one for String, char const *, and StringView argument lists. As long as there are <= 10 arguments the argv list will be allocated inline, otherwise on the heap.
This commit is contained in:
		
							parent
							
								
									0295d79339
								
							
						
					
					
						commit
						3fc0350caf
					
				
					 11 changed files with 77 additions and 28 deletions
				
			
		|  | @ -16,7 +16,7 @@ void KeymapStatusWidget::mousedown_event(GUI::MouseEvent& event) | ||||||
|     if (event.button() != GUI::MouseButton::Primary) |     if (event.button() != GUI::MouseButton::Primary) | ||||||
|         return; |         return; | ||||||
| 
 | 
 | ||||||
|     Core::Process::spawn("/bin/KeyboardSettings"); |     MUST(Core::Process::spawn("/bin/KeyboardSettings")); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| KeymapStatusWindow::KeymapStatusWindow() | KeymapStatusWindow::KeymapStatusWindow() | ||||||
|  |  | ||||||
|  | @ -302,7 +302,7 @@ void BrowserWindow::build_menus() | ||||||
|     settings_menu.add_separator(); |     settings_menu.add_separator(); | ||||||
|     auto open_settings_action = GUI::Action::create("&Settings", Gfx::Bitmap::try_load_from_file("/res/icons/16x16/settings.png").release_value_but_fixme_should_propagate_errors(), |     auto open_settings_action = GUI::Action::create("&Settings", Gfx::Bitmap::try_load_from_file("/res/icons/16x16/settings.png").release_value_but_fixme_should_propagate_errors(), | ||||||
|         [](auto&) { |         [](auto&) { | ||||||
|             Core::Process::spawn("/bin/BrowserSettings"); |             MUST(Core::Process::spawn("/bin/BrowserSettings")); | ||||||
|         }); |         }); | ||||||
|     settings_menu.add_action(move(open_settings_action)); |     settings_menu.add_action(move(open_settings_action)); | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -104,7 +104,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments) | ||||||
| 
 | 
 | ||||||
|         auto launch_origin_rect = icon_view->to_widget_rect(icon_view->content_rect(index)).translated(icon_view->screen_relative_rect().location()); |         auto launch_origin_rect = icon_view->to_widget_rect(icon_view->content_rect(index)).translated(icon_view->screen_relative_rect().location()); | ||||||
|         setenv("__libgui_launch_origin_rect", String::formatted("{},{},{},{}", launch_origin_rect.x(), launch_origin_rect.y(), launch_origin_rect.width(), launch_origin_rect.height()).characters(), 1); |         setenv("__libgui_launch_origin_rect", String::formatted("{},{},{},{}", launch_origin_rect.x(), launch_origin_rect.y(), launch_origin_rect.width(), launch_origin_rect.height()).characters(), 1); | ||||||
|         Core::Process::spawn(executable); |         MUST(Core::Process::spawn(executable)); | ||||||
|     }; |     }; | ||||||
| 
 | 
 | ||||||
|     auto statusbar = TRY(main_widget->try_add<GUI::Statusbar>()); |     auto statusbar = TRY(main_widget->try_add<GUI::Statusbar>()); | ||||||
|  |  | ||||||
|  | @ -328,7 +328,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments) | ||||||
| 
 | 
 | ||||||
|     auto open_settings_action = GUI::Action::create("&Settings", Gfx::Bitmap::try_load_from_file("/res/icons/16x16/settings.png").release_value_but_fixme_should_propagate_errors(), |     auto open_settings_action = GUI::Action::create("&Settings", Gfx::Bitmap::try_load_from_file("/res/icons/16x16/settings.png").release_value_but_fixme_should_propagate_errors(), | ||||||
|         [&](auto&) { |         [&](auto&) { | ||||||
|             Core::Process::spawn("/bin/TerminalSettings"); |             MUST(Core::Process::spawn("/bin/TerminalSettings")); | ||||||
|         }); |         }); | ||||||
| 
 | 
 | ||||||
|     TRY(terminal->context_menu().try_add_separator()); |     TRY(terminal->context_menu().try_add_separator()); | ||||||
|  | @ -336,7 +336,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments) | ||||||
| 
 | 
 | ||||||
|     auto file_menu = TRY(window->try_add_menu("&File")); |     auto file_menu = TRY(window->try_add_menu("&File")); | ||||||
|     TRY(file_menu->try_add_action(GUI::Action::create("Open New &Terminal", { Mod_Ctrl | Mod_Shift, Key_N }, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/app-terminal.png").release_value_but_fixme_should_propagate_errors(), [&](auto&) { |     TRY(file_menu->try_add_action(GUI::Action::create("Open New &Terminal", { Mod_Ctrl | Mod_Shift, Key_N }, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/app-terminal.png").release_value_but_fixme_should_propagate_errors(), [&](auto&) { | ||||||
|         Core::Process::spawn("/bin/Terminal"); |         MUST(Core::Process::spawn("/bin/Terminal")); | ||||||
|     }))); |     }))); | ||||||
| 
 | 
 | ||||||
|     TRY(file_menu->try_add_action(open_settings_action)); |     TRY(file_menu->try_add_action(open_settings_action)); | ||||||
|  |  | ||||||
|  | @ -54,7 +54,7 @@ WelcomeWidget::WelcomeWidget() | ||||||
|     m_help_button = *find_descendant_of_type_named<GUI::Button>("help_button"); |     m_help_button = *find_descendant_of_type_named<GUI::Button>("help_button"); | ||||||
|     m_help_button->set_icon(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/book-open.png").release_value_but_fixme_should_propagate_errors()); |     m_help_button->set_icon(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/book-open.png").release_value_but_fixme_should_propagate_errors()); | ||||||
|     m_help_button->on_click = [](auto) { |     m_help_button->on_click = [](auto) { | ||||||
|         Core::Process::spawn("/bin/Help"sv); |         MUST(Core::Process::spawn("/bin/Help"sv)); | ||||||
|     }; |     }; | ||||||
| 
 | 
 | ||||||
|     m_new_button = *find_descendant_of_type_named<GUI::Button>("new_button"); |     m_new_button = *find_descendant_of_type_named<GUI::Button>("new_button"); | ||||||
|  |  | ||||||
|  | @ -1,11 +1,14 @@ | ||||||
| /*
 | /*
 | ||||||
|  * Copyright (c) 2021, Andreas Kling <kling@serenityos.org> |  * Copyright (c) 2021, Andreas Kling <kling@serenityos.org> | ||||||
|  |  * Copyright (c) 2022, MacDue <macdue@dueutil.tech> | ||||||
|  * |  * | ||||||
|  * SPDX-License-Identifier: BSD-2-Clause |  * SPDX-License-Identifier: BSD-2-Clause | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| #include <AK/String.h> | #include <AK/String.h> | ||||||
|  | #include <AK/Vector.h> | ||||||
| #include <LibCore/Process.h> | #include <LibCore/Process.h> | ||||||
|  | #include <LibCore/System.h> | ||||||
| #include <errno.h> | #include <errno.h> | ||||||
| #include <spawn.h> | #include <spawn.h> | ||||||
| 
 | 
 | ||||||
|  | @ -17,21 +20,65 @@ extern char** environ; | ||||||
| 
 | 
 | ||||||
| namespace Core { | namespace Core { | ||||||
| 
 | 
 | ||||||
| pid_t Process::spawn(StringView path) | struct ArgvList { | ||||||
| { |     String m_path; | ||||||
|     String path_string = path; |     Vector<char const*, 10> m_argv; | ||||||
| 
 | 
 | ||||||
|     pid_t pid; |     ArgvList(String path, size_t size) | ||||||
|     char const* argv[] = { path_string.characters(), nullptr }; |         : m_path { path } | ||||||
|     if ((errno = posix_spawn(&pid, path_string.characters(), nullptr, nullptr, const_cast<char**>(argv), environ))) { |     { | ||||||
|         perror("Process::spawn posix_spawn"); |         m_argv.ensure_capacity(size + 2); | ||||||
|     } else { |         m_argv.append(m_path.characters()); | ||||||
| #ifdef __serenity__ |  | ||||||
|         if (disown(pid) < 0) |  | ||||||
|             perror("Process::spawn disown"); |  | ||||||
| #endif |  | ||||||
|     } |     } | ||||||
|     return pid; | 
 | ||||||
|  |     void append(char const* arg) | ||||||
|  |     { | ||||||
|  |         m_argv.append(arg); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     Span<char const*> get() | ||||||
|  |     { | ||||||
|  |         if (m_argv.is_empty() || m_argv.last() != nullptr) | ||||||
|  |             m_argv.append(nullptr); | ||||||
|  |         return m_argv; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     ErrorOr<pid_t> spawn() | ||||||
|  |     { | ||||||
|  |         auto pid = TRY(System::posix_spawn(m_path.characters(), nullptr, nullptr, const_cast<char**>(get().data()), environ)); | ||||||
|  | #ifdef __serenity__ | ||||||
|  |         TRY(System::disown(pid)); | ||||||
|  | #endif | ||||||
|  |         return pid; | ||||||
|  |     } | ||||||
|  | }; | ||||||
|  | 
 | ||||||
|  | ErrorOr<pid_t> Process::spawn(StringView path, Span<String const> arguments) | ||||||
|  | { | ||||||
|  |     ArgvList argv { path, arguments.size() }; | ||||||
|  |     for (auto const& arg : arguments) | ||||||
|  |         argv.append(arg.characters()); | ||||||
|  |     return argv.spawn(); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | ErrorOr<pid_t> Process::spawn(StringView path, Span<StringView const> arguments) | ||||||
|  | { | ||||||
|  |     Vector<String> backing_strings; | ||||||
|  |     backing_strings.ensure_capacity(arguments.size()); | ||||||
|  |     ArgvList argv { path, arguments.size() }; | ||||||
|  |     for (auto const& arg : arguments) { | ||||||
|  |         backing_strings.append(arg); | ||||||
|  |         argv.append(backing_strings.last().characters()); | ||||||
|  |     } | ||||||
|  |     return argv.spawn(); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | ErrorOr<pid_t> Process::spawn(StringView path, Span<char const* const> arguments) | ||||||
|  | { | ||||||
|  |     ArgvList argv { path, arguments.size() }; | ||||||
|  |     for (auto arg : arguments) | ||||||
|  |         argv.append(arg); | ||||||
|  |     return argv.spawn(); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -1,5 +1,6 @@ | ||||||
| /*
 | /*
 | ||||||
|  * Copyright (c) 2021, Andreas Kling <kling@serenityos.org> |  * Copyright (c) 2021, Andreas Kling <kling@serenityos.org> | ||||||
|  |  * Copyright (c) 2022, MacDue <macdue@dueutil.tech> | ||||||
|  * |  * | ||||||
|  * SPDX-License-Identifier: BSD-2-Clause |  * SPDX-License-Identifier: BSD-2-Clause | ||||||
|  */ |  */ | ||||||
|  | @ -7,12 +8,15 @@ | ||||||
| #pragma once | #pragma once | ||||||
| 
 | 
 | ||||||
| #include <AK/Forward.h> | #include <AK/Forward.h> | ||||||
|  | #include <AK/Span.h> | ||||||
| 
 | 
 | ||||||
| namespace Core { | namespace Core { | ||||||
| 
 | 
 | ||||||
| class Process { | class Process { | ||||||
| public: | public: | ||||||
|     static pid_t spawn(StringView path); |     static ErrorOr<pid_t> spawn(StringView path, Span<String const> arguments); | ||||||
|  |     static ErrorOr<pid_t> spawn(StringView path, Span<StringView const> arguments); | ||||||
|  |     static ErrorOr<pid_t> spawn(StringView path, Span<char const* const> arguments = {}); | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -130,7 +130,7 @@ bool AppFile::spawn() const | ||||||
|         return false; |         return false; | ||||||
| 
 | 
 | ||||||
|     auto pid = Core::Process::spawn(executable()); |     auto pid = Core::Process::spawn(executable()); | ||||||
|     if (pid < 0) |     if (pid.is_error()) | ||||||
|         return false; |         return false; | ||||||
| 
 | 
 | ||||||
|     return true; |     return true; | ||||||
|  |  | ||||||
|  | @ -154,7 +154,7 @@ ClockWidget::ClockWidget() | ||||||
|     m_calendar_launcher->set_icon(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/app-calendar.png").release_value_but_fixme_should_propagate_errors()); |     m_calendar_launcher->set_icon(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/app-calendar.png").release_value_but_fixme_should_propagate_errors()); | ||||||
|     m_calendar_launcher->set_tooltip("Calendar"); |     m_calendar_launcher->set_tooltip("Calendar"); | ||||||
|     m_calendar_launcher->on_click = [](auto) { |     m_calendar_launcher->on_click = [](auto) { | ||||||
|         Core::Process::spawn("/bin/Calendar"sv); |         MUST(Core::Process::spawn("/bin/Calendar")); | ||||||
|     }; |     }; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -48,9 +48,7 @@ ErrorOr<void> QuickLaunchEntryAppFile::launch() const | ||||||
| 
 | 
 | ||||||
| ErrorOr<void> QuickLaunchEntryExecutable::launch() const | ErrorOr<void> QuickLaunchEntryExecutable::launch() const | ||||||
| { | { | ||||||
|     auto pid = Core::Process::spawn(m_path); |     TRY(Core::Process::spawn(m_path)); | ||||||
|     if (pid < 0) |  | ||||||
|         return Error::from_syscall("Core::Process::spawn", -errno); |  | ||||||
|     return {}; |     return {}; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -109,7 +109,7 @@ ErrorOr<NonnullRefPtr<GUI::Menu>> build_system_menu() | ||||||
|     auto system_menu = TRY(GUI::Menu::try_create("\xE2\x9A\xA1")); // HIGH VOLTAGE SIGN
 |     auto system_menu = TRY(GUI::Menu::try_create("\xE2\x9A\xA1")); // HIGH VOLTAGE SIGN
 | ||||||
| 
 | 
 | ||||||
|     system_menu->add_action(GUI::Action::create("&About SerenityOS", Gfx::Bitmap::try_load_from_file("/res/icons/16x16/ladyball.png").release_value_but_fixme_should_propagate_errors(), [](auto&) { |     system_menu->add_action(GUI::Action::create("&About SerenityOS", Gfx::Bitmap::try_load_from_file("/res/icons/16x16/ladyball.png").release_value_but_fixme_should_propagate_errors(), [](auto&) { | ||||||
|         Core::Process::spawn("/bin/About"sv); |         MUST(Core::Process::spawn("/bin/About"sv)); | ||||||
|     })); |     })); | ||||||
| 
 | 
 | ||||||
|     system_menu->add_separator(); |     system_menu->add_separator(); | ||||||
|  | @ -229,12 +229,12 @@ ErrorOr<NonnullRefPtr<GUI::Menu>> build_system_menu() | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     system_menu->add_action(GUI::Action::create("&Settings", Gfx::Bitmap::try_load_from_file("/res/icons/16x16/app-settings.png").release_value_but_fixme_should_propagate_errors(), [](auto&) { |     system_menu->add_action(GUI::Action::create("&Settings", Gfx::Bitmap::try_load_from_file("/res/icons/16x16/app-settings.png").release_value_but_fixme_should_propagate_errors(), [](auto&) { | ||||||
|         Core::Process::spawn("/bin/Settings"sv); |         MUST(Core::Process::spawn("/bin/Settings"sv)); | ||||||
|     })); |     })); | ||||||
| 
 | 
 | ||||||
|     system_menu->add_separator(); |     system_menu->add_separator(); | ||||||
|     system_menu->add_action(GUI::Action::create("&Help", Gfx::Bitmap::try_load_from_file("/res/icons/16x16/app-help.png").release_value_but_fixme_should_propagate_errors(), [](auto&) { |     system_menu->add_action(GUI::Action::create("&Help", Gfx::Bitmap::try_load_from_file("/res/icons/16x16/app-help.png").release_value_but_fixme_should_propagate_errors(), [](auto&) { | ||||||
|         Core::Process::spawn("/bin/Help"sv); |         MUST(Core::Process::spawn("/bin/Help"sv)); | ||||||
|     })); |     })); | ||||||
|     system_menu->add_action(GUI::Action::create("&Run...", Gfx::Bitmap::try_load_from_file("/res/icons/16x16/app-run.png").release_value_but_fixme_should_propagate_errors(), [](auto&) { |     system_menu->add_action(GUI::Action::create("&Run...", Gfx::Bitmap::try_load_from_file("/res/icons/16x16/app-run.png").release_value_but_fixme_should_propagate_errors(), [](auto&) { | ||||||
|         posix_spawn_file_actions_t spawn_actions; |         posix_spawn_file_actions_t spawn_actions; | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 MacDue
						MacDue