Command.cpp 4.1 KB

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