HelperProcess.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334
  1. /*
  2. * Copyright (c) 2023, Andrew Kaster <akaster@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "HelperProcess.h"
  7. #include "Utilities.h"
  8. #include <AK/String.h>
  9. #include <QCoreApplication>
  10. ErrorOr<void> spawn_helper_process(StringView process_name, Span<StringView> arguments, Core::System::SearchInPath search_in_path, Optional<Span<StringView const>> environment)
  11. {
  12. auto paths = TRY(get_paths_for_helper_process(process_name));
  13. VERIFY(!paths.is_empty());
  14. ErrorOr<void> result;
  15. for (auto const& path : paths) {
  16. result = Core::System::exec(path, arguments, search_in_path, environment);
  17. if (!result.is_error())
  18. break;
  19. }
  20. return result;
  21. }
  22. ErrorOr<Vector<String>> get_paths_for_helper_process(StringView process_name)
  23. {
  24. Vector<String> paths;
  25. TRY(paths.try_append(TRY(String::formatted("./{}/{}", process_name, process_name))));
  26. TRY(paths.try_append(TRY(String::formatted("{}/{}", TRY(ak_string_from_qstring(QCoreApplication::applicationDirPath())), process_name))));
  27. TRY(paths.try_append(TRY(String::formatted("./{}", process_name))));
  28. // NOTE: Add platform-specific paths here
  29. return paths;
  30. }