Command.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 __serenity__
  17. String command(const String& command_string, Optional<LexicalPath> chdir)
  18. {
  19. auto parts = command_string.split(' ');
  20. if (parts.is_empty())
  21. return {};
  22. auto program = parts[0];
  23. parts.remove(0);
  24. return command(program, parts, chdir);
  25. }
  26. String command(const String& program, const Vector<String>& arguments, Optional<LexicalPath> chdir)
  27. {
  28. int stdout_pipe[2] = {};
  29. int stderr_pipe[2] = {};
  30. if (pipe2(stdout_pipe, O_CLOEXEC)) {
  31. perror("pipe2");
  32. VERIFY_NOT_REACHED();
  33. }
  34. if (pipe2(stderr_pipe, O_CLOEXEC)) {
  35. perror("pipe2");
  36. VERIFY_NOT_REACHED();
  37. }
  38. auto close_pipes = ScopeGuard([stderr_pipe, stdout_pipe] {
  39. // The write-ends of these pipes are closed manually
  40. close(stdout_pipe[0]);
  41. close(stderr_pipe[0]);
  42. });
  43. Vector<const char*> parts = { program.characters() };
  44. for (const auto& part : arguments) {
  45. parts.append(part.characters());
  46. }
  47. parts.append(nullptr);
  48. const char** argv = parts.data();
  49. posix_spawn_file_actions_t action;
  50. posix_spawn_file_actions_init(&action);
  51. if (chdir.has_value()) {
  52. posix_spawn_file_actions_addchdir(&action, chdir.value().string().characters());
  53. }
  54. posix_spawn_file_actions_adddup2(&action, stdout_pipe[1], STDOUT_FILENO);
  55. posix_spawn_file_actions_adddup2(&action, stderr_pipe[1], STDERR_FILENO);
  56. pid_t pid;
  57. if ((errno = posix_spawnp(&pid, program.characters(), &action, nullptr, const_cast<char**>(argv), environ))) {
  58. perror("posix_spawn");
  59. VERIFY_NOT_REACHED();
  60. }
  61. int wstatus;
  62. waitpid(pid, &wstatus, 0);
  63. posix_spawn_file_actions_destroy(&action);
  64. // close the write-ends so reading wouldn't block
  65. close(stdout_pipe[1]);
  66. close(stderr_pipe[1]);
  67. auto read_all_from_pipe = [](int pipe[2]) {
  68. auto result_file = Core::File::construct();
  69. if (!result_file->open(pipe[0], Core::OpenMode::ReadOnly, Core::File::ShouldCloseFileDescriptor::Yes)) {
  70. perror("open");
  71. VERIFY_NOT_REACHED();
  72. }
  73. return String::copy(result_file->read_all());
  74. };
  75. if (WEXITSTATUS(wstatus) != 0) {
  76. # ifdef DBG_FAILED_COMMANDS
  77. dbgln("command failed. stderr: {}", read_all_from_pipe(stderr_pipe));
  78. # endif
  79. return {};
  80. }
  81. auto result = read_all_from_pipe(stdout_pipe);
  82. if (result.is_null())
  83. return "";
  84. return result;
  85. }
  86. #endif
  87. }