Command.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "Command.h"
  27. #include <AK/LogStream.h>
  28. #include <AK/ScopeGuard.h>
  29. #include <LibCore/File.h>
  30. #include <fcntl.h>
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <sys/wait.h>
  34. #include <unistd.h>
  35. // #define DBG_FAILED_COMMANDS
  36. namespace Core {
  37. // Only supported in serenity mode because we use `posix_spawn_file_actions_addchdir`
  38. #ifdef __serenity__
  39. String command(const String& command_string, Optional<LexicalPath> chdir)
  40. {
  41. auto parts = command_string.split(' ');
  42. if (parts.is_empty())
  43. return {};
  44. auto program = parts[0];
  45. parts.remove(0);
  46. return command(program, parts, chdir);
  47. }
  48. String command(const String& program, const Vector<String>& arguments, Optional<LexicalPath> chdir)
  49. {
  50. int stdout_pipe[2] = {};
  51. int stderr_pipe[2] = {};
  52. if (pipe2(stdout_pipe, O_CLOEXEC)) {
  53. perror("pipe2");
  54. ASSERT_NOT_REACHED();
  55. }
  56. if (pipe2(stderr_pipe, O_CLOEXEC)) {
  57. perror("pipe2");
  58. ASSERT_NOT_REACHED();
  59. }
  60. auto close_pipes = ScopeGuard([stderr_pipe, stdout_pipe] {
  61. // The write-ends of these pipes are closed manually
  62. close(stdout_pipe[0]);
  63. close(stderr_pipe[0]);
  64. });
  65. Vector<const char*> parts = { program.characters() };
  66. for (const auto& part : arguments) {
  67. parts.append(part.characters());
  68. }
  69. parts.append(nullptr);
  70. const char** argv = parts.data();
  71. posix_spawn_file_actions_t action;
  72. posix_spawn_file_actions_init(&action);
  73. if (chdir.has_value()) {
  74. posix_spawn_file_actions_addchdir(&action, chdir.value().string().characters());
  75. }
  76. posix_spawn_file_actions_adddup2(&action, stdout_pipe[1], STDOUT_FILENO);
  77. posix_spawn_file_actions_adddup2(&action, stderr_pipe[1], STDERR_FILENO);
  78. pid_t pid;
  79. if ((errno = posix_spawnp(&pid, program.characters(), &action, nullptr, const_cast<char**>(argv), environ))) {
  80. perror("posix_spawn");
  81. ASSERT_NOT_REACHED();
  82. }
  83. int wstatus;
  84. waitpid(pid, &wstatus, 0);
  85. posix_spawn_file_actions_destroy(&action);
  86. // close the write-ends so reading wouldn't block
  87. close(stdout_pipe[1]);
  88. close(stderr_pipe[1]);
  89. auto read_all_from_pipe = [](int pipe[2]) {
  90. auto result_file = Core::File::construct();
  91. if (!result_file->open(pipe[0], Core::IODevice::ReadOnly, Core::File::ShouldCloseFileDescription::Yes)) {
  92. perror("open");
  93. ASSERT_NOT_REACHED();
  94. }
  95. return String::copy(result_file->read_all());
  96. };
  97. if (WEXITSTATUS(wstatus) != 0) {
  98. # ifdef DBG_FAILED_COMMANDS
  99. dbg() << "command failed. stderr: " << read_all_from_pipe(stderr_pipe);
  100. # endif
  101. return {};
  102. }
  103. auto result = read_all_from_pipe(stdout_pipe);
  104. if (result.is_null())
  105. return "";
  106. return result;
  107. }
  108. #endif
  109. }