TerminalWrapper.cpp 5.3 KB

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