TerminalWrapper.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  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 "TerminalWrapper.h"
  27. #include "ProcessStateWidget.h"
  28. #include <AK/String.h>
  29. #include <LibCore/ConfigFile.h>
  30. #include <LibGUI/GBoxLayout.h>
  31. #include <LibGUI/GMessageBox.h>
  32. #include <LibVT/TerminalWidget.h>
  33. #include <fcntl.h>
  34. #include <signal.h>
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <sys/ioctl.h>
  38. #include <sys/types.h>
  39. #include <sys/wait.h>
  40. #include <unistd.h>
  41. void TerminalWrapper::run_command(const String& command)
  42. {
  43. if (m_pid != -1) {
  44. GUI::MessageBox::show(
  45. "A command is already running in this TerminalWrapper",
  46. "Can't run command",
  47. GUI::MessageBox::Type::Error,
  48. GUI::MessageBox::InputType::OK,
  49. window());
  50. return;
  51. }
  52. int ptm_fd = posix_openpt(O_RDWR | O_CLOEXEC);
  53. if (ptm_fd < 0) {
  54. perror("posix_openpt");
  55. ASSERT_NOT_REACHED();
  56. }
  57. if (grantpt(ptm_fd) < 0) {
  58. perror("grantpt");
  59. ASSERT_NOT_REACHED();
  60. }
  61. if (unlockpt(ptm_fd) < 0) {
  62. perror("unlockpt");
  63. ASSERT_NOT_REACHED();
  64. }
  65. m_terminal_widget->set_pty_master_fd(ptm_fd);
  66. m_terminal_widget->on_command_exit = [this] {
  67. int wstatus;
  68. int rc = waitpid(m_pid, &wstatus, 0);
  69. if (rc < 0) {
  70. perror("waitpid");
  71. ASSERT_NOT_REACHED();
  72. }
  73. if (WIFEXITED(wstatus)) {
  74. m_terminal_widget->inject_string(String::format("\033[%d;1m(Command exited with code %d)\033[0m\n", wstatus == 0 ? 32 : 31, WEXITSTATUS(wstatus)));
  75. } else if (WIFSTOPPED(wstatus)) {
  76. m_terminal_widget->inject_string(String::format("\033[34;1m(Command stopped!)\033[0m\n"));
  77. } else if (WIFSIGNALED(wstatus)) {
  78. m_terminal_widget->inject_string(String::format("\033[34;1m(Command signaled with %s!)\033[0m\n", strsignal(WTERMSIG(wstatus))));
  79. }
  80. m_process_state_widget->set_tty_fd(-1);
  81. m_pid = -1;
  82. if (on_command_exit)
  83. on_command_exit();
  84. };
  85. m_pid = fork();
  86. if (m_pid == 0) {
  87. // Create a new process group.
  88. setsid();
  89. const char* tty_name = ptsname(ptm_fd);
  90. if (!tty_name) {
  91. perror("ptsname");
  92. exit(1);
  93. }
  94. close(ptm_fd);
  95. int pts_fd = open(tty_name, O_RDWR);
  96. if (pts_fd < 0) {
  97. perror("open");
  98. exit(1);
  99. }
  100. // NOTE: It's okay if this fails.
  101. (void)ioctl(0, TIOCNOTTY);
  102. close(0);
  103. close(1);
  104. close(2);
  105. int rc = dup2(pts_fd, 0);
  106. if (rc < 0) {
  107. perror("dup2");
  108. exit(1);
  109. }
  110. rc = dup2(pts_fd, 1);
  111. if (rc < 0) {
  112. perror("dup2");
  113. exit(1);
  114. }
  115. rc = dup2(pts_fd, 2);
  116. if (rc < 0) {
  117. perror("dup2");
  118. exit(1);
  119. }
  120. rc = close(pts_fd);
  121. if (rc < 0) {
  122. perror("close");
  123. exit(1);
  124. }
  125. rc = ioctl(0, TIOCSCTTY);
  126. if (rc < 0) {
  127. perror("ioctl(TIOCSCTTY)");
  128. exit(1);
  129. }
  130. setenv("TERM", "xterm", true);
  131. auto parts = command.split(' ');
  132. ASSERT(!parts.is_empty());
  133. const char** args = (const char**) calloc(parts.size() + 1, sizeof(const char*));
  134. for (int i = 0; i < parts.size(); i++) {
  135. args[i] = parts[i].characters();
  136. }
  137. rc = execvp(args[0], const_cast<char**>(args));
  138. if (rc < 0) {
  139. perror("execve");
  140. exit(1);
  141. }
  142. ASSERT_NOT_REACHED();
  143. }
  144. // Parent process, cont'd.
  145. m_process_state_widget->set_tty_fd(ptm_fd);
  146. }
  147. void TerminalWrapper::kill_running_command()
  148. {
  149. ASSERT(m_pid != -1);
  150. // Kill our child process and its whole process group.
  151. (void)killpg(m_pid, SIGTERM);
  152. }
  153. TerminalWrapper::TerminalWrapper(GUI::Widget* parent)
  154. : GUI::Widget(parent)
  155. {
  156. set_layout(make<GUI::VerticalBoxLayout>());
  157. RefPtr<Core::ConfigFile> config = Core::ConfigFile::get_for_app("Terminal");
  158. m_terminal_widget = TerminalWidget::construct(-1, false, config);
  159. add_child(*m_terminal_widget);
  160. m_process_state_widget = ProcessStateWidget::construct(this);
  161. }
  162. TerminalWrapper::~TerminalWrapper()
  163. {
  164. }