Command.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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/DeprecatedFile.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]) {
  64. auto result_file = Core::DeprecatedFile::construct();
  65. if (!result_file->open(pipe[0], Core::OpenMode::ReadOnly, Core::DeprecatedFile::ShouldCloseFileDescriptor::Yes)) {
  66. perror("open");
  67. VERIFY_NOT_REACHED();
  68. }
  69. return DeprecatedString::copy(result_file->read_all());
  70. };
  71. auto output = read_all_from_pipe(stdout_pipe);
  72. auto error = read_all_from_pipe(stderr_pipe);
  73. int wstatus { 0 };
  74. waitpid(pid, &wstatus, 0);
  75. posix_spawn_file_actions_destroy(&action);
  76. int exit_code = WEXITSTATUS(wstatus);
  77. if (exit_code != 0) {
  78. # ifdef DBG_FAILED_COMMANDS
  79. dbgln("command failed. stderr: {}", );
  80. # endif
  81. }
  82. return CommandResult { WEXITSTATUS(wstatus), output, error };
  83. }
  84. #endif
  85. }