TerminalWrapper.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "TerminalWrapper.h"
  7. #include <AK/String.h>
  8. #include <LibCore/ConfigFile.h>
  9. #include <LibGUI/Application.h>
  10. #include <LibGUI/BoxLayout.h>
  11. #include <LibGUI/MessageBox.h>
  12. #include <LibVT/TerminalWidget.h>
  13. #include <fcntl.h>
  14. #include <signal.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <sys/ioctl.h>
  19. #include <sys/types.h>
  20. #include <sys/wait.h>
  21. #include <unistd.h>
  22. namespace HackStudio {
  23. void TerminalWrapper::run_command(const String& command, Optional<String> working_directory, WaitForExit wait_for_exit)
  24. {
  25. if (m_pid != -1) {
  26. GUI::MessageBox::show(window(),
  27. "A command is already running in this TerminalWrapper",
  28. "Can't run command",
  29. GUI::MessageBox::Type::Error);
  30. return;
  31. }
  32. auto ptm_res = setup_master_pseudoterminal();
  33. if (ptm_res.is_error()) {
  34. perror("setup_master_pseudoterminal");
  35. return;
  36. }
  37. int ptm_fd = ptm_res.value();
  38. m_child_exited = false;
  39. m_child_exit_status.clear();
  40. m_pid = fork();
  41. if (m_pid < 0) {
  42. perror("fork");
  43. return;
  44. }
  45. if (m_pid > 0) {
  46. if (wait_for_exit == WaitForExit::Yes) {
  47. GUI::Application::the()->event_loop().spin_until([this]() {
  48. return m_child_exited;
  49. });
  50. }
  51. return;
  52. }
  53. if (working_directory.has_value()) {
  54. if (chdir(working_directory->characters())) {
  55. perror("chdir");
  56. exit(1);
  57. }
  58. }
  59. if (setup_slave_pseudoterminal(ptm_fd).is_error()) {
  60. perror("setup_pseudoterminal");
  61. exit(1);
  62. }
  63. auto parts = command.split(' ');
  64. VERIFY(!parts.is_empty());
  65. const char** args = (const char**)calloc(parts.size() + 1, sizeof(const char*));
  66. for (size_t i = 0; i < parts.size(); i++) {
  67. args[i] = parts[i].characters();
  68. }
  69. auto rc = execvp(args[0], const_cast<char**>(args));
  70. if (rc < 0) {
  71. perror("execve");
  72. exit(1);
  73. }
  74. VERIFY_NOT_REACHED();
  75. }
  76. ErrorOr<int> TerminalWrapper::setup_master_pseudoterminal(WaitForChildOnExit wait_for_child)
  77. {
  78. int ptm_fd = posix_openpt(O_RDWR | O_CLOEXEC);
  79. if (ptm_fd < 0) {
  80. perror("posix_openpt");
  81. VERIFY_NOT_REACHED();
  82. }
  83. if (grantpt(ptm_fd) < 0) {
  84. perror("grantpt");
  85. VERIFY_NOT_REACHED();
  86. }
  87. if (unlockpt(ptm_fd) < 0) {
  88. perror("unlockpt");
  89. VERIFY_NOT_REACHED();
  90. }
  91. m_terminal_widget->set_pty_master_fd(ptm_fd);
  92. m_terminal_widget->on_command_exit = [this, wait_for_child] {
  93. if (wait_for_child == WaitForChildOnExit::Yes) {
  94. int wstatus;
  95. int rc = waitpid(m_pid, &wstatus, 0);
  96. if (rc < 0) {
  97. perror("waitpid");
  98. VERIFY_NOT_REACHED();
  99. }
  100. if (WIFEXITED(wstatus)) {
  101. m_terminal_widget->inject_string(String::formatted("\033[{};1m(Command exited with code {})\033[0m\r\n", wstatus == 0 ? 32 : 31, WEXITSTATUS(wstatus)));
  102. } else if (WIFSTOPPED(wstatus)) {
  103. m_terminal_widget->inject_string("\033[34;1m(Command stopped!)\033[0m\r\n");
  104. } else if (WIFSIGNALED(wstatus)) {
  105. m_terminal_widget->inject_string(String::formatted("\033[34;1m(Command signaled with {}!)\033[0m\r\n", strsignal(WTERMSIG(wstatus))));
  106. }
  107. m_child_exit_status = WEXITSTATUS(wstatus);
  108. m_child_exited = true;
  109. }
  110. m_pid = -1;
  111. if (on_command_exit)
  112. on_command_exit();
  113. };
  114. terminal().scroll_to_bottom();
  115. return ptm_fd;
  116. }
  117. ErrorOr<void> TerminalWrapper::setup_slave_pseudoterminal(int master_fd)
  118. {
  119. setsid();
  120. const char* tty_name = ptsname(master_fd);
  121. if (!tty_name)
  122. return Error::from_errno(errno);
  123. close(master_fd);
  124. int pts_fd = open(tty_name, O_RDWR);
  125. if (pts_fd < 0)
  126. return Error::from_errno(errno);
  127. tcsetpgrp(pts_fd, getpid());
  128. // NOTE: It's okay if this fails.
  129. int rc = ioctl(0, TIOCNOTTY);
  130. close(0);
  131. close(1);
  132. close(2);
  133. rc = dup2(pts_fd, 0);
  134. if (rc < 0)
  135. return Error::from_errno(errno);
  136. rc = dup2(pts_fd, 1);
  137. if (rc < 0)
  138. return Error::from_errno(errno);
  139. rc = dup2(pts_fd, 2);
  140. if (rc < 0)
  141. return Error::from_errno(errno);
  142. rc = close(pts_fd);
  143. if (rc < 0)
  144. return Error::from_errno(errno);
  145. rc = ioctl(0, TIOCSCTTY);
  146. if (rc < 0)
  147. return Error::from_errno(errno);
  148. setenv("TERM", "xterm", true);
  149. return {};
  150. }
  151. void TerminalWrapper::kill_running_command()
  152. {
  153. VERIFY(m_pid != -1);
  154. // Kill our child process and its whole process group.
  155. [[maybe_unused]] auto rc = killpg(m_pid, SIGTERM);
  156. }
  157. void TerminalWrapper::clear_including_history()
  158. {
  159. m_terminal_widget->clear_including_history();
  160. }
  161. TerminalWrapper::TerminalWrapper(bool user_spawned)
  162. : m_user_spawned(user_spawned)
  163. {
  164. set_layout<GUI::VerticalBoxLayout>();
  165. m_terminal_widget = add<VT::TerminalWidget>(-1, false);
  166. if (user_spawned)
  167. run_command("Shell");
  168. }
  169. TerminalWrapper::~TerminalWrapper()
  170. {
  171. }
  172. int TerminalWrapper::child_exit_status() const
  173. {
  174. VERIFY(m_child_exit_status.has_value());
  175. return m_child_exit_status.value();
  176. }
  177. }