TerminalWrapper.cpp 5.5 KB

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