Command.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "Command.h"
  7. #include <AK/Format.h>
  8. #include <AK/ScopeGuard.h>
  9. #include <LibCore/File.h>
  10. #include <fcntl.h>
  11. #include <stdio.h>
  12. #include <sys/wait.h>
  13. #include <unistd.h>
  14. namespace Core {
  15. // Only supported in serenity mode because we use `posix_spawn_file_actions_addchdir`
  16. #ifdef AK_OS_SERENITY
  17. ErrorOr<CommandResult> command(DeprecatedString const& command_string, Optional<LexicalPath> chdir)
  18. {
  19. auto parts = command_string.split(' ');
  20. if (parts.is_empty())
  21. return Error::from_string_literal("empty command");
  22. auto program = parts[0];
  23. parts.remove(0);
  24. return command(program, parts, chdir);
  25. }
  26. ErrorOr<CommandResult> command(DeprecatedString const& program, Vector<DeprecatedString> const& arguments, Optional<LexicalPath> chdir)
  27. {
  28. int stdout_pipe[2] = {};
  29. int stderr_pipe[2] = {};
  30. if (pipe2(stdout_pipe, O_CLOEXEC)) {
  31. return Error::from_errno(errno);
  32. }
  33. if (pipe2(stderr_pipe, O_CLOEXEC)) {
  34. perror("pipe2");
  35. return Error::from_errno(errno);
  36. }
  37. auto close_pipes = ScopeGuard([stderr_pipe, stdout_pipe] {
  38. // The write-ends of these pipes are closed manually
  39. close(stdout_pipe[0]);
  40. close(stderr_pipe[0]);
  41. });
  42. Vector<char const*> parts = { program.characters() };
  43. for (auto const& part : arguments) {
  44. parts.append(part.characters());
  45. }
  46. parts.append(nullptr);
  47. char const** argv = parts.data();
  48. posix_spawn_file_actions_t action;
  49. posix_spawn_file_actions_init(&action);
  50. if (chdir.has_value()) {
  51. posix_spawn_file_actions_addchdir(&action, chdir.value().string().characters());
  52. }
  53. posix_spawn_file_actions_adddup2(&action, stdout_pipe[1], STDOUT_FILENO);
  54. posix_spawn_file_actions_adddup2(&action, stderr_pipe[1], STDERR_FILENO);
  55. pid_t pid;
  56. if ((errno = posix_spawnp(&pid, program.characters(), &action, nullptr, const_cast<char**>(argv), environ))) {
  57. perror("posix_spawn");
  58. VERIFY_NOT_REACHED();
  59. }
  60. // close the write-ends so reading wouldn't block
  61. close(stdout_pipe[1]);
  62. close(stderr_pipe[1]);
  63. auto read_all_from_pipe = [](int pipe[2]) -> ErrorOr<ByteBuffer> {
  64. auto result_file_or_error = Core::File::adopt_fd(pipe[0], Core::File::OpenMode::Read, Core::File::ShouldCloseFileDescriptor::Yes);
  65. auto result_file = TRY(result_file_or_error);
  66. return result_file->read_until_eof();
  67. };
  68. auto output = TRY(read_all_from_pipe(stdout_pipe));
  69. auto error = TRY(read_all_from_pipe(stderr_pipe));
  70. int wstatus { 0 };
  71. waitpid(pid, &wstatus, 0);
  72. posix_spawn_file_actions_destroy(&action);
  73. int exit_code = WEXITSTATUS(wstatus);
  74. if (exit_code != 0) {
  75. # ifdef DBG_FAILED_COMMANDS
  76. dbgln("command failed. stderr: {}", );
  77. # endif
  78. }
  79. return CommandResult { WEXITSTATUS(wstatus), output, error };
  80. }
  81. #endif
  82. }