TerminalWrapper.cpp 4.6 KB

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