Command.cpp 2.8 KB

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