TerminalWrapper.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. int ptm_fd = posix_openpt(O_RDWR | O_CLOEXEC);
  32. if (ptm_fd < 0) {
  33. perror("posix_openpt");
  34. VERIFY_NOT_REACHED();
  35. }
  36. if (grantpt(ptm_fd) < 0) {
  37. perror("grantpt");
  38. VERIFY_NOT_REACHED();
  39. }
  40. if (unlockpt(ptm_fd) < 0) {
  41. perror("unlockpt");
  42. VERIFY_NOT_REACHED();
  43. }
  44. m_terminal_widget->set_pty_master_fd(ptm_fd);
  45. m_terminal_widget->on_command_exit = [this] {
  46. int wstatus;
  47. int rc = waitpid(m_pid, &wstatus, 0);
  48. if (rc < 0) {
  49. perror("waitpid");
  50. VERIFY_NOT_REACHED();
  51. }
  52. if (WIFEXITED(wstatus)) {
  53. m_terminal_widget->inject_string(String::formatted("\033[{};1m(Command exited with code {})\033[0m\r\n", wstatus == 0 ? 32 : 31, WEXITSTATUS(wstatus)));
  54. } else if (WIFSTOPPED(wstatus)) {
  55. m_terminal_widget->inject_string("\033[34;1m(Command stopped!)\033[0m\r\n");
  56. } else if (WIFSIGNALED(wstatus)) {
  57. m_terminal_widget->inject_string(String::formatted("\033[34;1m(Command signaled with {}!)\033[0m\r\n", strsignal(WTERMSIG(wstatus))));
  58. }
  59. m_pid = -1;
  60. if (on_command_exit)
  61. on_command_exit();
  62. };
  63. m_pid = fork();
  64. if (m_pid < 0) {
  65. perror("fork");
  66. return;
  67. }
  68. if (m_pid == 0) {
  69. // Create a new process group.
  70. setsid();
  71. const char* tty_name = ptsname(ptm_fd);
  72. if (!tty_name) {
  73. perror("ptsname");
  74. exit(1);
  75. }
  76. close(ptm_fd);
  77. int pts_fd = open(tty_name, O_RDWR);
  78. if (pts_fd < 0) {
  79. perror("open");
  80. exit(1);
  81. }
  82. tcsetpgrp(pts_fd, getpid());
  83. // NOTE: It's okay if this fails.
  84. int rc = ioctl(0, TIOCNOTTY);
  85. close(0);
  86. close(1);
  87. close(2);
  88. rc = dup2(pts_fd, 0);
  89. if (rc < 0) {
  90. perror("dup2");
  91. exit(1);
  92. }
  93. rc = dup2(pts_fd, 1);
  94. if (rc < 0) {
  95. perror("dup2");
  96. exit(1);
  97. }
  98. rc = dup2(pts_fd, 2);
  99. if (rc < 0) {
  100. perror("dup2");
  101. exit(1);
  102. }
  103. rc = close(pts_fd);
  104. if (rc < 0) {
  105. perror("close");
  106. exit(1);
  107. }
  108. rc = ioctl(0, TIOCSCTTY);
  109. if (rc < 0) {
  110. perror("ioctl(TIOCSCTTY)");
  111. exit(1);
  112. }
  113. setenv("TERM", "xterm", true);
  114. auto parts = command.split(' ');
  115. VERIFY(!parts.is_empty());
  116. const char** args = (const char**)calloc(parts.size() + 1, sizeof(const char*));
  117. for (size_t i = 0; i < parts.size(); i++) {
  118. args[i] = parts[i].characters();
  119. }
  120. rc = execvp(args[0], const_cast<char**>(args));
  121. if (rc < 0) {
  122. perror("execve");
  123. exit(1);
  124. }
  125. VERIFY_NOT_REACHED();
  126. }
  127. // (In parent process)
  128. terminal().scroll_to_bottom();
  129. }
  130. void TerminalWrapper::kill_running_command()
  131. {
  132. VERIFY(m_pid != -1);
  133. // Kill our child process and its whole process group.
  134. [[maybe_unused]] auto rc = killpg(m_pid, SIGTERM);
  135. }
  136. void TerminalWrapper::clear_including_history()
  137. {
  138. m_terminal_widget->clear_including_history();
  139. }
  140. TerminalWrapper::TerminalWrapper(bool user_spawned)
  141. : m_user_spawned(user_spawned)
  142. {
  143. set_layout<GUI::VerticalBoxLayout>();
  144. m_terminal_widget = add<VT::TerminalWidget>(-1, false);
  145. if (user_spawned)
  146. run_command("Shell");
  147. }
  148. TerminalWrapper::~TerminalWrapper()
  149. {
  150. }
  151. }